@defisaver/positions-sdk 2.1.96 → 2.1.98-aave-v4-apy-2

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 (136) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/aaveV3/index.js +3 -3
  5. package/cjs/aaveV4/index.js +2 -0
  6. package/cjs/config/contracts.d.ts +61 -1
  7. package/cjs/config/contracts.js +2 -2
  8. package/cjs/contracts.d.ts +2963 -82
  9. package/cjs/helpers/aaveV4Helpers/index.d.ts +11 -1
  10. package/cjs/helpers/aaveV4Helpers/index.js +58 -1
  11. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  12. package/cjs/savings/morphoVaults/index.js +17 -17
  13. package/cjs/services/utils.d.ts +1 -1
  14. package/cjs/services/utils.js +2 -1
  15. package/cjs/types/aaveV4.d.ts +2 -0
  16. package/esm/aaveV3/index.js +3 -3
  17. package/esm/aaveV4/index.js +2 -0
  18. package/esm/config/contracts.d.ts +61 -1
  19. package/esm/config/contracts.js +2 -2
  20. package/esm/contracts.d.ts +2963 -82
  21. package/esm/helpers/aaveV4Helpers/index.d.ts +11 -1
  22. package/esm/helpers/aaveV4Helpers/index.js +56 -0
  23. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  24. package/esm/savings/morphoVaults/index.js +17 -17
  25. package/esm/services/utils.d.ts +1 -1
  26. package/esm/services/utils.js +2 -1
  27. package/esm/types/aaveV4.d.ts +2 -0
  28. package/package.json +48 -48
  29. package/src/aaveV2/index.ts +240 -240
  30. package/src/aaveV3/index.ts +634 -634
  31. package/src/aaveV3/merit.ts +97 -97
  32. package/src/aaveV3/merkl.ts +74 -74
  33. package/src/aaveV4/index.ts +238 -236
  34. package/src/aaveV4/lend.ts +185 -185
  35. package/src/claiming/aaveV3.ts +154 -154
  36. package/src/claiming/compV3.ts +22 -22
  37. package/src/claiming/ethena.ts +61 -61
  38. package/src/claiming/index.ts +12 -12
  39. package/src/claiming/king.ts +66 -66
  40. package/src/claiming/morphoBlue.ts +118 -118
  41. package/src/claiming/spark.ts +225 -225
  42. package/src/compoundV2/index.ts +245 -245
  43. package/src/compoundV3/index.ts +280 -280
  44. package/src/config/contracts.ts +1357 -1357
  45. package/src/constants/index.ts +10 -10
  46. package/src/contracts.ts +183 -183
  47. package/src/curveUsd/index.ts +254 -254
  48. package/src/eulerV2/index.ts +330 -330
  49. package/src/exchange/index.ts +25 -25
  50. package/src/fluid/index.ts +1800 -1800
  51. package/src/helpers/aaveHelpers/index.ts +203 -203
  52. package/src/helpers/aaveV4Helpers/index.ts +349 -291
  53. package/src/helpers/compoundHelpers/index.ts +280 -280
  54. package/src/helpers/curveUsdHelpers/index.ts +44 -44
  55. package/src/helpers/eulerHelpers/index.ts +230 -230
  56. package/src/helpers/fluidHelpers/index.ts +338 -338
  57. package/src/helpers/index.ts +11 -11
  58. package/src/helpers/liquityV2Helpers/index.ts +85 -85
  59. package/src/helpers/llamaLendHelpers/index.ts +56 -56
  60. package/src/helpers/makerHelpers/index.ts +52 -52
  61. package/src/helpers/morphoBlueHelpers/index.ts +406 -406
  62. package/src/helpers/sparkHelpers/index.ts +170 -170
  63. package/src/index.ts +51 -51
  64. package/src/liquity/index.ts +161 -161
  65. package/src/liquityV2/index.ts +703 -703
  66. package/src/llamaLend/index.ts +305 -305
  67. package/src/maker/index.ts +225 -225
  68. package/src/markets/aave/index.ts +118 -118
  69. package/src/markets/aave/marketAssets.ts +54 -54
  70. package/src/markets/aaveV4/index.ts +191 -191
  71. package/src/markets/compound/index.ts +243 -243
  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 +29 -29
  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 +1370 -1370
  81. package/src/markets/spark/index.ts +29 -29
  82. package/src/markets/spark/marketAssets.ts +12 -12
  83. package/src/moneymarket/moneymarketCommonService.ts +91 -91
  84. package/src/morphoBlue/index.ts +274 -274
  85. package/src/portfolio/index.ts +606 -606
  86. package/src/savings/index.ts +111 -111
  87. package/src/savings/makerDsr/index.ts +53 -53
  88. package/src/savings/makerDsr/options.ts +9 -9
  89. package/src/savings/morphoVaults/index.ts +80 -80
  90. package/src/savings/morphoVaults/options.ts +193 -193
  91. package/src/savings/skyOptions/index.ts +95 -95
  92. package/src/savings/skyOptions/options.ts +10 -10
  93. package/src/savings/sparkSavingsVaults/index.ts +60 -60
  94. package/src/savings/sparkSavingsVaults/options.ts +35 -35
  95. package/src/savings/summerVaults/index.ts +56 -56
  96. package/src/savings/summerVaults/options.ts +117 -117
  97. package/src/savings/yearnV3Vaults/index.ts +61 -61
  98. package/src/savings/yearnV3Vaults/options.ts +55 -55
  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 +122 -117
  103. package/src/services/viem.ts +57 -57
  104. package/src/setup.ts +8 -8
  105. package/src/spark/index.ts +460 -460
  106. package/src/staking/eligibility.ts +105 -105
  107. package/src/staking/index.ts +1 -1
  108. package/src/staking/staking.ts +201 -201
  109. package/src/types/aave.ts +200 -200
  110. package/src/types/aaveV4.ts +179 -177
  111. package/src/types/claiming.ts +114 -114
  112. package/src/types/common.ts +117 -117
  113. package/src/types/compound.ts +147 -147
  114. package/src/types/curveUsd.ts +125 -125
  115. package/src/types/euler.ts +177 -177
  116. package/src/types/fluid.ts +486 -486
  117. package/src/types/index.ts +16 -16
  118. package/src/types/liquity.ts +31 -31
  119. package/src/types/liquityV2.ts +130 -130
  120. package/src/types/llamaLend.ts +163 -163
  121. package/src/types/maker.ts +64 -64
  122. package/src/types/merit.ts +1 -1
  123. package/src/types/merkl.ts +70 -70
  124. package/src/types/morphoBlue.ts +228 -228
  125. package/src/types/portfolio.ts +64 -64
  126. package/src/types/savings/index.ts +25 -25
  127. package/src/types/savings/makerDsr.ts +13 -13
  128. package/src/types/savings/morphoVaults.ts +32 -32
  129. package/src/types/savings/sky.ts +14 -14
  130. package/src/types/savings/sparkSavingsVaults.ts +15 -15
  131. package/src/types/savings/summerVaults.ts +24 -24
  132. package/src/types/savings/yearnV3Vaults.ts +17 -17
  133. package/src/types/savings/yearnVaults.ts +14 -14
  134. package/src/types/spark.ts +136 -136
  135. package/src/umbrella/index.ts +69 -69
  136. package/src/umbrella/umbrellaUtils.ts +29 -29
@@ -1,606 +1,606 @@
1
- import Dec from 'decimal.js';
2
- import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
3
- import {
4
- AaveMarkets,
5
- AaveV4Spokes,
6
- CompoundMarkets,
7
- CrvUsdMarkets,
8
- EulerV2Markets,
9
- LiquityV2Markets,
10
- LlamaLendMarkets,
11
- MorphoBlueMarkets,
12
- SparkMarkets,
13
- } from '../markets';
14
- import { _getMorphoBlueAccountData, _getMorphoBlueMarketData, getMorphoEarn } from '../morphoBlue';
15
- import {
16
- AaveV2MarketData,
17
- AaveV3MarketData,
18
- AaveV4SpokeData,
19
- AaveVersions,
20
- CdpInfo,
21
- CompoundV2MarketsData,
22
- CompoundV3MarketsData,
23
- CompoundVersions,
24
- CrvUSDGlobalMarketData,
25
- EulerV2FullMarketData,
26
- LiquityV2MarketData,
27
- LlamaLendGlobalMarketData,
28
- MorphoBlueMarketInfo,
29
- PortfolioPositionsData,
30
- SparkMarketsData,
31
- } from '../types';
32
- import { _getCompoundV3AccountData, _getCompoundV3MarketsData } from '../compoundV3';
33
- import { _getSparkAccountData, _getSparkMarketsData } from '../spark';
34
- import { _getEulerV2AccountData, _getEulerV2MarketsData } from '../eulerV2';
35
- import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
36
- import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
37
- import { _getAaveV3AccountData, _getAaveV3MarketData, getStakeAaveData } from '../aaveV3';
38
- import { ZERO_ADDRESS } from '../constants';
39
- import { _getMakerCdpData, _getUserCdps } from '../maker';
40
- import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
41
- import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
42
- import { getViemProvider } from '../services/viem';
43
- import { _getLiquityTroveInfo, getLiquityStakingData } from '../liquity';
44
- import { _getLiquityV2MarketData, getLiquitySAndYBold, getLiquityV2Staking } from '../liquityV2';
45
- import { _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
46
- import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
47
- import { getUmbrellaData } from '../umbrella';
48
- import { getMeritUnclaimedRewards, getUnclaimedRewardsForAllMarkets } from '../claiming/aaveV3';
49
- import { getCompoundV3Rewards } from '../claiming/compV3';
50
- import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
51
- import { fetchMorphoBlueRewards } from '../claiming/morphoBlue';
52
- import { getKingRewards } from '../claiming/king';
53
- import { fetchEthenaAirdropRewards } from '../claiming/ethena';
54
- import { _getAaveV4AccountData, _getAaveV4SpokeData } from '../aaveV4';
55
-
56
- export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], isSim = false): Promise<{
57
- positions: PortfolioPositionsData;
58
- stakingPositions: any;
59
- rewardsData: any;
60
- markets: any;
61
- }> {
62
- const isMainnet = network === NetworkNumber.Eth;
63
- const isFluidSupported = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base, NetworkNumber.Plasma].includes(network);
64
- const isMorphoRewardsSupported = [NetworkNumber.Eth, NetworkNumber.Base].includes(network);
65
-
66
- const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
67
- const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
68
- const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
69
- const eulerV2Markets = Object.values(EulerV2Markets(network)).filter((market) => market.chainIds.includes(network));
70
- const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
71
- const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
72
- const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
73
- const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
74
- const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
75
- const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
76
- const liquityV2MarketsStaking = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)).filter(market => !market.isLegacy) : [];
77
- const aaveV4Spokes = Object.values(AaveV4Spokes(network)).filter((market) => market.chainIds.includes(network));
78
-
79
- const args: [NetworkNumber, any?] = [network, { batch: { multicall: { batchSize: isSim ? 500_000 : 2_500_000 } } }];
80
- const client = getViemProvider(provider, ...args);
81
- const defaultClient = getViemProvider(defaultProvider, ...args);
82
-
83
- const morphoMarketsData: Record<string, MorphoBlueMarketInfo> = {};
84
- const compoundV3MarketsData: Record<string, CompoundV3MarketsData> = {};
85
- const sparkMarketsData: Record<string, SparkMarketsData> = {};
86
- const eulerV2MarketsData: Record<string, EulerV2FullMarketData> = {};
87
- const aaveV3MarketsData: Record<string, AaveV3MarketData> = {};
88
- const makerCdps: Record<string, CdpInfo[]> = {};
89
- const aaveV2MarketsData: Record<string, AaveV2MarketData> = {};
90
- const compoundV2MarketsData: Record<string, CompoundV2MarketsData> = {};
91
- const crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData> = {};
92
- const llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData> = {};
93
- const liquityV2MarketsData: Record<string, LiquityV2MarketData> = {};
94
- const aaveV4SpokesData: Record<string, AaveV4SpokeData> = {};
95
-
96
- const markets = {
97
- morphoMarketsData,
98
- compoundV3MarketsData,
99
- sparkMarketsData,
100
- eulerV2MarketsData,
101
- aaveV3MarketsData,
102
- aaveV2MarketsData,
103
- compoundV2MarketsData,
104
- crvUsdMarketsData,
105
- llamaLendMarketsData,
106
- liquityV2MarketsData,
107
- aaveV4SpokesData,
108
- };
109
-
110
- const positions: PortfolioPositionsData = {};
111
- const stakingPositions: any = {};
112
- const rewardsData: any = {};
113
- const allAddresses = [...addresses];
114
-
115
- for (const address of allAddresses) {
116
- positions[address.toLowerCase() as EthAddress] = {
117
- aaveV3: {},
118
- aaveV4: {},
119
- morphoBlue: {},
120
- compoundV3: {},
121
- spark: {},
122
- eulerV2: {},
123
- maker: {},
124
- aaveV2: {},
125
- compoundV2: {},
126
- liquity: {},
127
- crvUsd: {},
128
- llamaLend: {},
129
- fluid: {
130
- error: '',
131
- data: {},
132
- },
133
- };
134
- }
135
-
136
- // TODO: check default values, probably needed when fetching portfolio on unsupported networks
137
- for (const address of addresses) {
138
- stakingPositions[address.toLowerCase() as EthAddress] = {
139
- aaveV3: {},
140
- morphoBlue: {},
141
- compoundV3: {},
142
- spark: {},
143
- aaveV2: {},
144
- compoundV2: {},
145
- liquity: {},
146
- liquityV2: {},
147
- fluid: {
148
- error: '',
149
- data: {},
150
- },
151
- };
152
-
153
- rewardsData[address.toLowerCase() as EthAddress] = {
154
- aaveV3merit: {},
155
- aaveV3: {},
156
- compV3: {},
157
- spark: {},
158
- spk: {},
159
- king: {},
160
- morpho: {},
161
- ethena: {},
162
- };
163
- }
164
-
165
- await Promise.allSettled([
166
- // === MARKET DATA (needs to be fetched first) ===
167
- ...morphoMarkets.map(async (market) => {
168
- const marketData = await _getMorphoBlueMarketData(client, network, market);
169
- morphoMarketsData[market.value] = marketData;
170
- }),
171
- ...compoundV3Markets.map(async (market) => {
172
- const marketData = await _getCompoundV3MarketsData(client, network, market, defaultClient);
173
- compoundV3MarketsData[market.value] = marketData;
174
- }),
175
- ...sparkMarkets.map(async (market) => {
176
- const marketData = await _getSparkMarketsData(client, network, market);
177
- sparkMarketsData[market.value] = marketData;
178
- }),
179
- ...eulerV2Markets.map(async (market) => {
180
- const marketData = await _getEulerV2MarketsData(client, network, market);
181
- eulerV2MarketsData[market.value] = marketData;
182
- }),
183
- ...aaveV3Markets.map(async (market) => {
184
- const marketData = await _getAaveV3MarketData(client, network, market);
185
- aaveV3MarketsData[market.value] = marketData;
186
- }),
187
- ...aaveV4Spokes.map(async (spoke) => {
188
- const spokeData = await _getAaveV4SpokeData(client, network, spoke);
189
- aaveV4SpokesData[spoke.value] = spokeData;
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
- await Promise.all([
434
- ...aaveV3Markets.map((market) => allAddresses.map(async (address) => {
435
- try {
436
- const accData = await _getAaveV3AccountData(client, network, address, { selectedMarket: market, ...aaveV3MarketsData[market.value] });
437
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: accData };
438
- } catch (error) {
439
- console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
440
- positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
441
- }
442
- })).flat(),
443
- ...aaveV4Spokes.map((spoke) => allAddresses.map(async (address) => {
444
- try {
445
- const accData = await _getAaveV4AccountData(client, network, aaveV4SpokesData[spoke.value], address);
446
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: '', data: accData };
447
- } catch (error) {
448
- console.error(`Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}:`, error);
449
- positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: `Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}`, data: null };
450
- }
451
- })).flat(),
452
- ...morphoMarkets.map((market) => addresses.map(async (address) => {
453
- try {
454
- const [accDataPromise, earnDataPromise] = await Promise.allSettled([
455
- _getMorphoBlueAccountData(client, network, address, market, morphoMarketsData[market.value]),
456
- getMorphoEarn(client, network, address, market, morphoMarketsData[market.value]),
457
- ]);
458
- if (accDataPromise.status === 'rejected') {
459
- console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, accDataPromise.reason);
460
- positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
461
- }
462
- if (earnDataPromise.status === 'rejected') {
463
- console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, earnDataPromise.reason);
464
- positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
465
- }
466
- if (accDataPromise.status !== 'rejected') {
467
- const accData = accDataPromise.value;
468
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: '', data: accData };
469
- }
470
- if (earnDataPromise.status !== 'rejected') {
471
- const earnData = earnDataPromise.value;
472
- if (earnData && new Dec(earnData.amount).gt(0)) {
473
- stakingPositions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = {
474
- error: '',
475
- data: earnData,
476
- };
477
- }
478
- }
479
- } catch (error) {
480
- console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
481
- positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
482
- }
483
- })).flat(),
484
- ...compoundV3Markets.map((market) => addresses.map(async (address) => {
485
- try {
486
- const accData = await _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: compoundV3MarketsData[market.value].assetsData });
487
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: '', data: accData };
488
- } catch (error) {
489
- console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
490
- positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
491
- }
492
- })).flat(),
493
- ...sparkMarkets.map((market) => allAddresses.map(async (address) => {
494
- try {
495
- const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: sparkMarketsData[market.value].assetsData, eModeCategoriesData: sparkMarketsData[market.value].eModeCategoriesData });
496
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: accData };
497
- } catch (error) {
498
- console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
499
- positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
500
- }
501
- })).flat(),
502
- ...eulerV2Markets.map((market) => addresses.map((address) => {
503
- const eulerV2SubAccounts = getEulerV2SubAccounts(address);
504
- const eulerV2Addresses = [address, ...eulerV2SubAccounts];
505
- return Promise.all(eulerV2Addresses.map(async (eulerAddress) => {
506
- try {
507
- const accData = await _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
508
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
509
- if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
510
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
511
- }
512
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: '', data: accData };
513
- }
514
- } catch (error) {
515
- console.error(`Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}:`, error);
516
- if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
517
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
518
- }
519
- 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 };
520
- }
521
- }));
522
- }).flat()).flat(),
523
- ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
524
- try {
525
- const cdpData = await _getMakerCdpData(client, network, cdpInfo);
526
- if (cdpData) {
527
- positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: '', data: cdpData };
528
- }
529
- } catch (error) {
530
- console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
531
- positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
532
- }
533
- })).flat(),
534
- ...aaveV2Markets.map((market) => addresses.map(async (address) => {
535
- try {
536
- const accData = await _getAaveV2AccountData(client, network, address, aaveV2MarketsData[market.value].assetsData, market);
537
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: '', data: accData };
538
- } catch (error) {
539
- console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
540
- positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
541
- }
542
- })).flat(),
543
- ...compoundV2Markets.map((market) => addresses.map(async (address) => {
544
- try {
545
- const accData = await _getCompoundV2AccountData(client, network, address, compoundV2MarketsData[market.value].assetsData);
546
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: '', data: accData };
547
- } catch (error) {
548
- console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
549
- positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
550
- }
551
- })).flat(),
552
- ...addresses.map(async (address) => {
553
- try {
554
- if (!isMainnet) return; // Liquity trove info is only available on mainnet
555
- const troveInfo = await _getLiquityTroveInfo(client, network, address);
556
- if (new Dec(troveInfo.collateral).gt(0)) positions[address.toLowerCase() as EthAddress].liquity = { error: '', data: troveInfo };
557
- } catch (error) {
558
- console.error(`Error fetching Liquity trove info for address ${address}:`, error);
559
- positions[address.toLowerCase() as EthAddress].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
560
- }
561
- }),
562
- ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
563
- try {
564
- const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
565
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
566
- positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: { ...accData, borrowRate: crvUsdMarketsData[market.value].borrowRate } };
567
- }
568
- } catch (error) {
569
- console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
570
- positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
571
- }
572
- })).flat(),
573
- ...llamaLendMarkets.map((market) => addresses.map(async (address) => {
574
- try {
575
- const accData = await _getLlamaLendUserData(client, network, address, market, llamaLendMarketsData[market.value]);
576
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
577
- positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: '', data: { ...accData, borrowRate: llamaLendMarketsData[market.value].borrowRate } };
578
- }
579
- } catch (error) {
580
- console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
581
- positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
582
- }
583
- })).flat(),
584
- // liquity sBold/yBold and staking options
585
- ...addresses.map(async (address) => {
586
- try {
587
- if (!isMainnet) {
588
- stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data: null };
589
- return;
590
- }
591
- const data = await getLiquitySAndYBold(client, network, stakingPositions[address.toLowerCase()].liquityV2, address);
592
- stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data };
593
- } catch (error) {
594
- console.error(`Error fetching SBold/YBold data for address ${address}:`, error);
595
- stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: `Error fetching sBold/yBold data for address ${address}`, data: null };
596
- }
597
- }),
598
- ]);
599
-
600
- return {
601
- positions,
602
- stakingPositions,
603
- rewardsData,
604
- markets,
605
- };
606
- }
1
+ import Dec from 'decimal.js';
2
+ import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
3
+ import {
4
+ AaveMarkets,
5
+ AaveV4Spokes,
6
+ CompoundMarkets,
7
+ CrvUsdMarkets,
8
+ EulerV2Markets,
9
+ LiquityV2Markets,
10
+ LlamaLendMarkets,
11
+ MorphoBlueMarkets,
12
+ SparkMarkets,
13
+ } from '../markets';
14
+ import { _getMorphoBlueAccountData, _getMorphoBlueMarketData, getMorphoEarn } from '../morphoBlue';
15
+ import {
16
+ AaveV2MarketData,
17
+ AaveV3MarketData,
18
+ AaveV4SpokeData,
19
+ AaveVersions,
20
+ CdpInfo,
21
+ CompoundV2MarketsData,
22
+ CompoundV3MarketsData,
23
+ CompoundVersions,
24
+ CrvUSDGlobalMarketData,
25
+ EulerV2FullMarketData,
26
+ LiquityV2MarketData,
27
+ LlamaLendGlobalMarketData,
28
+ MorphoBlueMarketInfo,
29
+ PortfolioPositionsData,
30
+ SparkMarketsData,
31
+ } from '../types';
32
+ import { _getCompoundV3AccountData, _getCompoundV3MarketsData } from '../compoundV3';
33
+ import { _getSparkAccountData, _getSparkMarketsData } from '../spark';
34
+ import { _getEulerV2AccountData, _getEulerV2MarketsData } from '../eulerV2';
35
+ import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
36
+ import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
37
+ import { _getAaveV3AccountData, _getAaveV3MarketData, getStakeAaveData } from '../aaveV3';
38
+ import { ZERO_ADDRESS } from '../constants';
39
+ import { _getMakerCdpData, _getUserCdps } from '../maker';
40
+ import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
41
+ import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
42
+ import { getViemProvider } from '../services/viem';
43
+ import { _getLiquityTroveInfo, getLiquityStakingData } from '../liquity';
44
+ import { _getLiquityV2MarketData, getLiquitySAndYBold, getLiquityV2Staking } from '../liquityV2';
45
+ import { _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
46
+ import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
47
+ import { getUmbrellaData } from '../umbrella';
48
+ import { getMeritUnclaimedRewards, getUnclaimedRewardsForAllMarkets } from '../claiming/aaveV3';
49
+ import { getCompoundV3Rewards } from '../claiming/compV3';
50
+ import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
51
+ import { fetchMorphoBlueRewards } from '../claiming/morphoBlue';
52
+ import { getKingRewards } from '../claiming/king';
53
+ import { fetchEthenaAirdropRewards } from '../claiming/ethena';
54
+ import { _getAaveV4AccountData, _getAaveV4SpokeData } from '../aaveV4';
55
+
56
+ export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], isSim = false): Promise<{
57
+ positions: PortfolioPositionsData;
58
+ stakingPositions: any;
59
+ rewardsData: any;
60
+ markets: any;
61
+ }> {
62
+ const isMainnet = network === NetworkNumber.Eth;
63
+ const isFluidSupported = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base, NetworkNumber.Plasma].includes(network);
64
+ const isMorphoRewardsSupported = [NetworkNumber.Eth, NetworkNumber.Base].includes(network);
65
+
66
+ const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
67
+ const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
68
+ const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
69
+ const eulerV2Markets = Object.values(EulerV2Markets(network)).filter((market) => market.chainIds.includes(network));
70
+ const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
71
+ const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
72
+ const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
73
+ const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
74
+ const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
75
+ const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
76
+ const liquityV2MarketsStaking = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)).filter(market => !market.isLegacy) : [];
77
+ const aaveV4Spokes = Object.values(AaveV4Spokes(network)).filter((market) => market.chainIds.includes(network));
78
+
79
+ const args: [NetworkNumber, any?] = [network, { batch: { multicall: { batchSize: isSim ? 500_000 : 2_500_000 } } }];
80
+ const client = getViemProvider(provider, ...args);
81
+ const defaultClient = getViemProvider(defaultProvider, ...args);
82
+
83
+ const morphoMarketsData: Record<string, MorphoBlueMarketInfo> = {};
84
+ const compoundV3MarketsData: Record<string, CompoundV3MarketsData> = {};
85
+ const sparkMarketsData: Record<string, SparkMarketsData> = {};
86
+ const eulerV2MarketsData: Record<string, EulerV2FullMarketData> = {};
87
+ const aaveV3MarketsData: Record<string, AaveV3MarketData> = {};
88
+ const makerCdps: Record<string, CdpInfo[]> = {};
89
+ const aaveV2MarketsData: Record<string, AaveV2MarketData> = {};
90
+ const compoundV2MarketsData: Record<string, CompoundV2MarketsData> = {};
91
+ const crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData> = {};
92
+ const llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData> = {};
93
+ const liquityV2MarketsData: Record<string, LiquityV2MarketData> = {};
94
+ const aaveV4SpokesData: Record<string, AaveV4SpokeData> = {};
95
+
96
+ const markets = {
97
+ morphoMarketsData,
98
+ compoundV3MarketsData,
99
+ sparkMarketsData,
100
+ eulerV2MarketsData,
101
+ aaveV3MarketsData,
102
+ aaveV2MarketsData,
103
+ compoundV2MarketsData,
104
+ crvUsdMarketsData,
105
+ llamaLendMarketsData,
106
+ liquityV2MarketsData,
107
+ aaveV4SpokesData,
108
+ };
109
+
110
+ const positions: PortfolioPositionsData = {};
111
+ const stakingPositions: any = {};
112
+ const rewardsData: any = {};
113
+ const allAddresses = [...addresses];
114
+
115
+ for (const address of allAddresses) {
116
+ positions[address.toLowerCase() as EthAddress] = {
117
+ aaveV3: {},
118
+ aaveV4: {},
119
+ morphoBlue: {},
120
+ compoundV3: {},
121
+ spark: {},
122
+ eulerV2: {},
123
+ maker: {},
124
+ aaveV2: {},
125
+ compoundV2: {},
126
+ liquity: {},
127
+ crvUsd: {},
128
+ llamaLend: {},
129
+ fluid: {
130
+ error: '',
131
+ data: {},
132
+ },
133
+ };
134
+ }
135
+
136
+ // TODO: check default values, probably needed when fetching portfolio on unsupported networks
137
+ for (const address of addresses) {
138
+ stakingPositions[address.toLowerCase() as EthAddress] = {
139
+ aaveV3: {},
140
+ morphoBlue: {},
141
+ compoundV3: {},
142
+ spark: {},
143
+ aaveV2: {},
144
+ compoundV2: {},
145
+ liquity: {},
146
+ liquityV2: {},
147
+ fluid: {
148
+ error: '',
149
+ data: {},
150
+ },
151
+ };
152
+
153
+ rewardsData[address.toLowerCase() as EthAddress] = {
154
+ aaveV3merit: {},
155
+ aaveV3: {},
156
+ compV3: {},
157
+ spark: {},
158
+ spk: {},
159
+ king: {},
160
+ morpho: {},
161
+ ethena: {},
162
+ };
163
+ }
164
+
165
+ await Promise.allSettled([
166
+ // === MARKET DATA (needs to be fetched first) ===
167
+ ...morphoMarkets.map(async (market) => {
168
+ const marketData = await _getMorphoBlueMarketData(client, network, market);
169
+ morphoMarketsData[market.value] = marketData;
170
+ }),
171
+ ...compoundV3Markets.map(async (market) => {
172
+ const marketData = await _getCompoundV3MarketsData(client, network, market, defaultClient);
173
+ compoundV3MarketsData[market.value] = marketData;
174
+ }),
175
+ ...sparkMarkets.map(async (market) => {
176
+ const marketData = await _getSparkMarketsData(client, network, market);
177
+ sparkMarketsData[market.value] = marketData;
178
+ }),
179
+ ...eulerV2Markets.map(async (market) => {
180
+ const marketData = await _getEulerV2MarketsData(client, network, market);
181
+ eulerV2MarketsData[market.value] = marketData;
182
+ }),
183
+ ...aaveV3Markets.map(async (market) => {
184
+ const marketData = await _getAaveV3MarketData(client, network, market);
185
+ aaveV3MarketsData[market.value] = marketData;
186
+ }),
187
+ ...aaveV4Spokes.map(async (spoke) => {
188
+ const spokeData = await _getAaveV4SpokeData(client, network, spoke);
189
+ aaveV4SpokesData[spoke.value] = spokeData;
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
+ await Promise.all([
434
+ ...aaveV3Markets.map((market) => allAddresses.map(async (address) => {
435
+ try {
436
+ const accData = await _getAaveV3AccountData(client, network, address, { selectedMarket: market, ...aaveV3MarketsData[market.value] });
437
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: accData };
438
+ } catch (error) {
439
+ console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
440
+ positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
441
+ }
442
+ })).flat(),
443
+ ...aaveV4Spokes.map((spoke) => allAddresses.map(async (address) => {
444
+ try {
445
+ const accData = await _getAaveV4AccountData(client, network, aaveV4SpokesData[spoke.value], address);
446
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: '', data: accData };
447
+ } catch (error) {
448
+ console.error(`Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}:`, error);
449
+ positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: `Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}`, data: null };
450
+ }
451
+ })).flat(),
452
+ ...morphoMarkets.map((market) => addresses.map(async (address) => {
453
+ try {
454
+ const [accDataPromise, earnDataPromise] = await Promise.allSettled([
455
+ _getMorphoBlueAccountData(client, network, address, market, morphoMarketsData[market.value]),
456
+ getMorphoEarn(client, network, address, market, morphoMarketsData[market.value]),
457
+ ]);
458
+ if (accDataPromise.status === 'rejected') {
459
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, accDataPromise.reason);
460
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
461
+ }
462
+ if (earnDataPromise.status === 'rejected') {
463
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, earnDataPromise.reason);
464
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
465
+ }
466
+ if (accDataPromise.status !== 'rejected') {
467
+ const accData = accDataPromise.value;
468
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: '', data: accData };
469
+ }
470
+ if (earnDataPromise.status !== 'rejected') {
471
+ const earnData = earnDataPromise.value;
472
+ if (earnData && new Dec(earnData.amount).gt(0)) {
473
+ stakingPositions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = {
474
+ error: '',
475
+ data: earnData,
476
+ };
477
+ }
478
+ }
479
+ } catch (error) {
480
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
481
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
482
+ }
483
+ })).flat(),
484
+ ...compoundV3Markets.map((market) => addresses.map(async (address) => {
485
+ try {
486
+ const accData = await _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: compoundV3MarketsData[market.value].assetsData });
487
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: '', data: accData };
488
+ } catch (error) {
489
+ console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
490
+ positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
491
+ }
492
+ })).flat(),
493
+ ...sparkMarkets.map((market) => allAddresses.map(async (address) => {
494
+ try {
495
+ const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: sparkMarketsData[market.value].assetsData, eModeCategoriesData: sparkMarketsData[market.value].eModeCategoriesData });
496
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: accData };
497
+ } catch (error) {
498
+ console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
499
+ positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
500
+ }
501
+ })).flat(),
502
+ ...eulerV2Markets.map((market) => addresses.map((address) => {
503
+ const eulerV2SubAccounts = getEulerV2SubAccounts(address);
504
+ const eulerV2Addresses = [address, ...eulerV2SubAccounts];
505
+ return Promise.all(eulerV2Addresses.map(async (eulerAddress) => {
506
+ try {
507
+ const accData = await _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
508
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
509
+ if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
510
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
511
+ }
512
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: '', data: accData };
513
+ }
514
+ } catch (error) {
515
+ console.error(`Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}:`, error);
516
+ if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
517
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
518
+ }
519
+ 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 };
520
+ }
521
+ }));
522
+ }).flat()).flat(),
523
+ ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
524
+ try {
525
+ const cdpData = await _getMakerCdpData(client, network, cdpInfo);
526
+ if (cdpData) {
527
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: '', data: cdpData };
528
+ }
529
+ } catch (error) {
530
+ console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
531
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
532
+ }
533
+ })).flat(),
534
+ ...aaveV2Markets.map((market) => addresses.map(async (address) => {
535
+ try {
536
+ const accData = await _getAaveV2AccountData(client, network, address, aaveV2MarketsData[market.value].assetsData, market);
537
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: '', data: accData };
538
+ } catch (error) {
539
+ console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
540
+ positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
541
+ }
542
+ })).flat(),
543
+ ...compoundV2Markets.map((market) => addresses.map(async (address) => {
544
+ try {
545
+ const accData = await _getCompoundV2AccountData(client, network, address, compoundV2MarketsData[market.value].assetsData);
546
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: '', data: accData };
547
+ } catch (error) {
548
+ console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
549
+ positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
550
+ }
551
+ })).flat(),
552
+ ...addresses.map(async (address) => {
553
+ try {
554
+ if (!isMainnet) return; // Liquity trove info is only available on mainnet
555
+ const troveInfo = await _getLiquityTroveInfo(client, network, address);
556
+ if (new Dec(troveInfo.collateral).gt(0)) positions[address.toLowerCase() as EthAddress].liquity = { error: '', data: troveInfo };
557
+ } catch (error) {
558
+ console.error(`Error fetching Liquity trove info for address ${address}:`, error);
559
+ positions[address.toLowerCase() as EthAddress].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
560
+ }
561
+ }),
562
+ ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
563
+ try {
564
+ const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
565
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
566
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: { ...accData, borrowRate: crvUsdMarketsData[market.value].borrowRate } };
567
+ }
568
+ } catch (error) {
569
+ console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
570
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
571
+ }
572
+ })).flat(),
573
+ ...llamaLendMarkets.map((market) => addresses.map(async (address) => {
574
+ try {
575
+ const accData = await _getLlamaLendUserData(client, network, address, market, llamaLendMarketsData[market.value]);
576
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
577
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: '', data: { ...accData, borrowRate: llamaLendMarketsData[market.value].borrowRate } };
578
+ }
579
+ } catch (error) {
580
+ console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
581
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
582
+ }
583
+ })).flat(),
584
+ // liquity sBold/yBold and staking options
585
+ ...addresses.map(async (address) => {
586
+ try {
587
+ if (!isMainnet) {
588
+ stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data: null };
589
+ return;
590
+ }
591
+ const data = await getLiquitySAndYBold(client, network, stakingPositions[address.toLowerCase()].liquityV2, address);
592
+ stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: '', data };
593
+ } catch (error) {
594
+ console.error(`Error fetching SBold/YBold data for address ${address}:`, error);
595
+ stakingPositions[address.toLowerCase() as EthAddress].liquityV2SBoldYBold = { error: `Error fetching sBold/yBold data for address ${address}`, data: null };
596
+ }
597
+ }),
598
+ ]);
599
+
600
+ return {
601
+ positions,
602
+ stakingPositions,
603
+ rewardsData,
604
+ markets,
605
+ };
606
+ }