Skip to content

Commit

Permalink
chore: use monetary methods for usd conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
bvotteler committed Nov 22, 2023
1 parent 6746e9a commit fc4b188
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
30 changes: 22 additions & 8 deletions api/currency-utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {
MonetaryAmount,
ExchangeRate,
Bitcoin,
InterBtc, // on Polkadot
Interlay, // On Polkadot
KBtc, // on Kusama
Kintsugi, // On Kusama
Kusama, // on Kusama
Polkadot // on Polkadot
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',
Expand Down Expand Up @@ -40,4 +42,16 @@ const getCoingeckoQueryUrl = (vsId, coingeckoIds) => {
return `https://api.coingecko.com/api/v3/simple/price?vs_currencies=${vsId}&ids=${idsString}`;
};

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

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

export { getCoingeckoId, getCoingeckoQueryUrl, getUsdMonetaryAmount };
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.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@interlay/interbtc-api": "2.6.0-rc.0",
"@interlay/monetary-js": "0.7.3",
"@polkadot/util-crypto": "12.6.1",
"big.js": "6.1.1",
"pg": "^8.10.0"
}
}
8 changes: 4 additions & 4 deletions api/tvl_dex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createInterBtcApi } from "@interlay/interbtc-api";
import { getCoingeckoId, getCoingeckoQueryUrl } from "./currency-utils";
import { getCoingeckoId, getCoingeckoQueryUrl, getUsdMonetaryAmount } from "./currency-utils";

const tvlDex = async (request, response) => {
if (request.method === 'GET') {
Expand All @@ -20,8 +20,8 @@ const tvlDex = async (request, response) => {
const queryUrl = getCoingeckoQueryUrl("usd", Array.from(coingeckoIds));
// return format: { <conigeckoId> : { <vs_id>: <price_as_number> }, ... }
// eg. {"bitcoin":{"usd":36478},"interlay":{"usd":0.0240072},"voucher-dot":{"usd":6.12}}
const response = await fetch(queryUrl, { headers: { "accept": "application/json" } });
const cgData = await response.json();
const cgResponse = await fetch(queryUrl, { headers: { "accept": "application/json" } });
const cgData = await cgResponse.json();

const amounts = pools.flatMap((pool) => pool.pooledCurrencies)
.map((monetaryAmount) => {
Expand All @@ -33,7 +33,7 @@ const tvlDex = async (request, response) => {
? cgData[cgId]["usd"]
: undefined;

const monetaryAmountUsd = usdPrice ? monetaryAmount.mul(usdPrice) : undefined;
const monetaryAmountUsd = usdPrice ? getUsdMonetaryAmount(monetaryAmount) : undefined;
const amountUsd = monetaryAmountUsd?.toString(true);
const atomicAmountUsd = monetaryAmountUsd?.toString();
return {
Expand Down

0 comments on commit fc4b188

Please sign in to comment.