-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Create a ERC1363Utils helper similar to existing ERC721Utils and ERC1155Utils #5133
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
Merged
Changes from 2 commits
Commits
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 | ||
| --- | ||
|
|
||
| `ERC1363Utils`: new helper similar to the existing `ERC721Utils` and `ERC1155Utils` | ||
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
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,96 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {IERC1363Receiver} from "../../../interfaces/IERC1363Receiver.sol"; | ||
| import {IERC1363Spender} from "../../../interfaces/IERC1363Spender.sol"; | ||
|
|
||
| /** | ||
| * @dev Library that provide common ERC-1363 utility functions. | ||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * See https://eips.ethereum.org/EIPS/eip-1363[ERC-1363]. | ||
| */ | ||
| library ERC1363Utils { | ||
| /** | ||
| * @dev Indicates a failure with the token `receiver`. Used in transfers. | ||
| * @param receiver Address to which tokens are being transferred. | ||
| */ | ||
| error ERC1363InvalidReceiver(address receiver); | ||
|
|
||
| /** | ||
| * @dev Indicates a failure with the token `spender`. Used in approvals. | ||
| * @param spender Address that may be allowed to operate on tokens without being their owner. | ||
| */ | ||
| error ERC1363InvalidSpender(address spender); | ||
|
|
||
| /** | ||
| * @dev Performs a call to {IERC1363Receiver-onTransferReceived} on a target address. | ||
| * | ||
| * Requirements: | ||
| * | ||
| * - The target has code (i.e. is a contract). | ||
| * - The target `to` must implement the {IERC1363Receiver} interface. | ||
| * - The target must return the {IERC1363Receiver-onTransferReceived} selector to accept the transfer. | ||
| */ | ||
| function checkOnERC1363TransferReceived( | ||
| address operator, | ||
| address from, | ||
| address to, | ||
| uint256 value, | ||
| bytes memory data | ||
| ) internal { | ||
| if (to.code.length == 0) { | ||
| revert ERC1363InvalidReceiver(to); | ||
| } | ||
|
|
||
| try IERC1363Receiver(to).onTransferReceived(operator, from, value, data) returns (bytes4 retval) { | ||
| if (retval != IERC1363Receiver.onTransferReceived.selector) { | ||
| revert ERC1363InvalidReceiver(to); | ||
| } | ||
| } catch (bytes memory reason) { | ||
| if (reason.length == 0) { | ||
| revert ERC1363InvalidReceiver(to); | ||
| } else { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| revert(add(32, reason), mload(reason)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Performs a call to {IERC1363Spender-onApprovalReceived} on a target address. | ||
| * | ||
| * Requirements: | ||
| * | ||
| * - The target has code (i.e. is a contract). | ||
| * - The target `spender` must implement the {IERC1363Spender} interface. | ||
| * - The target must return the {IERC1363Spender-onApprovalReceived} selector to accept the approval. | ||
| */ | ||
| function checkOnERC1363ApprovalReceived( | ||
| address operator, | ||
| address spender, | ||
| uint256 value, | ||
| bytes memory data | ||
| ) internal { | ||
| if (spender.code.length == 0) { | ||
| revert ERC1363InvalidSpender(spender); | ||
| } | ||
|
|
||
| try IERC1363Spender(spender).onApprovalReceived(operator, value, data) returns (bytes4 retval) { | ||
| if (retval != IERC1363Spender.onApprovalReceived.selector) { | ||
| revert ERC1363InvalidSpender(spender); | ||
| } | ||
| } catch (bytes memory reason) { | ||
| if (reason.length == 0) { | ||
| revert ERC1363InvalidSpender(spender); | ||
| } else { | ||
| /// @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
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.