Created
April 11, 2018 12:03
-
-
Save oelmekki/57a915e92f826c81980908dc3ff59361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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