Instantly share code, notes, and snippets.

Embed
What would you like to do?
const publicAddress =
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
async function findAssets(address) {
const resp = await fetch(`https://horizon.stellar.org/accounts/${address}`);
const json = await resp.json();
return json['balances'].filter(balance => balance['asset_type'] != 'native');
}
async function printBalances(assets) {
let total = 0;
Promise.all(
assets.map(async asset => {
const resp = await fetch(
`https://horizon.stellar.org/trade_aggregations?counter_asset_type=native&base_asset_code=${
asset['asset_code']
}&base_asset_issuer=${asset['asset_issuer']}&base_asset_type=${
asset['asset_type']
}&limit=1&order=desc&resolution=300000`,
);
const json = await resp.json();
const tokenPrice = json['_embedded']['records'][0]['close'];
const price =
parseFloat(tokenPrice, 10) * parseFloat(asset['balance'], 10);
total += price;
console.log(`${asset['asset_code']}: ${price}XLM`);
}),
).then(() => {
console.log(`\nTOTAL: ${total}XLM`);
});
}
findAssets(publicAddress).then(printBalances);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment