Skip to content

Commit

Permalink
chore: add monetary dependency, add usd conversions for dex tvl
Browse files Browse the repository at this point in the history
  • Loading branch information
bvotteler committed Nov 22, 2023
1 parent c6c240d commit 2f4f25e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
13 changes: 10 additions & 3 deletions api/currency-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
Kintsugi, // On Kusama
Kusama, // on Kusama
Polkadot // on Polkadot
} from '@interlay/monetary-js';

} from '@interlay/monetary-js';
import { isForeignAsset } from "@interlay/interbtc-api";


const COINGECKO_ID_BY_CURRENCY_TICKER = {
[Bitcoin.ticker]: 'bitcoin',
[Kintsugi.ticker]: 'kintsugi',
Expand All @@ -33,4 +35,9 @@ const getCoingeckoId = (currency) => {
return COINGECKO_ID_BY_CURRENCY_TICKER[currency.ticker];
};

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

export { getCoingeckoId, getCoingeckoQueryUrl };
1 change: 1 addition & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 33 additions & 7 deletions api/tvl_dex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createInterBtcApi, isForeignAsset } from "@interlay/interbtc-api";
import { getCoingeckoId } from "./currency-utils";
import { getCoingeckoId, getCoingeckoQueryUrl } from "./currency-utils";

const tvlDex = async (request, response) => {
if (request.method === 'GET') {
Expand All @@ -11,13 +11,39 @@ const tvlDex = async (request, response) => {
);

const pools = await interbtcApi.amm.getLiquidityPools();
// dedupe ids
const coingeckoIds = new Set(
pools.flatMap((pool) => pool.pooledCurrencies)
.map((monetaryAmount) => getCoingeckoId(monetaryAmount))
);
// base: usd, get price for all coingeckoIds
const queryUrl = getCoingeckoQueryUrl("usd", Array.from(coingeckoIds));
// return format: [ { <conigeckoId> : { <vs_id>: <price_as_number> } }, ... ]
const response = await fetch(queryUrl, { headers: { "accept": "application/json" } });
const cgData = await response.json();

const amounts = pools.flatMap((pool) => pool.pooledCurrencies)
.map((monetaryAmount) => ({
currency: monetaryAmount.currency,
coingeckoId: getCoingeckoId(monetaryAmount.currency),
atomicAmount: monetaryAmount.toString(true),
amount: monetaryAmount.toHuman()
}));
.map((monetaryAmount) => {
const atomicAmount = monetaryAmount.toString(true);
const amount = monetaryAmount.toString();

const cgId = getCoingeckoId(monetaryAmount.currency);
const usdPrice = (cgData[cgId] != undefined && cgData[cgId]["usd"] != undefined)
? cgData[cgId]["usd"]
: undefined;

const monetaryAmountUsd = usdPrice ? monetaryAmount.mul(usdPrice) : undefined;
const amountUsd = monetaryAmountUsd?.toString(true);
const atomicAmountUsd = monetaryAmountUsd?.toString();
return {
currency: monetaryAmount.currency,
coingeckoId: getCoingeckoId(monetaryAmount.currency),
atomicAmount,
amount,
atomicAmountUsd,
amountUsd
}
});

return response.status(200)
.setHeader("content-type", "application/json")
Expand Down

0 comments on commit 2f4f25e

Please sign in to comment.