suberra / funnel-contracts

Funnels are smart contracts that enforces renewable allowance as a proxy
MIT License
9 stars 0 forks source link

style: use custom errors instead #56

Closed edison0xyz closed 1 year ago

edison0xyz commented 1 year ago

Pushed another commit that addresses most of the comments. And also compared the gas usage.

Before PR:

✹ ✚
[⠆] Compiling...
No files changed, compilation skipped

Running 2 tests for test/MockSpenderReceiver.t.sol:MockSpenderReceiverTest
[PASS] testSupportInterfaceReceiver() (gas: 5598)
[PASS] testSupportInterfaceSpender() (gas: 6567)
Test result: ok. 2 passed; 0 failed; finished in 3.09ms

Running 11 tests for test/FunnelFactory.t.sol:FunnelFactoryTest
[PASS] testDeployFunnelForDifferentTokens() (gas: 298301)
[PASS] testDeployFunnelForToken() (gas: 156993)
[PASS] testDeployFunnelForTokenRevertsIfAlreadyDeployed() (gas: 156707)
[PASS] testDeployFunnelFromDifferentFactory() (gas: 558198)
[PASS] testGetFunnelForTokenRevertsIfNotDeployed() (gas: 12793)
[PASS] testIsFunnelFalseForDeployedFunnelFromDifferentFactory() (gas: 563350)
[PASS] testIsFunnelFalseForNonFunnel() (gas: 10875)
[PASS] testIsFunnelFalseForUndeployedFunnel() (gas: 8329)
[PASS] testIsFunnelTrueForDeployedFunnel() (gas: 155345)
[PASS] testNoCodeTokenReverts() (gas: 15399)
[PASS] testTransferFromFunnel() (gas: 294377)
Test result: ok. 11 passed; 0 failed; finished in 41.16ms

Running 18 tests for test/FunnelERC20.t.sol:FunnelERC20Test
[PASS] testApproveFuzzing(uint256) (runs: 256, μ: 118180, ~: 119891)
[PASS] testApproveMaxWithTransfer() (gas: 138686)
[PASS] testApproveWithTransferFuzzing(uint256,uint256) (runs: 256, μ: 148771, ~: 150398)
[PASS] testBalanceOfOnMint(uint256) (runs: 256, μ: 66292, ~: 67015)
[PASS] testBalanceOfOnMintLargeAmount() (gas: 66981)
[PASS] testBalanceOfOnTransfer(uint256,uint256) (runs: 256, μ: 104454, ~: 107372)
[PASS] testBalanceOfOnTransferLargeAmount() (gas: 87476)
[PASS] testBalanceOfReflectsSlot(uint256) (runs: 256, μ: 322, ~: 322)
[PASS] testFailApproveWithTransferInsufficientApproval() (gas: 100475)
[PASS] testFailApprovewithTransferInsufficientBalance() (gas: 108378)
[PASS] testFailTransferExceedsBalance(uint256) (runs: 256, μ: 40582, ~: 40682)
[PASS] testInitialAllowance() (gas: 25026)
[PASS] testTotalSupply(uint256) (runs: 256, μ: 42945, ~: 42933)
[PASS] testTransferFullBalance() (gas: 61369)
[PASS] testTransferFuzzing(uint256) (runs: 256, μ: 69128, ~: 69678)
[PASS] testTransferHalfBalance() (gas: 66255)
[PASS] testTransferOneToken() (gas: 64089)
[PASS] testTransferZeroTokens() (gas: 41433)
Test result: ok. 18 passed; 0 failed; finished in 44.70ms

Running 41 tests for test/Funnel.t.sol:FunnelTest
[PASS] testApprove() (gas: 144722)
[PASS] testApproveRenewable() (gas: 96800)
[PASS] testApproveRenewableAndCall() (gas: 92507)
[PASS] testApproveRenewableAndCallRevertNonContract() (gas: 84837)
[PASS] testApproveRenewableAndCallRevertNonReceiver() (gas: 85743)
[PASS] testBaseToken() (gas: 9949)
[PASS] testBaseTokenAllowance() (gas: 103891)
[PASS] testClearRenewableAllowanceOnNormalApprove() (gas: 145917)
[PASS] testExecuteMetaTransactionApproveRenewable() (gas: 136224)
[PASS] testExecuteMetaTransactionTransfer() (gas: 97013)
[PASS] testFailExecuteMetaTransactionBadNonce() (gas: 47605)
[PASS] testFailExecuteMetaTransactionReplayProtection() (gas: 143215)
[PASS] testFailPermitBadDeadline() (gas: 46660)
[PASS] testFailPermitBadNonce() (gas: 46695)
[PASS] testFailPermitPastDeadline() (gas: 15810)
[PASS] testFailPermitRenewableBadDeadline() (gas: 48805)
[PASS] testFailPermitRenewableBadNonce() (gas: 48754)
[PASS] testFailPermitRenewablePastDeadline() (gas: 20145)
[PASS] testFailPermitRenewableReplay() (gas: 125640)
[PASS] testFailPermitReplay() (gas: 123582)
[PASS] testFallbackToBaseToken() (gas: 25657)
[PASS] testInfiniteApproveTransferFrom() (gas: 134068)
[PASS] testInsufficientAllowance() (gas: 29392)
[PASS] testOverflow() (gas: 116539)
[PASS] testOverflow2() (gas: 116545)
[PASS] testOverflow3() (gas: 129944)
[PASS] testOverriddenName() (gas: 20769)
[PASS] testPermit() (gas: 128775)
[PASS] testPermitRenewable() (gas: 130791)
[PASS] testRecoveryRateCasting() (gas: 85939)
[PASS] testRecoveryRateExceeded() (gas: 13474)
[PASS] testRecoveryRateExceeded2() (gas: 13428)
[PASS] testRenewableAllowanceTransferFrom() (gas: 155321)
[PASS] testRenewableMaxAllowance() (gas: 98292)
[PASS] testSupportInterface() (gas: 5766)
[PASS] testSupportsInterfacePayable() (gas: 5836)
[PASS] testSupportsInterfaceProxy() (gas: 5763)
[PASS] testTransfer() (gas: 61980)
[PASS] testTransferFromAndCall() (gas: 138831)
[PASS] testTransferFromAndCallRevertNonContract() (gas: 113532)
[PASS] testTransferFromAndCallRevertNonReceiver() (gas: 111933)
Test result: ok. 41 passed; 0 failed; finished in 44.75ms

After custom errors:

[⠆] Compiling...
[⠑] Compiling 7 files with 0.8.17
[⠃] Solc 0.8.17 finished in 2.36s
Compiler run successful

Running 2 tests for test/MockSpenderReceiver.t.sol:MockSpenderReceiverTest
[PASS] testSupportInterfaceReceiver() (gas: 5598)
[PASS] testSupportInterfaceSpender() (gas: 6567)
Test result: ok. 2 passed; 0 failed; finished in 2.63ms

Running 11 tests for test/FunnelFactory.t.sol:FunnelFactoryTest
[PASS] testDeployFunnelForDifferentTokens() (gas: 298301)
[PASS] testDeployFunnelForToken() (gas: 156993)
[PASS] testDeployFunnelForTokenRevertsIfAlreadyDeployed() (gas: 156707)
[PASS] testDeployFunnelFromDifferentFactory() (gas: 558192)
[PASS] testGetFunnelForTokenRevertsIfNotDeployed() (gas: 12793)
[PASS] testIsFunnelFalseForDeployedFunnelFromDifferentFactory() (gas: 563344)
[PASS] testIsFunnelFalseForNonFunnel() (gas: 10875)
[PASS] testIsFunnelFalseForUndeployedFunnel() (gas: 8329)
[PASS] testIsFunnelTrueForDeployedFunnel() (gas: 155345)
[PASS] testNoCodeTokenReverts() (gas: 15399)
[PASS] testTransferFromFunnel() (gas: 294377)
Test result: ok. 11 passed; 0 failed; finished in 4.31ms

Running 41 tests for test/Funnel.t.sol:FunnelTest
[PASS] testApprove() (gas: 144722)
[PASS] testApproveRenewable() (gas: 96800)
[PASS] testApproveRenewableAndCall() (gas: 92507)
[PASS] testApproveRenewableAndCallRevertNonContract() (gas: 84765)
[PASS] testApproveRenewableAndCallRevertNonReceiver() (gas: 85695)
[PASS] testBaseToken() (gas: 9949)
[PASS] testBaseTokenAllowance() (gas: 103891)
[PASS] testClearRenewableAllowanceOnNormalApprove() (gas: 145917)
[PASS] testExecuteMetaTransactionApproveRenewable() (gas: 136224)
[PASS] testExecuteMetaTransactionTransfer() (gas: 97013)
[PASS] testFailExecuteMetaTransactionBadNonce() (gas: 47605)
[PASS] testFailExecuteMetaTransactionReplayProtection() (gas: 143215)
[PASS] testFailPermitBadDeadline() (gas: 46660)
[PASS] testFailPermitBadNonce() (gas: 46695)
[PASS] testFailPermitPastDeadline() (gas: 15729)
[PASS] testFailPermitRenewableBadDeadline() (gas: 48805)
[PASS] testFailPermitRenewableBadNonce() (gas: 48754)
[PASS] testFailPermitRenewablePastDeadline() (gas: 20064)
[PASS] testFailPermitRenewableReplay() (gas: 125640)
[PASS] testFailPermitReplay() (gas: 123582)
[PASS] testFallbackToBaseToken() (gas: 25657)
[PASS] testInfiniteApproveTransferFrom() (gas: 134068)
[PASS] testInsufficientAllowance() (gas: 29392)
[PASS] testOverflow() (gas: 116539)
[PASS] testOverflow2() (gas: 116545)
[PASS] testOverflow3() (gas: 129944)
[PASS] testOverriddenName() (gas: 20769)
[PASS] testPermit() (gas: 128775)
[PASS] testPermitRenewable() (gas: 130791)
[PASS] testRecoveryRateCasting() (gas: 85939)
[PASS] testRecoveryRateExceeded() (gas: 13474)
[PASS] testRecoveryRateExceeded2() (gas: 13491)
[PASS] testRenewableAllowanceTransferFrom() (gas: 155321)
[PASS] testRenewableMaxAllowance() (gas: 98292)
[PASS] testSupportInterface() (gas: 5766)
[PASS] testSupportsInterfacePayable() (gas: 5836)
[PASS] testSupportsInterfaceProxy() (gas: 5763)
[PASS] testTransfer() (gas: 61980)
[PASS] testTransferFromAndCall() (gas: 138831)
[PASS] testTransferFromAndCallRevertNonContract() (gas: 113465)
[PASS] testTransferFromAndCallRevertNonReceiver() (gas: 111885)
Test result: ok. 41 passed; 0 failed; finished in 9.57ms

Running 18 tests for test/FunnelERC20.t.sol:FunnelERC20Test
[PASS] testApproveFuzzing(uint256) (runs: 256, μ: 117869, ~: 119891)
[PASS] testApproveMaxWithTransfer() (gas: 138686)
[PASS] testApproveWithTransferFuzzing(uint256,uint256) (runs: 256, μ: 147948, ~: 150398)
[PASS] testBalanceOfOnMint(uint256) (runs: 256, μ: 65772, ~: 67015)
[PASS] testBalanceOfOnMintLargeAmount() (gas: 66981)
[PASS] testBalanceOfOnTransfer(uint256,uint256) (runs: 256, μ: 104370, ~: 107372)
[PASS] testBalanceOfOnTransferLargeAmount() (gas: 87476)
[PASS] testBalanceOfReflectsSlot(uint256) (runs: 256, μ: 322, ~: 322)
[PASS] testFailApproveWithTransferInsufficientApproval() (gas: 100475)
[PASS] testFailApprovewithTransferInsufficientBalance() (gas: 108378)
[PASS] testFailTransferExceedsBalance(uint256) (runs: 256, μ: 40575, ~: 40682)
[PASS] testInitialAllowance() (gas: 25026)
[PASS] testTotalSupply(uint256) (runs: 256, μ: 42753, ~: 42933)
[PASS] testTransferFullBalance() (gas: 61369)
[PASS] testTransferFuzzing(uint256) (runs: 256, μ: 68547, ~: 69678)
[PASS] testTransferHalfBalance() (gas: 66255)
[PASS] testTransferOneToken() (gas: 64089)
[PASS] testTransferZeroTokens() (gas: 41433)
Test result: ok. 18 passed; 0 failed; finished in 46.14ms

Generally we saved 20-80 gas for functions where there is an error thrown.