@@ -6,6 +6,7 @@ import "../../interfaces/IERC1155.sol";
66import "../../utils/Address.sol " ;
77import "../../utils/ContextUpgradeable.sol " ;
88import "../../utils/ERC165.sol " ;
9+ import '../../utils/StorageSlot.sol ' ;
910
1011/**
1112 * @dev Implementation of Multi-Token Standard contract. This implementation of the ERC-1155 standard
@@ -35,10 +36,10 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
3536 enum Operations { Add, Sub }
3637
3738 // 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" ) ;
3940
4041 // Operators
41- mapping ( address => mapping ( address => bool )) internal operators;
42+ bytes32 constant private _OPERATORS_SLOT_KEY = keccak256 ( " 0xsequence.ERC1155PackedBalanceUpgradeable. operators" ) ;
4243
4344
4445 /***********************************|
@@ -140,8 +141,8 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
140141 (uint256 bin , uint256 index ) = getIDBinIndex (_ids[0 ]);
141142
142143 // 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);
145146
146147 // Last bin updated
147148 uint256 lastBin = bin;
@@ -152,11 +153,11 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
152153 // If new bin
153154 if (bin != lastBin) {
154155 // 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) ;
157158
158- balFrom = balances[ _from][ bin] ;
159- balTo = balances[ _to][ bin] ;
159+ balFrom = _getBalance ( _from, bin) ;
160+ balTo = _getBalance ( _to, bin) ;
160161
161162 // Bin will be the most recent bin
162163 lastBin = bin;
@@ -168,8 +169,8 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
168169 }
169170
170171 // 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) ;
173174
174175 // If transfer to self, just make sure all amounts are valid
175176 } else {
@@ -209,7 +210,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
209210 external override
210211 {
211212 // Update operator status
212- operators[ _msgSender ()][ _operator] = _approved;
213+ _setOperator ( _msgSender (), _operator, _approved) ;
213214 emit ApprovalForAll (_msgSender (), _operator, _approved);
214215 }
215216
@@ -222,7 +223,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
222223 function isApprovedForAll (address _owner , address _operator )
223224 public override view returns (bool isOperator )
224225 {
225- return operators[ _owner][ _operator] ;
226+ return _getOperator ( _owner, _operator) ;
226227 }
227228
228229
@@ -244,7 +245,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
244245
245246 //Get bin and index of _id
246247 (bin, index) = getIDBinIndex (_id);
247- return getValueInBin (balances[ _owner][ bin] , index);
248+ return getValueInBin (_getBalance ( _owner, bin) , index);
248249 }
249250
250251 /**
@@ -261,7 +262,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
261262
262263 // First values
263264 (uint256 bin , uint256 index ) = getIDBinIndex (_ids[0 ]);
264- uint256 balance_bin = balances[ _owners[0 ]][ bin] ;
265+ uint256 balance_bin = _getBalance ( _owners[0 ], bin) ;
265266 uint256 last_bin = bin;
266267
267268 // Initialization
@@ -274,7 +275,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
274275
275276 // SLOAD if bin changed for the same owner or if owner changed
276277 if (bin != last_bin || _owners[i-1 ] != _owners[i]) {
277- balance_bin = balances[ _owners[i]][ bin] ;
278+ balance_bin = _getBalance ( _owners[i], bin) ;
278279 last_bin = bin;
279280 }
280281
@@ -308,7 +309,7 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
308309 (bin, index) = getIDBinIndex (_id);
309310
310311 // Update balance
311- balances[ _address][ bin] = _viewUpdateBinValue (balances[ _address][ bin] , index, _amount, _operation);
312+ _setBalance ( _address, bin, _viewUpdateBinValue (_getBalance ( _address, bin) , index, _amount, _operation) );
312313 }
313314
314315 /**
@@ -379,6 +380,26 @@ contract ERC1155PackedBalanceUpgradeable is ContextUpgradeable, IERC1155, ERC165
379380 return (_binValues >> rightShift) & mask;
380381 }
381382
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+
382403
383404 /***********************************|
384405 | ERC165 Functions |
0 commit comments