|
1 | 1 | import { BigNumber } from '../utils/numerics'; |
2 | 2 | import { StrategyStructOutput } from '../abis/types/CarbonController'; |
3 | 3 | import { Contracts } from './Contracts'; |
4 | | -import { isETHAddress, MultiCall, multicall } from './utils'; |
| 4 | +import { |
| 5 | + isETHAddress, |
| 6 | + MultiCall, |
| 7 | + MulticallService, |
| 8 | + DefaultMulticallService, |
| 9 | +} from './utils'; |
5 | 10 | import { Logger } from '../common/logger'; |
6 | 11 | import { |
7 | 12 | EncodedStrategy, |
@@ -51,13 +56,19 @@ function toStrategy(res: StrategyStructOutput): EncodedStrategy { |
51 | 56 | */ |
52 | 57 | export default class Reader implements Fetcher { |
53 | 58 | private _contracts: Contracts; |
| 59 | + private _multicallService: MulticallService; |
54 | 60 |
|
55 | | - public constructor(contracts: Contracts) { |
| 61 | + public constructor( |
| 62 | + contracts: Contracts, |
| 63 | + multicallService?: MulticallService |
| 64 | + ) { |
56 | 65 | this._contracts = contracts; |
| 66 | + this._multicallService = |
| 67 | + multicallService ?? new DefaultMulticallService(contracts.multicall); |
57 | 68 | } |
58 | 69 |
|
59 | 70 | private _multicall(calls: MultiCall[], blockHeight?: number) { |
60 | | - return multicall(calls, this._contracts.multicall, blockHeight); |
| 71 | + return this._multicallService.execute(calls, blockHeight); |
61 | 72 | } |
62 | 73 |
|
63 | 74 | public async strategy(id: BigNumber): Promise<EncodedStrategy> { |
@@ -90,39 +101,88 @@ export default class Reader implements Fetcher { |
90 | 101 | token0: string, |
91 | 102 | token1: string |
92 | 103 | ): Promise<EncodedStrategy[]> { |
93 | | - const res = await this._contracts.carbonController.strategiesByPair( |
94 | | - token0, |
95 | | - token1, |
96 | | - 0, |
97 | | - 0 |
98 | | - ) ?? []; |
99 | | - return res.map((r) => toStrategy(r)); |
| 104 | + const allStrategies: EncodedStrategy[] = []; |
| 105 | + let startIndex = 0; |
| 106 | + const chunkSize = 1000; |
| 107 | + |
| 108 | + while (true) { |
| 109 | + const res = |
| 110 | + (await this._contracts.carbonController.strategiesByPair( |
| 111 | + token0, |
| 112 | + token1, |
| 113 | + startIndex, |
| 114 | + startIndex + chunkSize |
| 115 | + )) ?? []; |
| 116 | + |
| 117 | + allStrategies.push(...res.map((r) => toStrategy(r))); |
| 118 | + |
| 119 | + if (res.length < chunkSize) break; |
| 120 | + |
| 121 | + startIndex += chunkSize; |
| 122 | + } |
| 123 | + |
| 124 | + return allStrategies; |
100 | 125 | } |
101 | 126 |
|
102 | | - // TODO: add a method to get all strategies by a list of pairs. Returns a collection of pairs and their strategies. It will use multicall to call strategiesByPair method from the contracts. |
103 | 127 | public async strategiesByPairs(pairs: TokenPair[]): Promise< |
104 | 128 | { |
105 | 129 | pair: TokenPair; |
106 | 130 | strategies: EncodedStrategy[]; |
107 | 131 | }[] |
108 | 132 | > { |
109 | | - const results = await this._multicall( |
| 133 | + const chunkSize = 1000; |
| 134 | + const results: { pair: TokenPair; strategies: EncodedStrategy[] }[] = []; |
| 135 | + const pairsNeedingMore: { pair: TokenPair; index: number }[] = []; |
| 136 | + |
| 137 | + // First, get the first chunk for all pairs using multicall |
| 138 | + const firstChunkResults = await this._multicall( |
110 | 139 | pairs.map((pair) => ({ |
111 | 140 | contractAddress: this._contracts.carbonController.address, |
112 | 141 | interface: this._contracts.carbonController.interface, |
113 | 142 | methodName: 'strategiesByPair', |
114 | | - methodParameters: [pair[0], pair[1], 0, 0], |
| 143 | + methodParameters: [pair[0], pair[1], 0, chunkSize], |
115 | 144 | })) |
116 | 145 | ); |
117 | | - if (!results || results.length === 0) return []; |
118 | | - console.debug('results', results); |
119 | | - return results.map((result, i) => { |
120 | | - const strategiesResult = result[0] as StrategyStructOutput[] ?? []; |
121 | | - return { |
122 | | - pair: pairs[i], |
| 146 | + |
| 147 | + if (!firstChunkResults || firstChunkResults.length === 0) return []; |
| 148 | + |
| 149 | + // Process first chunk results and identify pairs needing more |
| 150 | + firstChunkResults.forEach((result, i) => { |
| 151 | + const strategiesResult = (result[0] ?? []) as StrategyStructOutput[]; |
| 152 | + const currentPair = pairs[i]; |
| 153 | + |
| 154 | + results.push({ |
| 155 | + pair: currentPair, |
123 | 156 | strategies: strategiesResult.map((r) => toStrategy(r)), |
124 | | - }; |
| 157 | + }); |
| 158 | + |
| 159 | + // If we got a full chunk, we need to fetch more |
| 160 | + if (strategiesResult.length === chunkSize) { |
| 161 | + pairsNeedingMore.push({ pair: currentPair, index: i }); |
| 162 | + } |
125 | 163 | }); |
| 164 | + |
| 165 | + // Fetch remaining strategies for pairs that need it |
| 166 | + for (const { pair, index } of pairsNeedingMore) { |
| 167 | + let startIndex = chunkSize; |
| 168 | + |
| 169 | + while (true) { |
| 170 | + const res = |
| 171 | + (await this._contracts.carbonController.strategiesByPair( |
| 172 | + pair[0], |
| 173 | + pair[1], |
| 174 | + startIndex, |
| 175 | + startIndex + chunkSize |
| 176 | + )) ?? []; |
| 177 | + |
| 178 | + results[index].strategies.push(...res.map((r) => toStrategy(r))); |
| 179 | + |
| 180 | + if (res.length < chunkSize) break; |
| 181 | + startIndex += chunkSize; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return results; |
126 | 186 | } |
127 | 187 |
|
128 | 188 | public async tokensByOwner(owner: string) { |
@@ -164,7 +224,7 @@ export default class Reader implements Fetcher { |
164 | 224 | ); |
165 | 225 | if (!results || results.length === 0) return []; |
166 | 226 | return results.map((res, i) => { |
167 | | - return [pairs[i][0], pairs[i][1], res[0]]; |
| 227 | + return [pairs[i][0], pairs[i][1], Number(res[0])]; |
168 | 228 | }); |
169 | 229 | } |
170 | 230 |
|
|
0 commit comments