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 beforePUSH0opcode was introduced, thus allowing the usage of this base on not fully EVM compliant chains. - Replace solidity
reverterrors with their inline-assembly equivalence:- Declarations of errors were not removed to support recognition by other libraries.
- Emit
SubscriptionUpdatelogs 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,836to17,800 -
testCancelValid(): from20,777to20,723 -
testExpiresAt(): from42,743to42,056 -
testExpiresAtInvalidToken(): from12,901to12,865 -
testExtendSubscriptionInvalidToken(): from13,039to12,991 -
testIsRenewableInvalidToken(): from12,831to12,795 -
testRenewalDurationTooLong(): from47,961to47,581 -
testRenewalDurationTooShort(): from47,664to47,583 -
testRenewalExistingSubscription(): from145,175to144,285 -
testRenewalInsufficientPayment(): from26,874to26,650 -
testRenewalInvalidTokenId(): from22,316to22,304 -
testRenewalNewSubscription(): from53,103to52,702 -
testRenewalNotOwner(): from17,961to17,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.