@defisaver/positions-sdk 2.1.4 → 2.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/CLAUDE.md +32 -0
  4. package/README.md +64 -64
  5. package/cjs/curveUsd/index.js +2 -1
  6. package/cjs/fluid/index.js +2 -1
  7. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  8. package/cjs/types/curveUsd.d.ts +2 -0
  9. package/esm/curveUsd/index.js +2 -1
  10. package/esm/fluid/index.js +2 -1
  11. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  12. package/esm/types/curveUsd.d.ts +2 -0
  13. package/package.json +47 -47
  14. package/src/aaveV2/index.ts +240 -240
  15. package/src/aaveV3/index.ts +614 -614
  16. package/src/aaveV3/merit.ts +94 -94
  17. package/src/aaveV3/merkl.ts +74 -74
  18. package/src/claiming/aaveV3.ts +154 -154
  19. package/src/claiming/compV3.ts +22 -22
  20. package/src/claiming/index.ts +12 -12
  21. package/src/claiming/king.ts +66 -66
  22. package/src/claiming/morphoBlue.ts +118 -118
  23. package/src/claiming/spark.ts +225 -225
  24. package/src/compoundV2/index.ts +244 -244
  25. package/src/compoundV3/index.ts +274 -274
  26. package/src/config/contracts.ts +1228 -1228
  27. package/src/constants/index.ts +10 -10
  28. package/src/contracts.ts +120 -120
  29. package/src/curveUsd/index.ts +254 -250
  30. package/src/eulerV2/index.ts +324 -324
  31. package/src/exchange/index.ts +25 -25
  32. package/src/fluid/index.ts +1638 -1638
  33. package/src/helpers/aaveHelpers/index.ts +185 -185
  34. package/src/helpers/compoundHelpers/index.ts +283 -283
  35. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  36. package/src/helpers/eulerHelpers/index.ts +222 -222
  37. package/src/helpers/fluidHelpers/index.ts +326 -326
  38. package/src/helpers/index.ts +10 -10
  39. package/src/helpers/liquityV2Helpers/index.ts +82 -82
  40. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  41. package/src/helpers/makerHelpers/index.ts +52 -52
  42. package/src/helpers/morphoBlueHelpers/index.ts +396 -396
  43. package/src/helpers/sparkHelpers/index.ts +155 -155
  44. package/src/index.ts +47 -47
  45. package/src/liquity/index.ts +159 -159
  46. package/src/liquityV2/index.ts +657 -657
  47. package/src/llamaLend/index.ts +305 -305
  48. package/src/maker/index.ts +223 -223
  49. package/src/markets/aave/index.ts +116 -116
  50. package/src/markets/aave/marketAssets.ts +49 -49
  51. package/src/markets/compound/index.ts +227 -227
  52. package/src/markets/compound/marketsAssets.ts +90 -90
  53. package/src/markets/curveUsd/index.ts +69 -69
  54. package/src/markets/euler/index.ts +26 -26
  55. package/src/markets/fluid/index.ts +2456 -2456
  56. package/src/markets/index.ts +25 -25
  57. package/src/markets/liquityV2/index.ts +102 -102
  58. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  59. package/src/markets/llamaLend/index.ts +235 -235
  60. package/src/markets/morphoBlue/index.ts +895 -895
  61. package/src/markets/spark/index.ts +29 -29
  62. package/src/markets/spark/marketAssets.ts +11 -11
  63. package/src/moneymarket/moneymarketCommonService.ts +80 -80
  64. package/src/morphoBlue/index.ts +274 -274
  65. package/src/portfolio/index.ts +570 -570
  66. package/src/services/priceService.ts +159 -159
  67. package/src/services/utils.ts +99 -99
  68. package/src/services/viem.ts +32 -32
  69. package/src/setup.ts +8 -8
  70. package/src/spark/index.ts +445 -445
  71. package/src/staking/eligibility.ts +59 -59
  72. package/src/staking/index.ts +1 -1
  73. package/src/staking/staking.ts +170 -170
  74. package/src/types/aave.ts +189 -189
  75. package/src/types/claiming.ts +109 -109
  76. package/src/types/common.ts +105 -105
  77. package/src/types/compound.ts +136 -136
  78. package/src/types/curveUsd.ts +123 -121
  79. package/src/types/euler.ts +175 -175
  80. package/src/types/fluid.ts +448 -448
  81. package/src/types/index.ts +13 -13
  82. package/src/types/liquity.ts +30 -30
  83. package/src/types/liquityV2.ts +126 -126
  84. package/src/types/llamaLend.ts +159 -159
  85. package/src/types/maker.ts +63 -63
  86. package/src/types/merit.ts +1 -1
  87. package/src/types/merkl.ts +70 -70
  88. package/src/types/morphoBlue.ts +194 -194
  89. package/src/types/portfolio.ts +60 -60
  90. package/src/types/spark.ts +135 -135
  91. package/src/umbrella/index.ts +69 -69
  92. package/src/umbrella/umbrellaUtils.ts +29 -29
@@ -1,570 +1,570 @@
1
- import Dec from 'decimal.js';
2
- import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
3
- import {
4
- AaveMarkets,
5
- CompoundMarkets,
6
- CrvUsdMarkets,
7
- EulerV2Markets,
8
- LiquityV2Markets,
9
- LlamaLendMarkets,
10
- MorphoBlueMarkets,
11
- SparkMarkets,
12
- } from '../markets';
13
- import { _getMorphoBlueAccountData, _getMorphoBlueMarketData, getMorphoEarn } from '../morphoBlue';
14
- import {
15
- AaveV2MarketData,
16
- AaveV3MarketData,
17
- AaveVersions,
18
- CdpInfo,
19
- CompoundV2MarketsData,
20
- CompoundV3MarketsData,
21
- CompoundVersions,
22
- CrvUSDGlobalMarketData,
23
- EulerV2FullMarketData,
24
- LiquityV2MarketData,
25
- LlamaLendGlobalMarketData,
26
- MorphoBlueMarketInfo,
27
- PortfolioPositionsData,
28
- SparkMarketsData,
29
- } from '../types';
30
- import { _getCompoundV3AccountData, _getCompoundV3MarketsData } from '../compoundV3';
31
- import { _getSparkAccountData, _getSparkMarketsData } from '../spark';
32
- import { _getEulerV2AccountData, _getEulerV2MarketsData } from '../eulerV2';
33
- import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
34
- import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
35
- import { _getAaveV3AccountData, _getAaveV3MarketData, getStakeAaveData } from '../aaveV3';
36
- import { ZERO_ADDRESS } from '../constants';
37
- import { _getMakerCdpData, _getUserCdps } from '../maker';
38
- import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
39
- import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
40
- import { getViemProvider } from '../services/viem';
41
- import { _getLiquityTroveInfo, getLiquityStakingData } from '../liquity';
42
- import { _getLiquityV2MarketData, getLiquitySAndYBold, getLiquityV2Staking } from '../liquityV2';
43
- import { _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
44
- import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
45
- import { getUmbrellaData } from '../umbrella';
46
- import { getMeritUnclaimedRewards, getUnclaimedRewardsForAllMarkets } from '../claiming/aaveV3';
47
- import { getCompoundV3Rewards } from '../claiming/compV3';
48
- import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
49
- import { fetchMorphoBlueRewards } from '../claiming/morphoBlue';
50
- import { getKingRewards } from '../claiming/king';
51
-
52
- export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], summerFiAddresses: EthAddress[]): Promise<{
53
- positions: PortfolioPositionsData;
54
- stakingPositions: any;
55
- rewardsData: any;
56
- markets: any;
57
- }> {
58
- const isMainnet = network === NetworkNumber.Eth;
59
- const isFluidSupported = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base].includes(network);
60
-
61
- const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
62
- const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
63
- const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
64
- const eulerV2Markets = Object.values(EulerV2Markets(network)).filter((market) => market.chainIds.includes(network));
65
- const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
66
- const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
67
- const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
68
- const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
69
- const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
70
- const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
71
- const liquityV2MarketsStaking = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)).filter(market => !market.isLegacy) : [];
72
-
73
- const client = getViemProvider(provider, network, {
74
- batch: {
75
- multicall: {
76
- batchSize: 2500000,
77
- },
78
- },
79
- });
80
- const defaultClient = getViemProvider(defaultProvider, network, {
81
- batch: {
82
- multicall: {
83
- batchSize: 2500000,
84
- },
85
- },
86
- });
87
-
88
- const morphoMarketsData: Record<string, MorphoBlueMarketInfo> = {};
89
- const compoundV3MarketsData: Record<string, CompoundV3MarketsData> = {};
90
- const sparkMarketsData: Record<string, SparkMarketsData> = {};
91
- const eulerV2MarketsData: Record<string, EulerV2FullMarketData> = {};
92
- const aaveV3MarketsData: Record<string, AaveV3MarketData> = {};
93
- const makerCdps: Record<string, CdpInfo[]> = {};
94
- const aaveV2MarketsData: Record<string, AaveV2MarketData> = {};
95
- const compoundV2MarketsData: Record<string, CompoundV2MarketsData> = {};
96
- const crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData> = {};
97
- const llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData> = {};
98
- const liquityV2MarketsData: Record<string, LiquityV2MarketData> = {};
99
-
100
- const markets = {
101
- morphoMarketsData,
102
- compoundV3MarketsData,
103
- sparkMarketsData,
104
- eulerV2MarketsData,
105
- aaveV3MarketsData,
106
- aaveV2MarketsData,
107
- compoundV2MarketsData,
108
- crvUsdMarketsData,
109
- llamaLendMarketsData,
110
- liquityV2MarketsData,
111
- };
112
-
113
- const positions: PortfolioPositionsData = {};
114
- const stakingPositions: any = {};
115
- const rewardsData: any = {};
116
- const allAddresses = [...addresses, ...summerFiAddresses];
117
-
118
- for (const address of allAddresses) {
119
- positions[address.toLowerCase() as EthAddress] = {
120
- aaveV3: {},
121
- morphoBlue: {},
122
- compoundV3: {},
123
- spark: {},
124
- eulerV2: {},
125
- maker: {},
126
- aaveV2: {},
127
- compoundV2: {},
128
- liquity: {},
129
- crvUsd: {},
130
- llamaLend: {},
131
- fluid: {
132
- error: '',
133
- data: {},
134
- },
135
- };
136
- }
137
-
138
- // TODO: check default values, probably needed when fetching portfolio on unsupported networks
139
- for (const address of addresses) {
140
- stakingPositions[address.toLowerCase() as EthAddress] = {
141
- aaveV3: {},
142
- morphoBlue: {},
143
- compoundV3: {},
144
- spark: {},
145
- aaveV2: {},
146
- compoundV2: {},
147
- liquity: {},
148
- liquityV2: {},
149
- fluid: {
150
- error: '',
151
- data: {},
152
- },
153
- };
154
-
155
- rewardsData[address.toLowerCase() as EthAddress] = {
156
- aaveV3merit: {},
157
- aaveV3: {},
158
- compV3: {},
159
- spark: {},
160
- spk: {},
161
- king: {},
162
- morpho: {},
163
- };
164
- }
165
-
166
- await Promise.allSettled([
167
- // === MARKET DATA (needs to be fetched first) ===
168
- ...morphoMarkets.map(async (market) => {
169
- const marketData = await _getMorphoBlueMarketData(client, network, market);
170
- morphoMarketsData[market.value] = marketData;
171
- }),
172
- ...compoundV3Markets.map(async (market) => {
173
- const marketData = await _getCompoundV3MarketsData(client, network, market, defaultClient);
174
- compoundV3MarketsData[market.value] = marketData;
175
- }),
176
- ...sparkMarkets.map(async (market) => {
177
- const marketData = await _getSparkMarketsData(client, network, market);
178
- sparkMarketsData[market.value] = marketData;
179
- }),
180
- ...eulerV2Markets.map(async (market) => {
181
- const marketData = await _getEulerV2MarketsData(client, network, market);
182
- eulerV2MarketsData[market.value] = marketData;
183
- }),
184
- ...aaveV3Markets.map(async (market) => {
185
- const marketData = await _getAaveV3MarketData(client, network, market);
186
- aaveV3MarketsData[market.value] = marketData;
187
- }),
188
- ...aaveV2Markets.map(async (market) => {
189
- const marketData = await _getAaveV2MarketsData(client, network, market);
190
- aaveV2MarketsData[market.value] = marketData;
191
- }),
192
- ...compoundV2Markets.map(async (market) => {
193
- const marketData = await _getCompoundV2MarketsData(client, network);
194
- compoundV2MarketsData[market.value] = marketData;
195
- }),
196
- ...crvUsdMarkets.map(async (market) => {
197
- const marketData = await _getCurveUsdGlobalData(client, network, market);
198
- crvUsdMarketsData[market.value] = marketData;
199
- }),
200
- ...llamaLendMarkets.map(async (market) => {
201
- const marketData = await _getLlamaLendGlobalData(client, network, market);
202
- llamaLendMarketsData[market.value] = marketData;
203
- }),
204
- ...liquityV2Markets.map(async (market) => {
205
- const marketData = await _getLiquityV2MarketData(client, network, market);
206
- liquityV2MarketsData[market.value] = marketData;
207
- }),
208
-
209
- // === INDEPENDENT USER DATA (doesn't depend on market data) ===
210
- ...addresses.map(async (address) => {
211
- if (!isMainnet) return; // Maker CDPs are only available on mainnet
212
- const makerCdp = await _getUserCdps(client, network, address);
213
- makerCdps[address.toLowerCase() as EthAddress] = makerCdp;
214
- }),
215
- ...addresses.map(async (address) => {
216
- try {
217
- if (!isFluidSupported) return; // Fluid is not available on Optimism
218
- const userPositions = await _getUserPositionsPortfolio(client, network, address);
219
- for (const position of userPositions) {
220
- if (new Dec(position.userData.suppliedUsd).gt(0)) {
221
- positions[address.toLowerCase() as EthAddress].fluid.data[position.userData.nftId] = position.userData;
222
- }
223
- }
224
- } catch (error) {
225
- console.error(`Error fetching Fluid positions for address ${address}:`, error);
226
- positions[address.toLowerCase() as EthAddress].fluid = {
227
- error: `Error fetching Fluid positions for address ${address}`,
228
- data: {},
229
- };
230
- }
231
- }),
232
-
233
- // === STAKING DATA (independent of market data) ===
234
- ...addresses.map(async (address) => {
235
- try {
236
- if (!isFluidSupported) return;
237
- stakingPositions[address.toLowerCase()].fluid = await _getAllUserEarnPositionsWithFTokens(client, network, address);
238
- } catch (error) {
239
- console.error(`Error fetching Fluid lend data for address ${address}:`, error);
240
- stakingPositions[address.toLowerCase()].fluid = { error: `Error fetching Fluid lend data for address ${address}`, data: null };
241
- }
242
- }),
243
- ...addresses.map(async (address) => {
244
- try {
245
- if (!isMainnet) return;
246
- stakingPositions[address.toLowerCase()].liquity = await getLiquityStakingData(client, network, address);
247
- } catch (error) {
248
- console.error(`Error fetching Liquity staking data for address ${address}:`, error);
249
- stakingPositions[address.toLowerCase()].liquity = { error: `Error fetching Liquity staking data for address ${address}`, data: null };
250
- }
251
- }),
252
- ...addresses.map(async (address) => {
253
- try {
254
- if (!isMainnet) return;
255
- stakingPositions[address.toLowerCase()].aaveV3 = await getStakeAaveData(client, network, address);
256
- } catch (error) {
257
- console.error(`Error fetching Aave V3 staking data for address ${address}:`, error);
258
- stakingPositions[address.toLowerCase()].aaveV3 = { error: `Error fetching Aave V3 staking data for address ${address}`, data: null };
259
- }
260
- }),
261
- ...addresses.map(async (address) => {
262
- try {
263
- if (!isMainnet) return;
264
- stakingPositions[address.toLowerCase()].umbrella = await getUmbrellaData(client, network, address);
265
- } catch (error) {
266
- console.error(`Error fetching Umbrella staking data for address ${address}:`, error);
267
- stakingPositions[address.toLowerCase()].umbrella = { error: `Error fetching Umbrella staking data for address ${address}`, data: null };
268
- }
269
- }),
270
- // Liquity V2 staking
271
- ...liquityV2MarketsStaking.map(market => addresses.map(async (address) => {
272
- try {
273
- if (!isMainnet) {
274
- stakingPositions[address.toLowerCase()].liquityV2[market.value] = { error: '', data: null };
275
- return;
276
- }
277
- const liquityV2StakingData = await getLiquityV2Staking(client, network, market.value, address);
278
- stakingPositions[address.toLowerCase()].liquityV2[market.value] = { error: '', data: liquityV2StakingData };
279
- } catch (error) {
280
- console.error(`Error fetching Liquity V2 staking data for address ${address}, market ${market.value}:`, error);
281
- stakingPositions[address.toLowerCase()].liquityV2[market.value] = { error: `Error fetching Liquity V2 staking data for address ${address}`, data: null };
282
- }
283
- })).flat(),
284
-
285
- // === REWARDS DATA (independent of market data) ===
286
- // Batch King rewards
287
- (async () => {
288
- try {
289
- if (!isMainnet) {
290
- for (const address of addresses) {
291
- rewardsData[address.toLowerCase()].king = { error: '', data: [] };
292
- }
293
- return;
294
- }
295
- const kingRewards = await getKingRewards(client, network, addresses);
296
- for (const address of addresses) {
297
- const lowerAddress = address.toLowerCase() as EthAddress;
298
- rewardsData[lowerAddress].king = {
299
- error: '',
300
- data: kingRewards[lowerAddress] || [],
301
- };
302
- }
303
- } catch (error) {
304
- console.error('Error fetching King rewards data in batch:', error);
305
- for (const address of addresses) {
306
- rewardsData[address.toLowerCase() as EthAddress].king = {
307
- error: 'Error fetching King rewards data in batch',
308
- data: null,
309
- };
310
- }
311
- }
312
- })(),
313
- ...sparkMarkets.map((market) => addresses.map(async address => {
314
- try {
315
- if (!isMainnet) {
316
- rewardsData[address.toLowerCase()].spark[market.value] = { error: '', data: [] };
317
- return;
318
- }
319
- const sparkData = await fetchSparkRewards(client, network, address, market.providerAddress);
320
- rewardsData[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: sparkData };
321
- } catch (error) {
322
- console.error(`Error fetching Spark rewards data for address ${address}, market ${market.value}:`, error);
323
- rewardsData[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark rewards data for address ${address}`, data: null };
324
- }
325
- })).flat(),
326
- // CompV3 rewards
327
- ...compoundV3Markets.map(market => addresses.map(async (address) => {
328
- try {
329
- const compV3Rewards = await getCompoundV3Rewards(client, network, address, market.baseMarketAddress);
330
- rewardsData[address.toLowerCase() as EthAddress].compV3[market.value] = { error: '', data: compV3Rewards };
331
- } catch (error) {
332
- console.error(`Error fetching Compound V3 rewards data for address ${address}:`, error);
333
- rewardsData[address.toLowerCase() as EthAddress].compV3[market.value] = { error: `Error fetching Compound V3 rewards data for address ${address}`, data: null };
334
- }
335
- })).flat(),
336
- ...addresses.map(async (address) => {
337
- try {
338
- const aaveMeritData = await getMeritUnclaimedRewards(address, network);
339
- rewardsData[address.toLowerCase() as EthAddress].aaveV3merit = { error: '', data: aaveMeritData };
340
- } catch (error) {
341
- console.error(`Error fetching Aave V3 Merit rewards data for address ${address}:`, error);
342
- rewardsData[address.toLowerCase() as EthAddress].aaveV3merit = { error: `Error fetching Aave V3 Merit rewards data for address ${address}`, data: null };
343
- }
344
- }),
345
- ...aaveV3Markets.map(market => addresses.map(async (address) => {
346
- try {
347
- const aaveData = await getUnclaimedRewardsForAllMarkets(client, network, address, market.providerAddress);
348
- rewardsData[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: aaveData };
349
- } catch (error) {
350
- console.error(`Error fetching Aave V3 Merit rewards data for address ${address}:`, error);
351
- rewardsData[address.toLowerCase() as EthAddress].aaveV3 = { error: `Error fetching Aave V3 rewards data for address ${address}`, data: null };
352
- }
353
- })).flat(),
354
- // Batch Morpho Blue rewards
355
- (async () => {
356
- try {
357
- const morphoRewards = await fetchMorphoBlueRewards(client, network, addresses);
358
- for (const address of addresses) {
359
- const lowerAddress = address.toLowerCase() as EthAddress;
360
- rewardsData[lowerAddress].morpho = {
361
- error: '',
362
- data: morphoRewards[lowerAddress] || [],
363
- };
364
- }
365
- } catch (error) {
366
- console.error('Error fetching Morpho Blue rewards data in batch:', error);
367
- for (const address of addresses) {
368
- rewardsData[address.toLowerCase() as EthAddress].morpho = {
369
- error: 'Error fetching Morpho Blue rewards data in batch',
370
- data: null,
371
- };
372
- }
373
- }
374
- })(),
375
- // Batch Spark Airdrop rewards
376
- (async () => {
377
- try {
378
- if (!isMainnet) {
379
- for (const address of addresses) {
380
- rewardsData[address.toLowerCase()].spk = { error: '', data: [] };
381
- }
382
- return;
383
- }
384
-
385
- const sparkAirdropRewards = await fetchSparkAirdropRewards(client, network, addresses);
386
- for (const address of addresses) {
387
- const lowerAddress = address.toLowerCase() as EthAddress;
388
- rewardsData[lowerAddress].spk = {
389
- error: '',
390
- data: sparkAirdropRewards[lowerAddress] || [],
391
- };
392
- }
393
- } catch (error) {
394
- console.error('Error fetching Spark Airdrop rewards data in batch:', error);
395
- for (const address of addresses) {
396
- rewardsData[address.toLowerCase() as EthAddress].spk = {
397
- error: 'Error fetching Spark Airdrop rewards data in batch',
398
- data: null,
399
- };
400
- }
401
- }
402
- })(),
403
- ]);
404
-
405
-
406
- await Promise.all([
407
- ...aaveV3Markets.map((market) => allAddresses.map(async (address) => {
408
- try {
409
- const accData = await _getAaveV3AccountData(client, network, address, { selectedMarket: market, ...aaveV3MarketsData[market.value] });
410
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: accData };
411
- } catch (error) {
412
- console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
413
- positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
414
- }
415
- })).flat(),
416
- ...morphoMarkets.map((market) => addresses.map(async (address) => {
417
- try {
418
- const [accDataPromise, earnDataPromise] = await Promise.allSettled([
419
- _getMorphoBlueAccountData(client, network, address, market, morphoMarketsData[market.value]),
420
- getMorphoEarn(client, network, address, market, morphoMarketsData[market.value]),
421
- ]);
422
- if (accDataPromise.status === 'rejected') {
423
- console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, accDataPromise.reason);
424
- positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
425
- }
426
- if (earnDataPromise.status === 'rejected') {
427
- console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, earnDataPromise.reason);
428
- positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
429
- }
430
- if (accDataPromise.status !== 'rejected') {
431
- const accData = accDataPromise.value;
432
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: '', data: accData };
433
- }
434
- if (earnDataPromise.status !== 'rejected') {
435
- const earnData = earnDataPromise.value;
436
- if (earnData && new Dec(earnData.amount).gt(0)) {
437
- stakingPositions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = {
438
- error: '',
439
- data: earnData,
440
- };
441
- }
442
- }
443
- } catch (error) {
444
- console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
445
- positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
446
- }
447
- })).flat(),
448
- ...compoundV3Markets.map((market) => addresses.map(async (address) => {
449
- try {
450
- const accData = await _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: compoundV3MarketsData[market.value].assetsData });
451
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: '', data: accData };
452
- } catch (error) {
453
- console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
454
- positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
455
- }
456
- })).flat(),
457
- ...sparkMarkets.map((market) => allAddresses.map(async (address) => {
458
- try {
459
- const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: sparkMarketsData[market.value].assetsData });
460
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: accData };
461
- } catch (error) {
462
- console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
463
- positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
464
- }
465
- })).flat(),
466
- ...eulerV2Markets.map((market) => addresses.map((address) => {
467
- const eulerV2SubAccounts = getEulerV2SubAccounts(address);
468
- const eulerV2Addresses = [address, ...eulerV2SubAccounts];
469
- return Promise.all(eulerV2Addresses.map(async (eulerAddress) => {
470
- try {
471
- const accData = await _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
472
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
473
- if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
474
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
475
- }
476
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: '', data: accData };
477
- }
478
- } catch (error) {
479
- console.error(`Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}:`, error);
480
- if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
481
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
482
- }
483
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: `Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}`, data: null };
484
- }
485
- }));
486
- }).flat()).flat(),
487
- ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
488
- try {
489
- const cdpData = await _getMakerCdpData(client, network, cdpInfo);
490
- if (cdpData) {
491
- positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: '', data: cdpData };
492
- }
493
- } catch (error) {
494
- console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
495
- positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
496
- }
497
- })).flat(),
498
- ...aaveV2Markets.map((market) => addresses.map(async (address) => {
499
- try {
500
- const accData = await _getAaveV2AccountData(client, network, address, aaveV2MarketsData[market.value].assetsData, market);
501
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: '', data: accData };
502
- } catch (error) {
503
- console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
504
- positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
505
- }
506
- })).flat(),
507
- ...compoundV2Markets.map((market) => addresses.map(async (address) => {
508
- try {
509
- const accData = await _getCompoundV2AccountData(client, network, address, compoundV2MarketsData[market.value].assetsData);
510
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: '', data: accData };
511
- } catch (error) {
512
- console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
513
- positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
514
- }
515
- })).flat(),
516
- ...addresses.map(async (address) => {
517
- try {
518
- if (!isMainnet) return; // Liquity trove info is only available on mainnet
519
- const troveInfo = await _getLiquityTroveInfo(client, network, address);
520
- if (new Dec(troveInfo.collateral).gt(0)) positions[address.toLowerCase() as EthAddress].liquity = { error: '', data: troveInfo };
521
- } catch (error) {
522
- console.error(`Error fetching Liquity trove info for address ${address}:`, error);
523
- positions[address.toLowerCase() as EthAddress].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
524
- }
525
- }),
526
- ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
527
- try {
528
- const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
529
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
530
- positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: { ...accData, borrowRate: crvUsdMarketsData[market.value].borrowRate } };
531
- }
532
- } catch (error) {
533
- console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
534
- positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
535
- }
536
- })).flat(),
537
- ...llamaLendMarkets.map((market) => addresses.map(async (address) => {
538
- try {
539
- const accData = await _getLlamaLendUserData(client, network, address, market, llamaLendMarketsData[market.value]);
540
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
541
- positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: '', data: { ...accData, borrowRate: llamaLendMarketsData[market.value].borrowRate } };
542
- }
543
- } catch (error) {
544
- console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
545
- positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
546
- }
547
- })).flat(),
548
- // liquity sBold/yBold and staking options
549
- ...addresses.map(async (address) => {
550
- try {
551
- if (!isMainnet) {
552
- stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data: null };
553
- return;
554
- }
555
- const data = await getLiquitySAndYBold(client, network, stakingPositions[address.toLowerCase()].liquityV2, address);
556
- stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data };
557
- } catch (error) {
558
- console.error(`Error fetching SBold/YBold data for address ${address}:`, error);
559
- stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: `Error fetching sBold/yBold data for address ${address}`, data: null };
560
- }
561
- }),
562
- ]);
563
-
564
- return {
565
- positions,
566
- stakingPositions,
567
- rewardsData,
568
- markets,
569
- };
570
- }
1
+ import Dec from 'decimal.js';
2
+ import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
3
+ import {
4
+ AaveMarkets,
5
+ CompoundMarkets,
6
+ CrvUsdMarkets,
7
+ EulerV2Markets,
8
+ LiquityV2Markets,
9
+ LlamaLendMarkets,
10
+ MorphoBlueMarkets,
11
+ SparkMarkets,
12
+ } from '../markets';
13
+ import { _getMorphoBlueAccountData, _getMorphoBlueMarketData, getMorphoEarn } from '../morphoBlue';
14
+ import {
15
+ AaveV2MarketData,
16
+ AaveV3MarketData,
17
+ AaveVersions,
18
+ CdpInfo,
19
+ CompoundV2MarketsData,
20
+ CompoundV3MarketsData,
21
+ CompoundVersions,
22
+ CrvUSDGlobalMarketData,
23
+ EulerV2FullMarketData,
24
+ LiquityV2MarketData,
25
+ LlamaLendGlobalMarketData,
26
+ MorphoBlueMarketInfo,
27
+ PortfolioPositionsData,
28
+ SparkMarketsData,
29
+ } from '../types';
30
+ import { _getCompoundV3AccountData, _getCompoundV3MarketsData } from '../compoundV3';
31
+ import { _getSparkAccountData, _getSparkMarketsData } from '../spark';
32
+ import { _getEulerV2AccountData, _getEulerV2MarketsData } from '../eulerV2';
33
+ import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
34
+ import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
35
+ import { _getAaveV3AccountData, _getAaveV3MarketData, getStakeAaveData } from '../aaveV3';
36
+ import { ZERO_ADDRESS } from '../constants';
37
+ import { _getMakerCdpData, _getUserCdps } from '../maker';
38
+ import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
39
+ import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
40
+ import { getViemProvider } from '../services/viem';
41
+ import { _getLiquityTroveInfo, getLiquityStakingData } from '../liquity';
42
+ import { _getLiquityV2MarketData, getLiquitySAndYBold, getLiquityV2Staking } from '../liquityV2';
43
+ import { _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
44
+ import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
45
+ import { getUmbrellaData } from '../umbrella';
46
+ import { getMeritUnclaimedRewards, getUnclaimedRewardsForAllMarkets } from '../claiming/aaveV3';
47
+ import { getCompoundV3Rewards } from '../claiming/compV3';
48
+ import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
49
+ import { fetchMorphoBlueRewards } from '../claiming/morphoBlue';
50
+ import { getKingRewards } from '../claiming/king';
51
+
52
+ export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], summerFiAddresses: EthAddress[]): Promise<{
53
+ positions: PortfolioPositionsData;
54
+ stakingPositions: any;
55
+ rewardsData: any;
56
+ markets: any;
57
+ }> {
58
+ const isMainnet = network === NetworkNumber.Eth;
59
+ const isFluidSupported = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base].includes(network);
60
+
61
+ const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
62
+ const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
63
+ const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
64
+ const eulerV2Markets = Object.values(EulerV2Markets(network)).filter((market) => market.chainIds.includes(network));
65
+ const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
66
+ const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
67
+ const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
68
+ const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
69
+ const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
70
+ const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
71
+ const liquityV2MarketsStaking = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)).filter(market => !market.isLegacy) : [];
72
+
73
+ const client = getViemProvider(provider, network, {
74
+ batch: {
75
+ multicall: {
76
+ batchSize: 2500000,
77
+ },
78
+ },
79
+ });
80
+ const defaultClient = getViemProvider(defaultProvider, network, {
81
+ batch: {
82
+ multicall: {
83
+ batchSize: 2500000,
84
+ },
85
+ },
86
+ });
87
+
88
+ const morphoMarketsData: Record<string, MorphoBlueMarketInfo> = {};
89
+ const compoundV3MarketsData: Record<string, CompoundV3MarketsData> = {};
90
+ const sparkMarketsData: Record<string, SparkMarketsData> = {};
91
+ const eulerV2MarketsData: Record<string, EulerV2FullMarketData> = {};
92
+ const aaveV3MarketsData: Record<string, AaveV3MarketData> = {};
93
+ const makerCdps: Record<string, CdpInfo[]> = {};
94
+ const aaveV2MarketsData: Record<string, AaveV2MarketData> = {};
95
+ const compoundV2MarketsData: Record<string, CompoundV2MarketsData> = {};
96
+ const crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData> = {};
97
+ const llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData> = {};
98
+ const liquityV2MarketsData: Record<string, LiquityV2MarketData> = {};
99
+
100
+ const markets = {
101
+ morphoMarketsData,
102
+ compoundV3MarketsData,
103
+ sparkMarketsData,
104
+ eulerV2MarketsData,
105
+ aaveV3MarketsData,
106
+ aaveV2MarketsData,
107
+ compoundV2MarketsData,
108
+ crvUsdMarketsData,
109
+ llamaLendMarketsData,
110
+ liquityV2MarketsData,
111
+ };
112
+
113
+ const positions: PortfolioPositionsData = {};
114
+ const stakingPositions: any = {};
115
+ const rewardsData: any = {};
116
+ const allAddresses = [...addresses, ...summerFiAddresses];
117
+
118
+ for (const address of allAddresses) {
119
+ positions[address.toLowerCase() as EthAddress] = {
120
+ aaveV3: {},
121
+ morphoBlue: {},
122
+ compoundV3: {},
123
+ spark: {},
124
+ eulerV2: {},
125
+ maker: {},
126
+ aaveV2: {},
127
+ compoundV2: {},
128
+ liquity: {},
129
+ crvUsd: {},
130
+ llamaLend: {},
131
+ fluid: {
132
+ error: '',
133
+ data: {},
134
+ },
135
+ };
136
+ }
137
+
138
+ // TODO: check default values, probably needed when fetching portfolio on unsupported networks
139
+ for (const address of addresses) {
140
+ stakingPositions[address.toLowerCase() as EthAddress] = {
141
+ aaveV3: {},
142
+ morphoBlue: {},
143
+ compoundV3: {},
144
+ spark: {},
145
+ aaveV2: {},
146
+ compoundV2: {},
147
+ liquity: {},
148
+ liquityV2: {},
149
+ fluid: {
150
+ error: '',
151
+ data: {},
152
+ },
153
+ };
154
+
155
+ rewardsData[address.toLowerCase() as EthAddress] = {
156
+ aaveV3merit: {},
157
+ aaveV3: {},
158
+ compV3: {},
159
+ spark: {},
160
+ spk: {},
161
+ king: {},
162
+ morpho: {},
163
+ };
164
+ }
165
+
166
+ await Promise.allSettled([
167
+ // === MARKET DATA (needs to be fetched first) ===
168
+ ...morphoMarkets.map(async (market) => {
169
+ const marketData = await _getMorphoBlueMarketData(client, network, market);
170
+ morphoMarketsData[market.value] = marketData;
171
+ }),
172
+ ...compoundV3Markets.map(async (market) => {
173
+ const marketData = await _getCompoundV3MarketsData(client, network, market, defaultClient);
174
+ compoundV3MarketsData[market.value] = marketData;
175
+ }),
176
+ ...sparkMarkets.map(async (market) => {
177
+ const marketData = await _getSparkMarketsData(client, network, market);
178
+ sparkMarketsData[market.value] = marketData;
179
+ }),
180
+ ...eulerV2Markets.map(async (market) => {
181
+ const marketData = await _getEulerV2MarketsData(client, network, market);
182
+ eulerV2MarketsData[market.value] = marketData;
183
+ }),
184
+ ...aaveV3Markets.map(async (market) => {
185
+ const marketData = await _getAaveV3MarketData(client, network, market);
186
+ aaveV3MarketsData[market.value] = marketData;
187
+ }),
188
+ ...aaveV2Markets.map(async (market) => {
189
+ const marketData = await _getAaveV2MarketsData(client, network, market);
190
+ aaveV2MarketsData[market.value] = marketData;
191
+ }),
192
+ ...compoundV2Markets.map(async (market) => {
193
+ const marketData = await _getCompoundV2MarketsData(client, network);
194
+ compoundV2MarketsData[market.value] = marketData;
195
+ }),
196
+ ...crvUsdMarkets.map(async (market) => {
197
+ const marketData = await _getCurveUsdGlobalData(client, network, market);
198
+ crvUsdMarketsData[market.value] = marketData;
199
+ }),
200
+ ...llamaLendMarkets.map(async (market) => {
201
+ const marketData = await _getLlamaLendGlobalData(client, network, market);
202
+ llamaLendMarketsData[market.value] = marketData;
203
+ }),
204
+ ...liquityV2Markets.map(async (market) => {
205
+ const marketData = await _getLiquityV2MarketData(client, network, market);
206
+ liquityV2MarketsData[market.value] = marketData;
207
+ }),
208
+
209
+ // === INDEPENDENT USER DATA (doesn't depend on market data) ===
210
+ ...addresses.map(async (address) => {
211
+ if (!isMainnet) return; // Maker CDPs are only available on mainnet
212
+ const makerCdp = await _getUserCdps(client, network, address);
213
+ makerCdps[address.toLowerCase() as EthAddress] = makerCdp;
214
+ }),
215
+ ...addresses.map(async (address) => {
216
+ try {
217
+ if (!isFluidSupported) return; // Fluid is not available on Optimism
218
+ const userPositions = await _getUserPositionsPortfolio(client, network, address);
219
+ for (const position of userPositions) {
220
+ if (new Dec(position.userData.suppliedUsd).gt(0)) {
221
+ positions[address.toLowerCase() as EthAddress].fluid.data[position.userData.nftId] = position.userData;
222
+ }
223
+ }
224
+ } catch (error) {
225
+ console.error(`Error fetching Fluid positions for address ${address}:`, error);
226
+ positions[address.toLowerCase() as EthAddress].fluid = {
227
+ error: `Error fetching Fluid positions for address ${address}`,
228
+ data: {},
229
+ };
230
+ }
231
+ }),
232
+
233
+ // === STAKING DATA (independent of market data) ===
234
+ ...addresses.map(async (address) => {
235
+ try {
236
+ if (!isFluidSupported) return;
237
+ stakingPositions[address.toLowerCase()].fluid = await _getAllUserEarnPositionsWithFTokens(client, network, address);
238
+ } catch (error) {
239
+ console.error(`Error fetching Fluid lend data for address ${address}:`, error);
240
+ stakingPositions[address.toLowerCase()].fluid = { error: `Error fetching Fluid lend data for address ${address}`, data: null };
241
+ }
242
+ }),
243
+ ...addresses.map(async (address) => {
244
+ try {
245
+ if (!isMainnet) return;
246
+ stakingPositions[address.toLowerCase()].liquity = await getLiquityStakingData(client, network, address);
247
+ } catch (error) {
248
+ console.error(`Error fetching Liquity staking data for address ${address}:`, error);
249
+ stakingPositions[address.toLowerCase()].liquity = { error: `Error fetching Liquity staking data for address ${address}`, data: null };
250
+ }
251
+ }),
252
+ ...addresses.map(async (address) => {
253
+ try {
254
+ if (!isMainnet) return;
255
+ stakingPositions[address.toLowerCase()].aaveV3 = await getStakeAaveData(client, network, address);
256
+ } catch (error) {
257
+ console.error(`Error fetching Aave V3 staking data for address ${address}:`, error);
258
+ stakingPositions[address.toLowerCase()].aaveV3 = { error: `Error fetching Aave V3 staking data for address ${address}`, data: null };
259
+ }
260
+ }),
261
+ ...addresses.map(async (address) => {
262
+ try {
263
+ if (!isMainnet) return;
264
+ stakingPositions[address.toLowerCase()].umbrella = await getUmbrellaData(client, network, address);
265
+ } catch (error) {
266
+ console.error(`Error fetching Umbrella staking data for address ${address}:`, error);
267
+ stakingPositions[address.toLowerCase()].umbrella = { error: `Error fetching Umbrella staking data for address ${address}`, data: null };
268
+ }
269
+ }),
270
+ // Liquity V2 staking
271
+ ...liquityV2MarketsStaking.map(market => addresses.map(async (address) => {
272
+ try {
273
+ if (!isMainnet) {
274
+ stakingPositions[address.toLowerCase()].liquityV2[market.value] = { error: '', data: null };
275
+ return;
276
+ }
277
+ const liquityV2StakingData = await getLiquityV2Staking(client, network, market.value, address);
278
+ stakingPositions[address.toLowerCase()].liquityV2[market.value] = { error: '', data: liquityV2StakingData };
279
+ } catch (error) {
280
+ console.error(`Error fetching Liquity V2 staking data for address ${address}, market ${market.value}:`, error);
281
+ stakingPositions[address.toLowerCase()].liquityV2[market.value] = { error: `Error fetching Liquity V2 staking data for address ${address}`, data: null };
282
+ }
283
+ })).flat(),
284
+
285
+ // === REWARDS DATA (independent of market data) ===
286
+ // Batch King rewards
287
+ (async () => {
288
+ try {
289
+ if (!isMainnet) {
290
+ for (const address of addresses) {
291
+ rewardsData[address.toLowerCase()].king = { error: '', data: [] };
292
+ }
293
+ return;
294
+ }
295
+ const kingRewards = await getKingRewards(client, network, addresses);
296
+ for (const address of addresses) {
297
+ const lowerAddress = address.toLowerCase() as EthAddress;
298
+ rewardsData[lowerAddress].king = {
299
+ error: '',
300
+ data: kingRewards[lowerAddress] || [],
301
+ };
302
+ }
303
+ } catch (error) {
304
+ console.error('Error fetching King rewards data in batch:', error);
305
+ for (const address of addresses) {
306
+ rewardsData[address.toLowerCase() as EthAddress].king = {
307
+ error: 'Error fetching King rewards data in batch',
308
+ data: null,
309
+ };
310
+ }
311
+ }
312
+ })(),
313
+ ...sparkMarkets.map((market) => addresses.map(async address => {
314
+ try {
315
+ if (!isMainnet) {
316
+ rewardsData[address.toLowerCase()].spark[market.value] = { error: '', data: [] };
317
+ return;
318
+ }
319
+ const sparkData = await fetchSparkRewards(client, network, address, market.providerAddress);
320
+ rewardsData[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: sparkData };
321
+ } catch (error) {
322
+ console.error(`Error fetching Spark rewards data for address ${address}, market ${market.value}:`, error);
323
+ rewardsData[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark rewards data for address ${address}`, data: null };
324
+ }
325
+ })).flat(),
326
+ // CompV3 rewards
327
+ ...compoundV3Markets.map(market => addresses.map(async (address) => {
328
+ try {
329
+ const compV3Rewards = await getCompoundV3Rewards(client, network, address, market.baseMarketAddress);
330
+ rewardsData[address.toLowerCase() as EthAddress].compV3[market.value] = { error: '', data: compV3Rewards };
331
+ } catch (error) {
332
+ console.error(`Error fetching Compound V3 rewards data for address ${address}:`, error);
333
+ rewardsData[address.toLowerCase() as EthAddress].compV3[market.value] = { error: `Error fetching Compound V3 rewards data for address ${address}`, data: null };
334
+ }
335
+ })).flat(),
336
+ ...addresses.map(async (address) => {
337
+ try {
338
+ const aaveMeritData = await getMeritUnclaimedRewards(address, network);
339
+ rewardsData[address.toLowerCase() as EthAddress].aaveV3merit = { error: '', data: aaveMeritData };
340
+ } catch (error) {
341
+ console.error(`Error fetching Aave V3 Merit rewards data for address ${address}:`, error);
342
+ rewardsData[address.toLowerCase() as EthAddress].aaveV3merit = { error: `Error fetching Aave V3 Merit rewards data for address ${address}`, data: null };
343
+ }
344
+ }),
345
+ ...aaveV3Markets.map(market => addresses.map(async (address) => {
346
+ try {
347
+ const aaveData = await getUnclaimedRewardsForAllMarkets(client, network, address, market.providerAddress);
348
+ rewardsData[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: aaveData };
349
+ } catch (error) {
350
+ console.error(`Error fetching Aave V3 Merit rewards data for address ${address}:`, error);
351
+ rewardsData[address.toLowerCase() as EthAddress].aaveV3 = { error: `Error fetching Aave V3 rewards data for address ${address}`, data: null };
352
+ }
353
+ })).flat(),
354
+ // Batch Morpho Blue rewards
355
+ (async () => {
356
+ try {
357
+ const morphoRewards = await fetchMorphoBlueRewards(client, network, addresses);
358
+ for (const address of addresses) {
359
+ const lowerAddress = address.toLowerCase() as EthAddress;
360
+ rewardsData[lowerAddress].morpho = {
361
+ error: '',
362
+ data: morphoRewards[lowerAddress] || [],
363
+ };
364
+ }
365
+ } catch (error) {
366
+ console.error('Error fetching Morpho Blue rewards data in batch:', error);
367
+ for (const address of addresses) {
368
+ rewardsData[address.toLowerCase() as EthAddress].morpho = {
369
+ error: 'Error fetching Morpho Blue rewards data in batch',
370
+ data: null,
371
+ };
372
+ }
373
+ }
374
+ })(),
375
+ // Batch Spark Airdrop rewards
376
+ (async () => {
377
+ try {
378
+ if (!isMainnet) {
379
+ for (const address of addresses) {
380
+ rewardsData[address.toLowerCase()].spk = { error: '', data: [] };
381
+ }
382
+ return;
383
+ }
384
+
385
+ const sparkAirdropRewards = await fetchSparkAirdropRewards(client, network, addresses);
386
+ for (const address of addresses) {
387
+ const lowerAddress = address.toLowerCase() as EthAddress;
388
+ rewardsData[lowerAddress].spk = {
389
+ error: '',
390
+ data: sparkAirdropRewards[lowerAddress] || [],
391
+ };
392
+ }
393
+ } catch (error) {
394
+ console.error('Error fetching Spark Airdrop rewards data in batch:', error);
395
+ for (const address of addresses) {
396
+ rewardsData[address.toLowerCase() as EthAddress].spk = {
397
+ error: 'Error fetching Spark Airdrop rewards data in batch',
398
+ data: null,
399
+ };
400
+ }
401
+ }
402
+ })(),
403
+ ]);
404
+
405
+
406
+ await Promise.all([
407
+ ...aaveV3Markets.map((market) => allAddresses.map(async (address) => {
408
+ try {
409
+ const accData = await _getAaveV3AccountData(client, network, address, { selectedMarket: market, ...aaveV3MarketsData[market.value] });
410
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: accData };
411
+ } catch (error) {
412
+ console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
413
+ positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
414
+ }
415
+ })).flat(),
416
+ ...morphoMarkets.map((market) => addresses.map(async (address) => {
417
+ try {
418
+ const [accDataPromise, earnDataPromise] = await Promise.allSettled([
419
+ _getMorphoBlueAccountData(client, network, address, market, morphoMarketsData[market.value]),
420
+ getMorphoEarn(client, network, address, market, morphoMarketsData[market.value]),
421
+ ]);
422
+ if (accDataPromise.status === 'rejected') {
423
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, accDataPromise.reason);
424
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
425
+ }
426
+ if (earnDataPromise.status === 'rejected') {
427
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, earnDataPromise.reason);
428
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
429
+ }
430
+ if (accDataPromise.status !== 'rejected') {
431
+ const accData = accDataPromise.value;
432
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: '', data: accData };
433
+ }
434
+ if (earnDataPromise.status !== 'rejected') {
435
+ const earnData = earnDataPromise.value;
436
+ if (earnData && new Dec(earnData.amount).gt(0)) {
437
+ stakingPositions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = {
438
+ error: '',
439
+ data: earnData,
440
+ };
441
+ }
442
+ }
443
+ } catch (error) {
444
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
445
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
446
+ }
447
+ })).flat(),
448
+ ...compoundV3Markets.map((market) => addresses.map(async (address) => {
449
+ try {
450
+ const accData = await _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: compoundV3MarketsData[market.value].assetsData });
451
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: '', data: accData };
452
+ } catch (error) {
453
+ console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
454
+ positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
455
+ }
456
+ })).flat(),
457
+ ...sparkMarkets.map((market) => allAddresses.map(async (address) => {
458
+ try {
459
+ const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: sparkMarketsData[market.value].assetsData });
460
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: accData };
461
+ } catch (error) {
462
+ console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
463
+ positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
464
+ }
465
+ })).flat(),
466
+ ...eulerV2Markets.map((market) => addresses.map((address) => {
467
+ const eulerV2SubAccounts = getEulerV2SubAccounts(address);
468
+ const eulerV2Addresses = [address, ...eulerV2SubAccounts];
469
+ return Promise.all(eulerV2Addresses.map(async (eulerAddress) => {
470
+ try {
471
+ const accData = await _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
472
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
473
+ if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
474
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
475
+ }
476
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: '', data: accData };
477
+ }
478
+ } catch (error) {
479
+ console.error(`Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}:`, error);
480
+ if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
481
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
482
+ }
483
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: `Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}`, data: null };
484
+ }
485
+ }));
486
+ }).flat()).flat(),
487
+ ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
488
+ try {
489
+ const cdpData = await _getMakerCdpData(client, network, cdpInfo);
490
+ if (cdpData) {
491
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: '', data: cdpData };
492
+ }
493
+ } catch (error) {
494
+ console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
495
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
496
+ }
497
+ })).flat(),
498
+ ...aaveV2Markets.map((market) => addresses.map(async (address) => {
499
+ try {
500
+ const accData = await _getAaveV2AccountData(client, network, address, aaveV2MarketsData[market.value].assetsData, market);
501
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: '', data: accData };
502
+ } catch (error) {
503
+ console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
504
+ positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
505
+ }
506
+ })).flat(),
507
+ ...compoundV2Markets.map((market) => addresses.map(async (address) => {
508
+ try {
509
+ const accData = await _getCompoundV2AccountData(client, network, address, compoundV2MarketsData[market.value].assetsData);
510
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: '', data: accData };
511
+ } catch (error) {
512
+ console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
513
+ positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
514
+ }
515
+ })).flat(),
516
+ ...addresses.map(async (address) => {
517
+ try {
518
+ if (!isMainnet) return; // Liquity trove info is only available on mainnet
519
+ const troveInfo = await _getLiquityTroveInfo(client, network, address);
520
+ if (new Dec(troveInfo.collateral).gt(0)) positions[address.toLowerCase() as EthAddress].liquity = { error: '', data: troveInfo };
521
+ } catch (error) {
522
+ console.error(`Error fetching Liquity trove info for address ${address}:`, error);
523
+ positions[address.toLowerCase() as EthAddress].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
524
+ }
525
+ }),
526
+ ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
527
+ try {
528
+ const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
529
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
530
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: { ...accData, borrowRate: crvUsdMarketsData[market.value].borrowRate } };
531
+ }
532
+ } catch (error) {
533
+ console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
534
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
535
+ }
536
+ })).flat(),
537
+ ...llamaLendMarkets.map((market) => addresses.map(async (address) => {
538
+ try {
539
+ const accData = await _getLlamaLendUserData(client, network, address, market, llamaLendMarketsData[market.value]);
540
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
541
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: '', data: { ...accData, borrowRate: llamaLendMarketsData[market.value].borrowRate } };
542
+ }
543
+ } catch (error) {
544
+ console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
545
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
546
+ }
547
+ })).flat(),
548
+ // liquity sBold/yBold and staking options
549
+ ...addresses.map(async (address) => {
550
+ try {
551
+ if (!isMainnet) {
552
+ stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data: null };
553
+ return;
554
+ }
555
+ const data = await getLiquitySAndYBold(client, network, stakingPositions[address.toLowerCase()].liquityV2, address);
556
+ stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data };
557
+ } catch (error) {
558
+ console.error(`Error fetching SBold/YBold data for address ${address}:`, error);
559
+ stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: `Error fetching sBold/yBold data for address ${address}`, data: null };
560
+ }
561
+ }),
562
+ ]);
563
+
564
+ return {
565
+ positions,
566
+ stakingPositions,
567
+ rewardsData,
568
+ markets,
569
+ };
570
+ }