1

I want to get a list with the Elrond's esdt tokens (and balances) from an address (a wallet). I don't have any example, I tried things such as:

  const { address, account } = useGetAccountInfo();
  const objAddress = new Address(address);
  // const data1 = getAccount(address);
  const { network } = useGetNetworkConfig();
  const proxy = new ProxyProvider(network.apiAddress);

  proxy
    .getAddressEsdtList(objAddress)
    .then(({ returnData }) => {
      console.log(returnData);
    })
    .catch((err) => {
      console.error('Unable to call VM query', err);
    });

But in the console I get "undefined".

Thanks a lot!

Sergi-O
  • 51
  • 6

1 Answers1

1

In the latest erdjs version I don't have any getAddressEsdtList function for the provider; what could work is to extend the network providers.

So we know we can extend the available network providers with other functions and we now need to make a request with an address and receive all the tokens that the address helds. api.elrond.com has an endpoint that does this at accounts/{address}/tokens.
As we have an endpoint to make a request to, we can now extend the ApiNetworkProvider, making use of doGetGeneric.

// CustomNetworkProvider.js
import { ApiNetworkProvider } from "@elrondnetwork/erdjs-network-providers";

export class CustomNetworkProvider extends ApiNetworkProvider {
    async getTokens(address) {
        return await this.doGetGeneric(`accounts/${address}/tokens`);
    }
}
// index.js
import {CustomNetworkProvider} from "./CustomNetworkProvider.js";

const getProvider = () => {
    return new CustomNetworkProvider('https://api.elrond.com', { timeout: 5000 });
}

const provider = getProvider();
const address = 'erd1rf4hv70arudgzus0ymnnsnc4pml0jkywg2xjvzslg0mz4nn2tg7q7k0t6p';
const tokens = await provider.getTokens(address);

console.log(tokens);
Andrei
  • 191
  • 5
  • It works with your address, but not with mine!! Any idea? Thanks a lot!! – Sergi-O May 13 '22 at 08:09
  • This is because in the address you posted there are [no tokens](https://explorer.elrond.com/accounts/erd1wumnmqkg0xx48xeyqh4hkm29vldtgdk047cum2vz0vqyx3mmmq2qeh6e2h/tokens). This api returns only the ESDT tokens- (like RIDE, ITHEUM, MEX). You can see the account info with the EGLD balance using [this endpoint](https://api.elrond.com/accounts/erd1wumnmqkg0xx48xeyqh4hkm29vldtgdk047cum2vz0vqyx3mmmq2qeh6e2h). At the moment the balance is `28000000000000000`, you divide it by `10^18` and you get `0.028` EGLD. – Andrei May 13 '22 at 09:52
  • OMG!! You gave me the API in the main network. Thanks a lot because I learn a big thing!! I'm trying to learn using devnet: const getProvider = () => { return new CustomNetworkProvider('https://devnet-api.elrond.com', { timeout: 5000 }); – Sergi-O May 13 '22 at 10:25