Code Monkey home page Code Monkey logo

error-handling-in-solidity's Introduction

Error Contract

This Solidity smart contract demonstrates various error handling mechanisms and custom error types. It includes functions that use different error handling methods such as require, revert, and assert, along with a custom error type defined within the contract.

Getting Started

To understand the error handling mechanisms and custom error type implemented in this contract, follow the instructions below:

  1. Prerequisites: Ensure you have a compatible Ethereum development environment and compiler. This contract uses Solidity version 0.8.17.

  2. Deploy the Contract: Deploy the Error contract to your preferred Ethereum development network (e.g., Ganache, Rinkeby, etc.).

  3. Interact with the Functions: Once the contract is deployed, you can interact with its functions to observe the different error handling methods.

Functions

testRequire(uint _i)

This function uses the require statement to validate if the input _i is equal to 0. If the condition is not met, it reverts the transaction with the specified error message.

testRevert(uint _i)

This function employs the revert statement to check if the input _i is less than or equal to 10. If the condition holds true, the transaction is reverted with the provided error message.

testAssert()

The testAssert function uses the assert statement to validate if the public state variable num is equal to 0. If the condition is false, it triggers an assertion error.

InsufficientBalance(uint balance, uint withdrawAmount)

This contract includes a custom error type named InsufficientBalance. It accepts two parameters: balance and withdrawAmount. This custom error type can be used to revert transactions when a certain condition (insufficient balance in this case) is met.

testCustomError(uint _withdrawAmount)

The testCustomError function uses the custom error InsufficientBalance to check if the balance of the contract is less than the specified _withdrawAmount. If the condition is satisfied, the transaction is reverted using the custom error with relevant data.

License

This contract is licensed under the MIT License. See the LICENSE file for details.

Credits

This project is created by Rajat Verma.

Disclaimer

This contract is provided for educational purposes only. Use it in a controlled environment and do not use it in a production environment without proper security audits and testing.

error-handling-in-solidity's People

Contributors

thewreckingpanda 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.