Giter Site home page Giter Site logo

error_handling_metacrafter's Introduction

Error Handling

This smart contract demonstrates different error handling techniques using the require, assert, and revert statements. It is important to properly handle errors in smart contracts to ensure the security and integrity of the blockchain.

Description

The contract keeps track of balances in a mapping called 'balances'. The mapping maps addresses to uint values, allowing us to store a numeric balance for each address.

deposit function

The deposit function takes in an amount (_amount) and increments the balance mapping for msg.sender by that amount. This "deposits" new tokens into the user's account balance.

withdrawRequire function

First it validates that the balance for msg.sender is sufficient to withdraw the requested _amount using require. If not enough balance, it will revert with an error message. If there is enough balance, it decrements the mapping value by _amount to "withdraw" that amount.

withdrawAssert function

Similar to withdrawRequire, but uses assert to check the balance condition. If assertion fails, the transaction is reverted without a custom message.

withdrawRevert function

Uses an if statement to check the balance condition. If balance is insufficient, it reverts with a custom error message, and also returns the remaining gas back to the caller. Otherwise, decrements the balance to withdraw the tokens.

Execution

To run this program, you can use Remix, an online Solidity IDE. To get started, go to the Remix website at https://remix.ethereum.org/ Then compile this required file, and then deploy it.

License

This project is licensed under the MIT License - see LICENSE for details

error_handling_metacrafter's People

Contributors

gks2022004 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.