Skip to content

JohnPyWick/eth-avax_module1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Error Handling

Description

In this example, the AssertionContract demonstrates the usage of require(), assert(), and revert() statements. These statements (require(), assert(), and revert()) are essential in smart contracts to enforce conditions, validate inputs, and handle unexpected scenarios. Each statement serves a different purpose and can help improve the security and reliability of the contract.

Code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ErrorHandling {
    int public balance;

    function deposit(int _amount) public {
        // Use require() to check if the amount is greater than zero
        require(_amount > 0, "Balance must be greater than zero");
        
        // Increase the balance by the amount entered
        balance += _amount;
    }

    function withdraw(int _amount) public {
        // Using asseert to check if balance is less than the amount being withdrawn
        assert(_amount <= balance);

        // Reduce the balance by the amount entered
        balance -= _amount;

        // Using revert() to explicitly revert state changes,because the balance becomes zero and if user withdraw
        if (balance == 0) {
            revert("Balance will become zero, can't withdraw"); // Revert and provide an error message
        }
    }

}

Require()

Using require() to validate that the input amount is greater than zero. If the condition is not met, it will revert the transaction and provide an error message.

function deposit(int _amount) public {
        // Use require() to check if the amount is greater than zero
        require(_amount > 0, "Balance must be greater than zero");
        
        // Increase the balance by the amount entered
        balance += _amount;
    }

Assert()

Additionally, the assert() statement checks that balance is greater than the amount being withdrawn. If the condition is false, it will trigger an internal inconsistency and cause the transaction to revert.

function withdraw(int _amount) public {
        // Using asseert to check if balance is less than the amount being withdrawn
        assert(_amount <= balance);

        // Reduce the balance by the amount entered
        balance -= _amount;

Revert

Revert() in the function checks if the amount withdrawn will make the balance zero if it does it won't allow it and revert the changes.

function withdraw(int _amount) public {
        // Use require() to enforce certain conditions
        require(_amount <= balance, "Insufficient balance");

        // Perform the withdrawal logic
        balance -= _amount;

        // Using revert() to explicitly revert state changes,because the balance becomes zero and if user withdraw
        if (balance == 0) {
            revert("Balance will become zero, can't withdraw"); // Revert and provide an error message
        }
    }

Revert Explained

After performing the withdrawal logic, the function checks if the contract's value has reached zero. If it has, the revert() statement is used to explicitly revert the state changes made within the function. The revert function call can include an error message to provide more information about why the state changes were reverted.

// Using revert() to explicitly revert state changes is the balance is zero and user tries to withdraw
        if (balance == 0) {
            revert("Balance is zero can't withdraw"); // Revert and provide an error message
        }

Authors

License

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors