@defisaver/positions-sdk 2.1.52 → 2.1.53-aave-v4

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