-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Move ERC721 and ERC1155 receiver checks to dedicate libraries #4845
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
Merged
ernestognw
merged 11 commits into
OpenZeppelin:master
from
Amxx:feature/erc721-erc1155-utils
Jan 30, 2024
+540
−101
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a193bd9
Move ERC721 and ERC1155 receiver checks to dedicate libraries
Amxx 4eecf9f
doc
Amxx f372e76
doc
Amxx 0e853c0
doc
Amxx 9c5e827
up
Amxx dc2f8f4
fix lint
Amxx 29f3b16
Apply suggestions from code review
Amxx d292276
Nits
ernestognw aaf7cda
Add tests
ernestognw d551d5c
Fix tests
ernestognw ed00c2a
Remove unnecessary .only
ernestognw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`ERC721Utils` and `ERC1155Utils`: reusable libraries for performing the acceptance checks (safeTransfer) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {IERC1155Receiver} from "../IERC1155Receiver.sol"; | ||
import {IERC1155Errors} from "../../../interfaces/draft-IERC6093.sol"; | ||
|
||
/** | ||
* @dev Implementation of the ERC-1155 acceptance checks used in safe transfers. | ||
* See https://eips.ethereum.org/EIPS/eip-1155 | ||
*/ | ||
library ERC1155Utils { | ||
/** | ||
* @dev Performs an acceptance check by calling {IERC1155-onERC1155Received} on the `to` address if it | ||
* contains code at the moment of execution. This will revert if the recipient doesn't accept the token transfer. | ||
* The call is not executed if the target address is not a contract. | ||
*/ | ||
function checkOnERC1155Received( | ||
address operator, | ||
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address from, | ||
address to, | ||
uint256 id, | ||
uint256 value, | ||
bytes memory data | ||
) internal { | ||
if (to.code.length > 0) { | ||
try IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) returns (bytes4 response) { | ||
if (response != IERC1155Receiver.onERC1155Received.selector) { | ||
revert IERC1155Errors.ERC1155InvalidReceiver(to); | ||
} | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (bytes memory reason) { | ||
if (reason.length == 0) { | ||
revert IERC1155Errors.ERC1155InvalidReceiver(to); | ||
} else { | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @solidity memory-safe-assembly | ||
assembly { | ||
revert(add(32, reason), mload(reason)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @dev Performs a batch acceptance check by calling {IERC1155-onERC1155BatchReceived} on the `to` address if it | ||
* contains code at the moment of execution. This will revert if the recipient doesn't accept the token transfer. | ||
* The call is not executed if the target address is not a contract. | ||
*/ | ||
function checkOnERC1155BatchReceived( | ||
address operator, | ||
address from, | ||
address to, | ||
uint256[] memory ids, | ||
uint256[] memory values, | ||
bytes memory data | ||
) internal { | ||
if (to.code.length > 0) { | ||
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) returns ( | ||
bytes4 response | ||
) { | ||
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) { | ||
revert IERC1155Errors.ERC1155InvalidReceiver(to); | ||
} | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (bytes memory reason) { | ||
if (reason.length == 0) { | ||
revert IERC1155Errors.ERC1155InvalidReceiver(to); | ||
} else { | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @solidity memory-safe-assembly | ||
assembly { | ||
revert(add(32, reason), mload(reason)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {IERC721Receiver} from "../IERC721Receiver.sol"; | ||
import {IERC721Errors} from "../../../interfaces/draft-IERC6093.sol"; | ||
|
||
/** | ||
* @dev Implementation of the ERC-721 acceptance checks used in safe transfers. | ||
* See https://eips.ethereum.org/EIPS/eip-721 | ||
*/ | ||
library ERC721Utils { | ||
/** | ||
* @dev Performs an acceptance check by calling {IERC721Receiver-onERC721Received} on the `to` address if it | ||
* contains code at the moment of execution. This will revert if the recipient doesn't accept the token transfer. | ||
* The call is not executed if the target address is not a contract. | ||
*/ | ||
function checkOnERC721Received( | ||
address operator, | ||
address from, | ||
address to, | ||
uint256 tokenId, | ||
bytes memory data | ||
) internal { | ||
if (to.code.length > 0) { | ||
try IERC721Receiver(to).onERC721Received(operator, from, tokenId, data) returns (bytes4 retval) { | ||
if (retval != IERC721Receiver.onERC721Received.selector) { | ||
revert IERC721Errors.ERC721InvalidReceiver(to); | ||
} | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (bytes memory reason) { | ||
if (reason.length == 0) { | ||
revert IERC721Errors.ERC721InvalidReceiver(to); | ||
} else { | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @solidity memory-safe-assembly | ||
assembly { | ||
revert(add(32, reason), mload(reason)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.