Skip to content

Commit 33f7e28

Browse files
feat: add TermMax protocol adapter for fee collection
1 parent 3e0b035 commit 33f7e28

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

fees/termmax/index.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { EventLog, getAddress, zeroPadValue } from "ethers";
2+
import { Adapter, FetchOptions } from "../../adapters/types";
3+
import { CHAIN } from "../../helpers/chains";
4+
5+
const ABIS = {
6+
Market: {
7+
config:
8+
"function config() external view returns (address treasurer, uint64 maturity, tuple(uint32,uint32,uint32,uint32,uint32,uint32) feeConfig)",
9+
tokens:
10+
"function tokens() external view override returns (address fixedToken, address xToken, address gearingToken, address collateral, address debt)",
11+
},
12+
};
13+
14+
const Events = {
15+
CreateMarket: {
16+
eventAbi:
17+
"event CreateMarket(address indexed market, address indexed collateral, address indexed debtToken)",
18+
topic: "0x3f53d2c2743b2b162c0aa5d678be4058d3ae2043700424be52c04105df3e2411",
19+
},
20+
Transfer: {
21+
topic: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
22+
},
23+
};
24+
25+
const Factories = {
26+
arbitrum: {
27+
address: "0x14920Eb11b71873d01c93B589b40585dacfCA096",
28+
fromBlock: 322193553,
29+
},
30+
bsc: {
31+
address: "0x8Df05E11e72378c1710e296450Bf6b72e2F12019",
32+
fromBlock: 50519690,
33+
},
34+
ethereum: {
35+
address: "0x37Ba9934aAbA7a49cC29d0952C6a91d7c7043dbc",
36+
fromBlock: 22174761,
37+
},
38+
};
39+
40+
interface Market {
41+
ftAddr: string;
42+
treasurerAddr: string;
43+
underlyingAddr: string;
44+
}
45+
46+
const fetch = async (options: FetchOptions) => {
47+
const dailyFees = options.createBalances();
48+
49+
const logs = await options.getLogs({
50+
target: Factories[options.chain].address,
51+
eventAbi: Events.CreateMarket.eventAbi,
52+
topic: Events.CreateMarket.topic,
53+
fromBlock: Factories[options.chain].fromBlock,
54+
});
55+
const marketAddresses = logs.map((log) => log[0]);
56+
57+
const [allConfigs, allTokens] = await Promise.all([
58+
options.fromApi.multiCall({
59+
calls: marketAddresses,
60+
abi: ABIS.Market.config,
61+
}),
62+
options.fromApi.multiCall({
63+
calls: marketAddresses,
64+
abi: ABIS.Market.tokens,
65+
}),
66+
]);
67+
68+
const markets: Market[] = [];
69+
for (let i = 0; i < marketAddresses.length; i++) {
70+
const config = allConfigs[i];
71+
const tokens = allTokens[i];
72+
if (config && tokens) {
73+
markets.push({
74+
ftAddr: tokens[0],
75+
treasurerAddr: config[0],
76+
underlyingAddr: tokens[4],
77+
});
78+
}
79+
}
80+
81+
const treasurerAddrs = Array.from(
82+
new Set(markets.map((m) => m.treasurerAddr))
83+
);
84+
const promises: Promise<void>[] = [];
85+
for (const treasurerAddr of treasurerAddrs) {
86+
const task = async () => {
87+
const market = markets.find((m) => m.treasurerAddr === treasurerAddr);
88+
if (!market) return;
89+
90+
const logs = (await options.toApi.getLogs({
91+
targets: markets.map((market) => market.ftAddr),
92+
topics: [
93+
Events.Transfer.topic,
94+
null,
95+
zeroPadValue(treasurerAddr.toLowerCase(), 32),
96+
],
97+
fromTimestamp: options.fromTimestamp,
98+
toTimestamp: options.toTimestamp,
99+
})) as EventLog[];
100+
for (const log of logs) {
101+
console.log(market.underlyingAddr, log.data);
102+
dailyFees.add(market.underlyingAddr, BigInt(log.data));
103+
}
104+
};
105+
promises.push(task());
106+
}
107+
await Promise.all(promises);
108+
109+
return { dailyFees };
110+
};
111+
112+
const methodology = {
113+
dailyFees:
114+
"Fees collected by the TermMax protocol from users, distributed to the treasury",
115+
};
116+
117+
const adapters: Adapter = {
118+
version: 2,
119+
adapter: {
120+
[CHAIN.ETHEREUM]: {
121+
fetch,
122+
start: "2025-04-15",
123+
meta: { methodology },
124+
},
125+
[CHAIN.ARBITRUM]: {
126+
fetch,
127+
start: "2025-04-15",
128+
meta: { methodology },
129+
},
130+
[CHAIN.BSC]: {
131+
fetch,
132+
start: "2025-05-30",
133+
meta: { methodology },
134+
},
135+
},
136+
};
137+
138+
export default adapters;

0 commit comments

Comments
 (0)