4

On the DApp I want to get list of ERC20 tokens, with their balances. There's moralis API but it requires to sign a text message: "you authorise to use our api blah blah" that breaks workflow this gnosis-safe + walletConnect, because gnosis-safe tries to sign text message via multisig, that 1) requires money 2) doesn't work. What's the best workaround way to solve it?

3 Answers 3

3
+25

I once used Covalent hq to develop a portfolio and show the users all their tokens with balances and details.

It's easy to use and they have a lot of endpoints that you can try.

You just need to get an API key, which you can do when you register with them. They provide you with thousands of calls a day for free.

Maybe the endpoint that you are interested in is the following:

https://api.covalenthq.com/v1/1/address/<userAddress>/balances_v2/

Where you replace the '' with the address of the user you want to see all the tokens from, with the balance and type of each. For example:

https://api.covalenthq.com/v1/1/address/0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405/balances_v2/

Returns:

{
    "data": {
        "address": "0x6827b8f6cc60497d9bf5210d602c0ecafdf7c405",
        "updated_at": "2022-09-07T14:33:17.182020877Z",
        "next_update_at": "2022-09-07T14:38:17.182020957Z",
        "quote_currency": "USD",
        "chain_id": 1,
        "items": [
            {
                "contract_decimals": 18,
                "contract_name": "SHIBA INU",
                "contract_ticker_symbol": "SHIB",
                "contract_address": "0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce",
                "supports_erc": [
                    "erc20"
                ],
                "logo_url": "https://logos.covalenthq.com/tokens/1/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce.png",
                "last_transferred_at": "2022-08-14T03:39:39Z",
                "native_token": false,
                "type": "cryptocurrency",
                "balance": "2442436679081238537555081",
                "balance_24h": "2442436679081238537555081",
                "quote_rate": 1.2092496E-5,
                "quote_rate_24h": 1.2042391E-5,
                "quote": 29.535156,
                "quote_24h": 29.412777,
                "nft_data": null
            },
            {
                "contract_decimals": 18,
                "contract_name": "Ether",
                "contract_ticker_symbol": "ETH",
                "contract_address": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
                "supports_erc": null,
                "logo_url": "https://www.covalenthq.com/static/images/icons/display-icons/ethereum-eth-logo.png",
                "last_transferred_at": null,
                "native_token": true,
                "type": "cryptocurrency",
                "balance": "4810052077214178",
                "balance_24h": "4810052077214178",
                "quote_rate": 1536.6252,
                "quote_rate_24h": 1537.8783,
                "quote": 7.3912473,
                "quote_24h": 7.3972745,
                "nft_data": null
            },
            {
                "contract_decimals": 18,
                "contract_name": "Aave Token",
                "contract_ticker_symbol": "AAVE",
                "contract_address": "0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9",
                "supports_erc": [
                    "erc20"
                ],
                "logo_url": "https://logos.covalenthq.com/tokens/1/0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9.png",
                "last_transferred_at": "2022-08-14T04:22:33Z",
                "native_token": false,
                "type": "cryptocurrency",
                "balance": "87733412469464885",
                "balance_24h": "87733412469464885",
                "quote_rate": 83.5964,
                "quote_rate_24h": 83.81563,
                "quote": 7.334197,
                "quote_24h": 7.353431,
                "nft_data": null
            },
            {
                "contract_decimals": 18,
                "contract_name": "Uniswap",
                "contract_ticker_symbol": "UNI",
                "contract_address": "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984",
                "supports_erc": [
                    "erc20"
                ],
                "logo_url": "https://logos.covalenthq.com/tokens/1/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.png",
                "last_transferred_at": "2022-08-14T04:22:33Z",
                "native_token": false,
                "type": "cryptocurrency",
                "balance": "342764968664953381",
                "balance_24h": "342764968664953381",
                "quote_rate": 5.9344907,
                "quote_rate_24h": 5.893218,
                "quote": 2.0341356,
                "quote_24h": 2.0199888,
                "nft_data": null
            }
        ],
        "pagination": null
    },
    "error": false,
    "error_message": null,
    "error_code": null
}

You can use Postman to try out several endpoints.

When you setup an environment in postman to easily use variables like {{url}}, {{chain_id}}, {{address}}, etc., then you can use it like the following. The following endpoint gets the total history of the portfolio of an address:

{{url}}/v1/{{chain_id}}/address/{{address}}/portfolio_v2/

To get the interaction of an address with a specific contract or token contract:

{{url}}/v1/{{chain_id}}/address/{{address}}/transfers_v2/?contract-address={{contract-address}}

Get the events related to an address within a range of blocks:

{{url}}/v1/{{chain_id}}/events/address/{{address}}/?starting-block=15490875&ending-block=latest

Get all transactions an address has made and received:

{{url}}/v1/{{chain_id}}/address/{{address}}/transactions_v2/

And much more. You can change the chain_id to point to Ethereum mainnet, which has chain_id 1, Binance Smart Chain in mainnet which has chain_id 56, and more.

Check the API docs: https://www.covalenthq.com/docs/api/#/0/0/USD/1

To call the Covalent API using your API Key from Postman, remember to use your API key as the 'Username' and no password:

enter image description here

3

We published an article exactly about this a few weeks ago in the Chaintack blog. It explains how to query the balance of all ERC20 tokens from multiple blockchains, at any given date.

In summary, you need access to an archive node to query past dates (full nodes usually have only the latest 128 blocks), a list of ERC20 token contracts which you can download from a repo, and use the getBalance passing the blockTag flag.

For current dates, you can use a full node and you'd not need to use the blockTag. Check the article and feel free to reach out if you need some help.

2

The only way to fetch erc20 tokens an user is having is to search the whole blockchain, which is alot of data.

However, recently I made a similar project and I found this article very helpful.

You can use Alchemy's token API to fetch the tokens an address is holding.

How to get all tokens owned by an address

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.