OpenZeppelin ERC-777
Last updated
Was this helpful?
Last updated
Was this helpful?
This EIP defines standard interfaces and behaviors for token contracts.
It defines advanced features to interact with tokens. Namely, operators to send tokens on behalf of another address—contract or regular account—and send/receive hooks to offer token holders more control over their tokens.
Uses the same philosophy as Ether in that tokens are sent with send(dest, value, data)
.
Both contracts and regular addresses can control and reject which token they send by registering a tokensToSend
hook. (Rejection is done by revert
ing in the hook function.)
Both contracts and regular addresses can control and reject which token they receive by registering a tokensReceived
hook. (Rejection is done by revert
ing in the hook function.)
The holder can “authorize” and “revoke” operators which can send tokens on their behalf. These operators are intended to be verified contracts such as an exchange, a cheque processor or an automatic charging system.
Every token transaction contains data
and operatorData
bytes fields to be used freely to pass data from the holder and the operator, respectively.
It is backward compatible with wallets that do not contain the tokensReceived
hook function by deploying a proxy contract implementing the tokensReceived
hook for the wallet.
The standard also brings multiple quality-of-life improvements, such as getting rid of the confusion around decimals
, minting and burning with proper events, among others, but its killer feature is receive hooks. A hook is simply a function in a contract that is called when tokens are sent to it, meaning accounts and contracts can react to receiving tokens.
Furthermore, since contracts are required to implement these hooks in order to receive tokens, no tokens can get stuck in a contract that is unaware of the ERC777 protocol, as has happened countless times when using ERC20s.
An upcoming guide will cover how a contract can register itself as a recipient, send and receive hooks, and other advanced features of ERC777!
Interface of the ERC777Token standard as defined in the EIP.
The implementer setter function can be found in IERC1820Registry:
Function definition:
Description:
Sets the implementer
contract as account
's implementer for interfaceHash
.
account
being the zero address is an alias for the caller’s address. The zero address can also be used in implementer
to remove an old one.
Requirements:
the caller must be the current manager for account
.
We are going to discover the "new" functions that ERC20 does not have.
The send()
method is similar to transfer()
but with an additional function parameter data
. If we send()
to a contract, the recipient contract MUST implment the tokensReceived()
callback, otherwise the transaction would revert. This design makes sure that the recipient contract is aware of the ERC777 protocol.
Diving into the code, send()
will invoke the _callTokensToSend()
and _callTokensReceived()
callbacks:
implementer
must be registered in ERC1820 registry in order to use these two callbacks.
This standard defines a new way to interact with a token contract while remaining backward compatible with .
It takes advantage of to find out whether and where to notify contracts and regular addresses when they receive tokens as well as to allow compatibility with already-deployed contracts.
This standard tries to improve upon the widely used token standard. The main advantages of this standard are:
The tokensReceived
hook allows to send tokens to a contract and notify it in a single transaction, unlike which requires a double call (approve
/transferFrom
) to achieve this.
Like , ERC777 is a standard for , and is focused around allowing more complex interactions when trading tokens. More generally, it brings tokens and Ether closer together by providing the equivalent of a msg.value
field, but for tokens.
This enables a lot of interesting use cases, including atomic purchases using tokens (no need to do approve
and transferFrom
in two separate transactions), rejecting reception of tokens (by reverting on the hook call), redirecting the received tokens to other addresses (similarly to how does it), among many others.
The standard has you covered! The ERC777 standard is backwards compatible with ERC20, meaning you can interact with these tokens as if they were ERC20, using the standard functions, while still getting all of the niceties, including send hooks. See the to learn more.
We will replicate the GLD
example of the , this time using ERC777. As always, check out the to learn more about the details of each function.
In this case, we’ll be extending from the contract, which provides an implementation with compatibility support for ERC20. The API is quite similar to that of , and we’ll once again make use of to assign the initialSupply
to the deployer account. Unlike , this one includes some extra parameters, but you can safely ignore those for now.
You’ll notice both and are assigned, but not . The ERC777 specification makes it mandatory to include support for these functions (unlike ERC20, where it is optional and we had to include ), but also mandates that decimals
always returns a fixed value of 18
, so there’s no need to set it ourselves. For a review of decimals
's role and importance, refer back to our .
Finally, we’ll need to set the : special accounts (usually other smart contracts) that will be able to transfer tokens on behalf of their holders. If you’re not planning on using operators in your token, you can simply pass an empty array. Stay tuned for an upcoming in-depth guide on ERC777 operators!
That’s it for a basic token contract! We can now deploy it, and use the same method to query the deployer’s balance:
To move tokens from one account to another, we can use both method, or the new , which fulfills a very similar role, but adds an optional data
field:
A key difference when using is that token transfers to other contracts may revert with the following message:
This is a good thing! It means that the recipient contract has not registered itself as aware of the ERC777 protocol, so transfers to it are disabled to prevent tokens from being locked forever. As an example, , worth multiple tens of thousands of dollars, and lacks methods to get them out of there. This has happened to virtually every ERC20-backed project, usually due to user error.
This contract uses the to let token holders and recipients react to token movements by using setting implementers for the associated interfaces in said registry. See and .
See to learn how these are created.
Emits an event.
interfaceHash
must not be an interface id (i.e. it must not end in 28 zeroes).
implementer
must implement and return true when queried for support, unless implementer
is the caller. See .