Skip to content

Commit d896004

Browse files
fix: migrate Tokenlon adapter from GraphQL to event logs (#5097)
* fix: migrate Tokenlon adapter from GraphQL to event logs * fix(tokenlon): update start dates for all chains
1 parent 6d84c53 commit d896004

File tree

3 files changed

+80
-105
lines changed

3 files changed

+80
-105
lines changed

dexs/tokenlon-agg.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
2-
import adapter from '../dexs/tokenlon'
3-
const { breakdown, ...rest } = adapter
1+
import adapter from '../dexs/tokenlon';
2+
const { breakdown, ...rest } = adapter;
43

54
export default {
65
...rest,
76
adapter: breakdown['tokenlon-agg'],
8-
}
7+
};

dexs/tokenlon-dex.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
2-
import adapter from '../dexs/tokenlon'
3-
const { breakdown, ...rest } = adapter
1+
import adapter from '../dexs/tokenlon';
2+
const { breakdown, ...rest } = adapter;
43

54
export default {
65
...rest,
76
adapter: breakdown['tokenlon'],
8-
}
7+
};

dexs/tokenlon/index.ts

Lines changed: 74 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
1-
import * as sdk from "@defillama/sdk";
2-
import { BreakdownAdapter, ChainBlocks, FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types";
3-
import { CHAIN } from "../../helpers/chains";
4-
import { request } from "graphql-request";
5-
import { Chain } from "../../adapters/types";
6-
7-
interface IGraph {
8-
makerAssetAddr: string;
9-
makerAssetAmount: string;
10-
makerToken: string;
11-
makerTokenAmount: string;
12-
}
13-
14-
interface IData {
15-
fillOrders: IGraph[];
16-
swappeds: IGraph[];
17-
filledRFQs: IGraph[];
18-
}
19-
20-
type TEndpoint = {
21-
[s: string | Chain]: string;
22-
};
1+
import { FetchOptions, BreakdownAdapter } from '../../adapters/types';
2+
import { CHAIN } from '../../helpers/chains';
233

244
const config: any = {
255
[CHAIN.POLYGON]: { exchange: '0xac4F3817a726016fC8254119FC48bE838a21f17F' },
@@ -28,79 +8,46 @@ const config: any = {
288
[CHAIN.BSC]: { exchange: '0xac4F3817a726016fC8254119FC48bE838a21f17F' },
299
[CHAIN.OPTIMISM]: { exchange: '0xac4F3817a726016fC8254119FC48bE838a21f17F' },
3010
[CHAIN.ERA]: { exchange: '0xC63c379Ae456af9C73Bf81A7D745fAF1d9e180e0' },
31-
}
32-
33-
const endpoints: TEndpoint = {
34-
[CHAIN.ETHEREUM]: sdk.graph.modifyEndpoint('5JhweAV1Y3k3GbbEssfetBaoyDNWz1Y72zscRrYsAgVT'),
35-
};
36-
const rfqV2Endpoint = "https://subgraph.satsuma-prod.com/61c3dea518e9/imtoken-labs--349710/rfq-v2-subgraph/version/v0.0.1-test-version/api"
37-
38-
const fetchVolume = (chain: Chain) => {
39-
return async (
40-
__timestamp: number,
41-
_: ChainBlocks,
42-
{ createBalances, fromTimestamp, toTimestamp }: FetchOptions
43-
): Promise<FetchResultVolume> => {
44-
const dailyVolume = createBalances();
45-
const query = `
46-
{
47-
swappeds(first:1000, where:{timestamp_gte:${fromTimestamp}, timestamp_lte:${toTimestamp}}) {
48-
makerAssetAddr
49-
makerAssetAmount
50-
}
51-
fillOrders(first:1000, where:{timestamp_gte:${fromTimestamp}, timestamp_lte:${toTimestamp}}) {
52-
makerAssetAddr
53-
makerAssetAmount
54-
}
55-
}
56-
`;
57-
const response: IData = await request(endpoints[chain], query);
58-
const historicalData: IGraph[] = [...response.fillOrders, ...response.swappeds];
59-
historicalData.map((e: IGraph) => {
60-
dailyVolume.add(e.makerAssetAddr, e.makerAssetAmount);
61-
});
62-
63-
const rfqv2Query = `{
64-
filledRFQs(
65-
first: 1000
66-
where: {blockTimestamp_gte: ${fromTimestamp}, blockTimestamp_lte: ${toTimestamp}}
67-
) {
68-
makerToken
69-
makerTokenAmount
70-
}}`
71-
const rfqV2Response = await request(rfqV2Endpoint, rfqv2Query)
72-
const rfqv2Record: IGraph[] = [...rfqV2Response.filledRFQs]
73-
rfqv2Record.map((e: IGraph) => {
74-
dailyVolume.add(e.makerToken, e.makerTokenAmount);
75-
});
76-
77-
return { dailyVolume, timestamp: toTimestamp };
78-
};
7911
};
8012

8113
const abis = {
8214
FillOrder:
83-
"event FillOrder(string source, bytes32 indexed transactionHash, bytes32 indexed orderHash, address indexed userAddr, address takerAssetAddr, uint256 takerAssetAmount, address makerAddr, address makerAssetAddr, uint256 makerAssetAmount, address receiverAddr, uint256 settleAmount, uint16 feeFactor)",
15+
'event FillOrder(string source, bytes32 indexed transactionHash, bytes32 indexed orderHash, address indexed userAddr, address takerAssetAddr, uint256 takerAssetAmount, address makerAddr, address makerAssetAddr, uint256 makerAssetAmount, address receiverAddr, uint256 settleAmount, uint16 feeFactor)',
8416
Swapped:
85-
"event Swapped(string source, bytes32 indexed transactionHash, address indexed userAddr, address takerAssetAddr, uint256 takerAssetAmount, address makerAddr, address makerAssetAddr, uint256 makerAssetAmount, address receiverAddr, uint256 settleAmount, uint256 receivedAmount, uint16 feeFactor, uint16 subsidyFactor)",
17+
'event Swapped(string source, bytes32 indexed transactionHash, address indexed userAddr, address takerAssetAddr, uint256 takerAssetAmount, address makerAddr, address makerAssetAddr, uint256 makerAssetAmount, address receiverAddr, uint256 settleAmount, uint256 receivedAmount, uint16 feeFactor, uint16 subsidyFactor)',
8618
FilledRFQ:
87-
"event FilledRFQ(bytes32 indexed offerHash,address indexed user,address indexed maker,address takerToken,uint256 takerTokenAmount,address makerToken,uint256 makerTokenAmount,address recipient,uint256 settleAmount,uint256 feeFactor)",
19+
'event FilledRFQ(bytes32 indexed offerHash,address indexed user,address indexed maker,address takerToken,uint256 takerTokenAmount,address makerToken,uint256 makerTokenAmount,address recipient,uint256 settleAmount,uint256 feeFactor)',
8820
FillOrderByRFQ:
89-
"event FillOrder( string source,bytes32 indexed transactionHash,bytes32 indexed orderHash,address indexed userAddr,address takerAssetAddr,uint256 takerAssetAmount,address makerAddr,address makerAssetAddr,uint256 makerAssetAmount,address receiverAddr,uint256 settleAmount,uint16 feeFactor)",
90-
91-
Swap:
92-
"event Swap(bytes32 indexed swapHash,address indexed maker, address indexed taker,address recipient,address inputToken,uint256 inputAmount,address outputToken,uint256 outputAmount)",
93-
SwappedV2: "event Swapped((string source, bytes32 transactionHash, uint256 settleAmount, uint256 receivedAmount, uint16 feeFactor, uint16 subsidyFactor), (address makerAddr, address takerAssetAddr, address makerAssetAddr, uint256 takerAssetAmount, uint256 makerAssetAmount, address userAddr, address receiverAddr, uint256 salt, uint256 deadline) order)",
21+
'event FillOrder( string source,bytes32 indexed transactionHash,bytes32 indexed orderHash,address indexed userAddr,address takerAssetAddr,uint256 takerAssetAmount,address makerAddr,address makerAssetAddr,uint256 makerAssetAmount,address receiverAddr,uint256 settleAmount,uint16 feeFactor)',
22+
Swap: 'event Swap(bytes32 indexed swapHash,address indexed maker, address indexed taker,address recipient,address inputToken,uint256 inputAmount,address outputToken,uint256 outputAmount)',
23+
SwappedV2:
24+
'event Swapped((string source, bytes32 transactionHash, uint256 settleAmount, uint256 receivedAmount, uint16 feeFactor, uint16 subsidyFactor), (address makerAddr, address takerAssetAddr, address makerAssetAddr, uint256 takerAssetAmount, uint256 makerAssetAmount, address userAddr, address receiverAddr, uint256 salt, uint256 deadline) order)',
9425
};
9526

96-
const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, }: FetchOptions) => {
27+
const fetchEthereum = async (options: FetchOptions) => {
28+
const { createBalances, getLogs } = options;
9729
const dailyVolume = createBalances();
9830

99-
const pmmLogs = await getLogs({ target: "0x8D90113A1e286a5aB3e496fbD1853F265e5913c6", eventAbi: abis.FillOrder });
100-
const rfqv1Logs = await getLogs({ target: "0xfD6C2d2499b1331101726A8AC68CCc9Da3fAB54F", eventAbi: abis.FillOrderByRFQ });
101-
const rfqv2Logs = await getLogs({ target: "0x91c986709bb4fe0763edf8e2690ee9d5019bea4a", eventAbi: abis.FilledRFQ });
102-
const ammV1Logs = await getLogs({ target: "0x4a14347083B80E5216cA31350a2D21702aC3650d", eventAbi: abis.Swapped });
103-
const ammV2Logs = await getLogs({ target: "0x4a14347083B80E5216cA31350a2D21702aC3650d", eventAbi: abis.SwappedV2 });
31+
const pmmLogs = await getLogs({
32+
target: '0x8D90113A1e286a5aB3e496fbD1853F265e5913c6',
33+
eventAbi: abis.FillOrder,
34+
});
35+
const rfqv1Logs = await getLogs({
36+
target: '0xfD6C2d2499b1331101726A8AC68CCc9Da3fAB54F',
37+
eventAbi: abis.FillOrderByRFQ,
38+
});
39+
const rfqv2Logs = await getLogs({
40+
target: '0x91c986709bb4fe0763edf8e2690ee9d5019bea4a',
41+
eventAbi: abis.FilledRFQ,
42+
});
43+
const ammV1Logs = await getLogs({
44+
target: '0x4a14347083B80E5216cA31350a2D21702aC3650d',
45+
eventAbi: abis.Swapped,
46+
});
47+
const ammV2Logs = await getLogs({
48+
target: '0x4a14347083B80E5216cA31350a2D21702aC3650d',
49+
eventAbi: abis.SwappedV2,
50+
});
10451

10552
[ammV1Logs, rfqv1Logs, pmmLogs].flat().forEach((log: any) => {
10653
dailyVolume.add(log.makerAssetAddr, log.makerAssetAmount);
@@ -113,34 +60,64 @@ const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getLog
11360
[rfqv2Logs].flat().forEach((log: any) => {
11461
dailyVolume.add(log.makerToken, log.makerTokenAmount);
11562
});
116-
return { timestamp, dailyVolume };
63+
64+
return { dailyVolume };
11765
};
11866

119-
const fetchL2 = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, chain, }: FetchOptions) => {
67+
const fetchL2 = async (options: FetchOptions) => {
68+
const { createBalances, getLogs, chain } = options;
12069
const dailyVolume = createBalances();
121-
const ammLogs = await getLogs({ target: config[chain].exchange, eventAbi: abis.Swap });
70+
const ammLogs = await getLogs({
71+
target: config[chain].exchange,
72+
eventAbi: abis.Swap,
73+
});
12274

12375
[ammLogs].flat().forEach((log: any) => {
12476
dailyVolume.add(log.inputToken, log.inputAmount);
12577
});
126-
return { timestamp, dailyVolume };
127-
};
12878

129-
const adaptersMultiChain: any = {}
130-
adaptersMultiChain[CHAIN.ETHEREUM] = { fetch, start: '2020-12-15' }
131-
Object.keys(config).forEach(chain => {
132-
adaptersMultiChain[chain] = { fetch: fetchL2 }
133-
})
79+
return { dailyVolume };
80+
};
13481

13582
const adapter: BreakdownAdapter = {
83+
version: 2,
13684
breakdown: {
13785
tokenlon: {
13886
[CHAIN.ETHEREUM]: {
139-
fetch: fetchVolume(CHAIN.ETHEREUM),
140-
start: '2020-12-17',
87+
fetch: fetchEthereum,
88+
start: '2020-12-15',
89+
},
90+
},
91+
'tokenlon-agg': {
92+
[CHAIN.ETHEREUM]: {
93+
fetch: fetchEthereum,
94+
start: '2020-12-15',
95+
},
96+
[CHAIN.POLYGON]: {
97+
fetch: fetchL2,
98+
start: '2023-11-06',
99+
},
100+
[CHAIN.ARBITRUM]: {
101+
fetch: fetchL2,
102+
start: '2023-11-22',
103+
},
104+
[CHAIN.BASE]: {
105+
fetch: fetchL2,
106+
start: '2024-03-13',
107+
},
108+
[CHAIN.BSC]: {
109+
fetch: fetchL2,
110+
start: '2024-03-13',
111+
},
112+
[CHAIN.OPTIMISM]: {
113+
fetch: fetchL2,
114+
start: '2023-11-22',
115+
},
116+
[CHAIN.ERA]: {
117+
fetch: fetchL2,
118+
start: '2024-04-16',
141119
},
142120
},
143-
"tokenlon-agg": adaptersMultiChain,
144121
},
145122
};
146123

0 commit comments

Comments
 (0)