forked from DefiLlama/dimension-adapters
-
Notifications
You must be signed in to change notification settings - Fork 0
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 DefiLlama#2393 from EnsoBuild/master
Update Enso adapter to v2
- Loading branch information
Showing
1 changed file
with
42 additions
and
58 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,63 +1,47 @@ | ||
import { CHAIN } from "../../helpers/chains"; | ||
import { httpGet } from "../../utils/fetchURL"; | ||
import { Adapter, Fetch, FetchResultVolume } from "../../adapters/types"; | ||
import { getEnv } from "../../helpers/env"; | ||
|
||
function fetch(chainId: number): Fetch { | ||
return async (endTimestamp: number, _, options): Promise<FetchResultVolume> => { | ||
const totalVolume = options.createBalances(); | ||
const dailyVolume = options.createBalances(); | ||
const res = await httpGet( | ||
`https://api.enso.finance/api/v1/volume/${chainId}?timestamp=${endTimestamp}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${getEnv("ENSO_API_KEY")}`, | ||
}, | ||
}, | ||
); | ||
|
||
totalVolume.addUSDValue(res.totalVolume); | ||
dailyVolume.addUSDValue(res.dailyVolume); | ||
|
||
return { | ||
totalVolume, | ||
dailyVolume, | ||
timestamp: endTimestamp, | ||
}; | ||
import { FetchV2 } from "../../adapters/types"; | ||
import { queryDune } from "../../helpers/dune"; | ||
|
||
const chains: Record<string, { duneChain: string; start: string }> = { | ||
[CHAIN.ETHEREUM]: { duneChain: "ethereum", start: "2023-06-22" }, | ||
[CHAIN.OPTIMISM]: { duneChain: "optimism", start: "2023-09-19" }, | ||
[CHAIN.BSC]: { duneChain: "binance", start: "2023-09-20" }, | ||
[CHAIN.POLYGON]: { duneChain: "polygon", start: "2023-09-05" }, | ||
[CHAIN.BASE]: { duneChain: "base", start: "2023-12-24" }, | ||
[CHAIN.ARBITRUM]: { duneChain: "arbitrum", start: "2023-09-11" }, | ||
}; | ||
|
||
const queryId = "4687193"; | ||
|
||
const fetchVolume = (chain: string): FetchV2 => async ({ startTimestamp, endTimestamp }) => { | ||
const chainConfig = chains[chain]; | ||
if (!chainConfig) throw new Error(`Chain configuration not found for: ${chain}`); | ||
|
||
const data = await queryDune(queryId, { | ||
timestamp_from: startTimestamp, | ||
timestamp_to: endTimestamp, | ||
chain: chainConfig.duneChain, | ||
}); | ||
|
||
const chainData = data[0]; | ||
if (!chainData) throw new Error(`Dune query failed: ${JSON.stringify(data)}`); | ||
|
||
return { | ||
dailyVolume: chainData.volume_timerange, | ||
totalVolume: chainData.total_volume, | ||
timestamp: endTimestamp, | ||
}; | ||
} | ||
|
||
const adapter: Adapter = { | ||
adapter: { | ||
[CHAIN.ETHEREUM]: { | ||
fetch: fetch(1), | ||
start: "2023-06-22", | ||
}, | ||
[CHAIN.OPTIMISM]: { | ||
fetch: fetch(10), | ||
start: "2023-09-19", | ||
}, | ||
[CHAIN.BSC]: { | ||
fetch: fetch(56), | ||
start: "2023-09-20", | ||
}, | ||
[CHAIN.POLYGON]: { | ||
fetch: fetch(137), | ||
start: "2023-09-05", | ||
}, | ||
[CHAIN.BASE]: { | ||
fetch: fetch(8453), | ||
start: "2023-12-24", | ||
}, | ||
[CHAIN.ARBITRUM]: { | ||
fetch: fetch(42161), | ||
start: "2023-09-11", | ||
}, | ||
[CHAIN.LINEA]: { | ||
fetch: fetch(59144), | ||
start: "2023-12-15", | ||
}, | ||
}, | ||
}; | ||
|
||
const adapter: any = { | ||
version: 2, | ||
isExpensiveAdapter: true, | ||
adapter: Object.fromEntries( | ||
Object.entries(chains).map(([chain, { start }]) => [ | ||
chain, | ||
{ fetch: fetchVolume(chain), start }, | ||
]) | ||
), | ||
}; | ||
|
||
export default adapter; |