All tests pass after the gas optimization, total gas saved was 2.275%.
The main optimization strategy involves replacing the following expression:
i_tokenContract.balanceOf(address(this))
with the immutable variable i_price
. They should ideally have the same value.
This change enhances efficiency as Solidity checks an immutable value directly, avoiding the need to invoke an external function in a third-party contract.
Two functions were modified:
resolveDispute()
confirmReceipt()
At line 98
.
The impact has been measured in new isolated tests and in the client's tests that involved calls which executed the changes applied.
The total gas saved is 2.275%.
Test Type | Optimized Gas | Original Gas | Difference | % Saved on Original Gas |
---|---|---|---|---|
All tests | 9,262,601 | 9,477,939 | 215,338 | 2.27% |
Personalized | 1,525,223 | 1,561,105 | 35,882 | 2.29% |
Total | 10,787,824 | 11,039,044 | 251,220 | 2.275% |
Tests
This test focuses on the resolveDispute()
execution costs:
And this one focuses on the confirmReceipt()
's ones:
The tests were executed with 256 fuzz runs.
Function Name | Percentage Saved |
---|---|
confirmReceipt() |
2.34% |
resolveDispute() |
2.25% |
These are all the tests used in the analysis.
๐ง Notice โ ๏ธ: Some tests that involved checking for reverts have been excluded from the analysis. This is because they didn't execute the optimized code, thus its gas consumption won't be affected by the changes.
Test Name | Optimized Gas | Original Gas | Gas Saved |
---|---|---|---|
testConfirmReceiptGasConsumption | 746,542 | 764,468 | 17,926 |
testResolveDisputeGasConsumption | 778,681 | 796,637 | 17,956 |
testResolveDisputeChangesState | 779,946 | 797,902 | 17,956 |
testCanOnlyResolveInDisputedState | 756,577 | 774,521 | 17,944 |
testConfirmReceiptEmitsEvent | 747,825 | 765,751 | 17,926 |
testResolveDisputeWithBuyerAward | 830,930 | 848,886 | 17,956 |
testStateChangesOnConfirmedReceipt | 747,763 | 765,689 | 17,926 |
testResolveDisputeZeroArbiterFeeTransfer | 760,144 | 778,100 | 17,956 |
testResolveDisputeTransfersTokens | 830,506 | 848,462 | 17,956 |
testTransfersTokenOutOfContract | 748,383 | 766,309 | 17,926 |
testCanOnlyInitiateDisputeInConfirmedState | 748,501 | 766,427 | 17,926 |
testResolveDisputeZeroBuyerTransfer | 783,852 | 801,808 | 17,956 |
testResolveDisputeZeroSellerTransfer | 784,268 | 802,225 | 17,957 |
testResolveDisputeFeeExceedsBalance | 743,906 | 761,859 | 17,953 |
TOTAL | 10,787,824 | 11,039,044 | 251,220 |
Total saved percentage => 2.275%.
๐ Notice โน๏ธ: The percentage has been calculated with these numbers from the TOTAL:
(251,220 / 11,039,044) * 100
They mean:
(totalGasSaved / originalGasCost) * 100
Original
Optimized
Original
Optimized
Manual auditing.
Forge snapshot.
Bash scripts tailored to analyze forge snapshots.
๐ Notice โน๏ธ: I've personally created the bash scripts. Here is a link to the github repo Forge-Snapshots-Analyzer.
While the balanceOf()
method might enhance readability, developers should decide to balance between readability and efficiency. A proposed way to achieve both is by adding a comment above the i_price
variable, indicating its intended purpose and expected behavior:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.