Skip to content

Commit de41547

Browse files
RenanSouza2Amxx
andauthored
Use Arrays.unsafeMemoryAccess in ERC1155Supply (#5068)
Co-authored-by: Hadrien Croubois <[email protected]>
1 parent 337bfd5 commit de41547

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

contracts/token/ERC1155/extensions/ERC1155Supply.sol

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
pragma solidity ^0.8.20;
55

66
import {ERC1155} from "../ERC1155.sol";
7+
import {Arrays} from "../../../utils/Arrays.sol";
78

89
/**
910
* @dev Extension of ERC-1155 that adds tracking of total supply per id.
@@ -19,6 +20,8 @@ import {ERC1155} from "../ERC1155.sol";
1920
* CAUTION: This extension should not be added in an upgrade to an already deployed contract.
2021
*/
2122
abstract contract ERC1155Supply is ERC1155 {
23+
using Arrays for uint256[];
24+
2225
mapping(uint256 id => uint256) private _totalSupply;
2326
uint256 private _totalSupplyAll;
2427

@@ -57,9 +60,9 @@ abstract contract ERC1155Supply is ERC1155 {
5760
if (from == address(0)) {
5861
uint256 totalMintValue = 0;
5962
for (uint256 i = 0; i < ids.length; ++i) {
60-
uint256 value = values[i];
63+
uint256 value = values.unsafeMemoryAccess(i);
6164
// Overflow check required: The rest of the code assumes that totalSupply never overflows
62-
_totalSupply[ids[i]] += value;
65+
_totalSupply[ids.unsafeMemoryAccess(i)] += value;
6366
totalMintValue += value;
6467
}
6568
// Overflow check required: The rest of the code assumes that totalSupplyAll never overflows
@@ -69,11 +72,11 @@ abstract contract ERC1155Supply is ERC1155 {
6972
if (to == address(0)) {
7073
uint256 totalBurnValue = 0;
7174
for (uint256 i = 0; i < ids.length; ++i) {
72-
uint256 value = values[i];
75+
uint256 value = values.unsafeMemoryAccess(i);
7376

7477
unchecked {
7578
// Overflow not possible: values[i] <= balanceOf(from, ids[i]) <= totalSupply(ids[i])
76-
_totalSupply[ids[i]] -= value;
79+
_totalSupply[ids.unsafeMemoryAccess(i)] -= value;
7780
// Overflow not possible: sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
7881
totalBurnValue += value;
7982
}

0 commit comments

Comments
 (0)