|
| 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 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 | + dailyFees.add(market.underlyingAddr, BigInt(log.data)); |
| 102 | + } |
| 103 | + }; |
| 104 | + promises.push(task()); |
| 105 | + } |
| 106 | + await Promise.all(promises); |
| 107 | + |
| 108 | + return { dailyFees }; |
| 109 | +}; |
| 110 | + |
| 111 | +const methodology = { |
| 112 | + dailyFees: |
| 113 | + "Fees collected by the TermMax protocol from users, distributed to the treasury", |
| 114 | +}; |
| 115 | + |
| 116 | +const adapters: Adapter = { |
| 117 | + version: 2, |
| 118 | + adapter: { |
| 119 | + [CHAIN.ETHEREUM]: { |
| 120 | + fetch, |
| 121 | + start: "2025-04-15", |
| 122 | + meta: { methodology }, |
| 123 | + }, |
| 124 | + [CHAIN.ARBITRUM]: { |
| 125 | + fetch, |
| 126 | + start: "2025-04-15", |
| 127 | + meta: { methodology }, |
| 128 | + }, |
| 129 | + [CHAIN.BSC]: { |
| 130 | + fetch, |
| 131 | + start: "2025-05-30", |
| 132 | + meta: { methodology }, |
| 133 | + }, |
| 134 | + }, |
| 135 | +}; |
| 136 | + |
| 137 | +export default adapters; |
0 commit comments