Skip to content

Commit

Permalink
replace axios with wrapped method
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Feb 4, 2024
1 parent ef7809d commit a3f11f1
Show file tree
Hide file tree
Showing 81 changed files with 462 additions and 546 deletions.
12 changes: 9 additions & 3 deletions adapters/utils/runAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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
}
}

Expand All @@ -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
Expand Down
16 changes: 5 additions & 11 deletions aggregators/dexible/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
}
Expand Down
10 changes: 5 additions & 5 deletions dexs/apollox/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,17 +41,17 @@ 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);

return dailyVolume
};

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
Expand Down
8 changes: 4 additions & 4 deletions dexs/aux-exchange/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,8 +22,8 @@ const getResources = async (account: string): Promise<any[]> => {
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)
Expand Down Expand Up @@ -76,7 +76,7 @@ const getSwapEvent = async (pool: any, fromTimestamp: number): Promise<ISwapEven
if (start < 0) break;
const getEventByCreation = `${APTOS_PRC}/v1/accounts/${account}/events/${pool.swap_events.creation_num}?start=${start}&limit=25`;
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))
swap_events.push(...event)
const lastMin = Math.min(...listSequence)
Expand Down
7 changes: 3 additions & 4 deletions dexs/bluefin/index.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -23,9 +22,9 @@ const fetchURL = (baseURL: string, product: string): string => {

const computeVolume = async (timestamp: number, baseUrl: string, productIds: string[]): Promise<Volume> => {
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 {
Expand Down
4 changes: 2 additions & 2 deletions dexs/concordex-io/index.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
6 changes: 3 additions & 3 deletions dexs/defi-kingdoms/index.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand 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,
Expand Down
5 changes: 2 additions & 3 deletions dexs/defibox/index.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions dexs/dexalot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions dexs/drift-protocol/index.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
24 changes: 6 additions & 18 deletions dexs/dx25/index.ts
Original file line number Diff line number Diff line change
@@ -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
});


Expand Down
20 changes: 10 additions & 10 deletions dexs/fwx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions dexs/hashflow/index.ts
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions dexs/hyperliquid/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 {
Expand Down
Loading

0 comments on commit a3f11f1

Please sign in to comment.