Skip to content

Commit 0bf7999

Browse files
add Swaps.io to DEXs (#3015)
1 parent 8cf70c7 commit 0bf7999

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

dexs/swaps-io/index.ts

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Chain } from "@defillama/sdk/build/types";
2+
import { FetchOptions, FetchResult } from "../../adapters/types";
3+
import { CHAIN } from "../../helpers/chains";
4+
5+
const SUPPORTED_CHAIN_MAPPING: { [chain: Chain]: number } = {
6+
[CHAIN.ETHEREUM]: 1,
7+
[CHAIN.OPTIMISM]: 10,
8+
[CHAIN.BSC]: 56,
9+
[CHAIN.XDAI]: 100,
10+
[CHAIN.POLYGON]: 137,
11+
[CHAIN.SONIC]: 146,
12+
[CHAIN.ARBITRUM]: 42161,
13+
[CHAIN.BASE]: 8453,
14+
[CHAIN.AVAX]: 43114,
15+
[CHAIN.BLAST]: 81457,
16+
}
17+
18+
const BASE_URL = 'https://explorer.prod.swaps.io'
19+
const AGGERAGATE_ENDPOINT = '/api/v0/aggregate'
20+
const SWAPS_IO_LAUNCH_TIME = '2024-01-01'
21+
22+
const getRequestBody = (chainId: number, fromTime: number | null = null, toTime: number | null = null) => {
23+
return {
24+
"get_from_volume": true,
25+
"states": ["confirmed","awaiting_confirm"],
26+
"from_created_at": fromTime,
27+
"to_created_at": toTime,
28+
"from_chains": [String(chainId)]
29+
}
30+
}
31+
32+
const chain_total_cache = {};
33+
34+
async function fetchTotalVolumeCached(chainId: number) {
35+
const cacheKey = JSON.stringify(chainId);
36+
37+
if (chain_total_cache[cacheKey]) {
38+
return chain_total_cache[cacheKey];
39+
}
40+
41+
const total_res = await fetch(BASE_URL + AGGERAGATE_ENDPOINT, {
42+
method: 'POST',
43+
headers: {
44+
'Content-Type': 'application/json',
45+
},
46+
body: JSON.stringify(getRequestBody(chainId))
47+
}).then((response) => response.json());
48+
49+
chain_total_cache[cacheKey] = total_res;
50+
51+
return total_res;
52+
}
53+
54+
55+
function get_fetch_for_network(chain: Chain) {
56+
return async (options: FetchOptions): Promise<FetchResult> => {
57+
const chainId: number | undefined = SUPPORTED_CHAIN_MAPPING[chain] ?? options.api.chainId
58+
if (!chainId) throw new Error(`Chain ${chain} is not supported`)
59+
60+
const daily_res = await fetch(BASE_URL + AGGERAGATE_ENDPOINT, {
61+
method: 'POST',
62+
headers: {
63+
'Content-Type': 'application/json',
64+
},
65+
body: JSON.stringify(getRequestBody(chainId, options.fromTimestamp, options.toTimestamp))
66+
}).then((response) => response.json());
67+
68+
const total_res = await fetchTotalVolumeCached(chainId)
69+
70+
const dailyVolume = Math.trunc(daily_res["entries"][0]["get"]["from_volume"] / 100)
71+
const totalVolume = Math.trunc(total_res["entries"][0]["get"]["from_volume"] / 100)
72+
73+
return { dailyVolume, totalVolume }
74+
}
75+
}
76+
77+
export default {
78+
version: 2,
79+
adapter: {
80+
...Object.fromEntries(
81+
Object.keys(SUPPORTED_CHAIN_MAPPING).map(chain => [
82+
chain,
83+
{ fetch: get_fetch_for_network(chain), start: SWAPS_IO_LAUNCH_TIME }
84+
])
85+
),
86+
},
87+
};

0 commit comments

Comments
 (0)