diff --git a/api/tvl_loans.js b/api/tvl_loans.js new file mode 100644 index 000000000..1b29f5089 --- /dev/null +++ b/api/tvl_loans.js @@ -0,0 +1,59 @@ +import { createInterBtcApi } from "@interlay/interbtc-api"; +import { getCoingeckoId, getCoingeckoQueryUrl, getUsdMonetaryAmount } from "./currency-utils"; + +const tvlLoans = async (request, response) => { + if (request.method === 'GET') { + const interbtcApi = await createInterBtcApi( + process.env.REACT_APP_PARACHAIN_URL, + process.env.REACT_APP_BITCOIN_NETWORK, + undefined, + 3 + ); + + const loanAssets = await interbtcApi.loans.getLoanAssets(); + const liquidityAmounts = Object.values(loanAssets).map((loanAsset) => loanAsset.totalLiquidity); + + // dedupe ids + const coingeckoIds = new Set( + liquidityAmounts.map((monetaryAmount) => monetaryAmount.currency) + ); + // base: usd, get price for all coingeckoIds + const queryUrl = getCoingeckoQueryUrl("usd", Array.from(coingeckoIds)); + // return format: { : { : }, ... } + // eg. {"bitcoin":{"usd":36478},"interlay":{"usd":0.0240072}} + const cgResponse = await fetch(queryUrl, { headers: { "accept": "application/json" } }); + const cgData = await cgResponse.json(); + + const amounts = liquidityAmounts.map((monetaryAmount) => { + const atomicAmount = monetaryAmount.toString(true); + const amount = monetaryAmount.toString(); + + const cgId = getCoingeckoId(monetaryAmount.currency); + const usdPrice = (cgId != undefined && cgData[cgId] != undefined && cgData[cgId]["usd"] != undefined) + ? cgData[cgId]["usd"] + : undefined; + + const monetaryAmountUsd = usdPrice ? getUsdMonetaryAmount(monetaryAmount, usdPrice) : undefined; + const amountUsd = monetaryAmountUsd?.toString(); + return { + currency: monetaryAmount.currency, + coingeckoId: cgId, + atomicAmount, + amount, + amountUsd + } + }); + + return response.status(200) + .setHeader("content-type", "application/json") + .setHeader("cache-control", "public, maxage=0, s-maxage=300") + .json(amounts); + } else { + return response.status(400).send('Bad Request'); + } +} + +export default async function (request, response) { + return tvlLoans(request, response); +} + \ No newline at end of file diff --git a/vercel.json b/vercel.json index 3c9d49960..38fc91576 100644 --- a/vercel.json +++ b/vercel.json @@ -15,6 +15,10 @@ "api/tvl_dex.js": { "memory": 256, "maxDuration": 30 + }, + "api/tvl_loans.js": { + "memory": 256, + "maxDuration": 30 } }, "rewrites": [ @@ -41,6 +45,10 @@ { "source": "/tvl_dex", "destination": "/api/tvl_dex.js" + }, + { + "source": "/tvl_loans", + "destination": "/api/tvl_loans.js" } ], "headers": [