Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ERC1155 hooks #3876

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3a66f1f
update _safeTransferFrom
JulissaDantes Dec 1, 2022
7a69513
update tests
JulissaDantes Dec 1, 2022
422224e
Remove hooks
JulissaDantes Dec 1, 2022
e505779
update extensions
JulissaDantes Dec 5, 2022
79884e6
Update mocks
JulissaDantes Dec 13, 2022
96467b7
Introduce _update and _updateBatch
JulissaDantes Dec 13, 2022
293d0ee
Add changelog entry
JulissaDantes Dec 13, 2022
f84a858
Update test
JulissaDantes Dec 13, 2022
ba79c16
Merge branch 'next-v5.0' into refactor/erc1155-mint-transfer-burn
JulissaDantes Dec 16, 2022
0eb70ac
update extension
JulissaDantes Dec 19, 2022
21279a9
Merge branch 'master' into next-v5.0
JulissaDantes Dec 19, 2022
4469254
Merge branch 'next-v5.0' into refactor/erc1155-mint-transfer-burn
JulissaDantes Dec 19, 2022
bd0f915
fix post merge error
JulissaDantes Dec 19, 2022
11a8d92
update changelog
JulissaDantes Dec 19, 2022
7c8b929
Remove updateBatch
JulissaDantes Dec 20, 2022
a9799ae
Update ERC1155
JulissaDantes Dec 20, 2022
b21c37d
Merge branch 'next-v5.0' into refactor/erc1155-mint-transfer-burn
JulissaDantes Dec 20, 2022
4e513b6
Check non zero address as attribute
JulissaDantes Dec 27, 2022
de3ad93
Merge branch 'refactor/erc1155-mint-transfer-burn' of https://github.…
JulissaDantes Dec 27, 2022
48080eb
Update contracts/token/ERC1155/ERC1155.sol
JulissaDantes Jan 6, 2023
c1ee9ce
Update contracts/token/ERC1155/ERC1155.sol
JulissaDantes Jan 6, 2023
93345da
update test error message
JulissaDantes Jan 6, 2023
da263d6
Resolve conflicts
JulissaDantes Jan 6, 2023
8b11286
run lint
JulissaDantes Jan 6, 2023
d248351
fix lint
Amxx Jan 6, 2023
826e2ed
Update contracts/mocks/ERC1155PausableMock.sol
JulissaDantes Jan 9, 2023
0061bb7
Update contracts/mocks/ERC1155SupplyMock.sol
JulissaDantes Jan 9, 2023
4b040e2
Update package-lock.json
JulissaDantes Jan 9, 2023
c338f9c
Update contracts/token/ERC1155/ERC1155.sol
JulissaDantes Jan 9, 2023
943993c
Update contracts/token/ERC1155/ERC1155.sol
JulissaDantes Jan 9, 2023
276a20f
Update contracts/token/ERC1155/ERC1155.sol
JulissaDantes Jan 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Removed presets in favor of [OpenZeppelin Contracts Wizard](https://wizard.openzeppelin.com/).
* `TransparentUpgradeableProxy`: Removed `admin` and `implementation` getters, which were only callable by the proxy owner and thus not very useful. ([#3820](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3820))
* `ProxyAdmin`: Removed `getProxyAdmin` and `getProxyImplementation` getters. ([#3820](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3820))
* `ERC20`: Deleted `_beforeTokenTransfer` and `_afterTokenTransfer` hooks, added a new internal `_update` function for customizations, and refactored all extensions using those hooks to use `_update` instead. ([#3838](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3838))
* `ERC20`, `ERC1155`: Deleted `_beforeTokenTransfer` and `_afterTokenTransfer` hooks, added a new internal `_update` function for customizations, and refactored all extensions using those hooks to use `_update` instead. ([#3838](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3838), [#3876](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3876))
* `ERC165Storage`: Removed this contract in favor of inheritance based approach. ([#3880](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3880))

### How to upgrade from 4.x
Expand Down
7 changes: 3 additions & 4 deletions contracts/mocks/ERC1155PausableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ contract ERC1155PausableMock is ERC1155Mock, ERC1155Pausable {
_unpause();
}

function _beforeTokenTransfer(
address operator,
function _update(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal override(ERC1155, ERC1155Pausable) {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
) internal virtual override(ERC1155, ERC1155Pausable) {
JulissaDantes marked this conversation as resolved.
Show resolved Hide resolved
super._update(from, to, ids, amounts, data);
}
}
7 changes: 3 additions & 4 deletions contracts/mocks/ERC1155SupplyMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import "../token/ERC1155/extensions/ERC1155Supply.sol";
contract ERC1155SupplyMock is ERC1155Mock, ERC1155Supply {
constructor(string memory uri) ERC1155Mock(uri) {}

function _beforeTokenTransfer(
address operator,
function _update(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal override(ERC1155, ERC1155Supply) {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
) internal virtual override(ERC1155, ERC1155Supply) {
JulissaDantes marked this conversation as resolved.
Show resolved Hide resolved
super._update(from, to, ids, amounts, data);
}
}
231 changes: 67 additions & 164 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,59 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
_safeBatchTransferFrom(from, to, ids, amounts, data);
}

/**
* @dev Transfers `amount` tokens of token type `id` from `from` to `to`. Will mint (or burn) if `from` (or `to`) is the zero address.
*
* Emits a {TransferSingle} event if only one transfer is done, and {TransferBatch} event for multiple transfer operations.
JulissaDantes marked this conversation as resolved.
Show resolved Hide resolved
*
* Requirements:
*
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
* acceptance magic value.
*/
function _update(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");

address operator = _msgSender();

for (uint256 i = 0; i < ids.length; ++i) {
uint256 id = ids[i];
uint256 amount = amounts[i];
frangio marked this conversation as resolved.
Show resolved Hide resolved

uint256 fromBalance = _balances[id][from];
if (to == address(0)) {
require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
}
if (from != address(0)) {
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
unchecked {
_balances[id][from] = fromBalance - amount;
}
}
JulissaDantes marked this conversation as resolved.
Show resolved Hide resolved

if (to != address(0)) {
_balances[id][to] += amount;
}
}
if (ids.length == 1) {
JulissaDantes marked this conversation as resolved.
Show resolved Hide resolved
emit TransferSingle(operator, from, to, ids[0], amounts[0]);
if (to != address(0)) {
_doSafeTransferAcceptanceCheck(operator, from, to, ids[0], amounts[0], data);
}
JulissaDantes marked this conversation as resolved.
Show resolved Hide resolved
} else {
emit TransferBatch(operator, from, to, ids, amounts);
if (to != address(0)) {
_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
}
}
}

/**
* @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
*
Expand All @@ -163,27 +216,12 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256 id,
uint256 amount,
bytes memory data
) internal virtual {
) internal {
require(to != address(0), "ERC1155: transfer to the zero address");
JulissaDantes marked this conversation as resolved.
Show resolved Hide resolved

address operator = _msgSender();
require(from != address(0), "ERC1155: transfer from the zero address");
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);

_beforeTokenTransfer(operator, from, to, ids, amounts, data);

uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
unchecked {
_balances[id][from] = fromBalance - amount;
}
_balances[id][to] += amount;

emit TransferSingle(operator, from, to, id, amount);

_afterTokenTransfer(operator, from, to, ids, amounts, data);

_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
_update(from, to, ids, amounts, data);
}

/**
Expand All @@ -202,31 +240,10 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
) internal {
require(to != address(0), "ERC1155: transfer to the zero address");

address operator = _msgSender();

_beforeTokenTransfer(operator, from, to, ids, amounts, data);

for (uint256 i = 0; i < ids.length; ++i) {
uint256 id = ids[i];
uint256 amount = amounts[i];

uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
unchecked {
_balances[id][from] = fromBalance - amount;
}
_balances[id][to] += amount;
}

emit TransferBatch(operator, from, to, ids, amounts);

_afterTokenTransfer(operator, from, to, ids, amounts, data);

_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
require(from != address(0), "ERC1155: transfer from the zero address");
_update(from, to, ids, amounts, data);
}

/**
Expand Down Expand Up @@ -268,21 +285,11 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256 id,
uint256 amount,
bytes memory data
) internal virtual {
) internal {
require(to != address(0), "ERC1155: mint to the zero address");

address operator = _msgSender();
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);

_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);

_balances[id][to] += amount;
emit TransferSingle(operator, address(0), to, id, amount);

_afterTokenTransfer(operator, address(0), to, ids, amounts, data);

_doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);
_update(address(0), to, ids, amounts, data);
}

/**
Expand All @@ -301,23 +308,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
) internal {
require(to != address(0), "ERC1155: mint to the zero address");
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");

address operator = _msgSender();

_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);

for (uint256 i = 0; i < ids.length; i++) {
_balances[ids[i]][to] += amounts[i];
}

emit TransferBatch(operator, address(0), to, ids, amounts);

_afterTokenTransfer(operator, address(0), to, ids, amounts, data);

_doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
_update(address(0), to, ids, amounts, data);
}

/**
Expand All @@ -334,24 +327,11 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
address from,
uint256 id,
uint256 amount
) internal virtual {
) internal {
require(from != address(0), "ERC1155: burn from the zero address");

address operator = _msgSender();
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);

_beforeTokenTransfer(operator, from, address(0), ids, amounts, "");

uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
unchecked {
_balances[id][from] = fromBalance - amount;
}

emit TransferSingle(operator, from, address(0), id, amount);

_afterTokenTransfer(operator, from, address(0), ids, amounts, "");
_update(from, address(0), ids, amounts, "");
}

/**
Expand All @@ -367,28 +347,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
address from,
uint256[] memory ids,
uint256[] memory amounts
) internal virtual {
) internal {
require(from != address(0), "ERC1155: burn from the zero address");
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");

address operator = _msgSender();

_beforeTokenTransfer(operator, from, address(0), ids, amounts, "");

for (uint256 i = 0; i < ids.length; i++) {
uint256 id = ids[i];
uint256 amount = amounts[i];

uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
unchecked {
_balances[id][from] = fromBalance - amount;
}
}

emit TransferBatch(operator, from, address(0), ids, amounts);

_afterTokenTransfer(operator, from, address(0), ids, amounts, "");
_update(from, address(0), ids, amounts, "");
}

/**
Expand All @@ -406,64 +367,6 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
emit ApprovalForAll(owner, operator, approved);
}

/**
* @dev Hook that is called before any token transfer. This includes minting
* and burning, as well as batched variants.
*
* The same hook is called on both single and batched variants. For single
* transfers, the length of the `ids` and `amounts` arrays will be 1.
*
* Calling conditions (for each `id` and `amount` pair):
*
* - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* of token type `id` will be transferred to `to`.
* - When `from` is zero, `amount` tokens of token type `id` will be minted
* for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
* will be burned.
* - `from` and `to` are never both zero.
* - `ids` and `amounts` have the same, non-zero length.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {}

/**
* @dev Hook that is called after any token transfer. This includes minting
* and burning, as well as batched variants.
*
* The same hook is called on both single and batched variants. For single
* transfers, the length of the `id` and `amount` arrays will be 1.
*
* Calling conditions (for each `id` and `amount` pair):
*
* - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* of token type `id` will be transferred to `to`.
* - When `from` is zero, `amount` tokens of token type `id` will be minted
* for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
* will be burned.
* - `from` and `to` are never both zero.
* - `ids` and `amounts` have the same, non-zero length.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {}

function _doSafeTransferAcceptanceCheck(
address operator,
address from,
Expand Down
8 changes: 3 additions & 5 deletions contracts/token/ERC1155/extensions/ERC1155Pausable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ import "../../../security/Pausable.sol";
*/
abstract contract ERC1155Pausable is ERC1155, Pausable {
/**
* @dev See {ERC1155-_beforeTokenTransfer}.
* @dev See {ERC1155-_update}.
*
* Requirements:
*
* - the contract must not be paused.
*/
function _beforeTokenTransfer(
address operator,
function _update(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual override {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);

require(!paused(), "ERC1155Pausable: token transfer while paused");
super._update(from, to, ids, amounts, data);
}
}
8 changes: 3 additions & 5 deletions contracts/token/ERC1155/extensions/ERC1155Supply.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ abstract contract ERC1155Supply is ERC1155 {
}

/**
* @dev See {ERC1155-_beforeTokenTransfer}.
* @dev See {ERC1155-_update}.
*/
function _beforeTokenTransfer(
address operator,
function _update(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual override {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);

if (from == address(0)) {
for (uint256 i = 0; i < ids.length; ++i) {
_totalSupply[ids[i]] += amounts[i];
frangio marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -60,5 +57,6 @@ abstract contract ERC1155Supply is ERC1155 {
}
}
}
super._update(from, to, ids, amounts, data);
}
}
Loading