Skip to content

Commit

Permalink
Use Arrays.unsafeMemoryAccess in ERC1155Supply (#5068)
Browse files Browse the repository at this point in the history
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
  • Loading branch information
RenanSouza2 and Amxx committed Jun 7, 2024
1 parent 337bfd5 commit de41547
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions contracts/token/ERC1155/extensions/ERC1155Supply.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pragma solidity ^0.8.20;

import {ERC1155} from "../ERC1155.sol";
import {Arrays} from "../../../utils/Arrays.sol";

/**
* @dev Extension of ERC-1155 that adds tracking of total supply per id.
Expand All @@ -19,6 +20,8 @@ import {ERC1155} from "../ERC1155.sol";
* CAUTION: This extension should not be added in an upgrade to an already deployed contract.
*/
abstract contract ERC1155Supply is ERC1155 {
using Arrays for uint256[];

mapping(uint256 id => uint256) private _totalSupply;
uint256 private _totalSupplyAll;

Expand Down Expand Up @@ -57,9 +60,9 @@ abstract contract ERC1155Supply is ERC1155 {
if (from == address(0)) {
uint256 totalMintValue = 0;
for (uint256 i = 0; i < ids.length; ++i) {
uint256 value = values[i];
uint256 value = values.unsafeMemoryAccess(i);
// Overflow check required: The rest of the code assumes that totalSupply never overflows
_totalSupply[ids[i]] += value;
_totalSupply[ids.unsafeMemoryAccess(i)] += value;
totalMintValue += value;
}
// Overflow check required: The rest of the code assumes that totalSupplyAll never overflows
Expand All @@ -69,11 +72,11 @@ abstract contract ERC1155Supply is ERC1155 {
if (to == address(0)) {
uint256 totalBurnValue = 0;
for (uint256 i = 0; i < ids.length; ++i) {
uint256 value = values[i];
uint256 value = values.unsafeMemoryAccess(i);

unchecked {
// Overflow not possible: values[i] <= balanceOf(from, ids[i]) <= totalSupply(ids[i])
_totalSupply[ids[i]] -= value;
_totalSupply[ids.unsafeMemoryAccess(i)] -= value;
// Overflow not possible: sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
totalBurnValue += value;
}
Expand Down

0 comments on commit de41547

Please sign in to comment.