ERC777 - OpenZeppelin Docs (2024)

Like ERC20, ERC777 is a standard for fungible tokens, 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.

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.

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 PaymentSplitter does it), among many others.

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.

What If I Already Use ERC20?

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 EIP’s Backwards Compatibility section to learn more.

Constructing an ERC777 Token Contract

We will replicate the GLD example of the ERC20 guide, this time using ERC777. As always, check out the API reference to learn more about the details of each function.

// contracts/GLDToken.sol// SPDX-License-Identifier: MITpragma solidity ^0.6.0;import "@openzeppelin/contracts/token/ERC777/ERC777.sol";contract GLDToken is ERC777 { constructor(uint256 initialSupply, address[] memory defaultOperators) public ERC777("Gold", "GLD", defaultOperators) { _mint(msg.sender, initialSupply, "", ""); }}

In this case, we’ll be extending from the ERC777 contract, which provides an implementation with compatibility support for ERC20. The API is quite similar to that of ERC777, and we’ll once again make use of _mint to assign the initialSupply to the deployer account. Unlike ERC20’s _mint, this one includes some extra parameters, but you can safely ignore those for now.

You’ll notice both name and symbol are assigned, but not decimals. The ERC777 specification makes it mandatory to include support for these functions (unlike ERC20, where it is optional and we had to include ERC20Detailed), 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 ERC20 guide.

Finally, we’ll need to set the defaultOperators: 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 balanceOf method to query the deployer’s balance:

> GLDToken.balanceOf(deployerAddress)1000

To move tokens from one account to another, we can use both ERC20's transfer method, or the new ERC777's send, which fulfills a very similar role, but adds an optional data field:

> GLDToken.transfer(otherAddress, 300)> GLDToken.send(otherAddress, 300, "")> GLDToken.balanceOf(otherAddress)600> GLDToken.balanceOf(deployerAddress)400

Sending Tokens to Contracts

A key difference when using send is that token transfers to other contracts may revert with the following message:

ERC777: token recipient contract has no implementer for ERC777TokensRecipient

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, the Golem contract currently holds over 350k GNT tokens, 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.

An upcoming guide will cover how a contract can register itself as a recipient, send and receive hooks, and other advanced features of ERC777!

ERC777 - OpenZeppelin Docs (2024)

FAQs

What is ERC 777? ›

ERC-777 Meaning: ERC-777 - a token standard for producing fungible tokens on the Ethereum network, which allows a novel token contract interaction method that is backward compatible.

What is the usage of ERC777? ›

Like ERC20, ERC777 is a standard for fungible tokens, 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.

What is Openzeppelin ERC20? ›

An ERC20 token contract keeps track of fungible tokens: any one token is exactly equal to any other token; no tokens have special rights or behavior associated with them. This makes ERC20 tokens useful for things like a medium of exchange currency, voting rights, staking, and more.

What is the difference between a smart contract and a token? ›

Smart contracts can be used to create, manage, or serve as the description of tokens. A token is a digital representation of an asset that can include artwork, cryptocurrency, carbon credits, etc.

What are the key features of ERC-777? ›

Main Features of ERC-777

ERC-777 implements a new standard, ERC-820, which enables the registration of metadata for contracts to provide basic introspection capabilities. This approach enables backward compatibility as well as the extension of additional functionality.

What is ERC777 token? ›

ERC777 is a token standard on the Ethereum blockchain that offer features such as improved fungibility, built-in hooks, and callbacks while preserving backward compatiblity with ERC20 tokens.

What danger do ERC777 tokens pose? ›

What danger do ERC-777 tokens pose? –ERC-777 allows a sender of a transaction specified a contract to call, which can be used to perform reentrancy attack. From the EIP: The holder can “authorize” and “revoke” operators which can send tokens on their behalf.

What is erc677? ›

ERC-677 expands the functionality of smart contracts by enabling them to receive tokens and process related information in a single transaction.

What is the use of OpenZeppelin? ›

OpenZeppelin Contracts helps you minimize risk by using battle-tested libraries of smart contracts for Ethereum and other blockchains. It includes the most used implementations of ERC standards.

What are ERC tokens used for? ›

Key Takeaways. An ERC20 token is a standard for creating and issuing smart contracts on the Ethereum blockchain. ERC stands for "Ethereum Request for Comment," and the ERC20 standard was implemented in 2015.

Is an ERC20 token a security? ›

Use Cases and Varieties of ERC-20 Tokens

Note that this feature means such tokens may be considered securities by financial regulators, potentially subjecting the issuers to a range of legal obligations depending on the jurisdiction.

Is a wallet a smart contract? ›

The majority of prominent non-custodial wallets use accounts owned by third parties. Client Accounts: This type of account lacks a private key but includes an address, code, and storage space. To store and execute the code, contract-based wallets, also known as smart contract wallets, rely on contract accounts.

Is smart contract the same as blockchain? ›

What are smart contracts? Smart contracts are digital contracts stored on a blockchain that are automatically executed when predetermined terms and conditions are met.

Is a smart contract a Dapp? ›

Decentralized applications rely on smart contracts, but smart contracts don't necessarily rely on dApps. Decentralized apps use smart contracts to automate many processes, ranging from core services of the application to general governance functions.

Is ERC money legit? ›

Yes, the ERC is a legitimate refundable tax credit.

Who gets ERC money? ›

To qualify for the ERC, you must have been subject to a government order that fully or partially suspended your trade or business. If you use a third party to calculate or claim your ERC, you should ask them to give you a copy of the government orders – not a generic narrative about an order.

What is the ERC credit used for? ›

The Employee Retention Credit is a fully refundable tax credit that eligible employers claim against certain employment taxes. It is not a loan and does not have to be paid back. For most taxpayers, the refundable credit is in excess of the payroll taxes paid in a credit-generating period.

Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6090

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.