Skip to content

Instantly share code, notes, and snippets.

@oelmekki
Created April 11, 2018 12:03
Show Gist options
  • Save oelmekki/57a915e92f826c81980908dc3ff59361 to your computer and use it in GitHub Desktop.
Save oelmekki/57a915e92f826c81980908dc3ff59361 to your computer and use it in GitHub Desktop.
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