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

440 views Asked by At

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

2

There are 2 answers

0
Fionna On BEST ANSWER

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/[email protected]/dist/browser/algosdk.min.js"></script>

0
Mike Frankyvskyi On

Yes, you can use this: GET /v2/accounts/{address}

This will return an assets array, as well as some other things - you can read more here.