Skip to content

Commit 8d50a49

Browse files
nkrishangKien Ngo
andauthored
Gas Optimization Bounty submission #4 (#464)
* fix compiler warnings * yarn gas * Open pack gas bounty (#463) * Optimize function getRewardUnits * yarn gas * Prettier Pack.sol * Yarn gas * Update lib/chainlink * Submodule chainlink * Reverted submodule reference to 9d5ec20aa7c03c5f08722fa88f621075d300dcc1 --------- Co-authored-by: nkrishang <62195808+nkrishang@users.noreply.github.com> * comparison gas report --------- Co-authored-by: Kien Ngo <kienngo@skiff.com>
1 parent 4f9af22 commit 8d50a49

File tree

5 files changed

+101
-93
lines changed

5 files changed

+101
-93
lines changed

contracts/pack/Pack.sol

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -344,36 +344,38 @@ contract Pack is
344344

345345
(Token[] memory _token, ) = getPackContents(_packId);
346346
bool[] memory _isUpdated = new bool[](totalRewardKinds);
347-
for (uint256 i = 0; i < numOfRewardUnitsToDistribute; i += 1) {
347+
for (uint256 i; i < numOfRewardUnitsToDistribute; ) {
348348
uint256 randomVal = uint256(keccak256(abi.encode(random, i)));
349349
uint256 target = randomVal % totalRewardUnits;
350350
uint256 step;
351-
352-
for (uint256 j = 0; j < totalRewardKinds; j += 1) {
353-
uint256 totalRewardUnitsOfKind = _token[j].totalAmount / pack.perUnitAmounts[j];
354-
351+
for (uint256 j; j < totalRewardKinds; ) {
352+
uint256 perUnitAmount = pack.perUnitAmounts[j];
353+
uint256 totalRewardUnitsOfKind = _token[j].totalAmount / perUnitAmount;
355354
if (target < step + totalRewardUnitsOfKind) {
356-
_token[j].totalAmount -= pack.perUnitAmounts[j];
355+
_token[j].totalAmount -= perUnitAmount;
357356
_isUpdated[j] = true;
358-
359-
rewardUnits[i].assetContract = _token[j].assetContract;
360-
rewardUnits[i].tokenType = _token[j].tokenType;
361-
rewardUnits[i].tokenId = _token[j].tokenId;
362-
rewardUnits[i].totalAmount = pack.perUnitAmounts[j];
363-
357+
rewardUnits[i] = _token[j];
358+
rewardUnits[i].totalAmount = perUnitAmount;
364359
totalRewardUnits -= 1;
365-
366360
break;
367361
} else {
368362
step += totalRewardUnitsOfKind;
369363
}
364+
unchecked {
365+
++j;
366+
}
367+
}
368+
unchecked {
369+
++i;
370370
}
371371
}
372-
373-
for (uint256 i = 0; i < totalRewardKinds; i += 1) {
372+
for (uint256 i; i < totalRewardKinds; ) {
374373
if (_isUpdated[i]) {
375374
_updateTokenInBundle(_token[i], _packId, i);
376375
}
376+
unchecked {
377+
++i;
378+
}
377379
}
378380
}
379381

@@ -392,8 +394,11 @@ contract Pack is
392394
contents = new Token[](total);
393395
perUnitAmounts = new uint256[](total);
394396

395-
for (uint256 i = 0; i < total; i += 1) {
397+
for (uint256 i; i < total; ) {
396398
contents[i] = getTokenOfBundle(_packId, i);
399+
unchecked {
400+
++i;
401+
}
397402
}
398403
perUnitAmounts = pack.perUnitAmounts;
399404
}

gasreport.txt

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,99 @@
1-
No files changed, compilation skipped
1+
Compiling 638 files with 0.8.12
2+
Solc 0.8.12 finished in 161.31s
3+
Compiler run successful!
24

35
Running 3 tests for src/test/benchmark/EditionStakeBenchmark.t.sol:EditionStakeBenchmarkTest
4-
[PASS] test_benchmark_editionStake_claimRewards() (gas: 65580)
5-
[PASS] test_benchmark_editionStake_stake() (gas: 185145)
6-
[PASS] test_benchmark_editionStake_withdraw() (gas: 46326)
7-
Test result: ok. 3 passed; 0 failed; finished in 468.09ms
8-
9-
Running 5 tests for src/test/benchmark/SignatureDropBenchmark.t.sol:SignatureDropBenchmarkTest
10-
[PASS] test_bechmark_signatureDrop_claim_five_tokens() (gas: 141456)
11-
[PASS] test_bechmark_signatureDrop_setClaimConditions() (gas: 73774)
12-
[PASS] test_benchmark_signatureDrop_lazyMint() (gas: 124639)
13-
[PASS] test_benchmark_signatureDrop_lazyMint_for_delayed_reveal() (gas: 226228)
14-
[PASS] test_benchmark_signatureDrop_reveal() (gas: 9183)
15-
Test result: ok. 5 passed; 0 failed; finished in 468.49ms
16-
17-
Running 3 tests for src/test/benchmark/NFTStakeBenchmark.t.sol:NFTStakeBenchmarkTest
18-
[PASS] test_benchmark_nftStake_claimRewards() (gas: 68826)
19-
[PASS] test_benchmark_nftStake_stake_five_tokens() (gas: 549421)
20-
[PASS] test_benchmark_nftStake_withdraw() (gas: 39648)
21-
Test result: ok. 3 passed; 0 failed; finished in 468.46ms
22-
23-
Running 4 tests for src/test/benchmark/TokenERC1155Benchmark.t.sol:TokenERC1155BenchmarkTest
24-
[PASS] test_benchmark_tokenERC1155_burn() (gas: 5744)
25-
[PASS] test_benchmark_tokenERC1155_mintTo() (gas: 121028)
26-
[PASS] test_benchmark_tokenERC1155_mintWithSignature_pay_with_ERC20() (gas: 263757)
27-
[PASS] test_benchmark_tokenERC1155_mintWithSignature_pay_with_native_token() (gas: 292255)
28-
Test result: ok. 4 passed; 0 failed; finished in 469.08ms
6+
[PASS] test_benchmark_editionStake_claimRewards() (gas: 65558)
7+
[PASS] test_benchmark_editionStake_stake() (gas: 185123)
8+
[PASS] test_benchmark_editionStake_withdraw() (gas: 46304)
9+
Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 867.54ms
2910

3011
Running 3 tests for src/test/benchmark/PackBenchmark.t.sol:PackBenchmarkTest
31-
[PASS] test_benchmark_pack_addPackContents() (gas: 219449)
32-
[PASS] test_benchmark_pack_createPack() (gas: 1425232)
33-
[PASS] test_benchmark_pack_openPack() (gas: 170278)
34-
Test result: ok. 3 passed; 0 failed; finished in 469.74ms
12+
[PASS] test_benchmark_pack_addPackContents() (gas: 219427)
13+
[PASS] test_benchmark_pack_createPack() (gas: 1425210)
14+
[PASS] test_benchmark_pack_openPack() (gas: 154414)
15+
Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 871.94ms
16+
17+
Running 5 tests for src/test/benchmark/SignatureDropBenchmark.t.sol:SignatureDropBenchmarkTest
18+
[PASS] test_bechmark_signatureDrop_claim_five_tokens() (gas: 141434)
19+
[PASS] test_bechmark_signatureDrop_setClaimConditions() (gas: 73752)
20+
[PASS] test_benchmark_signatureDrop_lazyMint() (gas: 124617)
21+
[PASS] test_benchmark_signatureDrop_lazyMint_for_delayed_reveal() (gas: 226206)
22+
[PASS] test_benchmark_signatureDrop_reveal() (gas: 9165)
23+
Test result: ok. 5 passed; 0 failed; 0 skipped; finished in 872.08ms
3524

3625
Running 2 tests for src/test/benchmark/MultiwrapBenchmark.t.sol:MultiwrapBenchmarkTest
37-
[PASS] test_benchmark_multiwrap_unwrap() (gas: 92404)
38-
[PASS] test_benchmark_multiwrap_wrap() (gas: 475314)
39-
Test result: ok. 2 passed; 0 failed; finished in 476.27ms
26+
[PASS] test_benchmark_multiwrap_unwrap() (gas: 92386)
27+
[PASS] test_benchmark_multiwrap_wrap() (gas: 475292)
28+
Test result: ok. 2 passed; 0 failed; 0 skipped; finished in 873.82ms
4029

4130
Running 1 test for src/test/benchmark/AirdropERC20Benchmark.t.sol:AirdropERC20BenchmarkTest
42-
[PASS] test_benchmark_airdropERC20_airdrop() (gas: 32173710)
43-
Test result: ok. 1 passed; 0 failed; finished in 491.93ms
31+
[PASS] test_benchmark_airdropERC20_airdrop() (gas: 32173688)
32+
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 886.88ms
4433

4534
Running 1 test for src/test/benchmark/AirdropERC1155Benchmark.t.sol:AirdropERC1155BenchmarkTest
46-
[PASS] test_benchmark_airdropERC1155_airdrop() (gas: 38084693)
47-
Test result: ok. 1 passed; 0 failed; finished in 492.70ms
35+
[PASS] test_benchmark_airdropERC1155_airdrop() (gas: 38078671)
36+
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 891.33ms
37+
38+
Running 4 tests for src/test/benchmark/TokenERC1155Benchmark.t.sol:TokenERC1155BenchmarkTest
39+
[PASS] test_benchmark_tokenERC1155_burn() (gas: 5726)
40+
[PASS] test_benchmark_tokenERC1155_mintTo() (gas: 121006)
41+
[PASS] test_benchmark_tokenERC1155_mintWithSignature_pay_with_ERC20() (gas: 263735)
42+
[PASS] test_benchmark_tokenERC1155_mintWithSignature_pay_with_native_token() (gas: 292233)
43+
Test result: ok. 4 passed; 0 failed; 0 skipped; finished in 894.20ms
4844

4945
Running 1 test for src/test/benchmark/AirdropERC721Benchmark.t.sol:AirdropERC721BenchmarkTest
50-
[PASS] test_benchmark_airdropERC721_airdrop() (gas: 43898701)
51-
Test result: ok. 1 passed; 0 failed; finished in 498.05ms
46+
[PASS] test_benchmark_airdropERC721_airdrop() (gas: 43892679)
47+
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 902.28ms
5248

53-
Running 3 tests for src/test/benchmark/TokenERC20Benchmark.t.sol:TokenERC20BenchmarkTest
54-
[PASS] test_benchmark_tokenERC20_mintTo() (gas: 118533)
55-
[PASS] test_benchmark_tokenERC20_mintWithSignature_pay_with_ERC20() (gas: 181738)
56-
[PASS] test_benchmark_tokenERC20_mintWithSignature_pay_with_native_token() (gas: 206150)
57-
Test result: ok. 3 passed; 0 failed; finished in 145.19ms
49+
Running 3 tests for src/test/benchmark/PackVRFDirectBenchmark.t.sol:PackVRFDirectBenchmarkTest
50+
[PASS] test_benchmark_packvrf_createPack() (gas: 1391844)
51+
[PASS] test_benchmark_packvrf_openPack() (gas: 119959)
52+
[PASS] test_benchmark_packvrf_openPackAndClaimRewards() (gas: 3599)
53+
Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 212.61ms
5854

5955
Running 3 tests for src/test/benchmark/TokenStakeBenchmark.t.sol:TokenStakeBenchmarkTest
60-
[PASS] test_benchmark_tokenStake_claimRewards() (gas: 68065)
61-
[PASS] test_benchmark_tokenStake_stake() (gas: 178011)
62-
[PASS] test_benchmark_tokenStake_withdraw() (gas: 47890)
63-
Test result: ok. 3 passed; 0 failed; finished in 147.70ms
56+
[PASS] test_benchmark_tokenStake_claimRewards() (gas: 68043)
57+
[PASS] test_benchmark_tokenStake_stake() (gas: 177989)
58+
[PASS] test_benchmark_tokenStake_withdraw() (gas: 47868)
59+
Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 205.79ms
6460

65-
Running 3 tests for src/test/benchmark/PackVRFDirectBenchmark.t.sol:PackVRFDirectBenchmarkTest
66-
[PASS] test_benchmark_packvrf_createPack() (gas: 1391866)
67-
[PASS] test_benchmark_packvrf_openPack() (gas: 119981)
68-
[PASS] test_benchmark_packvrf_openPackAndClaimRewards() (gas: 3621)
69-
Test result: ok. 3 passed; 0 failed; finished in 153.05ms
61+
Running 3 tests for src/test/benchmark/NFTStakeBenchmark.t.sol:NFTStakeBenchmarkTest
62+
[PASS] test_benchmark_nftStake_claimRewards() (gas: 68804)
63+
[PASS] test_benchmark_nftStake_stake_five_tokens() (gas: 549399)
64+
[PASS] test_benchmark_nftStake_withdraw() (gas: 39631)
65+
Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 207.68ms
7066

7167
Running 4 tests for src/test/benchmark/TokenERC721Benchmark.t.sol:TokenERC721BenchmarkTest
72-
[PASS] test_benchmark_tokenERC721_burn() (gas: 10411)
73-
[PASS] test_benchmark_tokenERC721_mintTo() (gas: 149977)
74-
[PASS] test_benchmark_tokenERC721_mintWithSignature_pay_with_ERC20() (gas: 259382)
75-
[PASS] test_benchmark_tokenERC721_mintWithSignature_pay_with_native_token() (gas: 283724)
76-
Test result: ok. 4 passed; 0 failed; finished in 160.72ms
77-
78-
Running 3 tests for src/test/benchmark/DropERC1155Benchmark.t.sol:DropERC1155BenchmarkTest
79-
[PASS] test_bechmark_dropERC1155_claim() (gas: 186668)
80-
[PASS] test_bechmark_dropERC1155_setClaimConditions_five_conditions() (gas: 492331)
81-
[PASS] test_benchmark_dropERC1155_lazyMint() (gas: 123873)
82-
Test result: ok. 3 passed; 0 failed; finished in 758.20ms
68+
[PASS] test_benchmark_tokenERC721_burn() (gas: 10393)
69+
[PASS] test_benchmark_tokenERC721_mintTo() (gas: 149955)
70+
[PASS] test_benchmark_tokenERC721_mintWithSignature_pay_with_ERC20() (gas: 259360)
71+
[PASS] test_benchmark_tokenERC721_mintWithSignature_pay_with_native_token() (gas: 283702)
72+
Test result: ok. 4 passed; 0 failed; 0 skipped; finished in 228.01ms
8373

84-
Running 5 tests for src/test/benchmark/DropERC721Benchmark.t.sol:DropERC721BenchmarkTest
85-
[PASS] test_bechmark_dropERC721_claim_five_tokens() (gas: 211967)
86-
[PASS] test_bechmark_dropERC721_setClaimConditions_five_conditions() (gas: 500859)
87-
[PASS] test_benchmark_dropERC721_lazyMint() (gas: 124544)
88-
[PASS] test_benchmark_dropERC721_lazyMint_for_delayed_reveal() (gas: 226124)
89-
[PASS] test_benchmark_dropERC721_reveal() (gas: 8800)
90-
Test result: ok. 5 passed; 0 failed; finished in 366.44ms
74+
Running 3 tests for src/test/benchmark/TokenERC20Benchmark.t.sol:TokenERC20BenchmarkTest
75+
[PASS] test_benchmark_tokenERC20_mintTo() (gas: 118511)
76+
[PASS] test_benchmark_tokenERC20_mintWithSignature_pay_with_ERC20() (gas: 181716)
77+
[PASS] test_benchmark_tokenERC20_mintWithSignature_pay_with_native_token() (gas: 206128)
78+
Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 215.25ms
9179

9280
Running 2 tests for src/test/benchmark/DropERC20Benchmark.t.sol:DropERC20BenchmarkTest
93-
[PASS] test_bechmark_dropERC20_claim() (gas: 230441)
94-
[PASS] test_bechmark_dropERC20_setClaimConditions_five_conditions() (gas: 501045)
95-
Test result: ok. 2 passed; 0 failed; finished in 373.94ms
81+
[PASS] test_bechmark_dropERC20_claim() (gas: 230419)
82+
[PASS] test_bechmark_dropERC20_setClaimConditions_five_conditions() (gas: 501023)
83+
Test result: ok. 2 passed; 0 failed; 0 skipped; finished in 1.21s
9684

85+
Running 3 tests for src/test/benchmark/DropERC1155Benchmark.t.sol:DropERC1155BenchmarkTest
86+
[PASS] test_bechmark_dropERC1155_claim() (gas: 186646)
87+
[PASS] test_bechmark_dropERC1155_setClaimConditions_five_conditions() (gas: 492309)
88+
[PASS] test_benchmark_dropERC1155_lazyMint() (gas: 123851)
89+
Test result: ok. 3 passed; 0 failed; 0 skipped; finished in 1.22s
90+
91+
Running 5 tests for src/test/benchmark/DropERC721Benchmark.t.sol:DropERC721BenchmarkTest
92+
[PASS] test_bechmark_dropERC721_claim_five_tokens() (gas: 211945)
93+
[PASS] test_bechmark_dropERC721_setClaimConditions_five_conditions() (gas: 500837)
94+
[PASS] test_benchmark_dropERC721_lazyMint() (gas: 124522)
95+
[PASS] test_benchmark_dropERC721_lazyMint_for_delayed_reveal() (gas: 226102)
96+
[PASS] test_benchmark_dropERC721_reveal() (gas: 8783)
97+
Test result: ok. 5 passed; 0 failed; 0 skipped; finished in 636.48ms
98+
99+
Ran 16 test suites: 46 tests passed, 0 failed, 0 skipped (46 total tests)

src/test/TWMultichainRegistry.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ contract TWMultichainRegistryTest is ITWMultichainRegistryData, BaseTest {
5151
vm.stopPrank();
5252
}
5353

54-
function test_interfaceId() public view {
54+
function test_interfaceId() public pure {
5555
console2.logBytes4(type(IPluginMap).interfaceId);
5656
}
5757

src/test/pack/Pack.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ contract PackTest is BaseTest {
209209
Unit tests: `createPack`
210210
//////////////////////////////////////////////////////////////*/
211211

212-
function test_interface() public view {
212+
function test_interface() public pure {
213213
console2.logBytes4(type(IERC20).interfaceId);
214214
console2.logBytes4(type(IERC721).interfaceId);
215215
console2.logBytes4(type(IERC1155).interfaceId);

src/test/pack/PackVRFDirect.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ contract PackVRFDirectTest is BaseTest {
183183
Unit tests: `createPack`
184184
//////////////////////////////////////////////////////////////*/
185185

186-
function test_interface() public view {
186+
function test_interface() public pure {
187187
console2.logBytes4(type(IERC20).interfaceId);
188188
console2.logBytes4(type(IERC721).interfaceId);
189189
console2.logBytes4(type(IERC1155).interfaceId);

0 commit comments

Comments
 (0)