@@ -6,6 +6,7 @@ import "../../interfaces/IERC1155.sol";
6
6
import "../../utils/Address.sol " ;
7
7
import "../../utils/ContextUpgradeable.sol " ;
8
8
import "../../utils/ERC165.sol " ;
9
+ import '../../utils/StorageSlot.sol ' ;
9
10
10
11
/**
11
12
* @dev Implementation of Multi-Token Standard contract. This implementation of the ERC-1155 standard
@@ -35,10 +36,10 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
35
36
enum Operations { Add, Sub }
36
37
37
38
// Token IDs balances ; balances[address][id] => balance (using array instead of mapping for efficiency)
38
- mapping ( address => mapping ( uint256 => uint256 )) internal balances;
39
+ bytes32 constant private _BALANCES_SLOT_KEY = keccak256 ( " 0xsequence.ERC1155PackedBalanceUpgradeable. balances" ) ;
39
40
40
41
// Operators
41
- mapping ( address => mapping ( address => bool )) internal operators;
42
+ bytes32 constant private _OPERATORS_SLOT_KEY = keccak256 ( " 0xsequence.ERC1155PackedBalanceUpgradeable. operators" ) ;
42
43
43
44
44
45
/***********************************|
@@ -140,8 +141,8 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
140
141
(uint256 bin , uint256 index ) = getIDBinIndex (_ids[0 ]);
141
142
142
143
// Balance for current bin in memory (initialized with first transfer)
143
- uint256 balFrom = _viewUpdateBinValue (balances[ _from][ bin] , index, _amounts[0 ], Operations.Sub);
144
- uint256 balTo = _viewUpdateBinValue (balances[ _to][ bin] , index, _amounts[0 ], Operations.Add);
144
+ uint256 balFrom = _viewUpdateBinValue (_getBalance ( _from, bin) , index, _amounts[0 ], Operations.Sub);
145
+ uint256 balTo = _viewUpdateBinValue (_getBalance ( _to, bin) , index, _amounts[0 ], Operations.Add);
145
146
146
147
// Last bin updated
147
148
uint256 lastBin = bin;
@@ -152,11 +153,11 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
152
153
// If new bin
153
154
if (bin != lastBin) {
154
155
// Update storage balance of previous bin
155
- balances[ _from][ lastBin] = balFrom;
156
- balances[ _to][ lastBin] = balTo;
156
+ _setBalance ( _from, lastBin, balFrom) ;
157
+ _setBalance ( _to, lastBin, balTo) ;
157
158
158
- balFrom = balances[ _from][ bin] ;
159
- balTo = balances[ _to][ bin] ;
159
+ balFrom = _getBalance ( _from, bin) ;
160
+ balTo = _getBalance ( _to, bin) ;
160
161
161
162
// Bin will be the most recent bin
162
163
lastBin = bin;
@@ -168,8 +169,8 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
168
169
}
169
170
170
171
// Update storage of the last bin visited
171
- balances[ _from][ bin] = balFrom;
172
- balances[ _to][ bin] = balTo;
172
+ _setBalance ( _from, bin, balFrom) ;
173
+ _setBalance ( _to, bin, balTo) ;
173
174
174
175
// If transfer to self, just make sure all amounts are valid
175
176
} else {
@@ -209,7 +210,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
209
210
external override
210
211
{
211
212
// Update operator status
212
- operators[ _msgSender ()][ _operator] = _approved;
213
+ _setOperator ( _msgSender (), _operator, _approved) ;
213
214
emit ApprovalForAll (_msgSender (), _operator, _approved);
214
215
}
215
216
@@ -222,7 +223,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
222
223
function isApprovedForAll (address _owner , address _operator )
223
224
public override view returns (bool isOperator )
224
225
{
225
- return operators[ _owner][ _operator] ;
226
+ return _getOperator ( _owner, _operator) ;
226
227
}
227
228
228
229
@@ -244,7 +245,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
244
245
245
246
//Get bin and index of _id
246
247
(bin, index) = getIDBinIndex (_id);
247
- return getValueInBin (balances[ _owner][ bin] , index);
248
+ return getValueInBin (_getBalance ( _owner, bin) , index);
248
249
}
249
250
250
251
/**
@@ -261,7 +262,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
261
262
262
263
// First values
263
264
(uint256 bin , uint256 index ) = getIDBinIndex (_ids[0 ]);
264
- uint256 balance_bin = balances[ _owners[0 ]][ bin] ;
265
+ uint256 balance_bin = _getBalance ( _owners[0 ], bin) ;
265
266
uint256 last_bin = bin;
266
267
267
268
// Initialization
@@ -274,7 +275,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
274
275
275
276
// SLOAD if bin changed for the same owner or if owner changed
276
277
if (bin != last_bin || _owners[i-1 ] != _owners[i]) {
277
- balance_bin = balances[ _owners[i]][ bin] ;
278
+ balance_bin = _getBalance ( _owners[i], bin) ;
278
279
last_bin = bin;
279
280
}
280
281
@@ -308,7 +309,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
308
309
(bin, index) = getIDBinIndex (_id);
309
310
310
311
// Update balance
311
- balances[ _address][ bin] = _viewUpdateBinValue (balances[ _address][ bin] , index, _amount, _operation);
312
+ _setBalance ( _address, bin, _viewUpdateBinValue (_getBalance ( _address, bin) , index, _amount, _operation) );
312
313
}
313
314
314
315
/**
@@ -379,6 +380,26 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
379
380
return (_binValues >> rightShift) & mask;
380
381
}
381
382
383
+ /***********************************|
384
+ | Storage Functions |
385
+ |__________________________________*/
386
+
387
+ function _getBalance (address _owner , uint256 _id ) internal view returns (uint256 ) {
388
+ return StorageSlot.getUint256Slot (keccak256 (abi.encodePacked (_BALANCES_SLOT_KEY, _owner, _id))).value;
389
+ }
390
+
391
+ function _setBalance (address _owner , uint256 _id , uint256 _balance ) internal {
392
+ StorageSlot.getUint256Slot (keccak256 (abi.encodePacked (_BALANCES_SLOT_KEY, _owner, _id))).value = _balance;
393
+ }
394
+
395
+ function _getOperator (address _owner , address _operator ) internal view returns (bool ) {
396
+ return StorageSlot.getBooleanSlot (keccak256 (abi.encodePacked (_OPERATORS_SLOT_KEY, _owner, _operator))).value;
397
+ }
398
+
399
+ function _setOperator (address _owner , address _operator , bool _approved ) internal {
400
+ StorageSlot.getBooleanSlot (keccak256 (abi.encodePacked (_OPERATORS_SLOT_KEY, _owner, _operator))).value = _approved;
401
+ }
402
+
382
403
383
404
/***********************************|
384
405
| ERC165 Functions |
0 commit comments