ERC-777 · Final · ERC · 2017-11-20
ERC-777: Token Standard
An ERC-20-compatible token standard with send hooks and operators, letting holders and recipients react to transfers via the ERC-1820 registry; powerful, but its hooks have caused re-entrancy bugs.
What problem does it solve?
ERC-20 tokens give the receiving contract no signal when tokens arrive. Depositing into a contract needs two steps (approve, then transferFrom), and there is no way to attach data to a transfer or to let a holder block outgoing transfers.
ERC-777 keeps ERC-20 compatibility but adds three ideas:
- Hooks. Sender and recipient can each register a contract that is called on every transfer, and that contract can reject the transfer by reverting.
- Operators. An address can be allowed to send or burn tokens on behalf of a holder. This replaces allowances.
- Data. Every transfer carries
datafrom the holder andoperatorDatafrom the operator.
How it works
The core functions are send(to, amount, data), operatorSend(from, to, amount, data, operatorData), burn and operatorBurn, plus authorizeOperator, revokeOperator, isOperatorFor and defaultOperators. Events are Sent, Minted, Burned, AuthorizedOperator and RevokedOperator.
Hooks are found through the ERC-1820 registry, which maps (address, interfaceHash) to an implementer:
- Before balances change, if the holder registered an
ERC777TokensSender, the token calls itstokensToSend(...). - Balances are updated.
- If the recipient registered an
ERC777TokensRecipient, the token calls itstokensReceived(...). - If the recipient is a contract with no registered recipient hook,
sendand mint must revert. This protects tokens from being stuck. ERC-20transfercalls should still succeed, for compatibility.
Other rules:
granularity()is the smallest unit that can be moved. It is fixed at creation and is usually1.- If the token also offers ERC-20 functions,
decimals()must return18. - The token registers
ERC777Token(andERC20Tokenif compatible) for its own address in ERC-1820.
Sample code
A recipient contract that registers itself in ERC-1820 and accepts one token:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC1820Registry {
function setInterfaceImplementer(address account, bytes32 interfaceHash, address implementer) external;
}
contract Receiver777 {
IERC1820Registry constant REGISTRY =
IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
bytes32 constant RECIPIENT_HASH = keccak256("ERC777TokensRecipient");
address public immutable token;
mapping(address => uint256) public credited;
constructor(address _token) {
token = _token;
REGISTRY.setInterfaceImplementer(address(this), RECIPIENT_HASH, address(this));
}
function tokensReceived(
address, address from, address, uint256 amount, bytes calldata, bytes calldata
) external {
require(msg.sender == token, "wrong token");
credited[from] += amount;
}
}
Things to know
- Re-entrancy risk. The hooks hand control to outside code during a transfer. Protocols that assumed ERC-20 behaviour were exploited through ERC-777 tokens, so any contract that might receive one needs re-entrancy guards and checks-effects-interactions.
- OpenZeppelin deprecated its ERC-777 implementation and removed it in Contracts v5. New tokens generally use ERC-20, with ERC-2612
permitfor single-transaction approvals. - Default operators are fixed at deployment and can move any holder's tokens unless that holder revokes them. Check them before trusting a token.
- ERC-223 tackles the same "notify the recipient" problem without a registry.