diff --git a/dexs/dexalot/index.ts b/dexs/dexalot/index.ts index 3d12c94acf..9457de19d0 100644 --- a/dexs/dexalot/index.ts +++ b/dexs/dexalot/index.ts @@ -1,43 +1,68 @@ -import { FetchOptions, SimpleAdapter } from "../../adapters/types"; +import { BaseAdapter, FetchOptions, FetchResultV2, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; +import { httpGet } from "../../utils/fetchURL"; + +const historicalVolumeEndpoint = "https://api.dexalot.com/api/stats/chaindailyvolumes" interface IVolumeall { volumeusd: string; date: number; } -const address: any = { - [CHAIN.ARBITRUM]: "0x010224949cCa211Fb5dDfEDD28Dc8Bf9D2990368", - [CHAIN.AVAX]: "0xEed3c159F3A96aB8d41c8B9cA49EE1e5071A7cdD" +const supportedChains = [CHAIN.DEXALOT, CHAIN.AVAX, CHAIN.ARBITRUM, CHAIN.BASE] + +const chainToEnv = (chain: CHAIN) => { + switch (chain) { + case CHAIN.AVAX: + return "production-multi-avax" + case CHAIN.ARBITRUM: + return "production-multi-arb" + case CHAIN.BASE: + return "production-multi-base" + default: + return "production-multi-subnet" + } } -const event = "event SwapExecuted(uint256 indexed nonceAndMeta,address taker,address destTrader,uint256 destChainId,address srcAsset,address destAsset,uint256 srcAmount,uint256 destAmount)" - -const fetch = async (options: FetchOptions) => { - const dailyVolume = options.createBalances(); - const logs = await options.getLogs({ - target: address[options.chain], - eventAbi: event - }) - logs.forEach(log => { - dailyVolume.add(log.destAsset, log.destAmount) - }) - return { dailyVolume } +const fetchFromChain = (chain: CHAIN) => { + const endpoint = `${historicalVolumeEndpoint}?env=${chainToEnv(chain)}` + + return async (options: FetchOptions): Promise => { + const dayTimestamp = new Date(options.startOfDay * 1000) + const historicalVolume: IVolumeall[] = await httpGet(endpoint) + + const totalVolume = historicalVolume + .filter(volItem => new Date(volItem.date) <= dayTimestamp) + .reduce((acc, { volumeusd }) => acc + Number(volumeusd), 0) + const dailyVolume = historicalVolume + .find(dayItem => new Date(dayItem.date) === dayTimestamp)?.volumeusd + + return { + totalVolume: `${totalVolume}`, + dailyVolume: dailyVolume ? `${dailyVolume}` : undefined, + }; + } }; +const getStartTimestamp = (chain: CHAIN) => { + const endpoint = `${historicalVolumeEndpoint}?env=${chainToEnv(chain)}` + return async () => { + const historicalVolume: IVolumeall[] = await httpGet(endpoint) + return (new Date(historicalVolume[0].date).getTime()) / 1000 + } +} const adapter: SimpleAdapter = { version: 2, - adapter: { - [CHAIN.AVAX]: { - fetch: fetch, - start: 0, - }, - [CHAIN.ARBITRUM]: { - fetch: fetch, - start: 0, - }, - }, + adapter: supportedChains.reduce((acc, chain) => { + return { + ...acc, + [chain]: { + fetch: fetchFromChain(chain), + start: getStartTimestamp(chain), + } + } + }, {} as BaseAdapter), }; export default adapter; diff --git a/helpers/chains.ts b/helpers/chains.ts index 38e1556ee2..5b43632433 100644 --- a/helpers/chains.ts +++ b/helpers/chains.ts @@ -160,6 +160,7 @@ export enum CHAIN { SKALE_EUROPA = "skale_europa", IOTAEVM = "iotaevm", ZKLINK = "zklink", + DEXALOT = "dexalot", } // DonĀ“t use