ERC-1363 · Final · ERC · 2018-08-30
ERC-1363: Payable Token
Adds transferAndCall and approveAndCall to ERC-20 so a token payment and the receiving contract's reaction happen in a single transaction.
What problem does it solve?
With plain ERC-20, a contract is not told when it receives tokens. To pay a contract for a service you usually need two transactions: approve, then a call that makes the contract pull the tokens with transferFrom. That costs extra gas, adds UX friction, and tokens sent with a plain transfer to a contract that does not expect them can get stuck.
ERC-1363, "Payable Token", adds functions that move tokens and notify the recipient in the same transaction, borrowing the receiver-hook idea from ERC-721's onERC721Received. Typical uses: paying invoices, buying services, subscriptions or token-priced sales.
How it works
The token keeps every ERC-20 function and adds six (two overloads each, with and without a bytes data argument):
| Function | After the ERC-20 action, calls |
|---|---|
transferAndCall(to, value[, data]) |
to.onTransferReceived(operator, from, value, data) |
transferFromAndCall(from, to, value[, data]) |
to.onTransferReceived(operator, from, value, data) |
approveAndCall(spender, value[, data]) |
spender.onApprovalReceived(owner, value, data) |
- The receiver must return the function's selector as a "magic value" (
0x88a7ca5cforonTransferReceived,0x7b04a2d0foronApprovalReceived); any other return value reverts the whole operation. The receiver may also revert to reject the payment. - Inside the callback,
msg.senderis always the token contract, so the receiver knows which token arrived. - The token must support ERC-165 and report interface id
0xb0202a11.
Sample code
A contract that sells a service for an ERC-1363 token in one transaction:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC1363Receiver {
function onTransferReceived(address operator, address from, uint256 value, bytes calldata data)
external returns (bytes4);
}
contract PayableService is IERC1363Receiver {
address public immutable acceptedToken;
uint256 public immutable price;
mapping(address => uint256) public credits;
constructor(address token, uint256 _price) { acceptedToken = token; price = _price; }
function onTransferReceived(address, address from, uint256 value, bytes calldata)
external returns (bytes4)
{
require(msg.sender == acceptedToken, "wrong token"); // only trust the real token
require(value >= price, "underpaid");
credits[from] += value / price;
return IERC1363Receiver.onTransferReceived.selector; // 0x88a7ca5c
}
}
The payer then calls token.transferAndCall(address(service), price); no prior approve is needed.
Things to know
- Status: Final. The token stays fully ERC-20 compatible, unlike ERC-223 and ERC-777, which change transfer behaviour.
- Always check
msg.senderin the callback. Anyone can callonTransferReceiveddirectly with fake arguments. - The callback is an external call during a transfer, so apply normal reentrancy hygiene.
approveAndCallinherits ERC-20's allowance-change race condition; the spec suggests setting the allowance to 0 before changing it.