-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1747 from jaimi-io/update-dexalot-volumes
Add Dexalot chain volumes to dexalot
- Loading branch information
Showing
2 changed files
with
52 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<FetchResultV2> => { | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters