Skip to content

Commit

Permalink
refactor levana code
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Feb 15, 2024
1 parent 0ce23b8 commit 5cad0bf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
18 changes: 9 additions & 9 deletions dexs/levana/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import BigNumber from "bignumber.js";
import fetchURL from "../../utils/fetchURL";
const plimit = require('p-limit');
const limit = plimit(1);
import { getEnv } from "../../helpers/env";
import { httpGet } from "../../utils/fetchURL";

const INDEXER_URL = "https://indexer-mainnet.levana.finance";
const QUERIER_URL = "https://querier-mainnet.levana.finance";
Expand Down Expand Up @@ -32,11 +30,13 @@ export async function fetchVolume(kind: "daily" | "total", marketInfos: MarketIn

const url = (marketsStr: string) => `${INDEXER_URL}/rolling_trade_volume?market=${marketsStr}&timestamp=${timestamp}&interval_days=${intervalDays}`;

const result = (await Promise.all(marketInfos.map(marketInfo => limit(() => fetchURL(url(marketInfo.addr))))))
.map((response: any) => response)
.map((data: any) => BigNumber(data));
const result = (await Promise.all(marketInfos.map(marketInfo => httpGet(url(marketInfo.addr), {
headers: {
'x-levana-access': getEnv('LEVANA_API_KEY')
}
}))))

return result.reduce((a: BigNumber, b: BigNumber) => a.plus(b), BigNumber(0))
return result.reduce((a: number, b: number) => a + +b, 0)
}

export async function fetchMarketInfos(chain: Chain): Promise<MarketInfo[]> {
Expand All @@ -47,7 +47,7 @@ export async function fetchMarketInfos(chain: Chain): Promise<MarketInfo[]> {
}
}
const url = `${QUERIER_URL}/v1/perps/factory-market-status?network=${networkName[chain]}&factory=${factoryAddr[chain]}`
const result:FactoryResponse = await fetchURL(url);
const result:FactoryResponse = await httpGet(url);

return Object.entries(result).reduce((acc, [addr, {market_id}]) => {
acc.push({
Expand Down
20 changes: 4 additions & 16 deletions dexs/levana/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FetchResultVolume, SimpleAdapter} from "../../adapters/types";
import { FetchResultVolume, SimpleAdapter } from "../../adapters/types";
import { fetchMarketInfos, fetchVolume } from "./fetch";

const adapter: SimpleAdapter = {
Expand All @@ -11,11 +11,7 @@ const adapter: SimpleAdapter = {
fetchVolume("daily", marketInfos, timestamp),
fetchVolume("total", marketInfos, timestamp)
]);
return {
timestamp,
dailyVolume: dailyVolume.toString(),
totalVolume: totalVolume.toString()
}
return { timestamp, dailyVolume, totalVolume, }
},
start: 1688628356
},
Expand All @@ -28,11 +24,7 @@ const adapter: SimpleAdapter = {
fetchVolume("total", marketInfos, timestamp)
]);

return {
timestamp,
dailyVolume: dailyVolume.toString(),
totalVolume: totalVolume.toString()
}
return { timestamp, dailyVolume, totalVolume, }
},
start: 1691305909
},
Expand All @@ -45,11 +37,7 @@ const adapter: SimpleAdapter = {
fetchVolume("total", marketInfos, timestamp)
]);

return {
timestamp,
dailyVolume: dailyVolume.toString(),
totalVolume: totalVolume.toString()
}
return { timestamp, dailyVolume, totalVolume, }
},
start: 1695738685
}
Expand Down
1 change: 1 addition & 0 deletions helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const ENV_KEYS = new Set([
'SMARDEX_SUBGRAPH_API_KEY',
'PROD_VYBE_API_KEY',
'PERENNIAL_V2_SUBGRAPH_API_KEY',
'LEVANA_API_KEY',
'ZEROx_API_KEY',
'ZEROX_API_KEY'
])
Expand Down

0 comments on commit 5cad0bf

Please sign in to comment.