Fun

How to get all ERC-20 tokens owned by an address

News Feed - 2024-03-13 11:03:37

Dilip Kumar Patairya9 hours agoHow to get all ERC-20 tokens owned by an addressWallet APIs enable users to quickly locate ERC-20 tokens by wallet address. Chainbase, Alchemy and Moralis provide APIs to get token-related information.531 Total views1 Total sharesListen to article 0:00How toOwn this piece of crypto historyCollect this article as NFTJoin us on social networksFinding ERC-20 token balances in any given wallet is simple; when users open their wallets, they automatically display the balances of all the imported tokens. However, when an individual or an entity, such as law enforcement or a business, needs to ascertain the ERC-20 token balances of another address, direct access to the wallet is not possible.


In such cases, an application programming interface (API) can be used to check the balances in a wallet. An API would return all ERC-20 token balances owned by an address and could automate the process of checking token balances.


This article discusses how one can use the APIs of Chainbase, Alchemy and Moralis to get information about ERC-20 tokens owned by an address.Step-by-step guide to using Chainbase to find all ERC-20 tokens owned by an address


Chainbase — a Web3 blockchain interaction layer infrastructure — offers API services to help developers quickly access and utilize blockchain networks. Its APIs facilitate on-chain data queries.


To work with Chainbase, a user needs an account on the platform with an API key, an integrated development environment and a wallet address as input.Step 1: Create a free account and log in


Register a free account on Chainbase and get access to different APIs and its data cloud. Then log into Chainbase and visit the dashboard. Initiate a new project within the console and obtain an API key.Step 2: Use Chainbase API to write script


The next is to make an API call to Chainbase to retrieve ERC-20 token balances. The examples below use Javascript. The subsequent action entails executing a HTTP request with JavaScript, utilizing the Axios library to obtain ERC-20 token balances from Chainbase.


Use “fetch” to make a HTTP request to retrieve data from a server, as shown in the code snippet below:


The code in the above example queries Chainbase to fetch the first five ERC-20 token balances of a wallet (using Vitalik Buterin’s address as an example) on the Ethereum mainnet, displaying the results or any errors encountered.


While the native Fetch API is a common way to perform HTTP requests with JavaScript, in the example code, “fetching” is conceptually done by Axios, not by the native Fetch API. To use Axios, the user must install it using “npm install axios --save” in the terminal.


The above code uses Axios in JavaScript to query Chainbase for the first five ERC-20 token balances of Vitalik Buterin’s wallet on the Ethereum mainnet.Step 3: Print the token balances


The getAccountTokens API of Chainbase uses the chain ID and wallet address as parameters and returns all ERC-20 token balances the wallet owns. Users can also find information about a specific token by populating the “contract_address” field. To get data printed, the user must run the command “node .js” in the terminal.


The above code snippet displays balances and information for several ERC-20 tokens, including Eterium, Uniswap V2, dYdX and others, from a given wallet.Step-by-step guide to using Alchemy to find all ERC-20 tokens owned by an address


Alchemy offers APIs that work as a platform layer, reading or writing to the blockchain and delivering the precise information developers want. The following is the procedure to get token balances via Alchemy:Step 1: Install Node and the NPM


Install Node and the Node Package Manager (NPM) on the local machine. Node is a cross-platform, open-source JavaScript runtime environment, while the NPM provides access to a vast ecosystem of libraries and tools.Step 2: Sign up for an Alchemy account and create an app


On the Alchemy website, sign up for a free account. Then, sign in and navigate to the “Alchemy Dashboard” to create a new app. Set the blockchain to “Ethereum” and the network to “Mainnet.” Click on the app’s “View Key” button on the dashboard and note the HTTP URL and API key.


The URL will be in this form: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxxStep 3: Create a Node project


The next step involves initializing an empty repository and installing the required Node.js dependencies. For API interactions, particularly with the token API, users need to use the Alchemy software development kit (SDK), though Axios or Fetch could serve as suitable alternatives.


Axios is a Javascript library used to make HTTP requests from nodes. Fetch API is a built-in module in Node that facilitates making API requests and handling responses on the server. The Alchemy SDK is a program function library that supports access to the underlying data model and technologies.


The code is under-given:


The code will create a repository “eth-balance,” which holds all the files and dependencies, and the code will be written in the “main.js” file.Step 4: Get the token balances of an address


To retrieve token balances, users may utilize the getTokenBalances method, which requires a single argument: the wallet address for which the token balances are desired. Then, a user needs to add the following code to the main.js file.


Use the following command to run the script:


The output will be displayed as follows:


The above code displays the token balances for the wallet at address 0xd8da6bf26964af9d7eed9e03e53415d37aa96045, listing each token by its contract address along with the respective balance in hexadecimal format.


However, the output of the previous step — which lists token contract addresses and balances in the smallest units — is not easy to understand. To obtain detailed token information like name, symbol and decimal count, users should use the getTokenMetadata method. This function — requiring the contract address as input — delivers data in a structured format.


Moreover, users can refine their script to integrate the getTokenBalances with the getTokenMetadata method. For instance, they can eliminate zero-balance tokens and convert token balances into human-readable forms.Step-by-step guide to using Moralis to find all ERC-20 tokens owned by an address


Moralis has enterprise-grade Web3 APIs, facilitating the integration of Web3 in any tech stack. The following is the process for using Moralis to find all ERC-20 tokens owned by an address:Step 1: Set up Moralis


Install Node.js v14+ and NPM. Unpack the binary to the installation folder and set the system environment variable for Node. Command npm-install sets up an NPM package and any other packages that it might depend on.


Create a free Moralis account, log in to the Moralis dashboard and get the API key. Users can find it in Settings > Secrets. Here, users should locate the “Web3 API Key - Default” or a similarly named key and copy its value for use in their projects.Step 2: Find all ERC-20 tokens owned by an address


Moralis provides a “getWalletTokenBalances” endpoint to find all ERC-20 tokens owned by an address. It requires two parameters: address and chain. In this context, address represents the specific wallet address being queried for token balances. Chain indicates the particular blockchain network, such as Ethereum, on which those tokens are held.


Here is the Javascript code to use:


The above code initializes the Moralis SDK, sets it up with a provided API key, and then queries for all ERC-20 token balances associated with a specified Ethereum wallet address, printing the results in a JSON format to the console.Step 3: Run the script


Now, users can run the script and find the number of tokens. In JavaScript, users need to enter the following command:


The terminal will display the below JSON response:


The JSON snippet above details the holdings of ERC-20 tokens in a specific blockchain wallet featuring two tokens: ApeCoin (APE) and Wrapped Ether (WETH). It includes essential information like the contract addresses, names, symbols, logo URLs (when available), decimal precision, and the balances of the tokens within the wallet, expressed in the smallest denomination of each token.# Blockchain# Wallet# Transactions

News Feed

California Governor Newsom Signs Executive Order on Crypto, Blockchain, and Web3
California Governor Newsom Signs Executive Order on Crypto, Blockchain, and Web3 California Governor Gavin Newsom has signed an executive order on crypto and blockchain to create a
Biggest Movers: LUNC 13% Higher, as US CPI Boosts Crypto Markets
Biggest Movers: LUNC 13% Higher, as US CPI Boosts Crypto Markets Terra classic has been one of Thursday’s biggest gainers, as crypto markets found light in the form of the latest
President Xi Says China Should ‘Seize Opportunity’ to Adopt Blockchain
Xi Jinping, President of the People’s Republic of China and General Secretary of the Communist Party of China, said the country needs to “seize the opportunity” afforded by blockchain technology.
Bitgo Plans to Launch Wrapped Bitcoin on the Tron Blockchain
Bitgo Plans to Launch Wrapped Bitcoin on the Tron BlockchainSoon there will be another network that allows people to tokenize BTC in order to leverage the underlying blockchain&rsqu
Crypto Exchange Bybit Partners With Innovation Growth Hub to Launch Blockchain Education Program
Crypto Exchange Bybit Partners With Innovation Growth Hub to Launch Blockchain Education Program Bybit has said it is inviting African youths that wish to attend a blockchain educa
Zimbabwe Central Bank: Gold Coin Sales to Commence in Late July
Zimbabwe Central Bank: Gold Coin Sales to Commence in Late July Starting on July 25, the Zimbabwean public will be able to buy and hold gold coins which weigh one troy ounce each.
Helen Partz9 hours agoFTX hacker moves $120M amid Sam Bankman-Fried trial: ReportBlockchain analytics firm Elliptic didn’t rule out the possibility of the $477 million hack being an FTX inside job.3240 Total views14 To
USDC to embrace tap-and-go payments after Apple opens up NFC
Tom Mitchelhill5 hours agoUSDC to embrace tap-and-go payments after Apple opens up NFCUnited States stablecoin giant Circle says tap to pay functionally for USDC will be coming “soon” to iPhones.2212 Total views9 Tot
Here’s how much Bitcoin you could buy with all the fiat currency in the world
Tristan Greene6 hours agoHere’s how much Bitcoin you could buy with all the fiat currency in the world5,000 BTC used to get you a pizza — now it’s worth more than some countries.9468 Total views2 Total sharesListen
Thailand Issues New Regulations on Custodied Cryptocurrencies
Thailand Issues New Regulations on Custodied Cryptocurrencies The Securities and Exchange Commission (SEC) of Thailand has issued new regulations on custodied cryptocurrencies. Com
Blockfi Launches Crypto Trading Platform
Blockfi Launches Crypto Trading Platform Crypto financial services company Blockfi has launched a trading platform supporting three cryptocurrencies. The new offering adds to the
Antonio Juliano steps down as CEO of dYdX
Ana Paula Pereira8 hours agoAntonio Juliano steps down as CEO of dYdXThe decentralized exchange is now led by Ivo Crnkovic-Rubsamen, a former trader and dYdX chief strategy officer.1797 Total views10 Total sharesListen t