Kiến thức

How to Get an ERC-20 Token Address

Users, decentralized applications (dapps), and other smart contracts need the address of an ERC-20 token to call functions, send transactions, or query data. However, what exactly is a token address? And how do you get them? If you’re looking for the answers to these queries, join us in this tutorial, as we’ll show you the easiest way to get the address of any ERC-20 token!

Overview

In today’s article, we’ll kick things off by exploring the intricacies of ERC-20 token addresses. From there, we’ll jump straight into the main topic and show you how to get the address of any ERC-20 token using the industry’s leading token explorer – Moralis Money – and a block explorer like Etherscan. Next, we’ll also cover other ways to get ERC-20 token addresses using Moralis and its various APIs, which can help you in your Web3 development endeavors. More specifically, we’ll explore the following endpoints:

  • getWalletTokenBalances()
  • getTopERC20TokensByMarketCap()
  • getTopERC20TokensByPriceMovers()

Lastly, to top things off, we’ll give you an in-depth tutorial on how to get the addresses of all ERC-20 tokens in a wallet using Moralis!

Throughout this article, we’re going to utilize Moralis’ Web3 Market Data API and Token API to get the addresses of ERC-20 tokens. However, if you’re serious about building Web3 projects, we urge you to explore some additional Moralis tools as well. For instance, check out the Price API and explore the easiest way to get prices of crypto.

With the tools in Moralis’ Web3 API suite, you can build Web3 projects faster and more efficiently. As such, if you haven’t already, make sure to sign up with Moralis and get immediate access to our blockchain development tools for free!

What is an ERC-20 Token Address?

ERC-20 – short for Ethereum Request for Comment 20 – is the most widely adopted standard for creating tokens on the Ethereum blockchain. The standard defines a set of requirements, functions, and events all fungible tokens must implement to be considered ERC-20 compliant. Some prominent examples include functions for transferring tokens between addresses, checking the token’s total supply, etc.

One of the key goals of the ERC-20 standard is to guarantee identical values between tokens. For example, one ETH token will always equal another ETH.

The uniformity of the ERC-20 token standard is essential for the Ethereum ecosystem, making it significantly easier for Web3 wallets, decentralized applications (dapps), and other platforms to integrate tokens into their software. This is, for example, why you can seamlessly add any ERC-20 token to a wallet like MetaMask, a task that would be significantly harder without a prominent standard.

That said, what is the address of an ERC-20 token?

ERC-20 tokens are created by inputting some data into a template smart contract responsible for controlling and managing the token. When the token creator first deploys it to the blockchain, an address that looks similar to any public Ethereum wallet address is generated. Consequently, an ERC-20 token address is essentially a unique sequence of letters and numbers acting as a unique identifier for the smart contract in question.

Chuyên gia chia sẻ  Kết quả Real vs Mallorca: Thắng lợi không xứng đáng - Bongdaplus.vn

So, why is this address necessary?

In order for you to interact with a token, you need the address of the smart contract. You can think of it this way: if you want to send or receive anything to your account, you use your public address. Similarly, if you’re going to send or receive an ERC-20 token, you have to interact with its address!

Easiest Way to Get the Address of Any ERC-20 Token

With an overview of what a token address is, we’re ready to jump into the main tutorial. In this part, we’ll show you how to get the address of an ERC-20 token using two different tools:

  • Token explorers
  • Block explorers

So, without further ado, let’s look at how you can get an ERC-20 token’s address using a token explorer!

Use a Token Explorer to Get the Address of an ERC-20 Token

In this section, we’ll show you how to get the address of an ERC-20 token using the industry’s premier token explorer – Moralis Money!

Thanks to the accessibility of Moralis Money, you can effortlessly get any token address in three straightforward steps:

  • Step 1: Visit Moralis Money.
  • Step 2: Input a token name in the search bar at the top and click on it. Doing so takes you to the coin’s token page, and in this case, we’ll be using USDC as an example:
  • Step 3: Click on the copy button next to ”Coin Address”:

That’s it; when working with a token explorer like Moralis Money, it’s easy to get the address of an ERC-20 token!

What’s more, Moralis Money supports multiple blockchain networks. This means you can seamlessly find any ERC-20 token’s address across various chains, including Ethereum, Polygon, Arbitrum, and many others.

Use a Block Explorer to Get the Address of an ERC-20 Token

You can also use other tools like block explorers to easily get an ERC-20 token’s address. In this section, we’ll show you how it works using Etherscan!

  • Step 1: Visit Etherscan.
  • Step 2: Add the token name to the search bar at the top and click on it. In this case, we’ll also be using USDC as an example:
  • Step 3: Copy the address under the ”Other Info” section:

That’s it; it doesn’t have to be more challenging than that to get the address of an ERC-20 token when using a block explorer like Etherscan!

However, a significant drawback to using block explorers is that they are usually tied to one specific network. For instance, Etherscan is used for Ethereum, PolygonScan is used for Polygon, etc. This means you can’t find token addresses across chains like when working with Moralis Money!

Use Moralis to Get ERC-20 Token Addresses

In addition to using token and block explorers, you can also leverage Moralis and our various APIs to get ERC-20 token addresses in other ways that are more useful when building Web3 projects. To give you a few examples, in the following sections, we will cover three prominent endpoints that allow you to seamlessly get ERC-20 token addresses:

  • getWalletTokenBalances()
  • getTopERC20TokensByMarketCap()
  • getTopERC20TokensByPriceMovers()

If this sounds intriguing, join us below as we show you the getWalletTokenBalances() endpoint, allowing you to get the addresses of all ERC-20 tokens owned by a wallet in a heartbeat!

getWalletTokenBalances()

The easiest way to get the addresses of all ERC-20 tokens owned by a wallet is to leverage the Moralis Token API. With the Token API, you can effortlessly get the data you’re after with a single call to the getWalletTokenBalances() endpoint. Here’s an example of what it might look like:

Chuyên gia chia sẻ  Gõ Tiếng Việt

const response = await Moralis.EvmApi.token.getWalletTokenBalances({ “chain”: “0x1”, “address”: “0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326” });

All you have to do is configure the address and chain parameters to fit your query. In this instance, the address parameter is the wallet address from which you want to get the tokens. Once you call the getWalletTokenBalances() endpoint, you’ll get a response containing an array of all tokens and their addresses. It will look something like this:

[ { “token_address”: “0xefd6c64533602ac55ab64442307f6fe2c9307305”, “name”: “APE”, “symbol”: “APE”, “logo”: null, “thumbnail”: null, “decimals”: 18, “balance”: “101715701444169451516503179” }, { “token_address”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”, “name”: “Wrapped Ether”, “symbol”: “WETH”, “logo”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.webp”, “thumbnail”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.webp”, “decimals”: 18, “balance”: “85000000000000000” } ]

getTopERC20TokensByMarketCap()

Your best option for fetching the addresses of the top ERC-20 tokens by market cap is to use the Moralis Market Data API. With this tool, you can get the job done with a single API call to the getTopERC20TokensByMarketCap() endpoint. And this is what it looks like:

const response = await Moralis.EvmApi.marketData.getTopERC20TokensByMarketCap();

In return for calling the endpoint above, you’ll get a list of the top ERC-20 tokens by market cap, including each token’s address. Here’s an example of what the response might look like:

[ { “rank”: “1”, “token_name”: “Wrapped Ether”, “token_symbol”: “WETH”, “token_logo”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png”, “token_decimals”: “18”, “contract_address”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”, “price_usd”: “0.0285”, “price_24h_percent_change”: “0.0285”, “price_7d_percent_change”: “0.0285”, “market_cap_usd”: “0.0285” } //… ]

getTopERC20TokensByPriceMovers()

You can also leverage the Market Data API to get the addresses of the top ERC-20 tokens based on price change. All it requires is a single call to the getTopERC20TokensByPriceMovers() endpoint, and it looks something like this:

const response = await Moralis.EvmApi.marketData.getTopERC20TokensByPriceMovers();

Calling the getTopERC20TokensByPriceMovers() endpoint will give you the top gainers and losers based on price change. And each token in the response will have an address attached to it. Here’s an example of what the JSON response should look like:

{ “gainers”: [ { “rank”: 1, “token_name”: “Amber phantom Butterfly”, “token_symbol”: “APB”, “token_logo”: “https://market-data-images.s3.us-east-1.amazonaws.com/tokenImages/0x1650ec11bc9cfe2f3a4a18a9fcf5c2ad5e7979e8b5e92fae7cff5475d51f2bb4.png”, “token_decimals”: “18”, “contract_address”: “0x23da77203eead71e72d7326654c431ef563567a4”, “price_usd”: “0.00009505”, “price_24h_percent_change”: “1362.31”, “price_7d_percent_change”: “8.1”, “market_cap_usd”: “1072500” }, //… ], “losers”: [ { “rank”: 1, “token_name”: “ONSTON”, “token_symbol”: “ONSTON”, “token_logo”: “https://market-data-images.s3.us-east-1.amazonaws.com/tokenImages/0x52d802cacaf06b3f80e8c974091a44a171110ff7cf2a64db147209a9f92b2b02.png”, “token_decimals”: “18”, “contract_address”: “0x47b9f01b16e9c9cb99191dca68c9cc5bf6403957”, “price_usd”: “0.0004902”, “price_24h_percent_change”: “-78.89”, “price_7d_percent_change”: “-78.27”, “market_cap_usd”: “2320060” }, //… ] }

Full Tutorial: How to Get the Addresses of All ERC-20 Tokens in a Wallet

In this section, we’ll provide a complete tutorial on how to get the addresses of all ERC-20 tokens in a wallet using Moralis’ Token API. Thanks to the accessibility of this tool, we can effortlessly get the data we need in three straightforward steps:

  1. Get a Moralis API key
  2. Write a script calling the getWalletTokenBalances() endpoint
  3. Run the code

However, before we can jump into the first step of the tutorial, you need to take care of a few prerequisites!

Prerequisites

While the Token API supports multiple programming languages, we will be using JavaScript in this tutorial. So, before you can proceed, make sure you have the following ready:

  • Node v.14+
  • NPM/Yarn

Step 1: Get a Moralis API Key

A Moralis API key is one requirement for calling the Token API and the getWalletTokenBalances() endpoint. As such, if you don’t already have one, make sure to sign up with Moralis straight away by clicking on the ”Start for Free” button at the top right of the Moralis homepage:

With a Moralis account at hand, you can navigate to the ”Settings” tab, scroll down to the ”Secrets” section, and copy your API key:

Keep the key for now, as you’ll need it in the following step to initiate the Moralis SDK!

Step 2: Write a Script Calling the getWalletTokenBalances() Endpoint

Open your preferred integrated development environment (IDE) and set up a new project. From here, you need to install the Moralis SDK. As such, open a new terminal and run the following command in your project’s root folder:

Chuyên gia chia sẻ  Haizz là gì? Haizz là gì trên Facebook và trong tình yêu

npm install moralis @moralisweb3/common-evm-utils

Next, create a new ”index.js” file and add the following code:

const Moralis = require(“moralis”).default; const { EvmChain } = require(“@moralisweb3/common-evm-utils”); const runApp = async () => { await Moralis.start({ apiKey: “YOUR_API_KEY”, // …and any other configuration }); const address = “0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d”; const chain = EvmChain.ETHEREUM; const response = await Moralis.EvmApi.token.getWalletTokenBalances({ address, chain, }); console.log(response.toJSON()); }; runApp();

You then need to initiate the Moralis SDK by replacing YOUR_API_KEY with the key you copied in the first step:

From there, configure the address and chain parameters to fit your request. In this case, the address parameter is the wallet address from which you want to query the token addresses:

We then pass along the parameters when calling the getWalletTokenBalances() endpoint:

And that’s it; you’re now ready to run the code!

Step 3: Run the Code

For the final step, open a new terminal, cd into your project’s root folder, and run the following command to execute the script:

node index.js

In return, you’ll get an array of all tokens contained within the wallet in question. Here’s an example of what the response might look like:

[ { “token_address”: “0xefd6c64533602ac55ab64442307f6fe2c9307305”, “name”: “APE”, “symbol”: “APE”, “logo”: null, “thumbnail”: null, “decimals”: 18, “balance”: “101715701444169451516503179” }, { “token_address”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”, “name”: “Wrapped Ether”, “symbol”: “WETH”, “logo”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.webp”, “thumbnail”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.webp”, “decimals”: 18, “balance”: “85000000000000000” } ]

As you can see in the response above, each element or token has a token_address parameter. And with this data, you can easily get each ERC-20 token’s address!

Congratulations; you now know how to get the addresses of all ERC-20 tokens in a wallet using Moralis and the Token API!

Summary: How to Get the Address of an ERC-20 Token

In today’s article, we kicked things off by exploring the ins and outs of ERC-20 token addresses. In doing so, we learned that they are a unique sequence of numbers and letters used to identify and interact with ERC-20 tokens. Consequently, as you can imagine, they are fundamental to the Web3 ecosystem.

From there, we also showed you how to seamlessly get the address of an ERC-20 token using Moralis Money – an industry-leading token explorer. With this tool, you can effortlessly get the address of any ERC-20 token in three straightforward steps:

  • Step 1: Visit Moralis Money
  • Step 2: Go to the coin’s token page
  • Step 3: Copy the address

As such, if you have followed along this far, you now know how to get the address of an ERC-20 token using Moralis Money!

In addition to the method above, we showed you how to get the addresses of ERC-20 tokens using Moralis APIs. In doing so, we covered the following three endpoints:

  • getWalletTokenBalances()
  • getTopERC20TokensByMarketCap()
  • getTopERC20TokensByPriceMovers()

So, you now know how to get the addresses of ERC-20 tokens using the Moralis API!

If you liked this guide on how to get the address of an ERC-20 token, consider diving into more content here on the blog. For instance, explore the ERC-4337 standard or learn how to use the getLogs method.

Also, if you’re serious about building blockchain-based projects, make sure to explore some additional Moralis APIs. For instance, if you’re into NFT development, then you’ll definitely want to check out the NFT API. With this tool, you can get verified NFT collections, get all NFT tokens owned by a user address, and more!

If you want access to these industry-leading development tools, sign up with Moralis immediately!

Đánh giá bài viết post

Phạm Văn Sỹ

Tôi là Phạm Văn Sỹ chuyên gia uy tín trong lĩnh vực kinh tế và kinh doanh là sinh viên của trường Đại học Ngoại Thương. Với kiến thức sâu rộng sau 12 năm ở bên ngoài thương trường thị trường tôi mong muốn chia sẻ các kiến thức chuyên sâu hữu ích dành cho mọi người.

Related Articles

Check Also
Close
Back to top button