Bitcoin Forum

Alternate cryptocurrencies => Altcoin Discussion => Topic started by: themo7sn on June 18, 2023, 03:42:16 PM



Title: Newbie Question about crypto wallet development
Post by: themo7sn on June 18, 2023, 03:42:16 PM
I'm a senior developer but newbie in web3 development

I'm looking to build a crypto wallet functionalities into my NodeJS server.

Would like to understand some point related to the tokens

How to get the list of tokens for a coin address with their balances

for example

that address : https://bscscan.com/address/0xe934d2c5be5db0295a4de3177762a9e8c74ae4f4

it has

21311 Cake
0.002 NGM

It looks there's no direct way to do that in Web3 or EthersJS
And I don't prefer to use external service like Moralis or BSCScan.com, to save costs and support more chains without limits


Title: Re: Newbie Question about crypto wallet development
Post by: Stalker22 on June 18, 2023, 04:56:55 PM
This is about Altcoins. I recommend moving your discussion to the Altcoin Discussion (https://bitcointalk.org/index.php?board=67.0) section, as you are more likely to receive a better response there.


Title: Re: Newbie Question about crypto wallet development
Post by: vv181 on June 19, 2023, 01:14:40 PM
It looks there's no direct way to do that in Web3 or EthersJS
And I don't prefer to use external service like Moralis or BSCScan.com, to save costs and support more chains without limits

Due to how the ERC-20 token standard behaves it does indeed there is no direct way. If we also look at a popular wallet like Metamask, they only keep a handful of listed token contracts, if the token is not listed by them, the user has to add them manually.

Furthermore, you also might find this thorough explanations relevant, How to get all tokens by wallet address:

ERC-20 (and ERC-20-like such as TRC-20, BEP-20, etc.) token balance of each address is stored in the contract of the token.

Blockchain explorers scan each transaction for Transfer() events and if the emitter is a token contract, they update the token balances in their separate DB. The balance of all tokens per each address (from this separate DB) is then displayed as the token balance on the address detail page.

Etherscan and BSCScan currently don't provide an API that would return the token balances per address.

In order to get all ERC-20 token balances of an address, the easiest solution (apart from finding an API that returns the data) is to loop through all token contracts (or just the tokens that you're interested in), and call their balanceOf(address) function.

So as the Stack Overflow answer above, the likely implementation you might want to use is to loop the contracts. Besides, I recommend you take a reference on how MetaMask implements the functionalities.