Skip to content

Commit

Permalink
add asSingletonArrays function and refactor ERC1155 to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
clauBv23 committed Jun 1, 2023
1 parent 2ee1da1 commit 1ec6f50
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
function _safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) internal {
require(to != address(0), "ERC1155: transfer to the zero address");
require(from != address(0), "ERC1155: transfer from the zero address");
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);
(uint[] memory ids, uint[] memory amounts) = _asSingletonArrays(id, amount);
_update(from, to, ids, amounts, data);
}

Expand Down Expand Up @@ -269,8 +268,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
*/
function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal {
require(to != address(0), "ERC1155: mint to the zero address");
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);
(uint[] memory ids, uint[] memory amounts) = _asSingletonArrays(id, amount);
_update(address(0), to, ids, amounts, data);
}

Expand Down Expand Up @@ -302,8 +300,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
*/
function _burn(address from, uint256 id, uint256 amount) internal {
require(from != address(0), "ERC1155: burn from the zero address");
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);
(uint[] memory ids, uint[] memory amounts) = _asSingletonArrays(id, amount);
_update(from, address(0), ids, amounts, "");
}

Expand Down Expand Up @@ -376,10 +373,20 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
}
}

function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
uint256[] memory array = new uint256[](1);
array[0] = element;
function _asSingletonArrays(
uint256 element1,
uint256 element2
) private pure returns (uint256[] memory array1, uint256[] memory array2) {
assembly {
array1 := mload(0x40)
mstore(array1, 1)
mstore(add(array1, 0x20), element1)

return array;
array2 := add(array1, 0x40)
mstore(array2, 1)
mstore(add(array2, 0x20), element2)

mstore(0x40, add(array2, 0x40))
}
}
}

0 comments on commit 1ec6f50

Please sign in to comment.