1

I have written a contract with minimal declaration of errors; Hopping to achieve more readable and concise code. However, if I define more errors, would it cost more gas, if so how much per error?

error Contract__GenericError();
error Contract__GenericError2();
error Contract__GenericError3();

.....

2 Answers 2

1

Simply defining more errors won't increase your deployment gas costs, for example, these two contracts both cost 77113 gas to deploy.

contract A {
    uint x;
}
contract B {
    error Zero();
    error One();
    error Two();
    error Three();
    error Four();
    error Five();
    error Six();
    error Seven();
    uint x;
}

When you start to implement them into functions, of course, there will be a gas cost but for custom errors, they only take up 4 bytes, so they will usually be cheaper than using traditional require statements with hard-coded string error messages.

For reference, compare the new gas costs associated with deploying the contracts after we add a function:

167140 gas

contract A {
    uint x;

    function foo(uint _x ) public pure {
        require(_x <= 7, "One");
    }
}

130080 gas

contract B {
    error Zero();
    error One();
    error Two();
    error Three();
    error Four();
    error Five();
    error Six();
    error Seven();
    uint x;

    function foo(uint _x ) public pure {
        if(_x > 7) {
            revert One();
        }
    }
}

And if we add a couple more functions you can see the difference in gas costs increasing further:

273926 gas

contract A {
    uint x;

    function foo(uint _x ) public pure {
        require(_x <= 7, "One");
    }

    function bar(uint _x) public pure {
        require(_x <= 14, "Two");
    }

    function baz(uint _x) public pure {
        require(_x <= 21, "Three");
    }
}

183797 gas

contract B {
    error Zero();
    error One();
    error Two();
    error Three();
    error Four();
    error Five();
    error Six();
    error Seven();
    uint x;

    function foo(uint _x ) public pure {
        if(_x > 7) {
            revert One();
        }
    }

    function bar(uint _x ) public pure {
        if(_x > 14) {
            revert Two();
        }
    }

     function baz(uint _x ) public pure {
        if(_x > 21) {
            revert Three();
        }
    }
}

See this quote from the Solidity docs about using custom errors:

Using a custom error instance will usually be much cheaper than a string description, because you can use the name of the error to describe it, which is encoded in only four bytes. A longer description can be supplied via NatSpec which does not incur any costs.

0

Actually, custom errors are a way to save gas

for example:

contract A {
     error Contract__GenericError();
     function a() public {
         if(/* some condition */) {
              // do something
         } 
         else {
             revert Contract__GenericError();
         }
     }    
}

will cost less gas than:

contract B {
    function b() public {
         require(/* some condition */, "Contract__GenericError");
         // do something
    }    
}

because in b() it costs even more gas to write the error string

2
  • Yeh I Know reverting errors saves more gas. However, how much does it cost to define multiple errors? I wanna know if it's worth it adding more errors than I WOULD LIKE
    – Ibra
    Commented Oct 22, 2023 at 15:48
  • 1
    hey Ibrahim, i'm not sure I fully understand your case. are u saying u want to define a bunch of errors without using them later? Commented Oct 22, 2023 at 16:20

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.