Skip to content

Instantly share code, notes, and snippets.

@denalimarsh
Created November 2, 2020 17:10
Show Gist options
  • Save denalimarsh/8236e1f086e751d439148a69673bb2a1 to your computer and use it in GitHub Desktop.
Save denalimarsh/8236e1f086e751d439148a69673bb2a1 to your computer and use it in GitHub Desktop.
const KAVA_DENOM = "ukava";
const HARD_DENOM = "hard";
const USDX_DENOM = "usdx";
const BNB_DENOM = "bnb";
var usdFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
var getPrices = async () => {
var prices = [];
const usdxPrice = {name: USDX_DENOM, price: 1};
prices.push(usdxPrice);
const hardPrice = {name: HARD_DENOM, price: 0};
prices.push(hardPrice);
const kavaMarketResponse = await fetch("https://api.binance.com/api/v3/ticker/24hr?symbol=KAVAUSDT");
const kavaMarketData = await kavaMarketResponse.json();
const kavaPrice = {name: KAVA_DENOM, price: Number(kavaMarketData.lastPrice)};
prices.push(kavaPrice);
const bnbMarketResponse = await fetch("https://api.binance.com/api/v3/ticker/24hr?symbol=BNBUSDT");
const bnbMarketData = await bnbMarketResponse.json();
const bnbPrice = {name: BNB_DENOM, price: Number(bnbMarketData.lastPrice)};
prices.push(bnbPrice);
return prices;
}
var getTotalValues = async (prices) => {
var conversionMap = new Map();
conversionMap.set(USDX_DENOM, 10 ** 6);
conversionMap.set(KAVA_DENOM, 10 ** 6);
conversionMap.set(HARD_DENOM, 10 ** 6);
conversionMap.set(BNB_DENOM, 10 ** 8);
const response = await fetch("https://kava4.data.kava.io/harvest/accounts");
const data = await response.json();
const results = data && data.result;
var totalValues = [];
if(results && results.length > 0) {
const harvestAccAddress = "kava16zr7aqvk473073s6a5jgaxus6hx2vn5laum9s3"
const harvestAcc = results.find((item) => item.value.address === harvestAccAddress);
const coins = harvestAcc.value.coins;
for(coin of coins) {
const supply = Number(coin.amount)/conversionMap.get(coin.denom);
const price = prices.find((item) => item.name === coin.denom).price;
const value = supply * Number(price);
totalValues.push({denom: coin.denom, total_value: value});
}
}
return totalValues
}
var main = async () => {
const prices = await getPrices();
const totalValues = await getTotalValues(prices);
for(asset of totalValues) {
console.log(asset.denom, ":", usdFormatter.format(asset.total_value))
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment