Skip to content

Commit 72f05b0

Browse files
committed
udpate ring-dex adapter
1 parent f1a24eb commit 72f05b0

File tree

1 file changed

+89
-22
lines changed

1 file changed

+89
-22
lines changed

dexs/ring-dex.ts

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
import { CHAIN } from '../helpers/chains'
2-
import { FetchOptions, } from '../adapters/types'
3-
import { request } from 'graphql-request'
2+
import { FetchOptions, SimpleAdapter, } from '../adapters/types'
3+
import { formatAddress } from '../utils/utils';
4+
import { addOneToken } from '../helpers/prices';
45

5-
const v2Endpoints: any = {
6-
[CHAIN.ETHEREUM]: 'https://api.studio.thegraph.com/query/61509/ring-v2-eth-mainnet/version/latest',
7-
[CHAIN.BLAST]: 'https://api.studio.thegraph.com/query/61509/ring-v2-blast-mainnet/version/latest',
8-
[CHAIN.BSC]: 'https://api.studio.thegraph.com/query/109372/ring-v-2-bsc/version/latest',
6+
interface IRingDexConfig {
7+
factory: string;
8+
start: string;
9+
}
10+
11+
const RingDexConfigs: Record<string, IRingDexConfig> = {
12+
[CHAIN.ETHEREUM]: {
13+
factory: '0xeb2A625B704d73e82946D8d026E1F588Eed06416',
14+
start: '2024-07-07',
15+
},
16+
[CHAIN.BLAST]: {
17+
factory: '0x24F5Ac9A706De0cF795A8193F6AB3966B14ECfE6',
18+
start: '2024-03-01',
19+
},
20+
[CHAIN.BSC]: {
21+
factory: '0x4De602A30Ad7fEf8223dcf67A9fB704324C4dd9B',
22+
start: '2025-02-20',
23+
},
24+
[CHAIN.HYPERLIQUID]: {
25+
factory: '0x4AfC2e4cA0844ad153B090dc32e207c1DD74a8E4',
26+
start: '2025-07-18',
27+
},
928
}
1029

1130
const methodology = {
@@ -14,33 +33,81 @@ const methodology = {
1433
SupplySideRevenue: 'All fees are distributed to LPs.',
1534
}
1635

17-
const fetch = async (_: number, _1: any, { chain, startOfDay, dateString }: FetchOptions) => {
18-
const endpoint = v2Endpoints[chain]
36+
const defaultV2SwapEvent = 'event Swap(address indexed sender, uint amount0In, uint amount1In, uint amount0Out, uint amount1Out, address indexed to)';
1937

20-
const graphQuery = `{
21-
uniswapDayDatas( where: { date: ${startOfDay} }) { dailyVolumeUSD }
22-
}`
38+
const fetch = async (_: number, _1: any, options: FetchOptions) => {
39+
const allPairsLength = await options.api.call({ target: RingDexConfigs[options.chain].factory, abi: 'uint256:allPairsLength' })
40+
const calls: Array<any> = [];
41+
for (let i = 0; i < Number(allPairsLength); i++) {
42+
calls.push({ params:[i] })
43+
}
44+
const allPairs = await options.api.multiCall({
45+
target: RingDexConfigs[options.chain].factory,
46+
abi: 'function allPairs(uint256) view returns(address)',
47+
calls,
48+
})
49+
const pairTokens0 = await options.api.multiCall({
50+
abi: 'address:token0',
51+
calls: allPairs,
52+
})
53+
const pairTokens1 = await options.api.multiCall({
54+
abi: 'address:token1',
55+
calls: allPairs,
56+
})
57+
const pairTokens0Underlying = await options.api.multiCall({
58+
abi: 'address:token',
59+
calls: pairTokens0,
60+
permitFailure: true,
61+
})
62+
const pairTokens1Underlying = await options.api.multiCall({
63+
abi: 'address:token',
64+
calls: pairTokens1,
65+
permitFailure: true,
66+
})
2367

24-
const { uniswapDayDatas } = await request(endpoint, graphQuery)
25-
if (!uniswapDayDatas.length)
26-
throw new Error(`No data found for ${chain} at ${dateString}`)
68+
const pairs: Record<string, Array<string>> = {};
69+
for (let i = 0; i < allPairs.length; i++) {
70+
pairs[formatAddress(allPairs[i])] = [
71+
pairTokens0Underlying[i] ? pairTokens0Underlying[i] : pairTokens0[i],
72+
pairTokens1Underlying[i] ? pairTokens1Underlying[i] : pairTokens1[i],
73+
]
74+
}
2775

28-
const dailyVolume = uniswapDayDatas[0].dailyVolumeUSD
29-
const dailyFees = dailyVolume * 0.003
76+
const dailyVolume = options.createBalances()
77+
const swapLogs: Array<any> = await options.getLogs({
78+
eventAbi: defaultV2SwapEvent,
79+
targets: allPairs,
80+
flatten: true,
81+
onlyArgs: false,
82+
})
83+
for (const log of swapLogs) {
84+
const tokens = pairs[formatAddress(log.address)];
85+
if (tokens) {
86+
addOneToken({ chain: options.chain, balances: dailyVolume, token0: tokens[0], token1: tokens[1], amount0: log.args.amount0In, amount1: log.args.amount1In })
87+
addOneToken({ chain: options.chain, balances: dailyVolume, token0: tokens[0], token1: tokens[1], amount0: log.args.amount0Out, amount1: log.args.amount1Out })
88+
}
89+
}
3090

3191
return {
3292
dailyVolume,
33-
dailyFees,
34-
dailyUserFees: dailyFees,
35-
dailySupplySideRevenue: dailyFees,
93+
dailyFees: dailyVolume.clone(0.003),
3694
dailyRevenue: 0,
37-
}
95+
dailySupplySideRevenue: dailyVolume.clone(0.003),
96+
};
3897
}
3998

40-
export default {
99+
const adapter: SimpleAdapter = {
41100
version: 1,
42101
start: '2024-07-07',
43102
methodology,
44103
fetch,
45-
chains: Object.keys(v2Endpoints),
104+
adapter: {}
46105
}
106+
107+
for (const [chain, config] of Object.entries(RingDexConfigs)) {
108+
(adapter.adapter as any)[chain] = {
109+
start: config.start,
110+
}
111+
}
112+
113+
export default adapter;

0 commit comments

Comments
 (0)