Last active
October 11, 2023 16:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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