Thread by Patrick Collins
- Tweet
- Mar 19, 2022
- #Bitcoin #SmartContract
Thread
❓Trivia: Reverting a transaction if conditions aren't met is an essential tool for literally all smart contracts!
But as of v0.8.4 of solidity, which one of these is more gas efficient?
But as of v0.8.4 of solidity, which one of these is more gas efficient?
🔮 And the answer is... B is more gas efficient!
🤔 But maybe not in the way you'd expect...
Let's look at our sample contract here to test.
👇
🤔 But maybe not in the way you'd expect...
Let's look at our sample contract here to test.
👇
If our `doStuff` and `doOtherStuff` functions pass, they actually end up using the same amount of gas, so their is NO gas savings there!
However, if your transaction REVERTS (ie: You don't send enough value with your transaction) that's when we see a slliight gas saving.
However, if your transaction REVERTS (ie: You don't send enough value with your transaction) that's when we see a slliight gas saving.
Our `doOtherStuff` that uses the `revert` statement saves a tiny bit of gas - which is nice.
But the BIGGEST savings actually comes right at the deploy time.
In our `require` statement, we have to save a whole big string to our contract - our revert does not!
But the BIGGEST savings actually comes right at the deploy time.
In our `require` statement, we have to save a whole big string to our contract - our revert does not!
Our Test2 contract is a TON of gas comparatively! So right now, it's better to use:
if (x) revert error
However, plans for a try/catch or adding errors to require is in progress, to make it easier for developers to read and write:
github.com/ethereum/solidity/issues/11278
if (x) revert error
However, plans for a try/catch or adding errors to require is in progress, to make it easier for developers to read and write:
github.com/ethereum/solidity/issues/11278
Shoot me a like, retweet, and a follow if you learned something!
EDIT: More information: blog.soliditylang.org/2021/04/21/custom-errors/