ERC-1363 Payable Token Standard
页面最后更新: 2025年4月4日
Introduction
What is ERC-1363?
ERC-1363 is an extension interface for ERC-20 tokens that supports executing custom logic on a recipient contract after transfers, or on a spender contract after approvals, all within a single transaction.
Differences from ERC-20
Standard ERC-20 operations like transfer
, transferFrom
and approve
, do not allow code execution on the recipient or spender contract without a separate transaction.
This introduces complexity in UI development and friction on adoption because users must wait for the first transaction to be executed and then submit the second one.
They must also pay GAS twice.
ERC-1363 makes fungible tokens capable of performing actions more easily and working without the use of any off-chain listener. It allows to make a callback on a receiver or spender contract, after a transfer or an approval, in a single transaction.
Prerequisites
To better understand this page, we recommend you first read about:
Body
ERC-1363 introduces a standard API for ERC-20 tokens to interact with smart contracts after transfer
, transferFrom
or approve
.
This standard provides basic functionality to transfer tokens, as well as allow tokens to be approved so they can be spent by another on-chain third party, and then make a callback on the receiver or spender contract.
There are many proposed uses of smart contracts that can accept ERC-20 callbacks.
Examples could be:
- Crowdsales: tokens sent trigger instant reward allocation.
- Services: payment activates service access in one step.
- Invoices: tokens settle invoices automatically.
- Subscriptions: approving annual rate activates subscription within the first month’s payment.
For these reasons it was originally named "Payable Token".
The callback behavior further expands its utility, enabling seamless interactions like:
- Staking: tokens transferred trigger automatic locking in a staking contract.
- Voting: tokens received register votes in a governance system.
- Swapping: token approvals activate swap logic in a single step.
ERC-1363 tokens can be used for specific utilities in all cases that require a callback to be executed after a transfer or an approval received. ERC-1363 is also useful for avoiding token loss or token locking in smart contracts by verifying the recipient's ability to handle tokens.
Unlike other ERC-20 extension proposals, ERC-1363 doesn't override the ERC-20 transfer
and transferFrom
methods and defines the interfaces IDs to be implemented maintaining backward compatibility with ERC-20.
From EIP-1363:
Methods
Smart contracts implementing the ERC-1363 standard MUST implement all of the functions in the ERC1363
interface, as well as the ERC20
and ERC165
interfaces.
1pragma solidity ^0.8.0;23/**4 * @title ERC13635 * @dev An extension interface for ERC-20 tokens that supports executing code on a recipient contract6 * after `transfer` or `transferFrom`, or code on a spender contract after `approve`, in a single transaction.7 */8interface ERC1363 is ERC20, ERC165 {9 /*10 * NOTE: the ERC-165 identifier for this interface is 0xb0202a11.11 * 0xb0202a11 ===12 * bytes4(keccak256('transferAndCall(address,uint256)')) ^13 * bytes4(keccak256('transferAndCall(address,uint256,bytes)')) ^14 * bytes4(keccak256('transferFromAndCall(address,address,uint256)')) ^15 * bytes4(keccak256('transferFromAndCall(address,address,uint256,bytes)')) ^16 * bytes4(keccak256('approveAndCall(address,uint256)')) ^17 * bytes4(keccak256('approveAndCall(address,uint256,bytes)'))18 */1920 /**21 * @dev Moves a `value` amount of tokens from the caller's account to `to`22 * and then calls `ERC1363Receiver::onTransferReceived` on `to`.23 * @param to The address to which tokens are being transferred.24 * @param value The amount of tokens to be transferred.25 * @return A boolean value indicating the operation succeeded unless throwing.26 */27 function transferAndCall(address to, uint256 value) external returns (bool);2829 /**30 * @dev Moves a `value` amount of tokens from the caller's account to `to`31 * and then calls `ERC1363Receiver::onTransferReceived` on `to`.32 * @param to The address to which tokens are being transferred.33 * @param value The amount of tokens to be transferred.34 * @param data Additional data with no specified format, sent in call to `to`.35 * @return A boolean value indicating the operation succeeded unless throwing.36 */37 function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool);3839 /**40 * @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism41 * and then calls `ERC1363Receiver::onTransferReceived` on `to`.42 * @param from The address from which to send tokens.43 * @param to The address to which tokens are being transferred.44 * @param value The amount of tokens to be transferred.45 * @return A boolean value indicating the operation succeeded unless throwing.46 */47 function transferFromAndCall(address from, address to, uint256 value) external returns (bool);4849 /**50 * @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism51 * and then calls `ERC1363Receiver::onTransferReceived` on `to`.52 * @param from The address from which to send tokens.53 * @param to The address to which tokens are being transferred.54 * @param value The amount of tokens to be transferred.55 * @param data Additional data with no specified format, sent in call to `to`.56 * @return A boolean value indicating the operation succeeded unless throwing.57 */58 function transferFromAndCall(address from, address to, uint256 value, bytes calldata data) external returns (bool);5960 /**61 * @dev Sets a `value` amount of tokens as the allowance of `spender` over the caller's tokens62 * and then calls `ERC1363Spender::onApprovalReceived` on `spender`.63 * @param spender The address which will spend the funds.64 * @param value The amount of tokens to be spent.65 * @return A boolean value indicating the operation succeeded unless throwing.66 */67 function approveAndCall(address spender, uint256 value) external returns (bool);6869 /**70 * @dev Sets a `value` amount of tokens as the allowance of `spender` over the caller's tokens71 * and then calls `ERC1363Spender::onApprovalReceived` on `spender`.72 * @param spender The address which will spend the funds.73 * @param value The amount of tokens to be spent.74 * @param data Additional data with no specified format, sent in call to `spender`.75 * @return A boolean value indicating the operation succeeded unless throwing.76 */77 function approveAndCall(address spender, uint256 value, bytes calldata data) external returns (bool);78}7980interface ERC20 {81 event Transfer(address indexed from, address indexed to, uint256 value);82 event Approval(address indexed owner, address indexed spender, uint256 value);83 function transfer(address to, uint256 value) external returns (bool);84 function transferFrom(address from, address to, uint256 value) external returns (bool);85 function approve(address spender, uint256 value) external returns (bool);86 function totalSupply() external view returns (uint256);87 function balanceOf(address account) external view returns (uint256);88 function allowance(address owner, address spender) external view returns (uint256);89}9091interface ERC165 {92 function supportsInterface(bytes4 interfaceId) external view returns (bool);93}显示全部
A smart contract that wants to accept ERC-1363 tokens via transferAndCall
or transferFromAndCall
MUST implement the ERC1363Receiver
interface:
1/**2 * @title ERC1363Receiver3 * @dev Interface for any contract that wants to support `transferAndCall` or `transferFromAndCall` from ERC-1363 token contracts.4 */5interface ERC1363Receiver {6 /**7 * @dev Whenever ERC-1363 tokens are transferred to this contract via `ERC1363::transferAndCall` or `ERC1363::transferFromAndCall`8 * by `operator` from `from`, this function is called.9 *10 * NOTE: To accept the transfer, this must return11 * `bytes4(keccak256("onTransferReceived(address,address,uint256,bytes)"))`12 * (i.e. 0x88a7ca5c, or its own function selector).13 *14 * @param operator The address which called `transferAndCall` or `transferFromAndCall` function.15 * @param from The address which are tokens transferred from.16 * @param value The amount of tokens transferred.17 * @param data Additional data with no specified format.18 * @return `bytes4(keccak256("onTransferReceived(address,address,uint256,bytes)"))` if transfer is allowed unless throwing.19 */20 function onTransferReceived(address operator, address from, uint256 value, bytes calldata data) external returns (bytes4);21}显示全部
A smart contract that wants to accept ERC-1363 tokens via approveAndCall
MUST implement the ERC1363Spender
interface:
1/**2 * @title ERC1363Spender3 * @dev Interface for any contract that wants to support `approveAndCall` from ERC-1363 token contracts.4 */5interface ERC1363Spender {6 /**7 * @dev Whenever an ERC-1363 tokens `owner` approves this contract via `ERC1363::approveAndCall`8 * to spend their tokens, this function is called.9 *10 * NOTE: To accept the approval, this must return11 * `bytes4(keccak256("onApprovalReceived(address,uint256,bytes)"))`12 * (i.e. 0x7b04a2d0, or its own function selector).13 *14 * @param owner The address which called `approveAndCall` function and previously owned the tokens.15 * @param value The amount of tokens to be spent.16 * @param data Additional data with no specified format.17 * @return `bytes4(keccak256("onApprovalReceived(address,uint256,bytes)"))` if approval is allowed unless throwing.18 */19 function onApprovalReceived(address owner, uint256 value, bytes calldata data) external returns (bytes4);20}显示全部