ERC5643 icon indicating copy to clipboard operation
ERC5643 copied to clipboard

Gas optimize ERC5643

Open ByteEVM opened this issue 1 year ago • 0 comments

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 before PUSH0 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(): from 17,836 to 17,800
  • testCancelValid(): from 20,777 to 20,723
  • testExpiresAt(): from 42,743 to 42,056
  • testExpiresAtInvalidToken(): from 12,901 to 12,865
  • testExtendSubscriptionInvalidToken(): from 13,039 to 12,991
  • testIsRenewableInvalidToken(): from 12,831 to 12,795
  • testRenewalDurationTooLong(): from 47,961 to 47,581
  • testRenewalDurationTooShort(): from 47,664 to 47,583
  • testRenewalExistingSubscription(): from 145,175 to 144,285
  • testRenewalInsufficientPayment(): from 26,874 to 26,650
  • testRenewalInvalidTokenId(): from 22,316 to 22,304
  • testRenewalNewSubscription(): from 53,103 to 52,702
  • testRenewalNotOwner(): from 17,961 to 17,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.

ByteEVM avatar Jan 04 '24 18:01 ByteEVM