From 172e509dc85e1a459845709f98b1078fef23e431 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Thu, 14 Mar 2024 10:04:52 +0000 Subject: [PATCH] Optimize --- .gas-snapshot | 44 ++++++++++++++++++++++---------------------- src/DN69.sol | 14 ++++++-------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index e64fcc4..b338c8e 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -99,7 +99,7 @@ BenchTest:testMintPandora_13() (gas: 967960) BenchTest:testMintPandora_14() (gas: 1037484) BenchTest:testMintPandora_15() (gas: 1107011) BenchTest:testMintPandora_16() (gas: 1176495) -BenchTest:test__codesize() (gas: 39261) +BenchTest:test__codesize() (gas: 39269) DN404CustomUnitTest:testInitializeCorrectUnitSuccess() (gas: 129992) DN404CustomUnitTest:testInitializeWithUnitTooLargeReverts() (gas: 33824) DN404CustomUnitTest:testInitializeWithZeroUnitReverts() (gas: 13897) @@ -177,37 +177,37 @@ DN404Test:test__codesize() (gas: 57768) DN69OnlyERC20Test:testApprove() (gas: 35770) DN69OnlyERC20Test:testApprove(address,uint256) (runs: 256, μ: 30077, ~: 31321) DN69OnlyERC20Test:testBurn() (gas: 50088) -DN69OnlyERC20Test:testBurn(address,uint256,uint256) (runs: 256, μ: 51131, ~: 51289) -DN69OnlyERC20Test:testBurnInsufficientBalanceReverts(address,uint256,uint256) (runs: 256, μ: 44306, ~: 44370) +DN69OnlyERC20Test:testBurn(address,uint256,uint256) (runs: 256, μ: 51162, ~: 51289) +DN69OnlyERC20Test:testBurnInsufficientBalanceReverts(address,uint256,uint256) (runs: 256, μ: 44302, ~: 44370) DN69OnlyERC20Test:testInfiniteApproveTransferFrom() (gas: 102308) DN69OnlyERC20Test:testMaxSupplyTrick(uint256) (runs: 256, μ: 541, ~: 541) DN69OnlyERC20Test:testMetadata() (gas: 10090) DN69OnlyERC20Test:testMint() (gas: 45622) DN69OnlyERC20Test:testMintOverMaxLimitReverts() (gas: 41669) -DN69OnlyERC20Test:testMintz(address,uint256) (runs: 256, μ: 45935, ~: 46044) +DN69OnlyERC20Test:testMintz(address,uint256) (runs: 256, μ: 45909, ~: 46044) DN69OnlyERC20Test:testTransfer() (gas: 75015) -DN69OnlyERC20Test:testTransfer(address,uint256) (runs: 256, μ: 75372, ~: 75472) +DN69OnlyERC20Test:testTransfer(address,uint256) (runs: 256, μ: 75354, ~: 75472) DN69OnlyERC20Test:testTransferFrom() (gas: 84766) -DN69OnlyERC20Test:testTransferFrom(address,address,address,uint256,uint256) (runs: 256, μ: 105584, ~: 107714) +DN69OnlyERC20Test:testTransferFrom(address,address,address,uint256,uint256) (runs: 256, μ: 105596, ~: 107714) DN69OnlyERC20Test:testTransferFromInsufficientAllowanceReverts() (gas: 68344) -DN69OnlyERC20Test:testTransferFromInsufficientAllowanceReverts(address,uint256,uint256) (runs: 256, μ: 68865, ~: 69454) +DN69OnlyERC20Test:testTransferFromInsufficientAllowanceReverts(address,uint256,uint256) (runs: 256, μ: 68948, ~: 69445) DN69OnlyERC20Test:testTransferFromInsufficientBalanceReverts() (gas: 75287) -DN69OnlyERC20Test:testTransferFromInsufficientBalanceReverts(address,uint256,uint256) (runs: 256, μ: 76270, ~: 76408) +DN69OnlyERC20Test:testTransferFromInsufficientBalanceReverts(address,uint256,uint256) (runs: 256, μ: 76340, ~: 76408) DN69OnlyERC20Test:testTransferInsufficientBalanceReverts() (gas: 66753) -DN69OnlyERC20Test:testTransferInsufficientBalanceReverts(address,uint256,uint256) (runs: 256, μ: 67784, ~: 67840) -DN69OnlyERC20Test:test__codesize() (gas: 27362) -DN69Test:testERC1155Methods(uint256) (runs: 256, μ: 4333900, ~: 4579672) -DN69Test:testERC1155MethodsSelfTransfers(uint256) (runs: 256, μ: 2391674, ~: 2061834) -DN69Test:testFindOwnedIds() (gas: 2644645) -DN69Test:testMintToNonERC155RecipientReverts(uint256) (runs: 256, μ: 955944, ~: 796373) -DN69Test:testMintToRevertingERC155RecipientReverts(uint256) (runs: 256, μ: 1430314, ~: 979013) -DN69Test:testMintToZeroReverts(uint256) (runs: 256, μ: 733644, ~: 663451) -DN69Test:testMixed(uint256) (runs: 256, μ: 9087516, ~: 6381628) -DN69Test:testSafeBatchTransferFromToERC1155Recipient(uint256) (runs: 256, μ: 2278331, ~: 2180026) -DN69Test:testSafeTransferFromToERC1155Recipient(uint256) (runs: 256, μ: 1773197, ~: 1475327) -DN69Test:testTransferFromToERC1155Recipient(uint256) (runs: 256, μ: 2878031, ~: 2865161) -DN69Test:testTransferMixedReverts(uint256) (runs: 256, μ: 3561882, ~: 2679134) -DN69Test:test__codesize() (gas: 62522) +DN69OnlyERC20Test:testTransferInsufficientBalanceReverts(address,uint256,uint256) (runs: 256, μ: 67779, ~: 67840) +DN69OnlyERC20Test:test__codesize() (gas: 27370) +DN69Test:testERC1155Methods(uint256) (runs: 256, μ: 4321643, ~: 4575954) +DN69Test:testERC1155MethodsSelfTransfers(uint256) (runs: 256, μ: 2372618, ~: 2063511) +DN69Test:testFindOwnedIds() (gas: 2646889) +DN69Test:testMintToNonERC155RecipientReverts(uint256) (runs: 256, μ: 1004631, ~: 798245) +DN69Test:testMintToRevertingERC155RecipientReverts(uint256) (runs: 256, μ: 1365031, ~: 979335) +DN69Test:testMintToZeroReverts(uint256) (runs: 256, μ: 740676, ~: 664020) +DN69Test:testMixed(uint256) (runs: 256, μ: 9409669, ~: 7275644) +DN69Test:testSafeBatchTransferFromToERC1155Recipient(uint256) (runs: 256, μ: 2233737, ~: 2181630) +DN69Test:testSafeTransferFromToERC1155Recipient(uint256) (runs: 256, μ: 1768391, ~: 1475851) +DN69Test:testTransferFromToERC1155Recipient(uint256) (runs: 256, μ: 2880315, ~: 2866414) +DN69Test:testTransferMixedReverts(uint256) (runs: 256, μ: 3309253, ~: 2659490) +DN69Test:test__codesize() (gas: 62530) MappingsTest:testAddressPairMapSetAndGet(address[2],address[2],uint256,uint256) (runs: 256, μ: 45753, ~: 47075) MappingsTest:testBitmapSetAndGet(uint256) (runs: 256, μ: 445667, ~: 397521) MappingsTest:testBitmapSetAndGet(uint256,uint256,bool,bool) (runs: 256, μ: 25662, ~: 26319) diff --git a/src/DN69.sol b/src/DN69.sol index fdeabbb..24912ff 100644 --- a/src/DN69.sol +++ b/src/DN69.sol @@ -1050,8 +1050,6 @@ abstract contract DN69 { /// @dev Fallback modifier for the regular ERC1155 functions and other functions. modifier dn69Fallback() virtual { - DN69Storage storage $ = _getDN69Storage(); - uint256 fnSelector = _calldataload(0x00) >> 224; // We hide the regular ERC1155 functions that has variable amounts @@ -1073,12 +1071,10 @@ abstract contract DN69 { if (fnSelector == 0x2eb2c2d6) { uint256[] memory ids = _calldataUint256Array(0x44); unchecked { - uint256 amountsOffset = _calldataload(0x64) + 0x04; + uint256[] memory amounts = _calldataUint256Array(0x64); uint256 n = ids.length; - if (n != _calldataload(amountsOffset)) revert ArrayLengthsMismatch(); - for (uint256 o = amountsOffset + (n << 5); o != amountsOffset; o -= 0x20) { - if (_calldataload(o) != 1) revert InvalidNFTAmount(); - } + if (n != amounts.length) revert ArrayLengthsMismatch(); + while (n-- != 0) if (_get(amounts, n) != 1) revert InvalidNFTAmount(); } _safeBatchTransferNFTs( msg.sender, @@ -1109,7 +1105,9 @@ abstract contract DN69 { } // `balanceOf(address,uint256)`. if (fnSelector == 0x00fdd58e) { - _return(_toUint(owns(address(uint160(_calldataload(0x04))), _calldataload(0x24)))); + address owner = address(uint160(_calldataload(0x04))); + uint256 id = _calldataload(0x24); + _return(_toUint(owns(owner, id))); } // `implementsDN69()`. if (fnSelector == 0x0e0b0984) {