Skip to content

Instantly share code, notes, and snippets.

@helderjnpinto
Last active October 11, 2023 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helderjnpinto/4420a41cc15180c3b453ba6707fb4536 to your computer and use it in GitHub Desktop.
Save helderjnpinto/4420a41cc15180c3b453ba6707fb4536 to your computer and use it in GitHub Desktop.
test of solidity costs when reverts and deployed transaction gas for require, revert custom errors
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
// tx costs when reverts + deployed tx cost
// deployed gas cost 202596
contract Error {
uint num = 0;
// transaction cost 21910 gas
// execution cost 706 gas
function testRequire1(uint _i) public {
require(_i > 10, "Input must be greater than 10");
num = _i;
}
// custom error
error InvalidNumber();
error InsufficientBalance(uint argumentA);
// transaction cost 21686 gas
// execution cost 482 gas
function testRequire2(uint _i) public {
if (_i < 10) {
revert InvalidNumber();
}
num = _i;
}
// transaction cost 21782 gas
// execution cost 590 gas
function testRequire3(uint _i) public {
if (_i < 10) {
revert InsufficientBalance({argumentA: _i});
}
num = _i;
}
}
// deployed gas 146144
contract Error1 {
uint num = 0;
// tx cost 21888
function testRequire1(uint _i) public {
require(_i > 10, "Input must be greater than 10");
num = _i;
}
}
// deployed gas 57308
contract Error2 {
uint num = 0;
error InvalidNumber();
// tx cost 21642
function testRequire2(uint _i) public {
if (_i < 10) {
revert InvalidNumber();
}
num = _i;
}
}
// deployed gas 129296
contract Error3 {
uint num = 0;
error InsufficientBalance(uint argumentA);
// tx cost 21782
function testRequire3(uint _i) public {
if (_i < 10) {
revert InsufficientBalance({argumentA: _i});
}
num = _i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment