2

I am someone new to Solidity and going through the custom errors part with REVERT I have found examples where the word "ERROR" is thrown inside CONTRACT


pragma solidity 0.8.18;

contract EjercicioSemana4 {
    // definir address 'admin' con valor de 0x08Fb288FcC281969A0BBE6773857F99360f2Ca06
    address public admin = 0x08Fb288FcC281969A0BBE6773857F99360f2Ca06;
    // definir boolean public 'pausado' con valor de false
    bool public pausado;

    error SaldoInsuficiente();
...
    function transferir(address _destinatario, uint256 _cantidad) public {
        if (_cantidad >= balances[msg.sender]) {
            revert SaldoInsuficiente();
        } else {
            balances[msg.sender] -= _cantidad;
            balances[_destinatario] += _cantidad;
            emit Transfer(msg.sender, _destinatario, _cantidad);
        }
    }

On the other hand, reviewing examples on the web as well as in the Solidity documentation I see that they declare the ERROR outside CONTRACT


// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

/// Insufficient balance for transfer. Needed `required` but only
/// `available` available.
/// @param available balance available.
/// @param required requested amount to transfer.
error InsufficientBalance(uint256 available, uint256 required);

contract TestToken {
    mapping(address => uint) balance;
    function transfer(address to, uint256 amount) public {
        if (amount > balance[msg.sender])
            revert InsufficientBalance({
                available: balance[msg.sender],
                required: amount
            });
        balance[msg.sender] -= amount;
        balance[to] += amount;
    }
    // ...
}

Are there any relevant differences in the definition of the ERROR statement outside or inside of CONTRACT?

2 Answers 2

3

If it's defined outside you can share it among your contracts and maintain consistency in your code, other than that I don't think there are any other relevant differences in deployment or execution costs

0

No difference.

Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).

https://solidity-by-example.org/error/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.