ERC5643
ERC5643 copied to clipboard
Gas optimize ERC5643
I have applied some optimizations on repository-owned code whilst keeping it readable:
Changes
- Bump compiler version from
0.8.13
->0.8.19
, right beforePUSH0
opcode was introduced, thus allowing the usage of this base on not fully EVM compliant chains. - Replace solidity
revert
errors with their inline-assembly equivalence:- Declarations of errors were not removed to support recognition by other libraries.
- Emit
SubscriptionUpdate
logs and perform storage-related operations with inline-assembly. - Perform other casting and comparisons with inline-assembly to further optimize checks.
All forge tests passed successfully, with these before and after gas costs:
-
testCancelNotOwner()
: from17,836
to17,800
-
testCancelValid()
: from20,777
to20,723
-
testExpiresAt()
: from42,743
to42,056
-
testExpiresAtInvalidToken()
: from12,901
to12,865
-
testExtendSubscriptionInvalidToken()
: from13,039
to12,991
-
testIsRenewableInvalidToken()
: from12,831
to12,795
-
testRenewalDurationTooLong()
: from47,961
to47,581
-
testRenewalDurationTooShort()
: from47,664
to47,583
-
testRenewalExistingSubscription()
: from145,175
to144,285
-
testRenewalInsufficientPayment()
: from26,874
to26,650
-
testRenewalInvalidTokenId()
: from22,316
to22,304
-
testRenewalNewSubscription()
: from53,103
to52,702
-
testRenewalNotOwner()
: from17,961
to17,913
Notes
- More optimizations would have been possible (e.g. using
STATICCALL
) at the cost of code readability. - Further gas optimization suggestions would be relying on gas-efficient libraries such as ERC721A for NFT allowance and existence checks.