ReasonJun

Solidity : require 본문

Blockchain/Solidity

Solidity : require

ReasonJun 2023. 10. 17. 18:00
728x90

The require() function in Solidity is used to check for certain conditions before executing a function. If the condition passed to the require() function evaluates to false, the function will throw an exception and the transaction will be reverted.

 

The require() function is typically used to check for valid input data or to verify that a certain state invariant is true. For example, the following code uses the require() function to check that the amount passed to the withdraw() function is not greater than the balance of the sender account:

function withdraw(uint256 amount) public {
  require(amount <= balances[msg.sender]);
  balances[msg.sender] -= amount;
}

If the amount passed to the withdraw() function is greater than the balance of the sender account, the require() function will throw an exception and the transaction will be reverted.

 

The require() function should not be used to check for external errors, such as failed network requests or out-of-gas conditions. These types of errors should be handled using the revert() function or by reverting the transaction manually.

 

Benefits of using require()

There are a number of benefits to using the require() function in Solidity:

  • Improved security: The require() function can help to improve the security of code by making it more difficult for errors to go unnoticed and by preventing unauthorized users from modifying the state of the contract.
  • Increased maintainability: The require() function can make code more maintainable by making it easier to understand and identify the conditions that can cause errors.
  • Reduced code duplication: The require() function can help to reduce code duplication by allowing you to reuse code from existing contracts.

Tips for using require() effectively

Here are some tips for using the require() function effectively:

  • Use the require() function to check for valid input data or to verify that a certain state invariant is true.
  • Do not use the require() function to check for external errors, such as failed network requests or out-of-gas conditions.

Conclusion

The require() function is a powerful tool that can be used to improve the security, maintainability, and readability of Solidity code. By understanding how to use the require() function effectively, you can write better and more robust contracts.

 

Difference between require() and revert()

The require() and revert() functions are both used to handle errors in Solidity, but they have some key differences:

  • The require() function is used to check for specific conditions before executing a function. If the condition fails, the function will throw an exception and the transaction will be reverted.
  • The revert() function can be used anywhere in a contract, but it is most commonly used in functions that modify the state of the contract. If the revert() function is called, all changes made to the state of the contract so far will be undone.

In general, it is recommended to use the require() function to check for specific conditions before executing a function, and to use the revert() function to handle unexpected errors.

 

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


// Error handling: assert / revert / require
// Exception handling: try / catch

// 1) require is used to verify input and conditions before execution.
// 2) revert is similar to require.
// 3) assert is used to check code that should never be false.
// 4) Save gas by using custom errors.

contract Error_0 {
    function testRequire(uint _i) public pure {
        require(_i > 10, "Input must be greater than 10");
    }

    function testRevert(uint _i) public pure {
        if (_i <= 10) {
            revert("Input must be greater than 10");
        }
    }


    uint public num;

    function testAssert() public view {

        assert(num == 0);
    }

    // custom error
    error InsufficientBalance(uint balance, uint withdrawAmount);

    function testCustomError(uint _withdrawAmount) public view {
        uint bal = address(this).balance;
        if (bal < _withdrawAmount) {
            revert InsufficientBalance({balance: bal, withdrawAmount: _withdrawAmount});
        }
    }
}

 

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : payable  (0) 2023.10.18
Solidity : try / catch  (0) 2023.10.17
Solidity : revert  (0) 2023.10.17
Solidity : assert  (0) 2023.10.17
Solidity : encapsulation & hiding in OOP  (0) 2023.10.17
Comments