From a3f11f17ae37eec8dfaac440be2f5117a410bbef Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Sun, 4 Feb 2024 23:06:53 +0100 Subject: [PATCH] replace axios with wrapped method --- adapters/utils/runAdapter.ts | 12 +++- aggregators/dexible/index.ts | 16 ++--- dexs/apollox/index.ts | 10 +-- dexs/aux-exchange/index.ts | 8 +-- dexs/bluefin/index.ts | 7 +- dexs/concordex-io/index.ts | 4 +- dexs/defi-kingdoms/index.ts | 6 +- dexs/defibox/index.ts | 5 +- dexs/dexalot/index.ts | 10 +-- dexs/drift-protocol/index.ts | 6 +- dexs/dx25/index.ts | 24 ++----- dexs/fwx/index.ts | 20 +++--- dexs/hashflow/index.ts | 6 +- dexs/hyperliquid/index.ts | 4 +- dexs/minswap/index.ts | 6 +- dexs/moon-swap/index.ts | 4 +- dexs/muesliswap/index.ts | 4 +- dexs/mux-protocol/index.ts | 4 +- dexs/native/index.ts | 10 +-- dexs/noah-swap/index.ts | 5 +- dexs/orca/index.ts | 6 +- dexs/orderly-network-derivatives/index.ts | 10 +-- dexs/orderly-network/index.ts | 10 +-- dexs/oswap/index.ts | 7 +- dexs/paint-swap/index.ts | 1 - dexs/paycash/index.ts | 4 +- dexs/phoenix/index.ts | 6 +- dexs/ref-finance/index.ts | 10 +-- dexs/saber/index.ts | 6 +- dexs/saucerswap/index.ts | 6 +- dexs/sundaeswap/index.ts | 1 - dexs/surfone/index.ts | 6 +- dexs/synfutures/index.ts | 6 +- dexs/thorswap/index.ts | 4 +- dexs/thorwallet/index.ts | 8 +-- dexs/tigris/index.ts | 81 +++++++++------------ dexs/unicly/index.ts | 6 +- dexs/vanswap/index.ts | 9 +-- dexs/veax/index.ts | 30 +++----- dexs/vertex-protocol/index.ts | 18 ++--- dexs/wingriders/index.ts | 10 +-- dexs/zeta/index.ts | 6 +- fees/aimbot.ts | 15 +--- fees/allbridge-classic.ts | 2 +- fees/allbridge-core.ts | 7 +- fees/apollox/index.ts | 10 +-- fees/avalanche.ts | 26 +------ fees/cardano.ts | 5 +- fees/chimpexchange.ts | 1 - fees/firebird-finance.ts | 10 ++- fees/frax-fpi.ts | 4 +- fees/fwx/index.ts | 14 ++-- fees/ghostmarket/index.ts | 1 - fees/goku-money/index.ts | 15 ++-- fees/justlend.ts | 6 +- fees/liquid-bolt.ts | 4 +- fees/near/index.ts | 6 +- fees/sns.ts | 4 +- fees/solana.ts | 1 - fees/stride.ts | 46 ++++++------ fees/surfone/index.ts | 6 +- fees/thorswap/index.ts | 10 +-- fees/tigris/index.ts | 47 +++++------- fees/tron.ts | 6 +- fees/vertex-protocol.ts | 6 +- fees/xoxno/index.ts | 6 +- helpers/allium.ts | 22 +++--- helpers/bitqueryFees.ts | 4 +- helpers/blockscoutFees.ts | 6 +- helpers/dexVolumeLogs.ts | 2 +- helpers/etherscanFees.ts | 6 +- helpers/flipsidecrypto.ts | 24 +++---- helpers/getBlock.ts | 87 ++++++++++++----------- helpers/getChainFees.ts | 6 +- nfts/chains.ts | 30 ++++---- options/tigris/index.ts | 39 ++++------ options/typus/getChainData.ts | 14 ++-- protocols/pancakeswap/index.ts | 10 +-- users/chains.ts | 7 +- utils/fetchURL.ts | 61 +++++++++++++--- utils/prices.ts | 10 +-- 81 files changed, 462 insertions(+), 546 deletions(-) diff --git a/adapters/utils/runAdapter.ts b/adapters/utils/runAdapter.ts index 747303898f..dae5360adf 100644 --- a/adapters/utils/runAdapter.ts +++ b/adapters/utils/runAdapter.ts @@ -21,8 +21,8 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren try { const result: FetchResultGeneric = await fetchFunction(cleanCurrentDayTimestamp - 1, chainBlocks); const ignoreKeys = ['timestamp', 'block'] - if (id) - console.log("Result before cleaning", id, version, cleanCurrentDayTimestamp, chain, result, JSON.stringify(chainBlocks ?? {})) + // if (id) + // console.log("Result before cleaning", id, version, cleanCurrentDayTimestamp, chain, result, JSON.stringify(chainBlocks ?? {})) for (const [key, value] of Object.entries(result)) { if (ignoreKeys.includes(key)) continue; if (value === undefined || value === null) throw new Error(`Value: ${value} ${key} is undefined or null`) @@ -36,7 +36,8 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren ...result } } catch (error) { - throw { chain, error } + try { (error as any).chain = chain } catch { } + throw error } } @@ -50,6 +51,11 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren } } else if (_start) { const defaultStart = Math.trunc(Date.now() / 1000) + validStart[chain] = { // intentionally set to true to allow for backfilling + canRun: true, + startTimestamp: defaultStart + } + return; const start = await (_start as any)().catch(() => { console.error(`Failed to get start time for ${id} ${version} ${chain}`) return defaultStart diff --git a/aggregators/dexible/index.ts b/aggregators/dexible/index.ts index 670d875b63..b291d7ea74 100644 --- a/aggregators/dexible/index.ts +++ b/aggregators/dexible/index.ts @@ -1,6 +1,6 @@ import { BreakdownAdapter, Fetch, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import axios from 'axios'; +import { httpGet } from "../../utils/fetchURL"; const chains = [ CHAIN.ETHEREUM, @@ -60,11 +60,8 @@ const getVolume = async (chain: string, timestamp: number): Promise<{ totalVolume: string; }> => { const url = `${chainPath(chain)}timestamp=${timestamp}`; - const r = await axios.get(url); - if (!r.data) { - throw new Error("No data found in response"); - } - const data = r.data as IVolumeResponse; + const r = await httpGet(url); + const data = r as IVolumeResponse; return { timestamp: data.timestamp || timestamp, dailyVolume: data.dailyVolume, @@ -86,11 +83,8 @@ const adapter: BreakdownAdapter = { fetch: getFetch(chain), start: async () => { const url = `${chainPath(chain)}timestamp=${Math.ceil(Date.now() / 1000)}`; - const r = await axios.get(url); - if (!r.data) { - throw new Error("No data found in response"); - } - const data = r.data as IVolumeResponse; + const r = await httpGet(url); + const data = r as IVolumeResponse; return data.earliestTimestamp } } diff --git a/dexs/apollox/index.ts b/dexs/apollox/index.ts index b73de5eb27..b2919cd5bb 100644 --- a/dexs/apollox/index.ts +++ b/dexs/apollox/index.ts @@ -1,7 +1,7 @@ -import axios from "axios"; import { Chain } from "@defillama/sdk/build/general"; import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; +import { httpGet } from "../../utils/fetchURL"; type ResponseItem = { symbol: string; @@ -41,9 +41,9 @@ const v2VolumeAPI = const v1VolumeAPI = "https://www.apollox.finance/fapi/v1/ticker/24hr"; const fetchV2Volume = async (chain: Chain) => { - const { data = [] } = ( - await axios.get(v2VolumeAPI, { params: { chain, excludeCake: true } }) - ).data as { data: ResponseItem[] }; + const data = [] = ( + await httpGet(v2VolumeAPI, { params: { chain, excludeCake: true } }) + ) as ResponseItem[] const dailyVolume = data.reduce((p, c) => p + +c.qutoVol, 0); @@ -51,7 +51,7 @@ const fetchV2Volume = async (chain: Chain) => { }; const fetchV1Volume = async () => { - const data = (await axios.get(v1VolumeAPI)).data as V1TickerItem[]; + const data = (await httpGet(v1VolumeAPI)) as V1TickerItem[]; const dailyVolume = data.reduce((p, c) => p + +c.quoteVolume, 0); return dailyVolume diff --git a/dexs/aux-exchange/index.ts b/dexs/aux-exchange/index.ts index faec7a588e..533b81aa39 100644 --- a/dexs/aux-exchange/index.ts +++ b/dexs/aux-exchange/index.ts @@ -1,7 +1,7 @@ import { FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import axios from "axios"; import { getPrices } from "../../utils/prices"; +import { httpGet } from "../../utils/fetchURL"; interface ISwapEventData { in_au: string; @@ -22,8 +22,8 @@ const getResources = async (account: string): Promise => { do { let url = `${APTOS_PRC}/v1/accounts/${account}/resources?limit=9999` if (cursor) url += '&start=' + cursor - const res = await axios.get(url) - lastData = res.data + const res = await httpGet(url) + lastData = res data.push(...lastData) cursor = res.headers['x-aptos-cursor'] } while (lastData.length === 9999) @@ -76,7 +76,7 @@ const getSwapEvent = async (pool: any, fromTimestamp: number): Promise Number(e.sequence_number)) swap_events.push(...event) const lastMin = Math.min(...listSequence) diff --git a/dexs/bluefin/index.ts b/dexs/bluefin/index.ts index 757ed449f5..95aef2fe7b 100644 --- a/dexs/bluefin/index.ts +++ b/dexs/bluefin/index.ts @@ -1,7 +1,6 @@ import { BreakdownAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import axios from "axios"; -import BigNumber from "bignumber.js"; +import { httpGet } from "../../utils/fetchURL"; const URL_CONFIG = { "sui": "https://dapi.api.sui-prod.bluefin.io/marketData", @@ -23,9 +22,9 @@ const fetchURL = (baseURL: string, product: string): string => { const computeVolume = async (timestamp: number, baseUrl: string, productIds: string[]): Promise => { const dailyVolume = (await Promise.all(productIds.map((productId: string) => - axios.get(fetchURL(baseUrl, productId)) + httpGet(fetchURL(baseUrl, productId)) ))) - .map((e: any) => (Number(e.data._24hrClosePrice) / 10 ** 18) * (Number(e.data._24hrVolume) / 10 ** 18)) + .map((e: any) => (Number(e._24hrClosePrice) / 10 ** 18) * (Number(e._24hrVolume) / 10 ** 18)) .reduce((volume: number, sum: number) => sum + volume, 0); return { diff --git a/dexs/concordex-io/index.ts b/dexs/concordex-io/index.ts index 0c2b3b5cb1..214b3e25be 100644 --- a/dexs/concordex-io/index.ts +++ b/dexs/concordex-io/index.ts @@ -1,10 +1,10 @@ -import axios from 'axios' import type { SimpleAdapter } from '../../adapters/types' +import { httpPost } from '../../utils/fetchURL'; const POOLS_SERVICE_URL = 'https://cdex-liquidity-pool.concordex.io/v1/rpc' const rpc = (url: string, method: string, params: any) => - axios.post( + httpPost( url, { jsonrpc: '2.0', diff --git a/dexs/defi-kingdoms/index.ts b/dexs/defi-kingdoms/index.ts index a6b9935869..82afd12788 100644 --- a/dexs/defi-kingdoms/index.ts +++ b/dexs/defi-kingdoms/index.ts @@ -1,8 +1,8 @@ -import axios from "axios"; import { DISABLED_ADAPTER_KEY, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import disabledAdapter from "../../helpers/disabledAdapter"; +import { httpGet } from "../../utils/fetchURL"; const API = "https://nomics.com/data/exchange-volume-history?convert=USD&exchange=defikingdoms&interval=all" @@ -20,11 +20,11 @@ const adapter: SimpleAdapter = { [DISABLED_ADAPTER_KEY]: disabledAdapter, [CHAIN.HARMONY]: { start: async () => { - const data = (await axios.get(API)).data as IAPIResponse + const data = (await httpGet(API)) as IAPIResponse return new Date(data.items[0].timestamp).getTime() / 1000 }, fetch: async (timestamp: number) => { - const data = (await axios.get(API)).data as IAPIResponse + const data = (await httpGet(API)) as IAPIResponse const cleanTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) return { timestamp: cleanTimestamp, diff --git a/dexs/defibox/index.ts b/dexs/defibox/index.ts index 3fa5f85085..94726a9cd3 100644 --- a/dexs/defibox/index.ts +++ b/dexs/defibox/index.ts @@ -1,8 +1,7 @@ -import axios from "axios"; import { FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import fetchURL from "../../utils/fetchURL"; +import fetchURL, { httpPost } from "../../utils/fetchURL"; const endpoint = (chain: string) => `https://${chain}.defibox.io/api/swap/get24HInfo` const bal_endpoint = "https://eos.defibox.io/api/bal/get24HInfo" @@ -19,7 +18,7 @@ const graph = (chain: string) => { const swap_response: IVolume = (await fetchURL(endpoint(chain)))?.data.data volume = (bal_reponse?.volume_usd_24h? Number(bal_reponse.volume_usd_24h): 0) +(swap_response?.volume_usd_24h?Number(swap_response.volume_usd_24h):0) }else{ - const response: IVolume = chain !== CHAIN.BSC ? (await fetchURL(endpoint(chain)))?.data.data : (await axios.post(endpoint(chain), {} , { headers: {chainid: 56} })).data.data; + const response: IVolume = chain !== CHAIN.BSC ? (await fetchURL(endpoint(chain)))?.data.data : (await httpPost(endpoint(chain), {} , { headers: {chainid: 56} })).data; volume = response?.volume_usd_24h ? Number(response.volume_usd_24h): 0 } diff --git a/dexs/dexalot/index.ts b/dexs/dexalot/index.ts index ec701885f6..64ed02bae4 100644 --- a/dexs/dexalot/index.ts +++ b/dexs/dexalot/index.ts @@ -3,7 +3,7 @@ import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import customBackfill from "../../helpers/customBackfill"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; +import { httpGet } from "../../utils/fetchURL"; const historicalVolumeEndpoint = "https://api.dexalot.com/api/stats/dailyvolumes" @@ -14,9 +14,9 @@ interface IVolumeall { const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint, { headers: { + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint, { headers: { 'origin': 'https://app.dexalot.com' - }}))?.data; + }})) const totalVolume = historicalVolume .filter(volItem => (new Date(volItem.date).getTime() / 1000) <= dayTimestamp) .reduce((acc, { volumeusd }) => acc + Number(volumeusd), 0) @@ -32,9 +32,9 @@ const fetch = async (timestamp: number) => { }; const getStartTimestamp = async () => { - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint, { headers: { + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint, { headers: { 'origin': 'https://app.dexalot.com' - }}))?.data; + }})) return (new Date(historicalVolume[0].date).getTime()) / 1000 } diff --git a/dexs/drift-protocol/index.ts b/dexs/drift-protocol/index.ts index 5540cddf80..b9994f4ff6 100644 --- a/dexs/drift-protocol/index.ts +++ b/dexs/drift-protocol/index.ts @@ -1,17 +1,17 @@ -import axios from "axios"; import { CHAIN } from "../../helpers/chains"; +import { httpGet } from "../../utils/fetchURL"; const dailyVolEndpoint = "https://mainnet-beta.api.drift.trade/stats/24HourVolume"; async function fetch(type: "perp" | "spot") { - const volumeResponse = await axios.get( + const volumeResponse = await httpGet( `${dailyVolEndpoint}?${ type === "perp" ? "perpMarkets" : "spotMarkets" }=true` ); - const rawVolumeQuotePrecision = volumeResponse.data.data.volume; + const rawVolumeQuotePrecision = volumeResponse.data.volume; // Volume will be returned in 10^6 precision const volumeNumber = diff --git a/dexs/dx25/index.ts b/dexs/dx25/index.ts index a63edb39df..9e3360ecd7 100644 --- a/dexs/dx25/index.ts +++ b/dexs/dx25/index.ts @@ -1,29 +1,17 @@ -import axios from 'axios' import type { SimpleAdapter } from '../../adapters/types' import { CHAIN } from "../../helpers/chains"; +import { httpPost } from '../../utils/fetchURL'; const POOLS_SERVICE_URL = 'https://liquidity-pool.dx25.com/v1/rpc' const rpc = (url: string, method: string, params: any) => - axios.post( - url, - { - jsonrpc: '2.0', - method, - params, - id: '0', - }, - { - headers: { - 'Content-Type': 'application/json', - } - } - ) + httpPost(url, { jsonrpc: '2.0', method, params, id: '0', }, + { headers: { 'Content-Type': 'application/json', } }) .then(res => { - if (res.data.error) { - throw new Error(res.data.error.message) + if (res.error) { + throw new Error(res.error.message) } - return res.data.result + return res.result }); diff --git a/dexs/fwx/index.ts b/dexs/fwx/index.ts index b2cf30e94b..6cde318638 100644 --- a/dexs/fwx/index.ts +++ b/dexs/fwx/index.ts @@ -2,7 +2,7 @@ import { Chain } from "@defillama/sdk/build/general"; import { FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; +import { httpPost } from "../../utils/fetchURL"; interface IConfigs { pairs: IPairData[]; @@ -56,19 +56,19 @@ const fetch = (chain: Chain) => { const formattedDate = date.toISOString().replace(/\.(\d{3})Z$/, ".$1Z"); // * call api for daily volume - const pairDataRes = await axios.post(endpoints[chain].pairData); - const pairData = pairDataRes as { data: IConfigs }; - const pairs = pairData.data.pairs.map((p: IPairData) => p.pair_name); + const pairDataRes = await httpPost(endpoints[chain].pairData, {}); + const pairData = pairDataRes as IConfigs + const pairs = pairData.pairs.map((p: IPairData) => p.pair_name); // * call api for daily volume - const tradingVolumeRes = await axios.post(endpoints[chain].tradingVolume, { + const tradingVolumeRes = await httpPost(endpoints[chain].tradingVolume, { from_date: formattedDate, to_date: formattedDate, pair_names: pairs, type: ["all"], }); - const tradingVolume = tradingVolumeRes as { data: IChartRes }; - const totalVolumeData = tradingVolume.data.charts.find( + const tradingVolume = tradingVolumeRes as IChartRes + const totalVolumeData = tradingVolume.charts.find( (x: IChart) => x.type == "all" ); const dailyVolumeData = totalVolumeData?.data.find( @@ -77,14 +77,14 @@ const fetch = (chain: Chain) => { )?.daily_data; // * call api for daily open interest - const openInterestRes = await axios.post(endpoints[chain].openInterest, { + const openInterestRes = await httpPost(endpoints[chain].openInterest, { from_date: formattedDate, to_date: formattedDate, pair_names: pairs, type: ["all"], }); - const openInterest = openInterestRes as { data: IChartRes }; - const openInterestData = openInterest.data.charts.find( + const openInterest = openInterestRes as IChartRes + const openInterestData = openInterest.charts.find( (x: IChart) => x.type == "all" ); const dailyOpenInterestData = openInterestData?.data.find( diff --git a/dexs/hashflow/index.ts b/dexs/hashflow/index.ts index 8a83e9d08a..889001fa54 100644 --- a/dexs/hashflow/index.ts +++ b/dexs/hashflow/index.ts @@ -1,7 +1,7 @@ -import axios from "axios"; import type { BaseAdapter, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; const chains = [CHAIN.ETHEREUM, CHAIN.AVAX, CHAIN.BSC, CHAIN.ARBITRUM, CHAIN.OPTIMISM, CHAIN.POLYGON] @@ -15,7 +15,7 @@ interface IAPIResponse { } const getStartTime = async (chain: string) => { - const response = (await axios.get("https://hashflow2.metabaseapp.com/api/public/dashboard/f4b12fd4-d28c-4f08-95b9-78b00b83cf17/dashcard/104/card/97?parameters=%5B%5D")).data as IAPIResponse + const response = (await httpGet("https://hashflow2.metabaseapp.com/api/public/dashboard/f4b12fd4-d28c-4f08-95b9-78b00b83cf17/dashcard/104/card/97?parameters=%5B%5D")) as IAPIResponse const startTime = response.data.rows.filter(([c]) => normalizeChain(c) === chain).reduce((acc, [_chain, dateString]) => { const potentialStartTimestamp = dateToTs(dateString) if (potentialStartTimestamp < acc) return potentialStartTimestamp @@ -31,7 +31,7 @@ const adapter: SimpleAdapter = { [chain]: { fetch: async (timestamp) => { const cleanTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const response = (await axios.get("https://hashflow2.metabaseapp.com/api/public/dashboard/f4b12fd4-d28c-4f08-95b9-78b00b83cf17/dashcard/104/card/97?parameters=%5B%5D")).data as IAPIResponse + const response = (await httpGet("https://hashflow2.metabaseapp.com/api/public/dashboard/f4b12fd4-d28c-4f08-95b9-78b00b83cf17/dashcard/104/card/97?parameters=%5B%5D")) as IAPIResponse const vol = response.data.rows.filter(([c]) => normalizeChain(c) === chain).find(([_chain, dateString]) => dateToTs(dateString) === cleanTimestamp) return { timestamp: cleanTimestamp, diff --git a/dexs/hyperliquid/index.ts b/dexs/hyperliquid/index.ts index 980f1d1771..0702d0c1bb 100644 --- a/dexs/hyperliquid/index.ts +++ b/dexs/hyperliquid/index.ts @@ -1,7 +1,7 @@ import type { SimpleAdapter } from "../../adapters/types"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import { CHAIN } from "../../helpers/chains"; -import axios from "axios"; +import { httpPost } from "../../utils/fetchURL"; const URL = "https://api.hyperliquid.xyz/info"; @@ -11,7 +11,7 @@ interface Response { } const fetch = async (timestamp: number) => { - const {totalVolume, dailyVolume}: Response = (await axios.post(URL, {"type": "globalStats"})).data; + const {totalVolume, dailyVolume}: Response = (await httpPost(URL, {"type": "globalStats"})); const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)); return { diff --git a/dexs/minswap/index.ts b/dexs/minswap/index.ts index d8efc3e811..a3487205a1 100644 --- a/dexs/minswap/index.ts +++ b/dexs/minswap/index.ts @@ -1,8 +1,8 @@ -import axios from "axios"; import type { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import { getPrices } from "../../utils/prices"; +import { httpGet } from "../../utils/fetchURL"; interface IVolumeall { time: string; @@ -14,7 +14,7 @@ const historicalVolumeEndpoint = "https://api-mainnet-prod.minswap.org/defillama const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const vols: IVolumeall[] = (await axios.get(historicalVolumeEndpoint))?.data; + const vols: IVolumeall[] = (await httpGet(historicalVolumeEndpoint)); const dailyVolume = vols .find(dayItem => new Date(Number(dayItem.time)).getTime() / 1000 === dayTimestamp)?.volume @@ -34,7 +34,7 @@ const fetch = async (timestamp: number) => { } const getStartTimestamp = async () => { - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint))?.data; + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint)); return (new Date(Number(historicalVolume[0].time)).getTime()) / 1000; } diff --git a/dexs/moon-swap/index.ts b/dexs/moon-swap/index.ts index 9fd2c26dad..146be97ae0 100644 --- a/dexs/moon-swap/index.ts +++ b/dexs/moon-swap/index.ts @@ -1,7 +1,7 @@ import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; +import { httpPost } from "../../utils/fetchURL"; const historical = "https://moonswap.fi/api/route/opt/swap/dashboard/global-chart"; const START_TIME = 1634515198; @@ -13,7 +13,7 @@ interface IVolumeall { const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.post(historical, {start_time: START_TIME, skip: 0}))?.data.data.uniswapDayDatas; + const historicalVolume: IVolumeall[] = (await httpPost(historical, {start_time: START_TIME, skip: 0}))?.data.uniswapDayDatas; const totalVolume = historicalVolume .filter(volItem => Number(volItem.date) <= dayTimestamp) diff --git a/dexs/muesliswap/index.ts b/dexs/muesliswap/index.ts index 46f7dd4490..4799bb2ff2 100644 --- a/dexs/muesliswap/index.ts +++ b/dexs/muesliswap/index.ts @@ -1,7 +1,7 @@ -import axios from "axios"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp, univ2Adapter } from "../../helpers/getUniSubgraphVolume"; import { getPrices } from "../../utils/prices"; +import { httpGet } from "../../utils/fetchURL"; interface IVolumeall { time: number; @@ -12,7 +12,7 @@ const historicalVolumeEndpoint = "https://analyticsv3.muesliswap.com/historical- const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp*1000)) - const vols: IVolumeall[] = (await axios.get(historicalVolumeEndpoint))?.data; + const vols: IVolumeall[] = (await httpGet(historicalVolumeEndpoint)); const totalVolume = vols .filter((volItem: IVolumeall) => Number(volItem.time) <= dayTimestamp) .reduce((acc, { volume }) => acc + Number(volume), 0); diff --git a/dexs/mux-protocol/index.ts b/dexs/mux-protocol/index.ts index 9c98e28de0..d09404fe67 100644 --- a/dexs/mux-protocol/index.ts +++ b/dexs/mux-protocol/index.ts @@ -1,8 +1,8 @@ -import axios from "axios"; import type { BaseAdapter, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import { Chain } from "@defillama/sdk/build/general"; +import { httpGet } from "../../utils/fetchURL"; const chains = [CHAIN.AVAX, CHAIN.BSC, CHAIN.ARBITRUM, CHAIN.OPTIMISM, CHAIN.FANTOM] @@ -37,7 +37,7 @@ const startTime: TStartTime = { const graph = (chain: Chain) => { return async (timestamp: number) => { const cleanTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const response = (await axios.get(url)).data as IAPIResponse + const response = (await httpGet(url)) as IAPIResponse const volumehistorical: IVolumeall[] = response.data.rows .filter(([_,c]) => normalizeChain(c.toLowerCase()) == chain) .map(([date, _, ammountUSD]: [string, string, number]) => { diff --git a/dexs/native/index.ts b/dexs/native/index.ts index 1f9d3aa5ef..1a06502e4a 100644 --- a/dexs/native/index.ts +++ b/dexs/native/index.ts @@ -1,7 +1,7 @@ -import axios from "axios"; import type { BaseAdapter, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; const chains = [ CHAIN.ETHEREUM, @@ -24,11 +24,11 @@ interface ResEntry { const getStartTime = async (chain: string) => { - const response = await axios.get( + const response = await httpGet( `${NATIVE_ANALYTICS_ENDPOINT}?chain=${chain === CHAIN.AVAX ? "avalanche" : chain}` ); - const smallestDate = response.data.reduce( + const smallestDate = response.reduce( (minDate: number, current: ResEntry) => { return current.date < minDate ? current.date : minDate; }, @@ -48,11 +48,11 @@ const adapter: SimpleAdapter = { new Date(timestamp * 1000) ); - const response = await axios.get( + const response = await httpGet( `${NATIVE_ANALYTICS_ENDPOINT}?chain=${chain === CHAIN.AVAX ? "avalanche" : chain}` ); - const totalVol = response.data.reduce( + const totalVol = response.reduce( (sum: number, entry: ResEntry) => sum + entry.volumeUSD, 0 ); diff --git a/dexs/noah-swap/index.ts b/dexs/noah-swap/index.ts index cc87493faf..0ee90ac5a4 100644 --- a/dexs/noah-swap/index.ts +++ b/dexs/noah-swap/index.ts @@ -1,7 +1,8 @@ -import axios from "axios"; + import { SimpleAdapter } from "../../adapters/types" import { CHAIN } from "../../helpers/chains" import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpPost } from "../../utils/fetchURL"; interface IData { create_time: string; volume_usd_24h: string; @@ -15,7 +16,7 @@ const fetchVolume = async (timestamp: number) => { } const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)); const dateString = new Date(timestamp * 1000).toISOString().split("T")[0]; - const res: IData[] = (await axios.post(url, body, { headers: { "Chainid": 17777 }})).data.data as IData[]; + const res: IData[] = (await httpPost(url, body, { headers: { "Chainid": 17777 }})).data as IData[]; const dayItem = res.find(item => item.create_time.split('T')[0] === dateString); if (!dayItem) { return { diff --git a/dexs/orca/index.ts b/dexs/orca/index.ts index 6421c56e54..219eb9aade 100644 --- a/dexs/orca/index.ts +++ b/dexs/orca/index.ts @@ -1,11 +1,11 @@ -import axios from 'axios'; import { CHAIN } from '../../helpers/chains'; +import { httpGet } from '../../utils/fetchURL'; const wpEndpoint = "https://api.mainnet.orca.so/v1/whirlpool/list?whitelisted=true"; async function fetch(timestamp: number) { - const [whirlpools] = await Promise.all([axios.get(wpEndpoint)]); - const wpVol = whirlpools.data.whirlpools.reduce((sum: number, pool: any) => + const [whirlpools] = await Promise.all([httpGet(wpEndpoint)]); + const wpVol = whirlpools.whirlpools.reduce((sum: number, pool: any) => sum + (pool?.volume?.day || 0) , 0); return { diff --git a/dexs/orderly-network-derivatives/index.ts b/dexs/orderly-network-derivatives/index.ts index 3aef7f05b3..5d6f2529b1 100644 --- a/dexs/orderly-network-derivatives/index.ts +++ b/dexs/orderly-network-derivatives/index.ts @@ -1,6 +1,6 @@ -import axios from "axios"; import type { SimpleAdapter } from "../../adapters/types"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; const dateToTs = (date: string) => new Date(date).getTime() / 1000 @@ -10,15 +10,15 @@ const adapter: SimpleAdapter = { adapter: { "arbitrum":{ start: async()=>{ - const data = await axios.get(apiEVM) - return dateToTs(data.data[0].date) + const data = await httpGet(apiEVM) + return dateToTs(data[0].date) }, fetch: async(ts)=>{ - const data = await axios.get(apiEVM) + const data = await httpGet(apiEVM) const cleanTimestamp = getUniqStartOfTodayTimestamp(new Date(ts * 1000)) return { timestamp: cleanTimestamp, - dailyVolume: data.data.find((t:any)=>dateToTs(t.date) === cleanTimestamp)?.volume + dailyVolume: data.find((t:any)=>dateToTs(t.date) === cleanTimestamp)?.volume } } } diff --git a/dexs/orderly-network/index.ts b/dexs/orderly-network/index.ts index 9ccaa3f981..7266b45f3e 100644 --- a/dexs/orderly-network/index.ts +++ b/dexs/orderly-network/index.ts @@ -1,6 +1,6 @@ -import axios from "axios"; import type { SimpleAdapter } from "../../adapters/types"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; const dateToTs = (date: string) => new Date(date).getTime() / 1000 @@ -10,15 +10,15 @@ const adapter: SimpleAdapter = { adapter: { "near":{ start: async()=>{ - const data = await axios.get(apiNear) - return dateToTs(data.data[0].date) + const data = await httpGet(apiNear) + return dateToTs(data[0].date) }, fetch: async(ts)=>{ - const data = await axios.get(apiNear) + const data = await httpGet(apiNear) const cleanTimestamp = getUniqStartOfTodayTimestamp(new Date(ts * 1000)) return { timestamp: cleanTimestamp, - dailyVolume: data.data.find((t:any)=>dateToTs(t.date) === cleanTimestamp)?.volume + dailyVolume: data.find((t:any)=>dateToTs(t.date) === cleanTimestamp)?.volume } } }, diff --git a/dexs/oswap/index.ts b/dexs/oswap/index.ts index 4f721ee8c4..1e20c9b0c5 100644 --- a/dexs/oswap/index.ts +++ b/dexs/oswap/index.ts @@ -1,7 +1,6 @@ -import axios from "axios"; - import type { FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; +import { httpGet } from "../../utils/fetchURL"; interface ITicker { full_market_name: string; @@ -29,12 +28,12 @@ interface IExchangeRates { const OSWAP_STATS_ENDPOINT = "https://v2-stats.oswap.io/api/v1"; const getTickers = async () => { - const tickers: ITickers = (await axios.get(`${OSWAP_STATS_ENDPOINT}/tickers`))?.data; + const tickers: ITickers = (await httpGet(`${OSWAP_STATS_ENDPOINT}/tickers`)); return Object.values(tickers); } const getExchangeRates = async () => { - const exchangeRates: IExchangeRates = (await axios.get(`${OSWAP_STATS_ENDPOINT}/exchangeRates`))?.data; + const exchangeRates: IExchangeRates = (await httpGet(`${OSWAP_STATS_ENDPOINT}/exchangeRates`)); return exchangeRates; } diff --git a/dexs/paint-swap/index.ts b/dexs/paint-swap/index.ts index 28b0e38fbb..bf32d66215 100644 --- a/dexs/paint-swap/index.ts +++ b/dexs/paint-swap/index.ts @@ -1,4 +1,3 @@ -import axios from "axios"; import fetchURL from "../../utils/fetchURL" import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; diff --git a/dexs/paycash/index.ts b/dexs/paycash/index.ts index 4f3f6d76ab..a5fb140ddf 100644 --- a/dexs/paycash/index.ts +++ b/dexs/paycash/index.ts @@ -1,9 +1,9 @@ -import axios from "axios"; import { Chain } from "@defillama/sdk/build/general"; import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import customBackfill from "../../helpers/customBackfill"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpPost } from "../../utils/fetchURL"; const historicalVolumeEndpoint = "https://api.paycashswap.com/" const requestBody = { @@ -19,7 +19,7 @@ interface IVolumeall { const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.post(historicalVolumeEndpoint, requestBody))?.data.data.totalVolumeChart.points; + const historicalVolume: IVolumeall[] = (await httpPost(historicalVolumeEndpoint, requestBody))?.data.totalVolumeChart.points; const totalVolume = historicalVolume .filter(volItem => (new Date(volItem.timestamp).getTime() / 1000) <= dayTimestamp) .reduce((acc, { value }) => acc + Number(value), 0) diff --git a/dexs/phoenix/index.ts b/dexs/phoenix/index.ts index 4dcb4f978e..59e54fc77c 100644 --- a/dexs/phoenix/index.ts +++ b/dexs/phoenix/index.ts @@ -1,5 +1,5 @@ -import axios from 'axios'; import { CHAIN } from '../../helpers/chains'; +import { httpGet } from '../../utils/fetchURL'; const ONE_DAY_IN_SECONDS = 60 * 60 * 24 @@ -8,13 +8,13 @@ const volumeEndpoint = "https://9senbezsz3.execute-api.us-east-1.amazonaws.com/P async function fetch(timestamp: number) { const from = timestamp - ONE_DAY_IN_SECONDS; const to = timestamp; - const response = await axios.get(volumeEndpoint, { + const response = await httpGet(volumeEndpoint, { headers: { 'X-Api-Key': 'lty8FkZLKR87IA5g7uFEY6uH8MIk8mJT4OehR8hF' }, params: { start_timestamp: from, end_timestamp: to } }); return { - dailyVolume: response.data.volume_in_quote_units, + dailyVolume: response.volume_in_quote_units, timestamp: timestamp } } diff --git a/dexs/ref-finance/index.ts b/dexs/ref-finance/index.ts index 21c2f279d4..65adbb8c6d 100644 --- a/dexs/ref-finance/index.ts +++ b/dexs/ref-finance/index.ts @@ -1,6 +1,6 @@ -import axios from "axios"; import type { SimpleAdapter } from "../../adapters/types"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; const dateToTs = (date: string) => new Date(date).getTime() / 1000 @@ -10,15 +10,15 @@ const adapter: SimpleAdapter = { adapter: { "near":{ start: async()=>{ - const data = await axios.get(api) - return dateToTs(data.data[0].date) + const data = await httpGet(api) + return dateToTs(data[0].date) }, fetch: async(ts)=>{ - const data = await axios.get(api) + const data = await httpGet(api) const cleanTimestamp = getUniqStartOfTodayTimestamp(new Date(ts * 1000)) return { timestamp: cleanTimestamp, - dailyVolume: data.data.find((t:any)=>dateToTs(t.date) === cleanTimestamp)?.volume + dailyVolume: data.find((t:any)=>dateToTs(t.date) === cleanTimestamp)?.volume } } } diff --git a/dexs/saber/index.ts b/dexs/saber/index.ts index d8120e877c..f83e01c84d 100644 --- a/dexs/saber/index.ts +++ b/dexs/saber/index.ts @@ -1,11 +1,11 @@ -import axios from "axios" +import { httpPost } from "../../utils/fetchURL"; import { getPrices } from "../../utils/prices"; async function last24h(timestamp: number) { - const data = await axios.post('https://saberqltest.aleph.cloud/', { "query": "{\n pools {\n ammId\n name\n coin {\n chainId\n address\n name\n decimals\n symbol\n logoURI\n }\n pc {\n chainId\n address\n name\n decimals\n symbol\n logoURI\n }\n lp {\n chainId\n address\n name\n decimals\n symbol\n logoURI\n }\n stats {\n tvl_pc\n tvl_coin\n price\n vol24h\n }\n }\n}\n" }) + const data = await httpPost('https://saberqltest.aleph.cloud/', { "query": "{\n pools {\n ammId\n name\n coin {\n chainId\n address\n name\n decimals\n symbol\n logoURI\n }\n pc {\n chainId\n address\n name\n decimals\n symbol\n logoURI\n }\n lp {\n chainId\n address\n name\n decimals\n symbol\n logoURI\n }\n stats {\n tvl_pc\n tvl_coin\n price\n vol24h\n }\n }\n}\n" }) const coinId = "solana:So11111111111111111111111111111111111111112"; const prices = await getPrices([coinId], timestamp) - const vol = data.data.data.pools.reduce( + const vol = data.data.pools.reduce( (a: number, b: any) => a + b.stats.vol24h * (b.pc.address === "So11111111111111111111111111111111111111112" ? prices[coinId].price : 1) diff --git a/dexs/saucerswap/index.ts b/dexs/saucerswap/index.ts index ac7f6d6fdb..764f88b2bc 100644 --- a/dexs/saucerswap/index.ts +++ b/dexs/saucerswap/index.ts @@ -1,8 +1,8 @@ import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; import { getPrices } from "../../utils/prices"; +import { httpGet } from "../../utils/fetchURL"; const historicalVolumeEndpoint = (to: number) =>`https://server.saucerswap.finance/api/public/stats/platformData?field=VOLUME&interval=DAY&from=1650586&to=${to}` // https://server.saucerswap.finance/api/public/stats/platformData?field=VOLUME&interval=DAY&from=1650586&to=1682093355 @@ -13,9 +13,9 @@ interface IVolumeall { const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint(new Date().getTime() / 1000), { headers: { + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint(new Date().getTime() / 1000), { headers: { 'origin': 'https://analytics.saucerswap.finance', - }}))?.data; + }})); const totalVolume = historicalVolume .filter(volItem => Number(volItem.timestampSeconds) <= dayTimestamp) diff --git a/dexs/sundaeswap/index.ts b/dexs/sundaeswap/index.ts index c4d15ecd95..68d34fc86e 100644 --- a/dexs/sundaeswap/index.ts +++ b/dexs/sundaeswap/index.ts @@ -2,7 +2,6 @@ import fetchURL from "../../utils/fetchURL" import { DISABLED_ADAPTER_KEY, FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; import disabledAdapter from "../../helpers/disabledAdapter"; import { getPrices } from "../../utils/prices"; diff --git a/dexs/surfone/index.ts b/dexs/surfone/index.ts index 976330866a..40e4ea1af3 100644 --- a/dexs/surfone/index.ts +++ b/dexs/surfone/index.ts @@ -1,7 +1,7 @@ import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; +import { httpGet } from "../../utils/fetchURL"; const volumeEndpoint = "https://apigateway.surf.one/pool/24h/data" @@ -17,10 +17,10 @@ interface IVolume { const fetch = () => { return async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const response = (await axios.get(volumeEndpoint, { headers })); + const response = (await httpGet(volumeEndpoint, { headers })); //const response = await fetchURL(url[chain]); - const volume: IVolume = response.data.data; + const volume: IVolume = response.data; return { totalVolume: `${volume?.totalVolume || undefined}`, dailyVolume: `${volume?.totalTradeSize || undefined}`, diff --git a/dexs/synfutures/index.ts b/dexs/synfutures/index.ts index 22d6b358f6..dee6022b3c 100644 --- a/dexs/synfutures/index.ts +++ b/dexs/synfutures/index.ts @@ -1,12 +1,10 @@ // SynFutures v1 volume -import { Adapter, SimpleAdapter } from "../../adapters/types"; +import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; import { Chain } from '@defillama/sdk/build/general'; -import customBackfill from "../../helpers/customBackfill"; import { getPrices } from "../../utils/prices"; -const { request, gql } = require("graphql-request"); +const { request, } = require("graphql-request"); const info: {[key: string]: any} = { polygon: { diff --git a/dexs/thorswap/index.ts b/dexs/thorswap/index.ts index 9e4289e3cb..1bd0d44672 100644 --- a/dexs/thorswap/index.ts +++ b/dexs/thorswap/index.ts @@ -1,7 +1,7 @@ -import axios from "axios"; import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; const historicalVolumeEndpoint = "https://midgard.ninerealms.com/v2/history/swaps?interval=day&count=400" @@ -25,7 +25,7 @@ const calVolume = (total: IVolumeall): number => { const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint, { headers: {"x-client-id": "defillama"}}))?.data.intervals; + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint, { headers: {"x-client-id": "defillama"}})).intervals; const totalVolume = historicalVolume .filter(volItem => Number(volItem.startTime) <= dayTimestamp) .reduce((acc, res) => acc + calVolume(res), 0); diff --git a/dexs/thorwallet/index.ts b/dexs/thorwallet/index.ts index b6d53373a7..a30e9f424e 100644 --- a/dexs/thorwallet/index.ts +++ b/dexs/thorwallet/index.ts @@ -1,7 +1,7 @@ -import axios from 'axios' -import { Adapter, BaseAdapter, SimpleAdapter } from '../../adapters/types' +import { SimpleAdapter } from '../../adapters/types' import { CHAIN } from '../../helpers/chains' import { getUniqStartOfTodayTimestamp } from '../../helpers/getUniSubgraphVolume' +import { httpGet } from '../../utils/fetchURL' const historicalVolumeEndpoint = 'https://midgard.thorwallet.org/v2/history/swaps?interval=day&count=100' @@ -26,7 +26,7 @@ const calVolume = (total: IVolumeall): number => { const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint))?.data.intervals + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint)).intervals const totalVolume = historicalVolume .filter((volItem) => Number(volItem.startTime) <= dayTimestamp) .reduce((acc, res) => acc + calVolume(res), 0) @@ -43,7 +43,7 @@ const fetch = async (timestamp: number) => { } const getStartTimestamp = async () => { - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint))?.data.intervals + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint)).intervals return Number(historicalVolume[0]?.startTime) } diff --git a/dexs/tigris/index.ts b/dexs/tigris/index.ts index 1c9a437aec..c5eac5473f 100644 --- a/dexs/tigris/index.ts +++ b/dexs/tigris/index.ts @@ -1,7 +1,7 @@ import { Chain } from "@defillama/sdk/build/general"; import { Adapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import axios from 'axios'; +import { httpGet } from "../../utils/fetchURL"; const API_ENDPOINT = "https://flask.tigristrade.info"; @@ -12,68 +12,51 @@ interface ApiResponse { } const fetchFromAPI = async (chain: Chain, timestamp: number): Promise => { - try { - let endpoint; - if (chain === CHAIN.POLYGON) { - endpoint = "/fetch-polygon-data"; - } else if (chain === CHAIN.ARBITRUM) { - endpoint = "/fetch-arbitrum-data"; - } else { - throw new Error(`Unsupported chain: ${chain}`); - } - - const response = await axios.get(`${API_ENDPOINT}${endpoint}`, { - params: { - chain: chain, - timestamp: timestamp - } - }); + let endpoint; + if (chain === CHAIN.POLYGON) { + endpoint = "/fetch-polygon-data"; + } else if (chain === CHAIN.ARBITRUM) { + endpoint = "/fetch-arbitrum-data"; + } else { + throw new Error(`Unsupported chain: ${chain}`); + } - if (response.status !== 200) { - throw new Error("Failed to fetch data from the API"); + const response = await httpGet(`${API_ENDPOINT}${endpoint}`, { + params: { + chain: chain, + timestamp: timestamp } + }); - return response.data; - } catch (error) { - console.error("Error fetching from the API:", error); - throw error; - } + return response; } function startOfDayTimestamp(timestamp: number): number { - const date = new Date(timestamp * 1000); - date.setUTCHours(0, 0, 0, 0); - return Math.floor(date.getTime() / 1000); + const date = new Date(timestamp * 1000); + date.setUTCHours(0, 0, 0, 0); + return Math.floor(date.getTime() / 1000); } const fetch = (chain: Chain) => { return async (timestamp: number) => { - try { - const dataPoints = await fetchFromAPI(chain, timestamp); - - const adjustedTimestamp = startOfDayTimestamp(timestamp); - - - const matchingData = dataPoints.find(e => e.day === adjustedTimestamp); - - if (!matchingData) { - console.warn(`No matching data found for timestamp ${adjustedTimestamp}. Returning zero values.`); - return { - dailyVolume: '0', - totalVolume: '0', - timestamp: adjustedTimestamp - }; - } + const dataPoints = await fetchFromAPI(chain, timestamp); + const adjustedTimestamp = startOfDayTimestamp(timestamp); + const matchingData = dataPoints.find(e => e.day === adjustedTimestamp); + if (!matchingData) { + console.warn(`No matching data found for timestamp ${adjustedTimestamp}. Returning zero values.`); return { - dailyVolume: matchingData.dailyVolume.toString(), - totalVolume: matchingData.totalVolume.toString(), - timestamp: matchingData.day + dailyVolume: '0', + totalVolume: '0', + timestamp: adjustedTimestamp }; - } catch (e) { - console.error(e); - throw e; } + + return { + dailyVolume: matchingData.dailyVolume.toString(), + totalVolume: matchingData.totalVolume.toString(), + timestamp: matchingData.day + }; } } diff --git a/dexs/unicly/index.ts b/dexs/unicly/index.ts index 2d74516abc..b7c953f388 100644 --- a/dexs/unicly/index.ts +++ b/dexs/unicly/index.ts @@ -1,10 +1,10 @@ -import axios from "axios"; import { Chain } from "@defillama/sdk/build/general"; import { DISABLED_ADAPTER_KEY, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import customBackfill from "../../helpers/customBackfill"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import disabledAdapter from "../../helpers/disabledAdapter"; +import { httpGet } from "../../utils/fetchURL"; const historicalVolumeEndpoint = "https://api.viewblock.io/dex/unicly" @@ -17,7 +17,7 @@ const headers = { } const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint, { headers }))?.data.charts.volume; + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint, { headers })).charts.volume; const totalVolume = historicalVolume .filter(volItem => Math.floor(Number(volItem.timestamp) / 1000) <= dayTimestamp) .reduce((acc, { value }) => acc + Number(value), 0) @@ -33,7 +33,7 @@ const fetch = async (timestamp: number) => { }; const getStartTimestamp = async () => { - const historicalVolume: IVolumeall[] = (await axios.get(historicalVolumeEndpoint, { headers }))?.data.charts.volume; + const historicalVolume: IVolumeall[] = (await httpGet(historicalVolumeEndpoint, { headers })).charts.volume; return (new Date(historicalVolume[0].timestamp).getTime()) / 1000 } diff --git a/dexs/vanswap/index.ts b/dexs/vanswap/index.ts index c9e25c12bc..3edb4207af 100644 --- a/dexs/vanswap/index.ts +++ b/dexs/vanswap/index.ts @@ -1,10 +1,7 @@ -import fetchURL from "../../utils/fetchURL" -import { Chain } from "@defillama/sdk/build/general"; +import { httpPost } from "../../utils/fetchURL" import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import customBackfill from "../../helpers/customBackfill"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; const historicalVolumeEndpoint = "https://www.vanswap.org/info/DayDatas?first=1000&date=1577836800" @@ -15,7 +12,7 @@ interface IVolumeall { const fetch = async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await axios.post(historicalVolumeEndpoint))?.data.result; + const historicalVolume: IVolumeall[] = (await httpPost(historicalVolumeEndpoint, null))?.result; const totalVolume = historicalVolume .filter(volItem => (new Date(volItem.date).getTime()) <= dayTimestamp) .reduce((acc, { dailyVolumeUSD }) => acc + Number(dailyVolumeUSD), 0) @@ -31,7 +28,7 @@ const fetch = async (timestamp: number) => { }; const getStartTimestamp = async () => { - //const historicalVolume: IVolumeall[] = (await axios.post(historicalVolumeEndpoint))?.data.result; + //const historicalVolume: IVolumeall[] = (await httpPost(historicalVolumeEndpoint))?.result; //return (new Date(historicalVolume[0].date).getTime()); return 1647302400 } diff --git a/dexs/veax/index.ts b/dexs/veax/index.ts index 197d9171d9..b2ccf542e5 100644 --- a/dexs/veax/index.ts +++ b/dexs/veax/index.ts @@ -1,31 +1,17 @@ -import axios from 'axios' import type { SimpleAdapter } from '../../adapters/types' +import { httpPost } from '../../utils/fetchURL'; const POOLS_SERVICE_URL = 'https://veax-liquidity-pool.veax.com/v1/rpc' const rpc = (url: string, method: string, params: any) => - axios.post( - url, - { - jsonrpc: '2.0', - method, - params, - id: '0', - }, - { - headers: { - 'Content-Type': 'application/json', - } - } - ) - .then(res => { - if (res.data.error) { - throw new Error(res.data.error.message) - } - return res.data.result - }); - + httpPost(url, { jsonrpc: '2.0', method, params, id: '0', }, + { headers: { 'Content-Type': 'application/json', } } + ).then(res => { + if (res.error) + throw new Error(res.error.message) + return res.result + }); const adapter: SimpleAdapter = { adapter: { diff --git a/dexs/vertex-protocol/index.ts b/dexs/vertex-protocol/index.ts index 2ea7482b6c..87be6260f2 100644 --- a/dexs/vertex-protocol/index.ts +++ b/dexs/vertex-protocol/index.ts @@ -1,12 +1,6 @@ import { BreakdownAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import axios from "axios"; - -interface IVolumeall { - timestamp: string; - volume: string; - close_x18: string; -} +import { httpGet, httpPost } from "../../utils/fetchURL"; interface IProducts { spot_products: number[]; @@ -18,15 +12,13 @@ const gatewayBaseUrl = "https://gateway.prod.vertexprotocol.com/v1"; const archiveBaseUrl = "https://archive.prod.vertexprotocol.com/v1"; const fetchValidSymbols = async (): Promise => { - const symbols = (await axios.get(`${gatewayBaseUrl}/symbols`)).data; + const symbols = (await httpGet(`${gatewayBaseUrl}/symbols`)); return symbols.map((product: { product_id: number }) => product.product_id); }; const fetchProducts = async (): Promise => { const validSymbols = await fetchValidSymbols(); - const allProducts = ( - await axios.get(`${gatewayBaseUrl}/query?type=all_products`) - ).data.data; + const allProducts = (await httpGet(`${gatewayBaseUrl}/query?type=all_products`)).data; return { spot_products: allProducts.spot_products .map((product: { product_id: number }) => product.product_id) @@ -42,7 +34,7 @@ const fetchProducts = async (): Promise => { const computeVolume = async (timestamp: number, productIds: number[]) => { const snapshots = ( - await axios.post(archiveBaseUrl, { + await httpPost(archiveBaseUrl, { market_snapshots: { interval: { count: 2, @@ -52,7 +44,7 @@ const computeVolume = async (timestamp: number, productIds: number[]) => { product_ids: productIds, }, }) - ).data.snapshots; + ).snapshots; const lastCumulativeVolumes: Record = snapshots[0].cumulative_volumes; const prevCumulativeVolumes: Record = diff --git a/dexs/wingriders/index.ts b/dexs/wingriders/index.ts index 83efe3f211..d677dfe252 100644 --- a/dexs/wingriders/index.ts +++ b/dexs/wingriders/index.ts @@ -1,21 +1,21 @@ -import axios from "axios" import BigNumber from "bignumber.js"; import { Adapter } from "../../adapters/types" import { CHAIN } from "../../helpers/chains"; import { getPrices } from "../../utils/prices"; +import { httpPost } from "../../utils/fetchURL"; const volUrl = 'https://aggregator.mainnet.wingriders.com/volumeInAda'; async function fetchVolume(timestamp: number) { - const last24hVolInAda = await axios.post(volUrl, { "lastNHours": 24 }); - const totalVolumeInAda = await axios.post(volUrl); + const last24hVolInAda = await httpPost(volUrl, { "lastNHours": 24 }); + const totalVolumeInAda = await httpPost(volUrl, {}); const coinId = "coingecko:cardano"; const prices = await getPrices([coinId], timestamp) const adaPrice = prices[coinId].price; - const dailyVolume = (new BigNumber(last24hVolInAda.data).multipliedBy(adaPrice)).toString(); - const totalVolume = (new BigNumber(totalVolumeInAda.data).multipliedBy(adaPrice)).toString(); + const dailyVolume = (new BigNumber(last24hVolInAda).multipliedBy(adaPrice)).toString(); + const totalVolume = (new BigNumber(totalVolumeInAda).multipliedBy(adaPrice)).toString(); return { dailyVolume, diff --git a/dexs/zeta/index.ts b/dexs/zeta/index.ts index fa2217456c..e2c02a6eac 100644 --- a/dexs/zeta/index.ts +++ b/dexs/zeta/index.ts @@ -1,11 +1,11 @@ -import axios from "axios"; import { CHAIN } from "../../helpers/chains"; +import { httpGet } from "../../utils/fetchURL"; const volumeEndpoint = "https://api.zeta.markets/global/stats/"; async function fetch() { - const volumeResponse = await axios.get(volumeEndpoint); - const volume24h = volumeResponse.data.volume_24h; + const volumeResponse = await httpGet(volumeEndpoint); + const volume24h = volumeResponse.volume_24h; return { dailyVolume: Math.round(volume24h), diff --git a/fees/aimbot.ts b/fees/aimbot.ts index c20e8529f8..fd5cd6abe2 100644 --- a/fees/aimbot.ts +++ b/fees/aimbot.ts @@ -3,7 +3,7 @@ import { FetchResultFees, SimpleAdapter } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; import { getTimestampAtStartOfDayUTC } from "../utils/date"; import { getPrices } from "../utils/prices"; -const axios = require('axios'); +import { httpGet } from "../utils/fetchURL"; const profitShareAPI = "https://aimbotapi.onrender.com/api/openBot/profitShare"; interface IData { @@ -43,18 +43,9 @@ const fetch = async (timestamp: number): Promise => { }).reduce((a: number, b: number) => a+b,0); // fetch profit data from OpenBot profitShare API - const openBotFundData = await axios.get(profitShareAPI, (error:Error, response:any, body:string)=> { - if (!error && response.statusCode === 200) { - const res = JSON.parse(body); - console.log("Got a response: ", res); - return res; - } else { - console.log("Got an error: ", error, ", status code: ", response.statusCode); - return ""; - } - }); + const openBotFundData = await httpGet(profitShareAPI); - const openBotFundAmount = openBotFundData.data['total']; + const openBotFundAmount = openBotFundData['total']; const totalAmount = amount + openBotFundAmount; const ethAddress = "ethereum:0x0000000000000000000000000000000000000000"; diff --git a/fees/allbridge-classic.ts b/fees/allbridge-classic.ts index ce44fc5f13..872e62ab97 100644 --- a/fees/allbridge-classic.ts +++ b/fees/allbridge-classic.ts @@ -24,7 +24,7 @@ const getFeesFunction = (chain: Chain) => { const dailyFees = await getFees(chainCode, dateString, dateString); const dailyRevenue = dailyFees * 0.2; const dailySupplySideRevenue = dailyFees * 0.8; - console.log(`Fees for ${chain} on : ${dailyFees}`) + // console.log(`Fees for ${chain} on : ${dailyFees}`) return { timestamp, dailyFees: dailyFees !== undefined ? String(dailyFees) : undefined, diff --git a/fees/allbridge-core.ts b/fees/allbridge-core.ts index 7cd0b281d4..755d9b3dd7 100644 --- a/fees/allbridge-core.ts +++ b/fees/allbridge-core.ts @@ -5,8 +5,7 @@ import { getBlock } from "../helpers/getBlock"; import { ethers } from "ethers"; import * as sdk from "@defillama/sdk"; import { getPrices } from "../utils/prices"; -import axios from 'axios'; -import { getTimestampAtStartOfDayUTC } from "../utils/date"; +import { httpGet } from "../utils/fetchURL"; type TChainAddress = { [s: Chain | string]: string[]; @@ -131,8 +130,8 @@ const fetchFeesTron = async (chain: Chain, timestamp: number): Promise = const tronRpc = `https://api.trongrid.io` const getTronLogs = async (address: string, eventName: string, minBlockTimestamp: number, maxBlockTimestamp: number) => { const url = `${tronRpc}/v1/contracts/${address}/events?event_name=${eventName}&min_block_timestamp=${minBlockTimestamp}&max_block_timestamp=${maxBlockTimestamp}&limit=200`; - const res = await axios.get(url, {}); - return res.data.data; + const res = await httpGet(url); + return res.data; } const fetch = (chain: Chain) => { diff --git a/fees/apollox/index.ts b/fees/apollox/index.ts index 204b4e2ab3..7135baa1f4 100644 --- a/fees/apollox/index.ts +++ b/fees/apollox/index.ts @@ -1,7 +1,7 @@ -import axios from "axios"; -import {Adapter, ChainEndpoints} from "../../adapters/types"; -import {CHAIN} from "../../helpers/chains"; -import {Chain} from "@defillama/sdk/build/general"; +import { Adapter, ChainEndpoints } from "../../adapters/types"; +import { CHAIN } from "../../helpers/chains"; +import { Chain } from "@defillama/sdk/build/general"; +import { httpGet } from "../../utils/fetchURL"; const endpoints = { [CHAIN.BSC]: "https://www.apollox.finance/bapi/futures/v1/public/future/apx/fee/all", @@ -10,7 +10,7 @@ const endpoints = { const request = (urls: ChainEndpoints) => { return (chain: Chain) => { return async (timestamp: number) => { - const { data } = (await axios.get(urls[chain])).data; + const { data } = await httpGet(urls[chain]); const { alpFeeVOFor24Hour, allAlpFeeVO } = data return { timestamp, diff --git a/fees/avalanche.ts b/fees/avalanche.ts index 6c806253c7..709f2b8d03 100644 --- a/fees/avalanche.ts +++ b/fees/avalanche.ts @@ -1,30 +1,14 @@ import { Adapter, ProtocolType } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import axios from 'axios'; import { getTimestampAtStartOfDayUTC, getTimestampAtStartOfNextDayUTC } from "../utils/date"; import { getPrices } from "../utils/prices"; import { getBlock } from "../helpers/getBlock"; +import * as sdk from "@defillama/sdk"; -interface IFees { - timestamp: number; - value: number; -} - -interface IResult { - result: string; -} - const getBalance = async (block: string): Promise => { - const res: IResult = (await axios.post('https://api.avax.network/ext/bc/C/rpc', { - id: 1, - jsonrpc: '2.0', - method: 'eth_getBalance', - params: ['0x0100000000000000000000000000000000000000', block], - })).data; - - - return parseInt(res.result, 16) / 1e18; + const data = await sdk.api.eth.getBalance({ target: '0x0100000000000000000000000000000000000000', block: +block, chain: 'avax' }); + return +data.output / 1e18; }; @@ -36,10 +20,6 @@ const adapter: Adapter = { const endDatets = getTimestampAtStartOfNextDayUTC(timestamp) const [chainBlockToday, chainBlockNextday] = await Promise.all([getBlock(ts, "avax", {}), getBlock(endDatets, "avax", {})]); const [balanceToday, balanceNextday] = await Promise.all([getBalance(`0x${chainBlockToday.toString(16)}`), getBalance(`0x${chainBlockNextday.toString(16)}`)]); - // const a = `https://metrics.avax.network/v1/gas_used/mainnet?from=${ts}&to=${endDatets}&interval=day` - // const data: any[] = (await axios.get(a)).data.results - // const dailyFees: IFees = data.find(e => e.timestamp === ts) - // const avaxChainFee = dailyFees.value/1e9; const txFees = balanceNextday - balanceToday; const pricesObj = await getPrices(["coingecko:avalanche-2"], ts); const dailyFee = ((txFees) * pricesObj["coingecko:avalanche-2"].price); diff --git a/fees/cardano.ts b/fees/cardano.ts index 8c00a15863..68f3c51c22 100644 --- a/fees/cardano.ts +++ b/fees/cardano.ts @@ -1,10 +1,9 @@ -import axios from "axios"; import { Adapter, FetchResult, ProtocolType } from "../adapters/types"; import { IDate } from "../helpers/bitqueryFees"; import { CHAIN } from "../helpers/chains"; -import { getUniqStartOfTodayTimestamp } from "../helpers/getUniSubgraphVolume"; import { getTimestampAtStartOfDayUTC, getTimestampAtStartOfNextDayUTC } from "../utils/date"; import { getPrices } from "../utils/prices"; +import { httpPost } from "../utils/fetchURL"; interface ITxAda { @@ -31,7 +30,7 @@ const adapterQuery = async (form: string, till: string, network: string): Promis }); const headers = {"X-API-KEY": process.env.BIT_QUERY_API_KEY || '', "Content-Type": "application/json"}; - const result: ITxAda[] = (await axios.post("https://graphql.bitquery.io", body, { headers: headers }))?.data?.data.cardano.transactions; + const result: ITxAda[] = (await httpPost("https://graphql.bitquery.io", body, { headers: headers }))?.data.cardano.transactions; return result; } diff --git a/fees/chimpexchange.ts b/fees/chimpexchange.ts index 43de71290f..f2ab36e4fe 100644 --- a/fees/chimpexchange.ts +++ b/fees/chimpexchange.ts @@ -59,7 +59,6 @@ const graphs = (graphUrls: ChainEndpoints) => { (p: IPool) => p.id.split("-")[0] === e.id.split("-")[0] )?.swapFees || 0 ); - console.log("yesterdayValue", yesterdayValue.toNumber()); if (yesterdayValue.toNumber() === 0) return new BigNumber("0"); return new BigNumber(e.swapFees).minus(yesterdayValue); }) diff --git a/fees/firebird-finance.ts b/fees/firebird-finance.ts index 857ba929b3..b8669404f5 100644 --- a/fees/firebird-finance.ts +++ b/fees/firebird-finance.ts @@ -1,7 +1,7 @@ import { Adapter, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; import { getTimestampAtStartOfPreviousDayUTC } from "../utils/date"; -import axios from "axios"; +import { httpGet } from "../utils/fetchURL"; const endpoint = "https://api-be.firebird.finance/v1/hyper/fees/info"; @@ -15,11 +15,9 @@ interface FeeInfoResponse { const fetch = (chainId: number) => { return async (timestamp: number): Promise => { const dayTimestamp = getTimestampAtStartOfPreviousDayUTC(timestamp); - const response = await axios.get( - `${endpoint}?chain_id=${chainId}&day=${dayTimestamp}` - ); - const dailyFees = response.data.data.dailyFees.toString(); - const totalFees = response.data.data.totalFees.toString(); + const response: FeeInfoResponse = await httpGet( `${endpoint}?chain_id=${chainId}&day=${dayTimestamp}` ); + const dailyFees = response.data.dailyFees.toString(); + const totalFees = response.data.totalFees.toString(); return { timestamp: dayTimestamp, dailyFees, diff --git a/fees/frax-fpi.ts b/fees/frax-fpi.ts index 12cc042780..eececda1d9 100644 --- a/fees/frax-fpi.ts +++ b/fees/frax-fpi.ts @@ -1,8 +1,8 @@ import { Adapter, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../helpers/getUniSubgraphVolume"; -import axios from "axios"; import { getTimestampAtStartOfNextDayUTC } from "../utils/date"; +import { httpGet } from "../utils/fetchURL"; const endpoint = (year: number, month: number) => `https://api.frax.finance/v2/fpifpis/income-expense/detail?year=${year}&month=${month}`; interface IFPI { @@ -18,7 +18,7 @@ const fetch = async (timestamp: number) => { const date = new Date((timestamp * 1000)); const month = date.getMonth() + 1; const year = date.getFullYear(); - const response: IFPI[] = (await axios.get(endpoint(year, month)))?.data?.details; + const response: IFPI[] = (await httpGet(endpoint(year, month)))?.details; const historical = response.filter((e:IFPI) => e.chain === 'ethereum'); const dailyData = historical .filter((p: IFPI) => p.timestampSec >= dayTimestamp) diff --git a/fees/fwx/index.ts b/fees/fwx/index.ts index 454067c867..c2491c2143 100644 --- a/fees/fwx/index.ts +++ b/fees/fwx/index.ts @@ -2,7 +2,7 @@ import { Chain } from "@defillama/sdk/build/general"; import { Adapter, FetchResultFees } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; +import { httpPost } from "../../utils/fetchURL"; interface IEndpoint { dailyFee: string; @@ -40,10 +40,8 @@ const fetch = (chain: Chain) => { const formattedDate = date.toISOString().replace(/\.(\d{3})Z$/, ".$1Z"); // * call api for daily fee data - const dailyFeeRes = await axios.post(endpoints[chain].dailyFee, { - date: formattedDate, - }); - const dailyFeeData = dailyFeeRes.data as IDailyFeeData; + const dailyFeeRes = await httpPost(endpoints[chain].dailyFee, { date: formattedDate, }); + const dailyFeeData = dailyFeeRes as IDailyFeeData; const tokenInterestProfit = parseFloat(dailyFeeData.tokenInterestProfit); const dailyHoldersRevenue = 9 * tokenInterestProfit; const dailyFees = @@ -52,10 +50,8 @@ const fetch = (chain: Chain) => { const dailyRevenueString = dailyProtocolRevenueString; // * call api for total fee data - const companyRevenueRes = await axios.post( - endpoints[chain].realtimeCompanyRevenue - ); - const companyRevenue = companyRevenueRes.data as ICompanyRevenue; + const companyRevenueRes = await httpPost(endpoints[chain].realtimeCompanyRevenue, {}); + const companyRevenue = companyRevenueRes as ICompanyRevenue; const totalFee = parseFloat(companyRevenue.totalRevenue) + 9 * parseFloat(companyRevenue.totalInterestProfit); diff --git a/fees/ghostmarket/index.ts b/fees/ghostmarket/index.ts index 3fe2739691..dbdc619e4d 100644 --- a/fees/ghostmarket/index.ts +++ b/fees/ghostmarket/index.ts @@ -1,6 +1,5 @@ import { Adapter, DISABLED_ADAPTER_KEY } from "../../adapters/types"; import type { ChainEndpoints } from "../../adapters/types" -import { Chain } from '@defillama/sdk/build/general'; import { getTimestampAtStartOfDayUTC } from "../../utils/date"; import fetchURL from "../../utils/fetchURL"; import { CHAIN } from "../../helpers/chains"; diff --git a/fees/goku-money/index.ts b/fees/goku-money/index.ts index 36008824b9..d92da15b37 100644 --- a/fees/goku-money/index.ts +++ b/fees/goku-money/index.ts @@ -4,8 +4,8 @@ import { CHAIN } from "../../helpers/chains"; import { getBlock } from "../../helpers/getBlock"; import { Chain } from "@defillama/sdk/build/general"; import { ethers } from "ethers"; -import axios, { AxiosResponse } from "axios"; import BigNumber from "bignumber.js"; +import { httpGet } from "../../utils/fetchURL"; interface DexScreenerResponse { pairs: { @@ -103,10 +103,8 @@ type PYTH_CONFIG_KEYS = keyof PYTH_CONFIG_TYPE; const fetchGAIPrice = async () => { try { - const response: AxiosResponse = await axios.get( - "https://api.dexscreener.com/latest/dex/tokens/0xcd91716ef98798A85E79048B78287B13ae6b99b2" - ); - return response.data.pairs[0].priceUsd ?? "1"; + const response: DexScreenerResponse = await httpGet("https://api.dexscreener.com/latest/dex/tokens/0xcd91716ef98798A85E79048B78287B13ae6b99b2"); + return response.pairs[0].priceUsd ?? "1"; } catch (error) { return "1"; } @@ -119,14 +117,11 @@ const fetchPriceFeeds = async (): Promise> => { PYTH_PRICE_FEED_IDS.forEach((id) => params.append("ids[]", id)); - const response = await axios.get(`${baseURL}${resource}`, { - params: params, - }); - + const response: Feed[] = await httpGet(`${baseURL}${resource}`, { params: params, }); // price array to price map // key is price feed id // value is price in standard unit like $1.00 - return response.data.reduce((acc, curr) => { + return response.reduce((acc, curr) => { const adjustedPrice = Number(curr.price.price) * Math.pow(10, curr.price.expo); acc[curr.id] = adjustedPrice.toFixed(Math.abs(curr.price.expo)); diff --git a/fees/justlend.ts b/fees/justlend.ts index 8c67b78187..549ab7c09d 100644 --- a/fees/justlend.ts +++ b/fees/justlend.ts @@ -4,7 +4,7 @@ import * as sdk from "@defillama/sdk"; import { BigNumberish } from "ethers"; import { getPrices } from "../utils/prices"; import { fromHex, toHex } from "tron-format-address"; -import axios from "axios"; +import { httpGet } from "../utils/fetchURL"; interface IPrices { [address: string]: { @@ -189,8 +189,8 @@ const getMarketDetails = async (markets: string[], chain: CHAIN): Promise<{under const endpoint = `https://api.trongrid.io` const getLogs = async (address: string, min_block_timestamp: number, max_block_timestamp: number) => { const url = `${endpoint}/v1/contracts/${fromHex(address)}/events?event_name=AccrueInterest&min_block_timestamp=${min_block_timestamp}&max_block_timestamp=${max_block_timestamp}&limit=200`; - const res = await axios.get(url, {}); - return res.data.data; + const res = await httpGet(url); + return res.data; } const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); diff --git a/fees/liquid-bolt.ts b/fees/liquid-bolt.ts index d931771937..d0b95a521a 100644 --- a/fees/liquid-bolt.ts +++ b/fees/liquid-bolt.ts @@ -1,6 +1,6 @@ import { CHAIN } from "../helpers/chains"; import { BaseAdapter, Adapter } from "../adapters/types"; -import axios from "axios"; +import { httpGet } from "../utils/fetchURL"; const methodology = { UserFees: "20% performance fees on the arbitrage revenue", @@ -15,7 +15,7 @@ let cachedData: any = undefined; const getData = async () => { if (cachedData === undefined) { - cachedData = (await axios.get('https://stats.liquidbolt.finance/defillama-liquidbolt.json')).data; + cachedData = (await httpGet('https://stats.liquidbolt.finance/defillama-liquidbolt.json')); } return cachedData; } diff --git a/fees/near/index.ts b/fees/near/index.ts index 3529e3d7f2..25bcd51839 100644 --- a/fees/near/index.ts +++ b/fees/near/index.ts @@ -1,7 +1,7 @@ -import axios from "axios"; import { FetchResultFees, ProtocolType, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; interface IChart { date: string; txn_fee_usd: string; @@ -9,12 +9,12 @@ interface IChart { const fetchFees = async (timestamp: number): Promise => { const todayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)); const url = 'https://api.nearblocks.io/v1/charts' - const value: IChart[] = (await axios.get(url, { + const value: IChart[] = (await httpGet(url, { headers: { 'origin': 'https://nearblocks.io', 'referer': 'https://nearblocks.io/charts' , 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36' - }})).data.charts; + }})).charts; // const value: IChart[] = require('./near.json'); const dateStr = new Date(todayTimestamp * 1000).toISOString().split('T')[0]; diff --git a/fees/sns.ts b/fees/sns.ts index ceae6fc206..a044eeb41f 100644 --- a/fees/sns.ts +++ b/fees/sns.ts @@ -1,7 +1,7 @@ -import axios from "axios"; import { Adapter, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; import { getTimestampAtStartOfDayUTC } from "../utils/date"; +import { httpGet } from "../utils/fetchURL"; const API_URL = "https://sns-api.bonfida.com/v2/defilama/fees-adapter"; @@ -14,7 +14,7 @@ const fetch = async (timestamp: number): Promise => { const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp); const url = `${API_URL}?from=${todaysTimestamp}&to=${timestamp}`; - const { data }: { data: IData } = await axios.get(url); + const data: IData = await httpGet(url); return { timestamp: todaysTimestamp, dailyFees: data.daily_fees.toString(), diff --git a/fees/solana.ts b/fees/solana.ts index 4a87033194..d8b3ebe677 100644 --- a/fees/solana.ts +++ b/fees/solana.ts @@ -1,6 +1,5 @@ import { Adapter, ProtocolType } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import axios from 'axios'; import { getTimestampAtStartOfDayUTC } from "../utils/date"; import { getPrices } from "../utils/prices"; import { queryDune } from "../helpers/dune"; diff --git a/fees/stride.ts b/fees/stride.ts index e9544134fe..557ffe25c9 100644 --- a/fees/stride.ts +++ b/fees/stride.ts @@ -1,6 +1,6 @@ import { Adapter, FetchResult } from "../adapters/types"; -import axios from "axios"; import { CHAIN } from "../helpers/chains"; +import { httpGet } from "../utils/fetchURL"; interface DailyFeeResponse { fees: { @@ -16,14 +16,12 @@ const chainOverrides: { [key: string]: string } = { const fetch = (chain: string) => { return async (timestamp: number): Promise => { const overriddenChain = chainOverrides[chain] || chain; // Override if exists, else use original - const response = await axios.get( - `https://edge.stride.zone/api/${overriddenChain}/stats/fees` - ); + const response: DailyFeeResponse = await httpGet(`https://edge.stride.zone/api/${overriddenChain}/stats/fees`); return { timestamp: timestamp, - dailyFees: String(response.data.fees.dailyFees), - dailyRevenue: String(response.data.fees.dailyRevenue), + dailyFees: String(response.fees.dailyFees), + dailyRevenue: String(response.fees.dailyRevenue), }; }; }; @@ -69,24 +67,24 @@ const adapter: Adapter = { start: 0, meta, }, - evmos: { - fetch: fetch("evmos"), - runAtCurrTime: true, - start: 0, - meta, - }, - injective: { - fetch: fetch("injective"), - runAtCurrTime: true, - start: 0, - meta, - }, - umee: { - fetch: fetch("umee"), - runAtCurrTime: true, - start: 0, - meta, - }, + evmos: { + fetch: fetch("evmos"), + runAtCurrTime: true, + start: 0, + meta, + }, + injective: { + fetch: fetch("injective"), + runAtCurrTime: true, + start: 0, + meta, + }, + umee: { + fetch: fetch("umee"), + runAtCurrTime: true, + start: 0, + meta, + }, }, }; diff --git a/fees/surfone/index.ts b/fees/surfone/index.ts index 0e09d485f8..7daeade8d5 100644 --- a/fees/surfone/index.ts +++ b/fees/surfone/index.ts @@ -1,7 +1,7 @@ import { SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import axios from "axios"; +import { httpGet } from "../../utils/fetchURL"; const volumeEndpoint = "https://apigateway.surf.one/pool/24h/data" @@ -16,9 +16,9 @@ interface IVolume { const fetch = () => { return async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const response = (await axios.get(volumeEndpoint, { headers })); + const response = (await httpGet(volumeEndpoint, { headers })); - const volume: IVolume = response.data.data; + const volume: IVolume = response.data; return { dailyFees: `${volume?.totalTradeFee || undefined}`, timestamp: dayTimestamp, diff --git a/fees/thorswap/index.ts b/fees/thorswap/index.ts index 2836756913..783fff94e1 100644 --- a/fees/thorswap/index.ts +++ b/fees/thorswap/index.ts @@ -1,7 +1,7 @@ -import axios from "axios" import { FetchResultFees, SimpleAdapter } from "../../adapters/types" import { CHAIN } from "../../helpers/chains" import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; +import { httpGet } from "../../utils/fetchURL"; interface IRevenue { DAY: string; @@ -49,10 +49,10 @@ const fetchFees = async (timestamp: number): Promise => { const url2 = "https://api.flipsidecrypto.com/api/v2/queries/46dc4fa4-a362-420e-97ec-d3a58d46b9e7/data/latest" const url3 = `https://midgard.ninerealms.com/v2/history/earnings?interval=day&count=400` const [reveune, fees, earnings]: any = (await Promise.all([ - axios.get(url1), - axios.get(url2), - axios.get(url3, { headers: {"x-client-id": "defillama"}}) - ])).map(res => res.data) + httpGet(url1), + httpGet(url2), + httpGet(url3, { headers: {"x-client-id": "defillama"}}) + ])) const reveuneData: IRevenue[] = reveune; const feesData: IFees[] = fees; diff --git a/fees/tigris/index.ts b/fees/tigris/index.ts index a6ccbe9349..da0d043d63 100644 --- a/fees/tigris/index.ts +++ b/fees/tigris/index.ts @@ -1,7 +1,7 @@ import { Chain } from "@defillama/sdk/build/general"; import { Adapter, FetchResultFees } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import axios from 'axios'; +import { httpGet } from "../../utils/fetchURL"; const API_ENDPOINT = "https://flask.tigristrade.info"; @@ -19,45 +19,36 @@ interface ApiResponse { } const fetchFromAPI = async (chain: Chain, timestamp: number): Promise => { - try { - let endpoint; - if (chain === CHAIN.POLYGON) { - endpoint = "/fetch-polygon-data"; - } else if (chain === CHAIN.ARBITRUM) { - endpoint = "/fetch-arbitrum-data"; - } else { - throw new Error(`Unsupported chain: ${chain}`); - } - - const response = await axios.get(`${API_ENDPOINT}${endpoint}`, { - params: { - chain: chain, - timestamp: timestamp - } - }); + let endpoint; + if (chain === CHAIN.POLYGON) { + endpoint = "/fetch-polygon-data"; + } else if (chain === CHAIN.ARBITRUM) { + endpoint = "/fetch-arbitrum-data"; + } else { + throw new Error(`Unsupported chain: ${chain}`); + } - if (response.status !== 200) { - throw new Error("Failed to fetch data from the API"); + const response = await httpGet(`${API_ENDPOINT}${endpoint}`, { + params: { + chain: chain, + timestamp: timestamp } + }); - return response.data; - } catch (error) { - console.error("Error fetching from the API:", error); - throw error; - } + return response; } function startOfDayTimestamp(timestamp: number): number { - const date = new Date(timestamp * 1000); - date.setUTCHours(0, 0, 0, 0); - return Math.floor(date.getTime() / 1000); + const date = new Date(timestamp * 1000); + date.setUTCHours(0, 0, 0, 0); + return Math.floor(date.getTime() / 1000); } const fetch = (chain: Chain) => { return async (timestamp: number): Promise => { try { const dataPoints = await fetchFromAPI(chain, timestamp); - + const adjustedTimestamp = startOfDayTimestamp(timestamp); const matchingData = dataPoints.find(e => e.day === adjustedTimestamp); diff --git a/fees/tron.ts b/fees/tron.ts index 1c92131294..63dc77857c 100644 --- a/fees/tron.ts +++ b/fees/tron.ts @@ -1,8 +1,8 @@ import { Adapter, ProtocolType } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import axios from 'axios'; import { getTimestampAtStartOfDayUTC } from "../utils/date"; import { getPrices } from "../utils/prices"; +import { httpGet } from "../utils/fetchURL"; const adapter: Adapter = { adapter: { @@ -10,8 +10,8 @@ const adapter: Adapter = { fetch: async (timestamp: number) => { const ts = getTimestampAtStartOfDayUTC(timestamp) const today = new Date(ts * 1000).toISOString().substring(0, "2022-11-03".length) - const dailyFees = await axios.get(`https://apilist.tronscanapi.com/api/turnover?size=1000&start=1575158400000&end=${Date.now()}&type=0`); - const trxFeesToday = dailyFees.data.data.find((d:any)=>d.day===today) + const dailyFees = await httpGet(`https://apilist.tronscanapi.com/api/turnover?size=1000&start=1575158400000&end=${Date.now()}&type=0`); + const trxFeesToday = dailyFees.data.find((d:any)=>d.day===today) const pricesObj = await getPrices(["coingecko:tron"], ts); const usdFees = (trxFeesToday.total_trx_burn*pricesObj["coingecko:tron"].price).toString() // excludes trx burned for USDD diff --git a/fees/vertex-protocol.ts b/fees/vertex-protocol.ts index 0c0284848b..cf05b2b514 100644 --- a/fees/vertex-protocol.ts +++ b/fees/vertex-protocol.ts @@ -1,6 +1,6 @@ -import axios from "axios"; import { CHAIN } from "../helpers/chains"; import { Adapter, FetchResultFees } from "../adapters/types"; +import { httpPost } from "../utils/fetchURL"; interface MarketSnapshots { interval: { @@ -37,8 +37,8 @@ const query = async (max_time: number): Promise => { }; const archiveBaseUrl = "https://archive.prod.vertexprotocol.com/v1"; - const response = await axios.post(archiveBaseUrl, body); - return response.data; + const response = await httpPost(archiveBaseUrl, body); + return response; }; const sumAllProductStats = (stat_map: IData): number => { diff --git a/fees/xoxno/index.ts b/fees/xoxno/index.ts index 0092e286d0..1891a0e810 100644 --- a/fees/xoxno/index.ts +++ b/fees/xoxno/index.ts @@ -1,6 +1,6 @@ -import axios from "axios" import { FetchResultFees, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; +import { httpGet } from "../../utils/fetchURL"; interface IRaw { XO_RoyaltiesPaidUSD: number; @@ -14,11 +14,11 @@ const fetchFees = async (timestamp: number): Promise => { const toDayTime = new Date(timestamp * 1000).toISOString().split("T")[0]; const toDayTimeStr = new Date(beforeTime * 1000).toISOString().split("T")[0]; const url = `https://api.xoxno.com/getMarketplaceVolume?after=${startTimeStr}&before=${toDayTimeStr}&bin=1d`; - const response: IRaw[] = (await axios.get(url, { + const response: IRaw[] = (await httpGet(url, { headers: { origin: 'https://xoxno.com', } - })).data; + })); // const fs = require('fs'); // fs.writeFileSync('./xoxno.json', JSON.stringify(response)); // const response: IRaw[] = require('./xoxno.json'); diff --git a/helpers/allium.ts b/helpers/allium.ts index d133673966..5fa16d8e66 100644 --- a/helpers/allium.ts +++ b/helpers/allium.ts @@ -1,6 +1,6 @@ -import axios, { AxiosResponse } from "axios" import retry from "async-retry"; import { IJSON } from "../adapters/types"; +import { httpGet, httpPost } from "../utils/fetchURL"; const token = {} as IJSON @@ -10,7 +10,7 @@ const HEADERS = { }; export async function startAlliumQuery(sqlQuery: string) { - const query = await axios.post(`https://api.allium.so/api/v1/explorer/queries/phBjLzIZ8uUIDlp0dD3N/run-async`, { + const query = await httpPost(`https://api.allium.so/api/v1/explorer/queries/phBjLzIZ8uUIDlp0dD3N/run-async`, { parameters: { fullQuery: sqlQuery } @@ -18,14 +18,14 @@ export async function startAlliumQuery(sqlQuery: string) { headers: HEADERS }) - return query.data["run_id"] + return query["run_id"] } export async function retrieveAlliumResults(queryId: string) { - const results = await axios.get(`https://api.allium.so/api/v1/explorer/query-runs/${queryId}/results?f=json`, { + const results = await httpGet(`https://api.allium.so/api/v1/explorer/query-runs/${queryId}/results?f=json`, { headers: HEADERS }) - return results.data.data + return results.data } export async function queryAllium(sqlQuery: string) { @@ -44,19 +44,13 @@ export async function queryAllium(sqlQuery: string) { throw new Error("Couldn't get a token from allium") } - const statusReq = await axios.get(`https://api.allium.so/api/v1/explorer/query-runs/${token[sqlQuery]}/status`, { + const statusReq = await httpGet(`https://api.allium.so/api/v1/explorer/query-runs/${token[sqlQuery]}/status`, { headers: HEADERS }) - const status = statusReq.data + const status = statusReq if (status === "success") { - try{ - const results = await retrieveAlliumResults(token[sqlQuery]) - return results - } catch(e){ - console.log("query result", e) - throw e - } + return retrieveAlliumResults(token[sqlQuery]) } else if (status === "failed") { console.log(`Query ${sqlQuery} failed`, statusReq.data) bail(new Error(`Query ${sqlQuery} failed, error ${JSON.stringify(statusReq.data)}`)) diff --git a/helpers/bitqueryFees.ts b/helpers/bitqueryFees.ts index 12abc89897..8979b425b5 100644 --- a/helpers/bitqueryFees.ts +++ b/helpers/bitqueryFees.ts @@ -1,4 +1,4 @@ -import axios from "axios"; +import { httpPost } from "../utils/fetchURL"; interface IDate { date: string; // ex. 2022-01-01 @@ -30,7 +30,7 @@ const adapterBitqueryFeesEthereumNetwork = async (form: string, till: string, ne }); const headers = {"X-API-KEY": process.env.BIT_QUERY_API_KEY || '', "Content-Type": "application/json"}; - const result: ITx[] = (await axios.post("https://graphql.bitquery.io", body, { headers: headers }))?.data?.data.ethereum.transactions; + const result: ITx[] = (await httpPost("https://graphql.bitquery.io", body, { headers: headers }))?.data.ethereum.transactions; return result; } diff --git a/helpers/blockscoutFees.ts b/helpers/blockscoutFees.ts index 22ab0bfa4c..5622a7f5d7 100644 --- a/helpers/blockscoutFees.ts +++ b/helpers/blockscoutFees.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { getTimestampAtStartOfDayUTC } from "../utils/date"; import { getPrices } from "../utils/prices"; import { Adapter, ProtocolType } from "../adapters/types"; +import { httpGet } from '../utils/fetchURL'; export function blockscoutFeeAdapter(chain:string, url:string, coin:string){ const adapter: Adapter = { @@ -10,9 +10,9 @@ export function blockscoutFeeAdapter(chain:string, url:string, coin:string){ fetch: async (timestamp: number) => { const ts = getTimestampAtStartOfDayUTC(timestamp) const date = new Date(ts*1000).toISOString().slice(0, "2011-10-05".length) - const fees = await axios.get(`${url}&date=${date}`) + const fees = await httpGet(`${url}&date=${date}`) const prices = await getPrices([coin], timestamp); - const usdFees = Number(fees.data.result)/1e18*prices[coin].price + const usdFees = Number(fees.result)/1e18*prices[coin].price return { timestamp, diff --git a/helpers/dexVolumeLogs.ts b/helpers/dexVolumeLogs.ts index 644bae3cc4..f05373ed09 100644 --- a/helpers/dexVolumeLogs.ts +++ b/helpers/dexVolumeLogs.ts @@ -142,7 +142,7 @@ export async function getDexVolumeFeeV3({ chain, fromTimestamp, toTimestamp, fac // const token0s = await api.multiCall({ abi: 'address:token0', calls: pools! }) let fees = [] as any if (isFee) - fees = await api.multiCall({ abi: 'uint24:fee', calls: pools! }) + fees = await api.multiCall({ abi: 'function fee() view returns (uint24)', calls: pools! }) const token1s = await api.multiCall({ abi: 'address:token1', calls: pools! }) const logs = await sdk.getEventLogs({ diff --git a/helpers/etherscanFees.ts b/helpers/etherscanFees.ts index 48ecca1832..3b870d92e8 100644 --- a/helpers/etherscanFees.ts +++ b/helpers/etherscanFees.ts @@ -1,18 +1,18 @@ -import axios from 'axios'; import { getTimestampAtStartOfDayUTC } from "../utils/date"; import { getPrices } from "../utils/prices"; import { Adapter, ProtocolType } from "../adapters/types"; +import { httpPost } from '../utils/fetchURL'; export async function getEtherscanFees(timestamp: number, url:string, coin:string) { const ts = getTimestampAtStartOfDayUTC(timestamp) - const dailyFees = await axios.post(url, { responseType: 'blob', headers: { + const dailyFees = await httpPost(url, { responseType: 'blob', headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", "Content-Type": "text/csv; charset=utf-8", "Accept" : "text/csv; charset=utf-8", "origin": url, }}); - const feesToday = dailyFees.data?.split("\n").find((d: any) => d?.split(",")?.[1]?.slice(1, -1) == ts) + const feesToday = dailyFees.split("\n").find((d: any) => d?.split(",")?.[1]?.slice(1, -1) == ts) const pricesObj = await getPrices([coin], ts); return Number(feesToday?.split(",")[2].slice(1, -2)) / 1e18 * pricesObj[coin].price } diff --git a/helpers/flipsidecrypto.ts b/helpers/flipsidecrypto.ts index fd2809f331..108437aeb3 100644 --- a/helpers/flipsidecrypto.ts +++ b/helpers/flipsidecrypto.ts @@ -1,6 +1,6 @@ -import axios, { AxiosResponse } from "axios" import retry from "async-retry"; import { IJSON } from "../adapters/types"; +import { httpPost } from "../utils/fetchURL"; const token = {} as IJSON const FLIPSIDE_API_KEYS = process.env.FLIPSIDE_API_KEY?.split(',') ?? ["f3b65679-a179-4983-b794-e41cf40103ed"] @@ -23,10 +23,10 @@ async function _queryFlipside(sqlQuery: string, maxAgeMinutes: number = 90) { return await retry( async (bail, attempt: number) => { const FLIPSIDE_API_KEY = FLIPSIDE_API_KEYS[API_KEY_INDEX] - let query: undefined | AxiosResponse = undefined + let query: undefined | any = undefined if (!token[sqlQuery]) { try{ - query = await axios.post("https://api-v2.flipsidecrypto.xyz/json-rpc", + query = await httpPost("https://api-v2.flipsidecrypto.xyz/json-rpc", { "jsonrpc": "2.0", "method": "createQueryRun", @@ -49,7 +49,7 @@ async function _queryFlipside(sqlQuery: string, maxAgeMinutes: number = 90) { 'Content-Type': 'application/json' } }) - if(query?.data?.result?.queryRun?.id){ + if(query?.result?.queryRun?.id){ token[sqlQuery] = query?.data.result.queryRun.id } else { console.log("error query data", query?.data) @@ -78,7 +78,7 @@ async function _queryFlipside(sqlQuery: string, maxAgeMinutes: number = 90) { throw new Error("Couldn't get a token from flipsidecrypto") } - const queryStatus = await axios.post(`https://api-v2.flipsidecrypto.xyz/json-rpc`, { + const queryStatus = await httpPost(`https://api-v2.flipsidecrypto.xyz/json-rpc`, { "jsonrpc": "2.0", "method": "getQueryRun", "params": [ @@ -93,14 +93,14 @@ async function _queryFlipside(sqlQuery: string, maxAgeMinutes: number = 90) { } }) - const status = queryStatus.data.result.queryRun.state + const status = queryStatus.result.queryRun.state if (status === "QUERY_STATE_SUCCESS") { try { let fullRows:any[] = [] let pageNum = 1; let maxPages = 1; while(pageNum <= maxPages){ - const results = await axios.post(`https://api-v2.flipsidecrypto.xyz/json-rpc`, { + const results = await httpPost(`https://api-v2.flipsidecrypto.xyz/json-rpc`, { "jsonrpc": "2.0", "method": "getQueryRunResults", "params": [ @@ -119,12 +119,12 @@ async function _queryFlipside(sqlQuery: string, maxAgeMinutes: number = 90) { "x-api-key": FLIPSIDE_API_KEY } }) - if(results.data.result.rows === null){ + if(results.result.rows === null){ return [] // empty result } - pageNum = results.data.result.page.currentPageNumber + 1; - maxPages = results.data.result.page.totalPages; - fullRows = fullRows.concat(results.data.result.rows.map((t: any[]) => t.slice(0, -1))) + pageNum = results.result.page.currentPageNumber + 1; + maxPages = results.result.page.totalPages; + fullRows = fullRows.concat(results.result.rows.map((t: any[]) => t.slice(0, -1))) } return fullRows } catch (e) { @@ -137,7 +137,7 @@ async function _queryFlipside(sqlQuery: string, maxAgeMinutes: number = 90) { return []; // not returned but just there to make typescript happy } else if (status === "QUERY_STATE_RUNNING" && (attempt === MAX_RETRIES)) { console.log(`Flipside queryRunId ${token[sqlQuery]} still run will cancel!!`) - await axios.post(`https://api-v2.flipsidecrypto.xyz/json-rpc`, { + await httpPost(`https://api-v2.flipsidecrypto.xyz/json-rpc`, { "jsonrpc": "2.0", "method": "cancelQueryRun", "params": [ diff --git a/helpers/getBlock.ts b/helpers/getBlock.ts index 3af63682d9..25b070a63d 100644 --- a/helpers/getBlock.ts +++ b/helpers/getBlock.ts @@ -1,54 +1,61 @@ import { ChainBlocks } from "../adapters/types"; -import axios from "axios" import { providers } from "@defillama/sdk/build/general" import type { Chain } from "@defillama/sdk/build/general" import { CHAIN } from "./chains"; import * as sdk from "@defillama/sdk" +import { httpGet } from "../utils/fetchURL"; const retry = require("async-retry") async function getBlock(timestamp: number, chain: Chain, chainBlocks: ChainBlocks) { - if (chainBlocks[chain] !== undefined) { + if (chainBlocks[chain] !== undefined) return chainBlocks[chain] - } else { - let block: number | undefined = undefined - if (chain === CHAIN.CELO) - block = Number((await retry(async () => (await axios.get("https://explorer.celo.org/api?module=block&action=getblocknobytime×tamp=" + timestamp + "&closest=before").catch((e) => { - throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) - }))?.data?.result?.blockNumber))); - else if (chain === CHAIN.KAVA) - block = Number((await retry(async () => (await axios.get(`https://explorer.kava.io/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { - console.log(`Error getting block: ${chain} ${timestamp} ${e.message}`) - throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) - }))?.data?.result?.blockNumber))); - else if (chain === CHAIN.ONUS) - block = Number((await retry(async () => (await axios.get(`https://explorer.onuschain.io/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { - throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) - }))?.data?.result?.blockNumber))); - else if (chain as CHAIN === CHAIN.POLYGON_ZKEVM || chain === CHAIN.VISION || chain as CHAIN === CHAIN.ERA) - return sdk.api.util.lookupBlock(timestamp, { chain }).then((blockData: any) => blockData.block) // TODO after get block support chain polygon_zkevm then swith to use api https://coins.llama.fi/block - else if (chain as CHAIN === CHAIN.WAVES) - block = Number((await retry(async () => (await axios.get(`https://nodes.wavesnodes.com/blocks/heightByTimestamp/${timestamp}`).catch((e) => { - throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) - }))?.data?.height))); - else if (chain === CHAIN.BASE) - block = Number((await retry(async () => (await axios.get(`https://base.blockscout.com/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { - throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) - }))?.data?.result?.blockNumber))); - else if (chain === CHAIN.SCROLL) - block = Number((await retry(async () => (await axios.get(`https://blockscout.scroll.io/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { - throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) - }))?.data?.result?.blockNumber))); - else - block = Number((await retry(async () => (await axios.get(`https://coins.llama.fi/block/${chain}/${timestamp}`).catch((e) => { - console.log(`Error getting block: ${chain} ${timestamp} ${e.message}`) - throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) - }))?.data?.height, { retries: 3 }))); - if (block) chainBlocks[chain] = block + + + + let block: number | undefined = await sdk.blocks.getBlockNumber(chain, timestamp) + if (block) { + chainBlocks[chain] = block return block - // https://base.blockscout.com - // https://explorer.kava.io - //return sdk.api.util.lookupBlock(timestamp, { chain }).then(blockData => blockData.block) } + + if (chain === CHAIN.CELO) + block = Number((await retry(async () => (await httpGet("https://explorer.celo.org/api?module=block&action=getblocknobytime×tamp=" + timestamp + "&closest=before").catch((e) => { + throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) + }))?.result?.blockNumber))); + else if (chain === CHAIN.KAVA) + block = Number((await retry(async () => (await httpGet(`https://explorer.kava.io/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { + console.log(`Error getting block: ${chain} ${timestamp} ${e.message}`) + throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) + }))?.result?.blockNumber))); + else if (chain === CHAIN.ONUS) + block = Number((await retry(async () => (await httpGet(`https://explorer.onuschain.io/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { + throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) + }))?.result?.blockNumber))); + else if (chain as CHAIN === CHAIN.POLYGON_ZKEVM || chain === CHAIN.VISION || chain as CHAIN === CHAIN.ERA) + return sdk.api.util.lookupBlock(timestamp, { chain }).then((blockData: any) => blockData.block) // TODO after get block support chain polygon_zkevm then swith to use api https://coins.llama.fi/block + else if (chain as CHAIN === CHAIN.WAVES) + block = Number((await retry(async () => (await httpGet(`https://nodes.wavesnodes.com/blocks/heightByTimestamp/${timestamp}`).catch((e) => { + throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) + }))?.height))); + else if (chain === CHAIN.BASE) + block = Number((await retry(async () => (await httpGet(`https://base.blockscout.com/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { + throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) + }))?.result?.blockNumber))); + else if (chain === CHAIN.SCROLL) + block = Number((await retry(async () => (await httpGet(`https://blockscout.scroll.io/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`).catch((e) => { + throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) + }))?.result?.blockNumber))); + else + block = Number((await retry(async () => (await httpGet(`https://coins.llama.fi/block/${chain}/${timestamp}`).catch((e) => { + console.log(`Error getting block: ${chain} ${timestamp} ${e.message}`) + throw new Error(`Error getting block: ${chain} ${timestamp} ${e.message}`) + }))?.height, { retries: 3 }))); + if (block) chainBlocks[chain] = block + return block + // https://base.blockscout.com + // https://explorer.kava.io + //return sdk.api.util.lookupBlock(timestamp, { chain }).then(blockData => blockData.block) + } async function getBlocks(chain: Chain, timestamps: number[]) { diff --git a/helpers/getChainFees.ts b/helpers/getChainFees.ts index 0a1c3898e7..915c50cff4 100644 --- a/helpers/getChainFees.ts +++ b/helpers/getChainFees.ts @@ -1,5 +1,5 @@ -import axios from 'axios'; import { getTimestampAtStartOfDayUTC, getTimestampAtStartOfPreviousDayUTC } from '../utils/date'; +import { httpGet } from '../utils/fetchURL'; export const chainAdapter = (adapterKey: string, assetID: string, startTime: number) => { const fetch = async (timestamp: number) => { @@ -25,8 +25,8 @@ export const chainAdapter = (adapterKey: string, assetID: string, startTime: num }; export const getOneDayFees = async (assetID: string, startDate: string, endDate: string) => { - const result = await axios.get(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=FeeTotUSD&assets=${assetID}&start_time=${startDate}&end_time=${endDate}`); - if (!result.data.data[0]) { + const result = await httpGet(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=FeeTotUSD&assets=${assetID}&start_time=${startDate}&end_time=${endDate}`); + if (!result.data[0]) { throw new Error(`Failed to fetch CoinMetrics data for ${assetID} on ${endDate}`); } diff --git a/nfts/chains.ts b/nfts/chains.ts index 40f9aec99d..c14e47bdca 100644 --- a/nfts/chains.ts +++ b/nfts/chains.ts @@ -1,11 +1,11 @@ import { queryAllium } from "../helpers/allium"; import { queryFlipside } from "../helpers/flipsidecrypto"; -import axios from "axios" +import { httpGet, httpPost } from "../utils/fetchURL"; async function sumPricedTokens(timestamp: number, data:any[], token_mapping: {[address:string]:string}){ - const prices = await axios.get(`https://coins.llama.fi/prices/historical/${timestamp}/${Object.values(token_mapping).map(k=>"coingecko:"+k).join(',')}`) + const prices = await httpGet(`https://coins.llama.fi/prices/historical/${timestamp}/${Object.values(token_mapping).map(k=>"coingecko:"+k).join(',')}`) const totalUsd = data.reduce((sum:number, token:any)=>{ - const price = prices.data.coins["coingecko:"+token_mapping[token[0]]]?.price + const price = prices.coins["coingecko:"+token_mapping[token[0]]]?.price if(price){ sum += price*token[1] } @@ -32,7 +32,7 @@ async function optimism(start: number, end: number) { async function avalanche(start: number, end: number) { const data = await queryFlipside(`select currency_address, sum(price) from avalanche.nft.ez_nft_sales where BLOCK_TIMESTAMP > TO_TIMESTAMP_NTZ(${start}) AND BLOCK_TIMESTAMP < TO_TIMESTAMP_NTZ(${end}) group by CURRENCY_ADDRESS`) return { - volume: await sumPricedTokens(start, data.map(([token, value])=>[token, token.startsWith("0x")?value:value/1e18]), { + volume: await sumPricedTokens(start, data.map(([token, value]: any)=>[token, token.startsWith("0x")?value:value/1e18]), { "ETH": "avalanche-2", "AVAX": "avalanche-2", "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7": "avalanche-2" @@ -58,14 +58,14 @@ async function flow(start: number, end: number) { } async function immutablex(start: number, _end: number) { - const data = await axios.post('https://qbolqfa7fnctxo3ooupoqrslem.appsync-api.us-east-2.amazonaws.com/graphql', + const data = await httpPost('https://qbolqfa7fnctxo3ooupoqrslem.appsync-api.us-east-2.amazonaws.com/graphql', {"operationName":"getMetricsAll","variables":{"address":"global"},"query":"query getMetricsAll($address: String!) {\n getMetricsAll(address: $address) {\n items {\n type\n trade_volume_usd\n trade_volume_eth\n floor_price_usd\n floor_price_eth\n trade_count\n owner_count\n __typename\n }\n __typename\n }\n latestTrades(address: $address) {\n items {\n transfers {\n token {\n token_address\n quantity\n token_id\n type\n usd_rate\n __typename\n }\n __typename\n }\n txn_time\n txn_id\n __typename\n }\n __typename\n }\n}"}, {headers:{ "x-api-key": "da2-ceptv3udhzfmbpxr3eqisx3coe" }} ) return { - volume: data.data.data.getMetricsAll.items.slice(1).reduce((closest:any, item:any)=>{ + volume: data.data.getMetricsAll.items.slice(1).reduce((closest:any, item:any)=>{ if(Math.abs(new Date(item.type).getTime()/1e3 - start) < Math.abs(new Date(closest.type).getTime()/1e3 - start)){ return item } @@ -75,11 +75,11 @@ async function immutablex(start: number, _end: number) { } async function ronin(_start: number, _end: number) { - const data = await axios.post('https://graphql-gateway.axieinfinity.com/graphql', + const data = await httpPost('https://graphql-gateway.axieinfinity.com/graphql', {"operationName":"GetOverviewToday","variables":{},"query":"query GetOverviewToday {\n marketStats {\n last24Hours {\n ...OverviewFragment\n __typename\n }\n last7Days {\n ...OverviewFragment\n __typename\n }\n last30Days {\n ...OverviewFragment\n __typename\n }\n __typename\n }\n}\n\nfragment OverviewFragment on SettlementStats {\n count\n axieCount\n volume\n volumeUsd\n __typename\n}\n"} ) return { - volume: Number(data.data.data.marketStats.last24Hours.volumeUsd) + volume: Number(data.data.marketStats.last24Hours.volumeUsd) } } @@ -93,27 +93,27 @@ function getAlliumVolume(chain: string) { } async function cardano(_start: number, _end: number) { - const data = await axios.get("https://server.jpgstoreapis.com/analytics/marketStats?timeframe=24h", { + const data = await httpGet("https://server.jpgstoreapis.com/analytics/marketStats?timeframe=24h", { headers:{ "X-Jpgstore-Csrf-Protection": "1" } }) - const price = await axios.get("https://coins.llama.fi/prices/current/coingecko:cardano") + const price = await httpGet("https://coins.llama.fi/prices/current/coingecko:cardano") return { - volume: Number(data.data.marketStats.volume)*price.data.coins["coingecko:cardano"].price + volume: Number(data.marketStats.volume)*price.coins["coingecko:cardano"].price } } async function ethereum(_start: number, _end: number) { - const data = await axios.get("https://nft.llama.fi/exchangeStats") - const price = await axios.get("https://coins.llama.fi/prices/current/coingecko:ethereum") + const data = await httpGet("https://nft.llama.fi/exchangeStats") + const price = await httpGet("https://coins.llama.fi/prices/current/coingecko:ethereum") return { - volume: Number(data.data.reduce((sum:number, ex:any)=>{ + volume: Number(data.reduce((sum:number, ex:any)=>{ if(["AlphaSharks", "Gem"].includes(ex.exchangeName) || ex.exchangeName.includes("Aggregator")){ return sum } return sum+ex["1DayVolume"] - }, 0))*price.data.coins["coingecko:ethereum"].price + }, 0))*price.coins["coingecko:ethereum"].price } } diff --git a/options/tigris/index.ts b/options/tigris/index.ts index 5b5849b649..d3a935d401 100644 --- a/options/tigris/index.ts +++ b/options/tigris/index.ts @@ -1,7 +1,7 @@ import { Chain } from "@defillama/sdk/build/general"; import { Adapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import axios from 'axios'; +import { httpGet } from "../../utils/fetchURL"; const API_ENDPOINT = "https://flask.tigristrade.info"; @@ -12,32 +12,23 @@ interface ApiResponse { } const fetchFromAPI = async (chain: Chain, timestamp: number): Promise => { - try { - let endpoint; - if (chain === CHAIN.POLYGON) { - endpoint = "/fetch-polygon-data"; - } else if (chain === CHAIN.ARBITRUM) { - endpoint = "/fetch-arbitrum-data"; - } else { - throw new Error(`Unsupported chain: ${chain}`); - } - - const response = await axios.get(`${API_ENDPOINT}${endpoint}`, { - params: { - chain: chain, - timestamp: timestamp - } - }); + let endpoint; + if (chain === CHAIN.POLYGON) { + endpoint = "/fetch-polygon-data"; + } else if (chain === CHAIN.ARBITRUM) { + endpoint = "/fetch-arbitrum-data"; + } else { + throw new Error(`Unsupported chain: ${chain}`); + } - if (response.status !== 200) { - throw new Error("Failed to fetch data from the API"); + const response = await httpGet(`${API_ENDPOINT}${endpoint}`, { + params: { + chain: chain, + timestamp: timestamp } + }); - return response.data; - } catch (error) { - console.error("Error fetching from the API:", error); - throw error; - } + return response; } function startOfDayTimestamp(timestamp: number): number { diff --git a/options/typus/getChainData.ts b/options/typus/getChainData.ts index 5172f18635..3cf4c93adf 100644 --- a/options/typus/getChainData.ts +++ b/options/typus/getChainData.ts @@ -1,5 +1,5 @@ -import axios from "axios"; import * as sdk from "@defillama/sdk"; +import { httpPost } from "../../utils/fetchURL"; interface ChainData { totalPremiumVolume: { [key: string]: number }; @@ -16,7 +16,7 @@ async function getChainData( let end_timestamp = Number(timestamp); let start_timestamp = end_timestamp - 24 * 60 * 60; - var response = await axios.post( + var response = await httpPost( "https://fullnode.mainnet.sui.io:443", { jsonrpc: "2.0", @@ -37,11 +37,11 @@ async function getChainData( } ); - var data = response.data.result.data; + var data = response.result.data; if (backFillTimestamp) { - while (response.data.result.hasNextPage) { - response = await axios.post( + while (response.result.hasNextPage) { + response = await httpPost( "https://fullnode.mainnet.sui.io:443", { jsonrpc: "2.0", @@ -53,7 +53,7 @@ async function getChainData( "0x321848bf1ae327a9e022ccb3701940191e02fa193ab160d9c0e49cd3c003de3a::typus_dov_single::DeliveryEvent", }, descending_order: true, - cursor: response.data.result.nextCursor, + cursor: response.result.nextCursor, }, }, { @@ -62,7 +62,7 @@ async function getChainData( }, } ); - data = data.concat(response.data.result.data); + data = data.concat(response.result.data); const timestamp = Number(data.at(-1).timestampMs) / 1000; if (timestamp <= Number(backFillTimestamp)) { diff --git a/protocols/pancakeswap/index.ts b/protocols/pancakeswap/index.ts index d04ef2145b..19c892e506 100644 --- a/protocols/pancakeswap/index.ts +++ b/protocols/pancakeswap/index.ts @@ -4,8 +4,8 @@ import { CHAIN } from "../../helpers/chains"; import disabledAdapter from "../../helpers/disabledAdapter"; import { getGraphDimensions } from "../../helpers/getUniSubgraph" -import axios from "axios"; import * as sdk from "@defillama/sdk"; +import { httpGet } from "../../utils/fetchURL"; const endpoints = { [CHAIN.BSC]: "https://proxy-worker.pancake-swap.workers.dev/bsc-exchange", @@ -152,8 +152,8 @@ const getResources = async (account: string): Promise => { do { let url = `${APTOS_PRC}/v1/accounts/${account}/resources?limit=9999` if (cursor) url += '&start=' + cursor - const res = await axios.get(url) - lastData = res.data + const res = await httpGet(url) + lastData = res data.push(...lastData) cursor = res.headers['x-aptos-cursor'] } while (lastData.length === 9999) @@ -213,13 +213,13 @@ const getSwapEvent = async (pool: any, fromTimestamp: number, toTimestamp: numbe if (start < 0) break; const getEventByCreation = `${APTOS_PRC}/v1/accounts/${account}/events/${pool.swap_events.creation_num}?start=${start}&limit=${limit}`; try { - const event: any[] = (await axios.get(getEventByCreation)).data; + const event: any[] = (await httpGet(getEventByCreation)); const listSequence: number[] = event.map(e => Number(e.sequence_number)) const lastMin = Math.min(...listSequence) if (lastMin >= Infinity || lastMin <= -Infinity) break; const lastVision = event.find(e => Number(e.sequence_number) === lastMin)?.version; const urlBlock = `${APTOS_PRC}/v1/blocks/by_version/${lastVision}`; - const block = (await axios.get(urlBlock)).data; + const block = (await httpGet(urlBlock)); const lastTimestamp = toUnixTime(block.block_timestamp); const lastTimestampNumber = lastTimestamp if (lastTimestampNumber >= fromTimestamp && lastTimestampNumber <= toTimestamp) { diff --git a/users/chains.ts b/users/chains.ts index 3bede72de9..2171c6a17d 100644 --- a/users/chains.ts +++ b/users/chains.ts @@ -1,7 +1,6 @@ import { queryFlipside } from "../helpers/flipsidecrypto"; -import axios from 'axios'; import { queryAllium } from "../helpers/allium"; -import { gql, request } from "graphql-request"; +import { httpGet } from "../utils/fetchURL"; function getUsersChain(chain: string) { return async (start: number, end: number) => { @@ -95,7 +94,7 @@ function findClosestItem(results:any[], timestamp:number, getTimestamp:(x:any)=> const toIso = (d:number) => new Date(d*1e3).toISOString() function coinmetricsData(assetID: string) { return async (start: number, end: number) => { - const result = (await axios.get(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=AdrActCnt&assets=${assetID}&start_time=${toIso(start - 24*3600)}&end_time=${toIso(end + 24*3600)}`)).data.data; + const result = (await httpGet(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=AdrActCnt&assets=${assetID}&start_time=${toIso(start - 24*3600)}&end_time=${toIso(end + 24*3600)}`)).data; const closestDatapoint = findClosestItem(result, start, t=>t.time) if (!closestDatapoint) { throw new Error(`Failed to fetch CoinMetrics data for ${assetID} on ${end}, no data`); @@ -108,7 +107,7 @@ function coinmetricsData(assetID: string) { /* // https://tronscan.org/#/data/stats2/accounts/activeAccounts async function tronscan(start: number, _end: number) { - const results = (await axios.get(`https://apilist.tronscanapi.com/api/account/active_statistic?type=day&start_timestamp=${(start - 2*24*3600)*1e3}`)).data.data; + const results = (await httpGet(`https://apilist.tronscanapi.com/api/account/active_statistic?type=day&start_timestamp=${(start - 2*24*3600)*1e3}`)).data; return findClosestItem(results, start, t=>t.day_time).active_count } */ diff --git a/utils/fetchURL.ts b/utils/fetchURL.ts index 4df391d18a..f0d8d9e707 100644 --- a/utils/fetchURL.ts +++ b/utils/fetchURL.ts @@ -1,14 +1,55 @@ -import axios from "axios" -import asyncRetry from "async-retry" +import axios, { AxiosRequestConfig } from "axios" -export default async function fetchURL(url: string) { - return asyncRetry(async () => await axios.get(url), { - retries: 3 - }) +export default async function fetchURL(url: string, retries = 3) { + try { + const res = await httpGet(url) + return res + } catch (error) { + if (retries > 0) return fetchURL(url, retries - 1) + throw error + } } -export async function postURL(url: string, data: any) { - return asyncRetry(async () => await axios.post(url, data), { - retries: 3 - }) +export async function postURL(url: string, data: any, retries = 3) { + try { + const res = await httpPost(url, data) + return res + } catch (error) { + if (retries > 0) return postURL(url, data, retries - 1) + throw error + } } + +function formAxiosError(url: string, error: any, options?: any) { + let e = new Error((error as any)?.message) + try { + const axiosError = (error as any)?.response?.data?.message || (error as any)?.response?.data?.error || (error as any)?.response?.statusText || (error as any)?.response?.data; + (e as any).url = url; + Object.keys(options || {}).forEach((key) => (e as any)[key] = options[key]); + if (axiosError) (e as any).axiosError = axiosError; + delete (e as any).stack + } catch { } + return e +} + +export async function httpGet(url: string, options?: AxiosRequestConfig) { + try { + const res = await axios.get(url, options) + if (res.status !== 200) throw new Error(`Error fetching ${url}: ${res.status} ${res.statusText}`) + if (!res.data) throw new Error(`Error fetching ${url}: no data`) + return res.data + } catch (error) { + throw formAxiosError(url, error, { method: 'GET' }) + } +} + +export async function httpPost(url: string, data: any, options?: AxiosRequestConfig) { + try { + const res = await axios.post(url, data, options) + if (res.status !== 200) throw new Error(`Error fetching ${url}: ${res.status} ${res.statusText}`) + if (!res.data) throw new Error(`Error fetching ${url}: no data`) + return res.data + } catch (error) { + throw formAxiosError(url, error, { method: 'POST' }) + } +} \ No newline at end of file diff --git a/utils/prices.ts b/utils/prices.ts index ee99be32a5..ac8deafd58 100644 --- a/utils/prices.ts +++ b/utils/prices.ts @@ -1,5 +1,5 @@ -import axios from 'axios' import { IJSON } from '../adapters/types' +import { httpGet } from './fetchURL' interface Price { decimals: number, price: number, symbol: string, timestamp: number } const pricesStore: IJSON> = {} @@ -13,17 +13,17 @@ export const getPrices = async (tokens: string[], timestamp: number) => { const storedtokens = Object.keys(pricesStore[timestamp ?? '']) tokens2Fetch = tokens.filter(token => !storedtokens.includes(token)) } - let res = { data: { coins: {} } } + let res = { coins: {} } if (tokens2Fetch.length > 0) - res = await axios.get(`https://coins.llama.fi/prices${path}/${tokens2Fetch.join(",")}`) + res = await httpGet(`https://coins.llama.fi/prices${path}/${tokens2Fetch.join(",")}`) if (timestamp) pricesStore[timestamp] = { ...pricesStore[timestamp], - ...res.data.coins + ...res.coins } return { ...getObjFromAttrs(pricesStore[timestamp ?? ''], tokens), - ...res.data.coins, + ...res.coins, } as IJSON }