javascriptalgorand

How can I get all the assets in an address using algorand-js-sdk?


I'm new to blockchain dev and am using the JS SDK for Algorand. Is it possible to retrieve all the assets in a wallet by its address?

Thank you


Solution

  • You can use an endpoint from either algod (algo daemon of the node) or indexer (Postgres database) to get this information.

    Algod method:

    const address = "XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA";
    const accountInfo = await algodClient.accountInformation(address).do();
    

    https://algorand.github.io/js-algorand-sdk/classes/Algodv2.html#accountInformation

    Indexer method:

    const address = "XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA";
    const accountAssets = await indexerClient.lookupAccountAssets(address).do();
    

    https://algorand.github.io/js-algorand-sdk/classes/Indexer.html#lookupAccountAssets

    For demonstration purpose, I just randomly found an address from Algoexplorer.

    const token  = "";
    const port   = "";
    const algodServer = "https://mainnet-api.algonode.cloud";
    const indexerServer = "https://mainnet-idx.algonode.cloud";
    const algodClient = new algosdk.Algodv2(token, algodServer, port);
    const indexerClient = new algosdk.Indexer(token, indexerServer, port);
    
    (async () => {
      const address = "A4YW55ZEC2TTIN2TEVN56IRVGLWQYQRUNJTPG37OTWE4EJWNAH3EUKHZKA";
      const algodAccountAssets = await algodClient.accountInformation(address).do();
     
      const indexerAccountAssets = await indexerClient.lookupAccountAssets(address).do();
    
      console.log("algod results: ", algodAccountAssets)
      console.log("indexer results: ", indexerAccountAssets)
    })();
    <script src="https://unpkg.com/algosdk@v1.18.1/dist/browser/algosdk.min.js"></script>