@defisaver/positions-sdk 2.1.22-dev-savings-4 → 2.1.23-dev-sena

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