multiversx

Getting list with the MultiversX's ESDT tokens (and balances) from an address (a wallet)


I want to get a list with the MultiversX'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".


Solution

  • 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);