-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
730 additions
and
187 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
--- | ||
"@cartesi/rollups": major | ||
--- | ||
|
||
Added contract `AssetTransferToENS` that can be used as a destination for `DELEGATECALL` vouchers to transfer assets to ENS-identified accounts. | ||
Added library `LibAddress` for safe low level call and safe delegate call. |
This file contains 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 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,86 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {ENS} from "@ensdomains/ens-contracts/contracts/registry/ENS.sol"; | ||
import {AddrResolver} from "@ensdomains/ens-contracts/contracts/resolvers/profiles/AddrResolver.sol"; | ||
|
||
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | ||
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol"; | ||
|
||
import {LibAddress} from "../library/LibAddress.sol"; | ||
|
||
contract AssetTransferToENS { | ||
using LibAddress for address; | ||
using SafeERC20 for IERC20; | ||
|
||
ENS immutable _ens; | ||
|
||
constructor(ENS ens) { | ||
_ens = ens; | ||
} | ||
|
||
function sendEtherToENS( | ||
bytes32 node, | ||
uint256 value, | ||
bytes memory payload | ||
) external { | ||
address recipient = _resolveENS(node); | ||
recipient.safeCall(value, payload); | ||
} | ||
|
||
function sendERC20ToENS( | ||
IERC20 token, | ||
bytes32 node, | ||
uint256 value | ||
) external { | ||
address recipient = _resolveENS(node); | ||
token.safeTransfer(recipient, value); | ||
} | ||
|
||
function sendERC721ToENS( | ||
IERC721 token, | ||
bytes32 node, | ||
uint256 tokenId, | ||
bytes calldata data | ||
) external { | ||
address recipient = _resolveENS(node); | ||
token.safeTransferFrom(address(this), recipient, tokenId, data); | ||
} | ||
|
||
function sendERC1155ToENS( | ||
IERC1155 token, | ||
bytes32 node, | ||
uint256 id, | ||
uint256 value, | ||
bytes calldata data | ||
) external { | ||
address recipient = _resolveENS(node); | ||
token.safeTransferFrom(address(this), recipient, id, value, data); | ||
} | ||
|
||
function sendBatchERC1155ToENS( | ||
IERC1155 token, | ||
bytes32 node, | ||
uint256[] memory ids, | ||
uint256[] memory values, | ||
bytes calldata data | ||
) external { | ||
address recipient = _resolveENS(node); | ||
token.safeBatchTransferFrom( | ||
address(this), | ||
recipient, | ||
ids, | ||
values, | ||
data | ||
); | ||
} | ||
|
||
function _resolveENS(bytes32 node) internal view returns (address) { | ||
AddrResolver resolver = AddrResolver(_ens.resolver(node)); | ||
return resolver.addr(node); | ||
} | ||
} |
This file contains 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,48 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
pragma solidity ^0.8.8; | ||
|
||
import {LibError} from "../library/LibError.sol"; | ||
|
||
library LibAddress { | ||
using LibError for bytes; | ||
|
||
/// @notice Perform a low level call and raise error if failed | ||
/// @param destination The address that will be called | ||
/// @param value The amount of Wei to be transferred through the call | ||
/// @param payload The payload, which—in the case of Solidity | ||
/// contracts—encodes a function call | ||
function safeCall( | ||
address destination, | ||
uint256 value, | ||
bytes memory payload | ||
) internal { | ||
bool success; | ||
bytes memory returndata; | ||
|
||
(success, returndata) = destination.call{value: value}(payload); | ||
|
||
if (!success) { | ||
returndata.raise(); | ||
} | ||
} | ||
|
||
/// @notice Perform a delegate call and raise error if failed | ||
/// @param destination The address that will be called | ||
/// @param payload The payload, which—in the case of Solidity | ||
/// libraries—encodes a function call | ||
function safeDelegateCall( | ||
address destination, | ||
bytes memory payload | ||
) internal { | ||
bool success; | ||
bytes memory returndata; | ||
|
||
(success, returndata) = destination.delegatecall(payload); | ||
|
||
if (!success) { | ||
returndata.raise(); | ||
} | ||
} | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.