Skip to content

Commit a4122dd

Browse files
committed
Broke ERC20 functionalty into smaller facets
1 parent b410d90 commit a4122dd

File tree

9 files changed

+162
-112
lines changed

9 files changed

+162
-112
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@
2525
"strings": true
2626
},
2727
"editor.suggestOnTriggerCharacters": true,
28-
"editor.suggestSelection": "first"
28+
"editor.suggestSelection": "first",
29+
"cSpell.words": [
30+
"accesscontrol"
31+
]
2932
}

src/token/ERC20/ERC20/ERC20BurnFacet.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ contract ERC20BurnFacet {
2929
/**
3030
* @dev Storage position determined by the keccak256 hash of the diamond storage identifier.
3131
*/
32-
bytes32 constant STORAGE_POSITION = keccak256("compose.erc20");
32+
bytes32 constant STORAGE_POSITION = keccak256("compose.erc20.transfer");
3333

3434
/**
3535
* @dev ERC-8042 compliant storage struct for ERC20 token data.
36-
* @custom:storage-location erc8042:compose.erc20
36+
* @custom:storage-location erc8042:compose.erc20.transfer
3737
*/
38-
struct ERC20Storage {
38+
struct ERC20TransferStorage {
3939
mapping(address owner => uint256 balance) balanceOf;
4040
uint256 totalSupply;
4141
mapping(address owner => mapping(address spender => uint256 allowance)) allowance;
@@ -46,7 +46,7 @@ contract ERC20BurnFacet {
4646
* @dev Uses inline assembly to set the storage slot reference.
4747
* @return s The ERC20 storage struct reference.
4848
*/
49-
function getStorage() internal pure returns (ERC20Storage storage s) {
49+
function getStorage() internal pure returns (ERC20TransferStorage storage s) {
5050
bytes32 position = STORAGE_POSITION;
5151
assembly {
5252
s.slot := position
@@ -59,7 +59,7 @@ contract ERC20BurnFacet {
5959
* @param _value The amount of tokens to burn.
6060
*/
6161
function burn(uint256 _value) external {
62-
ERC20Storage storage s = getStorage();
62+
ERC20TransferStorage storage s = getStorage();
6363
uint256 balance = s.balanceOf[msg.sender];
6464
if (balance < _value) {
6565
revert ERC20InsufficientBalance(msg.sender, balance, _value);
@@ -78,7 +78,7 @@ contract ERC20BurnFacet {
7878
* @param _value The amount of tokens to burn.
7979
*/
8080
function burnFrom(address _account, uint256 _value) external {
81-
ERC20Storage storage s = getStorage();
81+
ERC20TransferStorage storage s = getStorage();
8282
uint256 currentAllowance = s.allowance[_account][msg.sender];
8383
if (currentAllowance < _value) {
8484
revert ERC20InsufficientAllowance(msg.sender, currentAllowance, _value);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.30;
3+
4+
contract ERC20MetadataFacet {
5+
6+
/**
7+
* @dev Storage position determined by the keccak256 hash of the diamond storage identifier.
8+
*/
9+
bytes32 constant STORAGE_POSITION = keccak256("compose.erc20.metadata");
10+
11+
/**
12+
* @dev ERC-8042 compliant storage struct for ERC20 token data.
13+
* @custom:storage-location erc8042:compose.erc20.metadata
14+
*/
15+
struct ERC20MetadataStorage {
16+
string name;
17+
string symbol;
18+
uint8 decimals;
19+
}
20+
21+
/**
22+
* @notice Returns the ERC20 storage struct from the predefined diamond storage slot.
23+
* @dev Uses inline assembly to set the storage slot reference.
24+
* @return s The ERC20 storage struct reference.
25+
*/
26+
function getStorage() internal pure returns (ERC20MetadataStorage storage s) {
27+
bytes32 position = STORAGE_POSITION;
28+
assembly {
29+
s.slot := position
30+
}
31+
}
32+
33+
/**
34+
* @notice Returns the name of the token.
35+
* @return The token name.
36+
*/
37+
function name() external view returns (string memory) {
38+
return getStorage().name;
39+
}
40+
41+
/**
42+
* @notice Returns the symbol of the token.
43+
* @return The token symbol.
44+
*/
45+
function symbol() external view returns (string memory) {
46+
return getStorage().symbol;
47+
}
48+
49+
/**
50+
* @notice Returns the number of decimals used for token precision.
51+
* @return The number of decimals.
52+
*/
53+
function decimals() external view returns (uint8) {
54+
return getStorage().decimals;
55+
}
56+
}

src/token/ERC20/ERC20/ERC20Mod.sol

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,24 @@ event Approval(address indexed _owner, address indexed _spender, uint256 _value)
6060
/*
6161
* @notice Storage slot identifier, defined using keccak256 hash of the library diamond storage identifier.
6262
*/
63-
bytes32 constant STORAGE_POSITION = keccak256("compose.erc20");
63+
bytes32 constant STORAGE_POSITION = keccak256("compose.erc20.transfer");
6464

6565
/*
6666
* @notice ERC-20 storage layout using the ERC-8042 standard.
67-
* @custom:storage-location erc8042:compose.erc20
67+
* @custom:storage-location erc8042:compose.erc20.transfer
6868
*/
69-
struct ERC20Storage {
69+
struct ERC20TransferStorage {
7070
mapping(address owner => uint256 balance) balanceOf;
7171
uint256 totalSupply;
7272
mapping(address owner => mapping(address spender => uint256 allowance)) allowance;
73-
uint8 decimals;
74-
string name;
75-
string symbol;
7673
}
7774

7875
/**
7976
* @notice Returns a pointer to the ERC-20 storage struct.
8077
* @dev Uses inline assembly to bind the storage struct to the fixed storage position.
8178
* @return s The ERC-20 storage struct.
8279
*/
83-
function getStorage() pure returns (ERC20Storage storage s) {
80+
function getStorage() pure returns (ERC20TransferStorage storage s) {
8481
bytes32 position = STORAGE_POSITION;
8582
assembly {
8683
s.slot := position
@@ -94,7 +91,7 @@ function getStorage() pure returns (ERC20Storage storage s) {
9491
* @param _value The number of tokens to mint.
9592
*/
9693
function mint(address _account, uint256 _value) {
97-
ERC20Storage storage s = getStorage();
94+
ERC20TransferStorage storage s = getStorage();
9895
if (_account == address(0)) {
9996
revert ERC20InvalidReceiver(address(0));
10097
}
@@ -110,7 +107,7 @@ function mint(address _account, uint256 _value) {
110107
* @param _value The number of tokens to burn.
111108
*/
112109
function burn(address _account, uint256 _value) {
113-
ERC20Storage storage s = getStorage();
110+
ERC20TransferStorage storage s = getStorage();
114111
if (_account == address(0)) {
115112
revert ERC20InvalidSender(address(0));
116113
}
@@ -133,7 +130,7 @@ function burn(address _account, uint256 _value) {
133130
* @param _value The number of tokens to transfer.
134131
*/
135132
function transferFrom(address _from, address _to, uint256 _value) {
136-
ERC20Storage storage s = getStorage();
133+
ERC20TransferStorage storage s = getStorage();
137134
if (_from == address(0)) {
138135
revert ERC20InvalidSender(address(0));
139136
}
@@ -165,7 +162,7 @@ function transferFrom(address _from, address _to, uint256 _value) {
165162
* @param _value The number of tokens to transfer.
166163
*/
167164
function transfer(address _to, uint256 _value) {
168-
ERC20Storage storage s = getStorage();
165+
ERC20TransferStorage storage s = getStorage();
169166
if (_to == address(0)) {
170167
revert ERC20InvalidReceiver(address(0));
171168
}
@@ -190,7 +187,7 @@ function approve(address _spender, uint256 _value) {
190187
if (_spender == address(0)) {
191188
revert ERC20InvalidSpender(address(0));
192189
}
193-
ERC20Storage storage s = getStorage();
190+
ERC20TransferStorage storage s = getStorage();
194191
s.allowance[msg.sender][_spender] = _value;
195192
emit Approval(msg.sender, _spender, _value);
196193
}

src/token/ERC20/ERC20/ERC20Facet.sol renamed to src/token/ERC20/ERC20/ERC20TransferFacet.sol

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.8.30;
33

4-
contract ERC20Facet {
4+
contract ERC20TransferFacet {
55
/**
66
* @notice Thrown when an account has insufficient balance for a transfer or burn.
77
* @param _sender Address attempting the transfer.
@@ -55,56 +55,29 @@ contract ERC20Facet {
5555
/**
5656
* @dev Storage position determined by the keccak256 hash of the diamond storage identifier.
5757
*/
58-
bytes32 constant STORAGE_POSITION = keccak256("compose.erc20");
58+
bytes32 constant STORAGE_POSITION = keccak256("compose.erc20.transfer");
5959

6060
/**
6161
* @dev ERC-8042 compliant storage struct for ERC20 token data.
62-
* @custom:storage-location erc8042:compose.erc20
62+
* @custom:storage-location erc8042:compose.erc20.transfer
6363
*/
64-
struct ERC20Storage {
64+
struct ERC20TransferStorage {
6565
mapping(address owner => uint256 balance) balanceOf;
6666
uint256 totalSupply;
67-
mapping(address owner => mapping(address spender => uint256 allowance)) allowance;
68-
uint8 decimals;
69-
string name;
70-
string symbol;
67+
mapping(address owner => mapping(address spender => uint256 allowance)) allowance;
7168
}
7269

7370
/**
7471
* @notice Returns the ERC20 storage struct from the predefined diamond storage slot.
7572
* @dev Uses inline assembly to set the storage slot reference.
7673
* @return s The ERC20 storage struct reference.
7774
*/
78-
function getStorage() internal pure returns (ERC20Storage storage s) {
75+
function getStorage() internal pure returns (ERC20TransferStorage storage s) {
7976
bytes32 position = STORAGE_POSITION;
8077
assembly {
8178
s.slot := position
8279
}
83-
}
84-
85-
/**
86-
* @notice Returns the name of the token.
87-
* @return The token name.
88-
*/
89-
function name() external view returns (string memory) {
90-
return getStorage().name;
91-
}
92-
93-
/**
94-
* @notice Returns the symbol of the token.
95-
* @return The token symbol.
96-
*/
97-
function symbol() external view returns (string memory) {
98-
return getStorage().symbol;
99-
}
100-
101-
/**
102-
* @notice Returns the number of decimals used for token precision.
103-
* @return The number of decimals.
104-
*/
105-
function decimals() external view returns (uint8) {
106-
return getStorage().decimals;
107-
}
80+
}
10881

10982
/**
11083
* @notice Returns the total supply of tokens.
@@ -141,7 +114,7 @@ contract ERC20Facet {
141114
* @return True if the approval was successful.
142115
*/
143116
function approve(address _spender, uint256 _value) external returns (bool) {
144-
ERC20Storage storage s = getStorage();
117+
ERC20TransferStorage storage s = getStorage();
145118
if (_spender == address(0)) {
146119
revert ERC20InvalidSpender(address(0));
147120
}
@@ -158,7 +131,7 @@ contract ERC20Facet {
158131
* @return True if the transfer was successful.
159132
*/
160133
function transfer(address _to, uint256 _value) external returns (bool) {
161-
ERC20Storage storage s = getStorage();
134+
ERC20TransferStorage storage s = getStorage();
162135
if (_to == address(0)) {
163136
revert ERC20InvalidReceiver(address(0));
164137
}
@@ -183,7 +156,7 @@ contract ERC20Facet {
183156
* @return True if the transfer was successful.
184157
*/
185158
function transferFrom(address _from, address _to, uint256 _value) external returns (bool) {
186-
ERC20Storage storage s = getStorage();
159+
ERC20TransferStorage storage s = getStorage();
187160
if (_from == address(0)) {
188161
revert ERC20InvalidSender(address(0));
189162
}

src/token/ERC20/ERC20Bridgeable/ERC20BridgeableFacet.sol

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ contract ERC20BridgeableFacet {
7676
* @notice Storage slot for ERC-20 token using ERC8042 for storage location standardization
7777
* @dev Storage position determined by the keccak256 hash of the diamond storage identifier.
7878
*/
79-
bytes32 constant ERC20_STORAGE_POSITION = keccak256("compose.erc20");
79+
bytes32 constant ERC20_TRANSFER_STORAGE_POSITION = keccak256("compose.erc20.transfer");
8080

8181
/**
8282
* @dev ERC-8042 compliant storage struct for ERC20 token data.
83-
* @custom:storage-location erc8042:compose.erc20
83+
* @custom:storage-location erc8042:compose.erc20.transfer
8484
*/
85-
struct ERC20Storage {
85+
struct ERC20TransferStorage {
8686
mapping(address owner => uint256 balance) balanceOf;
8787
uint256 totalSupply;
8888
}
@@ -92,8 +92,8 @@ contract ERC20BridgeableFacet {
9292
* @return s The ERC20 storage struct reference.
9393
*/
9494

95-
function getERC20Storage() internal pure returns (ERC20Storage storage s) {
96-
bytes32 position = ERC20_STORAGE_POSITION;
95+
function getERC20TransferStorage() internal pure returns (ERC20TransferStorage storage s) {
96+
bytes32 position = ERC20_TRANSFER_STORAGE_POSITION;
9797
assembly {
9898
s.slot := position
9999
}
@@ -133,7 +133,7 @@ contract ERC20BridgeableFacet {
133133
* @param _value The amount to mint.
134134
*/
135135
function crosschainMint(address _account, uint256 _value) external {
136-
ERC20Storage storage erc20 = getERC20Storage();
136+
ERC20TransferStorage storage erc20Transfer = getERC20TransferStorage();
137137

138138
AccessControlStorage storage acs = getAccessControlStorage();
139139

@@ -149,8 +149,8 @@ contract ERC20BridgeableFacet {
149149
}
150150

151151
unchecked {
152-
erc20.totalSupply += _value;
153-
erc20.balanceOf[_account] += _value;
152+
erc20Transfer.totalSupply += _value;
153+
erc20Transfer.balanceOf[_account] += _value;
154154
}
155155
emit Transfer(address(0), _account, _value);
156156
emit CrosschainMint(_account, _value, msg.sender);
@@ -162,7 +162,7 @@ contract ERC20BridgeableFacet {
162162
* @param _value The amount to burn.
163163
*/
164164
function crosschainBurn(address _from, uint256 _value) external {
165-
ERC20Storage storage erc20 = getERC20Storage();
165+
ERC20TransferStorage storage erc20Transfer = getERC20TransferStorage();
166166

167167
AccessControlStorage storage acs = getAccessControlStorage();
168168

@@ -176,15 +176,15 @@ contract ERC20BridgeableFacet {
176176
revert ERC20InvalidReciever(address(0));
177177
}
178178

179-
uint256 accountBalance = erc20.balanceOf[_from];
179+
uint256 accountBalance = erc20Transfer.balanceOf[_from];
180180

181181
if (accountBalance < _value) {
182182
revert ERC20InsufficientBalance(_from, accountBalance, _value);
183183
}
184184

185185
unchecked {
186-
erc20.totalSupply -= _value;
187-
erc20.balanceOf[_from] -= _value;
186+
erc20Transfer.totalSupply -= _value;
187+
erc20Transfer.balanceOf[_from] -= _value;
188188
}
189189

190190
emit Transfer(_from, address(0), _value);

0 commit comments

Comments
 (0)