Skip to content

Commit

Permalink
♻️ Make ERC20 Module-Friendly (#234)
Browse files Browse the repository at this point in the history
### 🕓 Changelog

This PR refactors the `ERC20` contract to make it module-friendly and
ready for the breaking `0.4.0` release. Furthermore, I also bump the
`pragma`s to the latest release candidate
[`v0.4.0rc2`](https://github.com/vyperlang/vyper/releases/tag/v0.4.0rc2).
Finally, please note that `decimals` are now an `immutable` parameter
and the initial supply logic has been removed from the `ERC20` master
contract and delegated to the user instead.

---------

Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
  • Loading branch information
pcaversaccio authored Apr 15, 2024
1 parent 2e432ac commit f5387eb
Show file tree
Hide file tree
Showing 54 changed files with 298 additions and 313 deletions.
78 changes: 39 additions & 39 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -259,77 +259,77 @@ ERC1155Test:testUriBaseAndTokenUriNotSet() (gas: 2969060)
ERC1155Test:testUriBaseAndTokenUriSet() (gas: 65810)
ERC1155Test:testUriNoBaseURI() (gas: 3019122)
ERC1155Test:testUriNoTokenUri() (gas: 20507)
ERC20Invariants:invariantOwner() (runs: 256, calls: 3840, reverts: 3387)
ERC20Invariants:invariantTotalSupply() (runs: 256, calls: 3840, reverts: 3387)
ERC20Invariants:statefulFuzzOwner() (runs: 256, calls: 3840, reverts: 3386)
ERC20Invariants:statefulFuzzTotalSupply() (runs: 256, calls: 3840, reverts: 3386)
ERC20Test:testApproveExceedingBalanceCase1() (gas: 40983)
ERC20Test:testApproveExceedingBalanceCase2() (gas: 48004)
ERC20Test:testApproveFromZeroAddress() (gas: 13438)
ERC20Test:testApproveSuccessCase1() (gas: 43686)
ERC20Test:testApproveSuccessCase2() (gas: 50789)
ERC20Test:testApproveToZeroAddress() (gas: 16731)
ERC20Test:testBalanceOf() (gas: 18668)
ERC20Test:testBurnFromExceedingBalance() (gas: 31343)
ERC20Test:testBurnFromFromZeroAddress() (gas: 15812)
ERC20Test:testBurnFromInsufficientAllowanceCase1() (gas: 41740)
ERC20Test:testBurnFromInsufficientAllowanceCase2() (gas: 41771)
ERC20Test:testBurnFromSuccessCase1() (gas: 40310)
ERC20Test:testBurnFromSuccessCase2() (gas: 65863)
ERC20Test:testBurnFromUnlimitedAllowance() (gas: 50663)
ERC20Test:testBurnFromZeroAddress() (gas: 11542)
ERC20Test:testBurnInvalidAmount() (gas: 14718)
ERC20Test:testBurnSuccessCase1() (gas: 25797)
ERC20Test:testBurnSuccessCase2() (gas: 31362)
ERC20Test:testCachedDomainSeparator() (gas: 10588)
ERC20Test:testDomainSeparator() (gas: 11838)
ERC20Test:testEIP712Domain() (gas: 18802)
ERC20Test:testBalanceOf() (gas: 18674)
ERC20Test:testBurnFromExceedingBalance() (gas: 31372)
ERC20Test:testBurnFromFromZeroAddress() (gas: 15849)
ERC20Test:testBurnFromInsufficientAllowanceCase1() (gas: 41777)
ERC20Test:testBurnFromInsufficientAllowanceCase2() (gas: 41808)
ERC20Test:testBurnFromSuccessCase1() (gas: 40347)
ERC20Test:testBurnFromSuccessCase2() (gas: 65900)
ERC20Test:testBurnFromUnlimitedAllowance() (gas: 50700)
ERC20Test:testBurnFromZeroAddress() (gas: 11565)
ERC20Test:testBurnInvalidAmount() (gas: 14741)
ERC20Test:testBurnSuccessCase1() (gas: 25820)
ERC20Test:testBurnSuccessCase2() (gas: 31385)
ERC20Test:testCachedDomainSeparator() (gas: 10578)
ERC20Test:testDomainSeparator() (gas: 11808)
ERC20Test:testEIP712Domain() (gas: 18756)
ERC20Test:testFuzzApproveSuccess(address,uint256) (runs: 256, μ: 40347, ~: 41203)
ERC20Test:testFuzzBurnFromInsufficientAllowance(address,uint256,uint8) (runs: 256, μ: 207186, ~: 207496)
ERC20Test:testFuzzBurnFromSuccess(address,uint256) (runs: 256, μ: 340887, ~: 341770)
ERC20Test:testFuzzBurnInvalidAmount(address,uint256) (runs: 256, μ: 16673, ~: 16673)
ERC20Test:testFuzzBurnSuccessCase(uint256) (runs: 256, μ: 316124, ~: 315925)
ERC20Test:testFuzzDomainSeparator(uint8) (runs: 256, μ: 12054, ~: 12074)
ERC20Test:testFuzzEIP712Domain(bytes1,uint8,bytes32,uint256[]) (runs: 256, μ: 22283, ~: 22287)
ERC20Test:testFuzzBurnFromInsufficientAllowance(address,uint256,uint8) (runs: 256, μ: 207271, ~: 207473)
ERC20Test:testFuzzBurnFromSuccess(address,uint256) (runs: 256, μ: 340816, ~: 341694)
ERC20Test:testFuzzBurnInvalidAmount(address,uint256) (runs: 256, μ: 16696, ~: 16696)
ERC20Test:testFuzzBurnSuccessCase(uint256) (runs: 256, μ: 316038, ~: 315838)
ERC20Test:testFuzzDomainSeparator(uint8) (runs: 256, μ: 12026, ~: 12044)
ERC20Test:testFuzzEIP712Domain(bytes1,uint8,bytes32,uint256[]) (runs: 256, μ: 22215, ~: 22196)
ERC20Test:testFuzzMintNonMinter(string,uint256) (runs: 256, μ: 13258, ~: 13186)
ERC20Test:testFuzzMintSuccess(string,uint256) (runs: 256, μ: 54985, ~: 55948)
ERC20Test:testFuzzPermitInvalid(string,string,uint16) (runs: 256, μ: 45394, ~: 45392)
ERC20Test:testFuzzPermitSuccess(string,string,uint16) (runs: 256, μ: 71112, ~: 71108)
ERC20Test:testFuzzMintSuccess(string,uint256) (runs: 256, μ: 54884, ~: 55954)
ERC20Test:testFuzzPermitInvalid(string,string,uint16) (runs: 256, μ: 45374, ~: 45372)
ERC20Test:testFuzzPermitSuccess(string,string,uint16) (runs: 256, μ: 71069, ~: 71065)
ERC20Test:testFuzzRenounceOwnershipNonOwner(address) (runs: 256, μ: 14051, ~: 14051)
ERC20Test:testFuzzRenounceOwnershipSuccess(address) (runs: 256, μ: 48675, ~: 48660)
ERC20Test:testFuzzSetMinterNonOwner(address,string) (runs: 256, μ: 15873, ~: 15872)
ERC20Test:testFuzzSetMinterSuccess(string) (runs: 256, μ: 33792, ~: 33792)
ERC20Test:testFuzzTransferFromInsufficientAllowance(address,address,uint256,uint8) (runs: 256, μ: 205590, ~: 206007)
ERC20Test:testFuzzTransferFromSuccess(address,address,uint256) (runs: 256, μ: 234695, ~: 235531)
ERC20Test:testFuzzTransferFromInsufficientAllowance(address,address,uint256,uint8) (runs: 256, μ: 205485, ~: 205954)
ERC20Test:testFuzzTransferFromSuccess(address,address,uint256) (runs: 256, μ: 234403, ~: 235478)
ERC20Test:testFuzzTransferInvalidAmount(address,address,uint256) (runs: 256, μ: 17068, ~: 17068)
ERC20Test:testFuzzTransferOwnershipNonOwner(address,address) (runs: 256, μ: 14163, ~: 14163)
ERC20Test:testFuzzTransferOwnershipSuccess(address,address) (runs: 256, μ: 75698, ~: 75672)
ERC20Test:testFuzzTransferSuccess(address,uint256) (runs: 256, μ: 206238, ~: 206924)
ERC20Test:testFuzzTransferSuccess(address,uint256) (runs: 256, μ: 206185, ~: 206871)
ERC20Test:testHasOwner() (gas: 12659)
ERC20Test:testInitialSetup() (gas: 1585065)
ERC20Test:testInitialSetup() (gas: 1596955)
ERC20Test:testMintNonMinter() (gas: 12745)
ERC20Test:testMintOverflow() (gas: 16974)
ERC20Test:testMintSuccess() (gas: 51998)
ERC20Test:testMintSuccess() (gas: 52004)
ERC20Test:testMintToZeroAddress() (gas: 15711)
ERC20Test:testPermitBadChainId() (gas: 43919)
ERC20Test:testPermitBadNonce() (gas: 41024)
ERC20Test:testPermitExpiredDeadline() (gas: 17509)
ERC20Test:testPermitOtherSignature() (gas: 41988)
ERC20Test:testPermitReplaySignature() (gas: 73869)
ERC20Test:testPermitSuccess() (gas: 70078)
ERC20Test:testPermitBadChainId() (gas: 43909)
ERC20Test:testPermitBadNonce() (gas: 41027)
ERC20Test:testPermitExpiredDeadline() (gas: 17476)
ERC20Test:testPermitOtherSignature() (gas: 41968)
ERC20Test:testPermitReplaySignature() (gas: 73862)
ERC20Test:testPermitSuccess() (gas: 70035)
ERC20Test:testRenounceOwnershipNonOwner() (gas: 10928)
ERC20Test:testRenounceOwnershipSuccess() (gas: 22925)
ERC20Test:testSetMinterNonOwner() (gas: 12816)
ERC20Test:testSetMinterRemoveOwnerAddress() (gas: 13939)
ERC20Test:testSetMinterSuccess() (gas: 33536)
ERC20Test:testSetMinterToZeroAddress() (gas: 15936)
ERC20Test:testTotalSupply() (gas: 11555)
ERC20Test:testTotalSupply() (gas: 11561)
ERC20Test:testTransferFromExceedingBalance() (gas: 32754)
ERC20Test:testTransferFromFromZeroAddress() (gas: 18009)
ERC20Test:testTransferFromInsufficientAllowanceCase1() (gas: 43555)
ERC20Test:testTransferFromInsufficientAllowanceCase2() (gas: 43564)
ERC20Test:testTransferFromSuccess() (gas: 61800)
ERC20Test:testTransferFromToZeroAddress() (gas: 32817)
ERC20Test:testTransferFromUnlimitedAllowance() (gas: 72147)
ERC20Test:testTransferFromZeroAddress() (gas: 19910)
ERC20Test:testTransferFromZeroAddress() (gas: 19933)
ERC20Test:testTransferInvalidAmount() (gas: 15692)
ERC20Test:testTransferOwnershipNonOwner() (gas: 12706)
ERC20Test:testTransferOwnershipSuccess() (gas: 54050)
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
### ♻️ Refactoring

- **Authentication**
- [`AccessControl`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/auth/AccessControl.vy): Make `AccessControl` module-friendly. ([#216](https://github.com/pcaversaccio/snekmate/pull/216))
- [`Ownable`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/auth/Ownable.vy): Make `Ownable` module-friendly. ([#218](https://github.com/pcaversaccio/snekmate/pull/218))
- [`Ownable2Step`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/auth/Ownable2Step.vy): Make `Ownable2Step` module-friendly. ([#219](https://github.com/pcaversaccio/snekmate/pull/219))
- [`AccessControl`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/auth/AccessControl.vy): Make `AccessControl` module-friendly. ([#216](https://github.com/pcaversaccio/snekmate/pull/216))
- **Extensions**
- [`ERC2981`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/extensions/ERC2981.vy): Make `ERC2981` module-friendly. ([#233](https://github.com/pcaversaccio/snekmate/pull/233))
- **Governance**
- [`TimelockController`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/governance/TimelockController.vy): Make `TimelockController` module-friendly. ([#220](https://github.com/pcaversaccio/snekmate/pull/220))
- **Tokens**
- [`ERC20`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/tokens/ERC20.vy): Make `ERC20` module-friendly. ([#233](https://github.com/pcaversaccio/snekmate/pull/233))
- **Utility Functions**
- [`Base64`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/Base64.vy): Make `Base64` module-friendly. ([#222](https://github.com/pcaversaccio/snekmate/pull/222))
- [`BatchDistributor`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/BatchDistributor.vy): Make `BatchDistributor` module-friendly. ([#223](https://github.com/pcaversaccio/snekmate/pull/223))
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ src
│ ├── ERC20 — "Modern and Gas-Efficient ERC-20 + EIP-2612 Implementation"
│ ├── ERC721 — "Modern and Gas-Efficient ERC-721 + EIP-4494 Implementation"
│ ├── ERC1155 — "Modern and Gas-Efficient ERC-1155 Implementation"
│ └── interfaces
│ ├── IERC20Permit — "EIP-2612 Interface Definition"
│ ├── IERC721Enumerable — "EIP-721 Optional Enumeration Interface Definition"
│ ├── IERC721Metadata — "EIP-721 Optional Metadata Interface Definition"
│ ├── IERC721Permit — "EIP-4494 Interface Definition"
│ ├── IERC721Receiver — "EIP-721 Token Receiver Interface Definition"
│ ├── IERC1155 — "EIP-1155 Interface Definition"
│ ├── IERC1155MetadataURI — "EIP-1155 Optional Metadata Interface Definition"
│ ├── IERC1155Receiver — "EIP-1155 Token Receiver Interface Definition"
│ └── IERC4906 — "EIP-4906 Interface Definition"
│ ├── interfaces
│ │ ├── IERC20Permit — "EIP-2612 Interface Definition"
│ │ ├── IERC721Enumerable — "EIP-721 Optional Enumeration Interface Definition"
│ │ ├── IERC721Metadata — "EIP-721 Optional Metadata Interface Definition"
│ │ ├── IERC721Permit — "EIP-4494 Interface Definition"
│ │ ├── IERC721Receiver — "EIP-721 Token Receiver Interface Definition"
│ │ ├── IERC1155 — "EIP-1155 Interface Definition"
│ │ ├── IERC1155MetadataURI — "EIP-1155 Optional Metadata Interface Definition"
│ │ ├── IERC1155Receiver — "EIP-1155 Token Receiver Interface Definition"
│ │ └── IERC4906 — "EIP-4906 Interface Definition"
│ └── mocks
│ └── ERC20Mock — "ERC20 Module Reference Implementation"
└── utils
├── Base64 — "Base64 Encoding and Decoding Functions"
├── BatchDistributor — "Batch Sending Both Native and ERC-20 Tokens"
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/auth/AccessControl.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title Multi-Role-Based Access Control Functions
@custom:contract-name AccessControl
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/auth/Ownable.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title Owner-Based Access Control Functions
@custom:contract-name Ownable
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/auth/Ownable2Step.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title 2-Step Ownership Transfer Functions
@custom:contract-name Ownable2Step
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/auth/interfaces/IAccessControl.vyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title AccessControl Interface Definition
@custom:contract-name IAccessControl
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/auth/mocks/AccessControlMock.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title AccessControl Module Reference Implementation
@custom:contract-name AccessControlMock
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/auth/mocks/Ownable2StepMock.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title Ownable2Step Module Reference Implementation
@custom:contract-name Ownable2StepMock
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/auth/mocks/OwnableMock.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title Ownable Module Reference Implementation
@custom:contract-name OwnableMock
Expand Down
4 changes: 2 additions & 2 deletions src/snekmate/extensions/ERC2981.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title ERC-721 and ERC-1155 Compatible ERC-2981 Reference Implementation
@custom:contract-name ERC2981
Expand Down Expand Up @@ -50,7 +50,7 @@
internal
def _burn(token_id: uint256):
...
erc721._burn(token_id)
erc2981._reset_token_royalty(token_id)
```
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/extensions/ERC4626.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title Modern and Gas-Efficient ERC-4626 Tokenised Vault Implementation
@custom:contract-name ERC4626
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/extensions/interfaces/IERC2981.vyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title EIP-2981 Interface Definition
@custom:contract-name IERC2981
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/extensions/mocks/ERC2981Mock.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title ERC2981 Module Reference Implementation
@custom:contract-name ERC2981Mock
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/governance/TimelockController.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title Multi-Role-Based Timelock Controller Reference Implementation
@custom:contract-name TimelockController
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/governance/mocks/TimelockControllerMock.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title TimelockController Module Reference Implementation
@custom:contract-name TimelockControllerMock
Expand Down
2 changes: 1 addition & 1 deletion src/snekmate/tokens/ERC1155.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ~=0.4.0rc1
# pragma version ~=0.4.0rc2
"""
@title Modern and Gas-Efficient ERC-1155 Implementation
@custom:contract-name ERC1155
Expand Down
Loading

0 comments on commit f5387eb

Please sign in to comment.