Skip to content

Commit

Permalink
Feat: expose TVLs fetched via lib as part of api endpoints (#1605)
Browse files Browse the repository at this point in the history
* bump lib to v 2.6.0 and update imports

* return pooled MonetaryAmounts in json format for dex and lending

* align polkadot util-crypto version in dependencies
  • Loading branch information
bvotteler authored Dec 5, 2023
1 parent 7128cf6 commit c98b33e
Show file tree
Hide file tree
Showing 12 changed files with 2,534 additions and 673 deletions.
59 changes: 59 additions & 0 deletions api/currency-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
MonetaryAmount,
ExchangeRate,
Bitcoin,
InterBtc,
Interlay,
KBtc,
Kintsugi,
Kusama,
Polkadot
} from '@interlay/monetary-js';
import { isForeignAsset } from "@interlay/interbtc-api";
import Big from "big.js";

const COINGECKO_ID_BY_CURRENCY_TICKER = {
[Bitcoin.ticker]: 'bitcoin',
[Kintsugi.ticker]: 'kintsugi',
[KBtc.ticker]: 'bitcoin',
[Kusama.ticker]: 'kusama',
[Polkadot.ticker]: 'polkadot',
[Interlay.ticker]: 'interlay',
[InterBtc.ticker]: 'bitcoin'
};

const getCoingeckoId = (currency) => {
if (isForeignAsset(currency)) {
// Force V[DOT/KSM] prices.
switch (currency.ticker) {
case 'VDOT':
return 'voucher-dot';
case 'VKSM':
return 'voucher-ksm';
default:
return currency.foreignAsset.coingeckoId;
}
}
return COINGECKO_ID_BY_CURRENCY_TICKER[currency.ticker];
};

const getCoingeckoQueryUrl = (vsId, coingeckoIds) => {
const idsString = coingeckoIds.join(",");
return `https://api.coingecko.com/api/v3/simple/price?vs_currencies=${vsId}&ids=${idsString}`;
};

const getUsdMonetaryAmount = (monetaryAmount, usdPrice) => {
const usdCurrency = {
name: "US Dollar",
decimals: 2,
ticker: "USD",
humanDecimals: 2
};

const rate = new Big(usdPrice);

const xToUsd = new ExchangeRate(monetaryAmount.currency, usdCurrency, rate);
return xToUsd.toCounter(monetaryAmount);
}

export { getCoingeckoId, getCoingeckoQueryUrl, getUsdMonetaryAmount };
Loading

2 comments on commit c98b33e

@vercel
Copy link

@vercel vercel bot commented on c98b33e Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on c98b33e Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.