Skip to main content

ERC1363

ERC-1363: 可支付的代币

ERC-1363 是 ERC-20 代币标准的扩展,它添加了一些新的功能,使得代币可以在被转移的同时执行一些逻辑。这意味着 ERC-1363 代币可以在支付或转账时触发合约中的函数,这种功能在执行如众筹等需要在收到代币时立即触发合约逻辑的场景中非常有用。

ERC-1363 定义了transferAndCalltransferFromAndCall方法,这些方法在转移代币时会调用接收方合约的特定回调函数。这允许接收方合约在处理收到的代币时执行额外的逻辑,比如更新状态、发放许可、触发事件等。

总结来说,ERC-165 是关于如何检测合约接口的标准,而 ERC-1363 则是关于如何在 ERC-20 代币转移的同时执行附加逻辑的标准。两者都为以太坊智能合约的互操作性和功能性提供了重要的机制。

IERC1363

  1. transferAndCall:转移代币并调用接收方合约的回调函数。
  2. transferFromAndCall :使用授权机制转移代币并调用接收方合约的回调函数。
  3. approveAndCall:设置代币授权并调用接收方合约的回调函数。
contracts/interfaces/IERC1363.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC1363.sol)

pragma solidity ^0.8.20;

import {IERC20} from "./IERC20.sol";
import {IERC165} from "./IERC165.sol";

/**
* @title IERC1363
* @dev Interface of the ERC-1363 standard as defined in the https://eips.ethereum.org/EIPS/eip-1363[ERC-1363].
*
* Defines an extension interface for ERC-20 tokens that supports executing code on a recipient contract
* after `transfer` or `transferFrom`, or code on a spender contract after `approve`, in a single transaction.
*/
interface IERC1363 is IERC20, IERC165 {
/*
* Note: the ERC-165 identifier for this interface is 0xb0202a11.
* 0xb0202a11 ===
* bytes4(keccak256('transferAndCall(address,uint256)')) ^
* bytes4(keccak256('transferAndCall(address,uint256,bytes)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256,bytes)')) ^
* bytes4(keccak256('approveAndCall(address,uint256)')) ^
* bytes4(keccak256('approveAndCall(address,uint256,bytes)'))
*/

/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value) external returns (bool);

/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @param data Additional data with no specified format, sent in call to `to`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool);

/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param from The address which you want to send tokens from.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferFromAndCall(address from, address to, uint256 value) external returns (bool);

/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param from The address which you want to send tokens from.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @param data Additional data with no specified format, sent in call to `to`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferFromAndCall(address from, address to, uint256 value, bytes calldata data) external returns (bool);

/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function approveAndCall(address spender, uint256 value) external returns (bool);

/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
* @param data Additional data with no specified format, sent in call to `spender`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function approveAndCall(address spender, uint256 value, bytes calldata data) external returns (bool);
}

参数 data 是什么?

在 ERC-1363 标准中,data 参数是一个字节序列(bytes 类型),它可以包含任何数据,没有特定的格式要求。当使用transferAndCalltransferFromAndCallapproveAndCall方法时,这个data 参数被传递给接收者或批准者的智能合约。

这个data 参数的目的是提供一个灵活的机制,使得在代币转移或批准的同时,可以发送额外的信息或指令给接收者或批准者的合约。这些信息可能是用于合约内部逻辑的指令、附加的参数、或者是一些上下文信息,以便接收者合约可以根据这些信息执行相应的逻辑。

例如,如果你正在使用transferAndCall方法将代币发送给一个众筹智能合约,你可能会在data 参数中包含关于你希望资助的特定项目的信息。当众筹合约收到代币和数据后,它可以解析这些数据并据此记录你的资助意向。

在智能合约中,这个data 参数通常会通过abi.decode方法来解析,以便提取里面包含的信息。接收者合约应该具备解析并正确处理data 参数的逻辑,以确保能够理解发送方的意图并相应地执行操作。