From fc4b18833ff3e38dfc820ce814d53ef822037e1a Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 22 Nov 2023 11:12:31 +0200 Subject: [PATCH] chore: use monetary methods for usd conversion --- api/currency-utils.js | 30 ++++++++++++++++++++++-------- api/package-lock.json | 1 + api/package.json | 1 + api/tvl_dex.js | 8 ++++---- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/api/currency-utils.js b/api/currency-utils.js index 58e9f86f3..9edbd9f70 100644 --- a/api/currency-utils.js +++ b/api/currency-utils.js @@ -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', @@ -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 }; \ No newline at end of file +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 }; \ No newline at end of file diff --git a/api/package-lock.json b/api/package-lock.json index 124d156d0..12276b8ea 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -12,6 +12,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" }, "devDependencies": { diff --git a/api/package.json b/api/package.json index 03c5b5695..63f72574e 100644 --- a/api/package.json +++ b/api/package.json @@ -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" } } diff --git a/api/tvl_dex.js b/api/tvl_dex.js index 7f939e001..57fcabf15 100644 --- a/api/tvl_dex.js +++ b/api/tvl_dex.js @@ -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') { @@ -20,8 +20,8 @@ const tvlDex = async (request, response) => { const queryUrl = getCoingeckoQueryUrl("usd", Array.from(coingeckoIds)); // return format: { : { : }, ... } // 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) => { @@ -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 {