ERC20 - OpenZeppelin Docs (2024)

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.

OpenZeppelin Contracts provides many ERC20-related contracts. On the API reference you’ll find detailed information on their properties and usage.

Constructing an ERC20 Token Contract

Using Contracts, we can easily create our own ERC20 token contract, which will be used to track Gold (GLD), an internal currency in a hypothetical game.

Here’s what our GLD token might look like.

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

Our contracts are often used via inheritance, and here we’re reusing ERC20 for both the basic standard implementation and the name, symbol, and decimals optional extensions. Additionally, we’re creating an initialSupply of tokens, which will be assigned to the address that deploys the contract.

For a more complete discussion of ERC20 supply mechanisms, see Creating ERC20 Supply.

That’s it! Once deployed, we will be able to query the deployer’s balance:

> GLDToken.balanceOf(deployerAddress)1000000000000000000000

We can also transfer these tokens to other accounts:

> GLDToken.transfer(otherAddress, 300000000000000000000)> GLDToken.balanceOf(otherAddress)300000000000000000000> GLDToken.balanceOf(deployerAddress)700000000000000000000

A Note on decimals

Often, you’ll want to be able to divide your tokens into arbitrary amounts: say, if you own 5 GLD, you may want to send 1.5 GLD to a friend, and keep 3.5 GLD to yourself. Unfortunately, Solidity and the EVM do not support this behavior: only integer (whole) numbers can be used, which poses an issue. You may send 1 or 2 tokens, but not 1.5.

To work around this, ERC20 provides a decimals field, which is used to specify how many decimal places a token has. To be able to transfer 1.5 GLD, decimals must be at least 1, since that number has a single decimal place.

How can this be achieved? It’s actually very simple: a token contract can use larger integer values, so that a balance of 50 will represent 5 GLD, a transfer of 15 will correspond to 1.5 GLD being sent, and so on.

It is important to understand that decimals is only used for display purposes. All arithmetic inside the contract is still performed on integers, and it is the different user interfaces (wallets, exchanges, etc.) that must adjust the displayed values according to decimals. The total token supply and balance of each account are not specified in GLD: you need to divide by 10^decimals to get the actual GLD amount.

You’ll probably want to use a decimals value of 18, just like Ether and most ERC20 token contracts in use, unless you have a very special reason not to. When minting tokens or transferring them around, you will be actually sending the number num GLD * 10^decimals.

By default, ERC20 uses a value of 18 for decimals. To use a different value, you will need to call _setupDecimals in your constructor.

So if you want to send 5 tokens using a token contract with 18 decimals, the the method to call will actually be:

transfer(recipient, 5 * 10^18);

Preset ERC20 contract

A preset ERC20 is available, ERC20PresetMinterPauser. It is preset to allow for token minting (create), stop all token transfers (pause) and allow holders to burn (destroy) their tokens. The contract uses Access Control to control access to the minting and pausing functionality. The account that deploys the contract will be granted the minter and pauser roles, as well as the default admin role.

This contract is ready to deploy without having to write any Solidity code. It can be used as-is for quick prototyping and testing, but is also suitable for production environments.

ERC20 - OpenZeppelin Docs (2024)

FAQs

Is ERC20 an OpenZeppelin? ›

This makes ERC20 tokens useful for things like a medium of exchange currency, voting rights, staking, and more. OpenZeppelin Contracts provides many ERC20-related contracts. On the API reference you'll find detailed information on their properties and usage.

What happens when ERC20 transaction fails? ›

Whenever there is an ERC-20 token transfer might have failed error, the amount/token the sender sent does not leave the sender address but the “Gas Fee” is deducted. The main reasons are on the contract & token failure nature.

How many decimals should my token have? ›

Keep in mind that there are no floats in Solidity. Therefore most tokens adopt 18 decimals and will return the total supply and other results as followed 1000000000000000000 for 1 token. Not every token has 18 decimals and this is something you really need to watch for when dealing with tokens.

How do you make an ERC20 token in OpenZeppelin? ›

Declare contract and create constructor

Give contract your token name and also in ERC20("", "") put your token name and symbol parameters. Also constructor takes one more parameter - initialSupply, this will decide how much tokens you want to supply. note that the default decimals in openzeppeline in 18.

Is ERC20 the same as ERC721? ›

The main distinction between ERC20 and ERC721 tokens is that the former is a fungible token, but the latter is a non -fungible token. ERC20 tokens are interchangeable and represent a single entity, whereas ERC721 tokens represent a collection of assets.

Is ERC20 an Sxp? ›

The Swipe Token (SXP) is available both as an ERC-20 token (on Ethereum) and as a BEP-2 token (on the Binance Chain).

Can an ETH transaction be stuck forever? ›

Since Ethereum transactions have to be processed sequentially, users can get completely stuck. In other words, the longest-running transaction has to be processed before any more recent transaction from that wallet has a chance to be processed.

Do failed transactions get refunded? ›

If the failure has happened at the payment gateway level or due to third-party validation, then a refund happens instantly. If the failure happens from the customer's bank, then TAT generally depends on the customer's bank. Some banks refund the money within 24 hours and some take 5-7 working days to refund it.

How long can an ERC-20 transaction take? ›

CoinList waits for 30 confirmations to consider an ETH or ERC-20 transaction final. Although typically this should only take about ~5 minutes,this can take anywhere from five minutes to four hours. You can see the number of confirmations in your CoinList wallet.

How many decimal places is enough? ›

An analogous rule for percentages might be to use enough decimal places to ensure two significant digits for the range of values across groups, eg, if the range is 10% or more use whole numbers, if less than 1% use two decimal places, and otherwise one.

How many decimals are needed for ethereum? ›

ETH is divisible up to 18 decimal places so you don't have to buy 1 whole ETH. You can buy fractions at a time – as little as 0.000000000000000001 ETH if you want.

Is money always to 2 decimal places? ›

All money is a decimal value with two places. You will see examples including $1.25, $65.98, and $26.34. No matter how big the value gets, you can only have two decimal places. One cent is 0.01.

How much does it cost to launch ERC20 token? ›

Due to this development, entrepreneurs are very keen to know about the cost to create ERC20 tokens. On average if you are looking to start a decent ICO project, it will cost you between $500-$2000 for creating millions of ERC20 tokens.

How do I create an ERC token without coding? ›

Users can start the token creation process by connecting their Ethereum wallet (selecting between Wallet Connect or MetaMask) or create one by selecting the “Get wallet” button. They will then need to add enough funds to pay for contract deployment and set up their tokens.

How to create an ERC20 token for free? ›

Open Metamask and click on the Add Token button, select the Custom Token option and paste the contract's address in the first field. Metamask will fetch the Token Symbol and decimals automatically. Click on next and your token will be added to the wallet, it will be available under the assets section in Metamask.

Can an NFT be ERC-20? ›

ERC-20 is the technical standard for fungible tokens created using the Ethereum blockchain. A fungible token is one that is interchangeable with another token—where the well-known non-fungible tokens (NFTs) are not interchangeable. ERC-20 allows different smart-contract enabled tokens a way to be exchanged.

Can ERC721 hold ERC-20? ›

The ERC20 can be linked to ERC721 in one of many ways. To Sub-license means to transfer one (of many) sub-licenses to a new licensee. This is implemented as ERC20.

Should I use ERC721 or ERC 1155? ›

ERC-721 vs ERC-1155: Major Differences

Batch Transfers: ERC-1155 can facilitate batch transfers, reducing network congestion and lowering gas costs by up to 90%. ERC-721's need for transactions to be created for individual NFTs leads to slow, inefficient transactions and higher gas fees.

What are the most popular ERC-20 tokens? ›

Top Ethereum ERC20 Tokens by Market Cap [2022]
  • Aave (AAVE)
  • Chainlink (LINK)
  • Maker (MKR)
  • Wrapped BTC.
  • Matic (MATIC)
  • Apecoin (APE)
  • Cronos (CRO)
  • Bottomline.
Sep 28, 2022

What is the best ERC20 token? ›

Top 10 Ethereum ERC20 Tokens to Buy in 2022
  • BudBlockz.
  • UniSwap.
  • 0x (ZRX)
  • Chainlink.
  • Tether.
  • Shiba Inu.
  • The SandBox.
  • Aave.
Nov 14, 2022

Is Coinbase ERC-20 or Polygon? ›

Coinbase Wallet supports thousands of tokens, including all ERC-20 tokens and all tokens on EVM-compatible chains, such as Avalanche C-Chain and Polygon. This includes stablecoins (like USDC and DAI) on all of Coinbase Wallet's supported networks. Coinbase Wallet also supports tokens hosted on the Solana Network.

How do you get a transaction unstuck? ›

There are three main ways to fix a stuck transaction: cancelling the transaction, increasing the gas fee, or introducing a new transaction with a custom nonce. Before fixing a stuck transaction, it is important to verify the transaction in a block explorer like Etherscan to confirm that it is pending.

How long can an ETH transaction stay pending? ›

How long do I have to wait for a pending transaction?
AssetTypical time pending;
BNB5 minutes or less
Ethereum5 minutes or less
Cardano5 minutes or less
Solana5 minutes or less
3 more rows

How do I cancel stuck Ethereum? ›

The easiest way to cancel a stuck Ethereum transaction is in-application cancellation, which requires users to exit the Ethereum wallet application and close their browser, reopen it and log back into the application.

What can I do if a merchant refuses to refund? ›

If asking the merchant for a refund didn't work, request a chargeback with your credit card issuer. Many card issuers let you dispute transactions by phone, mail or online. You may also be able to submit a dispute directly through your card issuer's mobile app.

What are two types of errors that may cause a transaction to fail? ›

Transaction failure
  • Logical errors − Where a transaction cannot complete because it has some code error or any internal error condition.
  • System errors − Where the database system itself terminates an active transaction because the DBMS is not able to execute it, or it has to stop because of some system condition.

What happens if a transaction is successful but not received? ›

In this case, the amount may get reversed to your payouts account within 24hrs from the transfer status has changed to successful, if the customer's account details are incorrect or if the beneficiary bank has any technical issues.

How can I speed up my ERC20 transaction? ›

  1. Go to your pending transaction details and tap [Speed up] after ensuring the following -
  2. Tap [Speed up] to choose a higher fee to replace your original pending transaction.
  3. Choose a higher fee tier or you can tap [Advanced] to customize your gas price and gas limit (for advanced users)

What happens if an ETH transaction fails? ›

That payment is calculated in gas, and gas is always paid in ETH. You are paying for the computation, regardless of whether your transaction succeeds or fails. Even if it fails, the miners must validate and execute your transaction, which takes computational power.

What is a stuck transaction? ›

As you've probably noticed, a stuck transaction holds up all the transactions you submitted after it. This happens because every Ethereum transaction has what is called a nonce value, which essentially serves as a unique identifier for each transaction submitted by your Ethereum account.

Can money have 3 decimal places? ›

Most currencies have two decimals. Some currencies do not have decimals, and some have three decimals.

Do more decimal places increase accuracy? ›

Roughly speaking, more digits to the right of the decimal point means more precision. The more digits you track on the right of the decimal point, the less you can have on the left of the decimal point.

What are the 3 decimal rules? ›

Rules for Decimals. Line up your decimal points. Add 0s after the decimal point so that all of the numbers have the same number of places after the decimal. Add as you would with whole numbers.

How to set decimals in erc20 token? ›

The ERC-20 token standard defines the decimals() function that your contract implements, effectively shifting all numbers by the declared number of zeros to simulate decimals. So if you wanted to mint 1 token with 18 decimals, you'd need to pass the value 1000000000000000000 (18 zeros).

How much ETH do you need to validate? ›

Validators receive interest on their staked coins, which are denominated in Ether, as a reward for their active participation in the network. To become a validator on Ethereum, users must invest 32 ETH.

Why do I need 32 Ethereum? ›

To become a full validator on Ethereum, ETH holders must stake 32 ETH by depositing the funds into the official deposit contract that has been developed by the Ethereum Foundation. There are many opportunities for people with ETH to begin staking on the Ethereum network and earn rewards.

How many decimal places should be recorded from an electronic balance? ›

reading should be recorded to 2 decimal places – i.e. 3.01 g or 2.99 g or 3.00 g. Any weighing's made with an electronic balance should be recorded with all the digits shown on the screen. Because it is a delicate instrument that can be damaged easily, certain precautions should be taken.

How many pass the decimal point is money rounded too? ›

The US and Canadian dollar can't be transacted with more than 2 decimal places. When numbers represent money, we use rounding to replace an un-representable, un-transactable money amount with one that represents a cash tender.

How many decimal places will a balance read to? ›

The most common analytical balances display four to five decimal point results and are used to weigh materials—chemicals and drugs, usually in powdered, liquid, or solid form.

What type of token is ERC20? ›

An ERC20 token is a standard used for creating and issuing smart contracts on the Ethereum blockchain. Smart contracts can then be used to create smart property or tokenized assets that people can invest in. ERC stands for "Ethereum request for comment," and the ERC20 standard was implemented in 2015.

What type of coin is ERC20? ›

An ERC20 token is a type of virtual asset that is based on the Ethereum blockchain. ERC20 tokens are virtual assets running on the Ethereum blockchain; they have value and users can send and receive them. People can create these tokens via smart contracts on the Ehtereum network.

What is ERC20 network type? ›

ERC-20 is the technical standard for fungible tokens created using the Ethereum blockchain. A fungible token is one that is interchangeable with another token—where the well-known non-fungible tokens (NFTs) are not interchangeable. ERC-20 allows different smart-contract enabled tokens a way to be exchanged.

Is ERC20 the same as ETH? ›

What's the difference between ETH and ERC20? ETH, or “Ether,” is the native cryptocurrency of the Ethereum network, and is used to facilitate transactions on the Ethereum blockchain. ERC20 is the protocol standard for creating Ethereum-based tokens, which can be utilized and deployed in the Ethereum network.

Is Coinbase only ERC20? ›

What assets are supported on Coinbase Wallet? Coinbase Wallet supports thousands of tokens, including all ERC-20 tokens and all tokens on EVM-compatible chains, such as Avalanche C-Chain and Polygon. This includes stablecoins (like USDC and DAI) on all of Coinbase Wallet's supported networks.

What are the most popular ERC20 tokens? ›

Top Ethereum ERC20 Tokens by Market Cap [2022]
  • Aave (AAVE)
  • Chainlink (LINK)
  • Maker (MKR)
  • Wrapped BTC.
  • Matic (MATIC)
  • Apecoin (APE)
  • Cronos (CRO)
  • Bottomline.
Sep 28, 2022

Is Shib an ERC20? ›

Shiba Inu's primary token is SHIB, an ERC-20 token with a fixed supply of one quadrillion tokens.

How much is ERC20 worth? ›

The current price is $0.0082 per ERC20.

How do I identify my ERC20? ›

The exchange will make it clear what type of chain they are using; however, one easy way to differentiate between these two is by the way the address is structured. An ERC20 Address only starts with '0' and 'x'. For example: 0xbc6e. A TRC20 Address only begins with a capital 'T'.

What wallets use ERC20? ›

6 of the Best ERC-20 wallets
  • Atomic Wallet (Desktop & Android) ...
  • Ledger Nano S and X (Hardware ERC-20 Wallet) ...
  • Trezor (Hardware ERC-20 Wallet) ...
  • MyEtherWallet (Online & Mobile ERC-20 Wallet) ...
  • MetaMask (Web-Based & Mobile ERC-20 Wallet) ...
  • Coinbase (Exchange)
May 5, 2022

Is ERC20 a secret network? ›

Secret tokens are programmable like ERC-20s but preserve privacy like Zcash or Monero. They are the foundation of Secret DeFi, a cross-chain, front-running resistant, and privacy-preserving decentralized financial ecosystem powered by Secret Network.

Can I send ERC20 tokens to my Ethereum address? ›

You can now send and receive ERC20 tokens. Get started by sending your ERC20 tokens to the Ethereum account of your choice. The token accounts will only appear after the token transaction has been confirmed on the Ethereum blockchain.

What is OpenZeppelin? ›

OpenZeppelin is an open-source framework to build secure smart contracts. OpenZeppelin provides a complete suite of security products and audit services to build, manage, and inspect all aspects of software development and operations for decentralized applications.

Why is ERC20 fees so high? ›

The main reason for the high fees of Bitcoin miners is the organic market. Bitcoin block size is 1MB, which means that miners can only confirm 1MB of exchange value for each square (like clockwork). This is why the excavator fees on ERC-20 are so high.

Can I swap ERC20 to Ethereum? ›

Users must have some Ethereum (ETH) balance before they can swap/transfer for ERC20 tokens. Also, it is recommended that users leave $3 - $10 worth of ETH balance at all times in their Bitcoin.com ETH wallet to cover the transaction fees which are paid in ETH in case one wants to send out or swap ERC20 tokens.

What will happen to ERC20 tokens after merge? ›

Any ETH and ERC-20 tokens sent to your CoinJar account on the ETHPOW chain will be lost permanently. It is highly unlikely that ERC-20 tokens and stablecoins on the ETHPOW chain will have any value after the Merge, so always make sure you are buying or trading tokens on the new ETH mainnet.

Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6275

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.