solmate icon indicating copy to clipboard operation
solmate copied to clipboard

Gas Optimization: Custom Errors

Open JTraversa opened this issue 3 years ago • 4 comments

Description

Implemented Custom Errors across all major contracts.

Fixed up tests to accomodate custom errors.

E.g. hevm.expectRevert("NOT_MINTED"); -> hevm.expectRevert(abi.encodeWithSignature("NOT_MINTED()"));

Gas savings are ubiquitous: E.g.

Revert Strings:
[PASS] testTransfer(address,uint256) (runs: 256, μ: 58776, ~: 60487)
[PASS] testTransferFrom(address,uint256,uint256) (runs: 256, μ: 87191, ~: 92844)
Custom Errors:
[PASS] testTransfer(address,uint256) (runs: 256, μ: 58310, ~: 60487) -- Saves  ~466
[PASS] testTransferFrom(address,uint256,uint256) (runs: 256, μ: 88263, ~: 92844) -- Saves ~1072

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • [x] Ran forge snapshot?
  • [x] Ran npm run lint?
  • [x] Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

JTraversa avatar Jul 10 '22 01:07 JTraversa

mind doing this against the v7 branch instead of master?

happy to merge this after resolving the comments above across the diff

transmissions11 avatar Jul 10 '22 02:07 transmissions11

For sure, ill review / take care of all the comments and rebase on v7

JTraversa avatar Jul 10 '22 02:07 JTraversa

I think custom errors should include the emitter:

error SomeError(address emitter, ..., ..., ...);
emit SomeError(address(this), ..., ..., ...);

This helps end-users identify which contract was reverted with a failed transaction, which is especially useful for complex transactions involving multiple contracts. Also, custom errors should be highly informative and I would argue that the emitter is a piece of important information that belongs to any custom error.

pcaversaccio avatar Jul 12 '22 09:07 pcaversaccio

What's with the min version in the contracts? pragma solidity >=0.8.0;

Shouldn't it be >=0.8.4 at least?

https://blog.soliditylang.org/2021/04/21/custom-errors

zaskoh avatar Nov 20 '22 21:11 zaskoh