From 031673c13b0b74c2687b3ffab69b8ddd07861401 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:07:17 +0200 Subject: [PATCH] update aqua network #11386 --- projects/aqua-network/index.js | 65 ++++++++++++++-------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/projects/aqua-network/index.js b/projects/aqua-network/index.js index 948b43a8cb0..621f7abeac2 100644 --- a/projects/aqua-network/index.js +++ b/projects/aqua-network/index.js @@ -1,51 +1,38 @@ -const utils = require("../helper/utils"); -const { getApiTvl } = require("../helper/historicalApi"); - +const { get } = require('../helper/http') const AQUA_STATS_URL = "https://amm-api.aqua.network/api/external/v1/statistics/totals/?size=all" -function findClosestDate(items) { - const currentDate = new Date().getTime(); - - let closestItem = null; - let closestDiff = Infinity; - - for (let item of items) { - const itemDate = new Date(item.date).getTime(); - const diff = Math.abs(currentDate - itemDate); - - if (diff < closestDiff) { - closestItem = item; - closestDiff = diff; - } - } - - return closestItem; +let _data + +async function getData() { + if (!_data) + _data = get(AQUA_STATS_URL) + const data = await _data + const res = {} + data.forEach((item) => { + res[item.date] = item.tvl / 1e8 + }) + return res } -async function current() { - var aquaHistoricalData = ( - await utils.fetchURL(AQUA_STATS_URL) - ).data; - - const currentItem = findClosestDate(aquaHistoricalData); - - return parseFloat(currentItem.tvl) / 10e7; +function formatUnixTimestamp(unixTimestamp) { + const date = new Date(unixTimestamp * 1000); // Convert Unix timestamp to milliseconds + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based + const day = String(date.getDate()).padStart(2, '0'); + return `${year}-${month}-${day}`; } -function tvl(time) { - return getApiTvl(time, current, async () => { - var aquaHistoricalData = ( - await utils.fetchURL(AQUA_STATS_URL) - ).data; - - return aquaHistoricalData.map((item) => ({ - date: new Date(item.date), - totalLiquidityUSD: parseFloat(item.tvl) / 10e7, - })); - }); +async function tvl(api) { + const key = formatUnixTimestamp(api.timestamp) + const allData = await getData() + const usdValue = allData[key] + if (!usdValue) + throw new Error('No data found for current date'); + api.addCGToken('tether', usdValue) } module.exports = { + start: 1719792000, misrepresentedTokens: true, methodology: 'counts the liquidity of the Pools on AMM, data is pulled from the Aquarius API: "https://amm-api.aqua.network/api/external/v1/statistics/totals/".',