@defisaver/positions-sdk 2.1.34 → 2.1.36

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