@defisaver/positions-sdk 2.0.1 → 2.0.5

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 (78) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/config/contracts.d.ts +1018 -16
  5. package/cjs/config/contracts.js +12 -4
  6. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  7. package/cjs/markets/aave/marketAssets.js +1 -1
  8. package/cjs/staking/staking.js +1 -1
  9. package/esm/config/contracts.d.ts +1018 -16
  10. package/esm/config/contracts.js +12 -4
  11. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  12. package/esm/markets/aave/marketAssets.js +1 -1
  13. package/esm/staking/staking.js +1 -1
  14. package/package.json +47 -47
  15. package/src/aaveV2/index.ts +236 -236
  16. package/src/aaveV3/index.ts +489 -489
  17. package/src/compoundV2/index.ts +240 -240
  18. package/src/compoundV3/index.ts +270 -270
  19. package/src/config/contracts.ts +1090 -1082
  20. package/src/constants/index.ts +6 -6
  21. package/src/contracts.ts +107 -107
  22. package/src/curveUsd/index.ts +250 -250
  23. package/src/eulerV2/index.ts +314 -314
  24. package/src/exchange/index.ts +25 -25
  25. package/src/fluid/index.ts +1568 -1568
  26. package/src/helpers/aaveHelpers/index.ts +170 -170
  27. package/src/helpers/compoundHelpers/index.ts +261 -261
  28. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  29. package/src/helpers/eulerHelpers/index.ts +259 -259
  30. package/src/helpers/fluidHelpers/index.ts +324 -324
  31. package/src/helpers/index.ts +10 -10
  32. package/src/helpers/liquityV2Helpers/index.ts +80 -80
  33. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  34. package/src/helpers/makerHelpers/index.ts +52 -52
  35. package/src/helpers/morphoBlueHelpers/index.ts +390 -390
  36. package/src/helpers/sparkHelpers/index.ts +155 -155
  37. package/src/index.ts +45 -45
  38. package/src/liquity/index.ts +104 -104
  39. package/src/liquityV2/index.ts +408 -408
  40. package/src/llamaLend/index.ts +296 -296
  41. package/src/maker/index.ts +223 -223
  42. package/src/markets/aave/index.ts +116 -116
  43. package/src/markets/aave/marketAssets.ts +44 -44
  44. package/src/markets/compound/index.ts +216 -216
  45. package/src/markets/compound/marketsAssets.ts +83 -83
  46. package/src/markets/curveUsd/index.ts +69 -69
  47. package/src/markets/euler/index.ts +26 -26
  48. package/src/markets/fluid/index.ts +2456 -2456
  49. package/src/markets/index.ts +25 -25
  50. package/src/markets/liquityV2/index.ts +102 -102
  51. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  52. package/src/markets/llamaLend/index.ts +235 -235
  53. package/src/markets/morphoBlue/index.ts +895 -895
  54. package/src/markets/spark/index.ts +29 -29
  55. package/src/markets/spark/marketAssets.ts +10 -10
  56. package/src/moneymarket/moneymarketCommonService.ts +80 -80
  57. package/src/morphoBlue/index.ts +222 -222
  58. package/src/portfolio/index.ts +285 -285
  59. package/src/services/priceService.ts +159 -159
  60. package/src/services/utils.ts +63 -63
  61. package/src/services/viem.ts +30 -30
  62. package/src/setup.ts +8 -8
  63. package/src/spark/index.ts +456 -456
  64. package/src/staking/staking.ts +192 -192
  65. package/src/types/aave.ts +194 -194
  66. package/src/types/common.ts +87 -87
  67. package/src/types/compound.ts +136 -136
  68. package/src/types/curveUsd.ts +121 -121
  69. package/src/types/euler.ts +174 -174
  70. package/src/types/fluid.ts +450 -450
  71. package/src/types/index.ts +11 -11
  72. package/src/types/liquity.ts +30 -30
  73. package/src/types/liquityV2.ts +126 -126
  74. package/src/types/llamaLend.ts +157 -157
  75. package/src/types/maker.ts +63 -63
  76. package/src/types/morphoBlue.ts +194 -194
  77. package/src/types/portfolio.ts +60 -60
  78. package/src/types/spark.ts +137 -137
@@ -1,285 +1,285 @@
1
- import Dec from 'decimal.js';
2
- import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
3
- import {
4
- AaveMarkets,
5
- CompoundMarkets, CrvUsdMarkets, EulerV2Markets, LlamaLendMarkets, MorphoBlueMarkets, SparkMarkets,
6
- } from '../markets';
7
- import { _getMorphoBlueAccountData, _getMorphoBlueMarketData } from '../morphoBlue';
8
- import {
9
- AaveV2MarketData,
10
- AaveV3MarketData,
11
- AaveVersions,
12
- CdpInfo,
13
- CompoundV2MarketsData,
14
- CompoundV3MarketsData,
15
- CompoundVersions,
16
- CrvUSDGlobalMarketData,
17
- EulerV2FullMarketData,
18
- LlamaLendGlobalMarketData,
19
- MorphoBlueMarketInfo,
20
- PortfolioPositionsData,
21
- SparkMarketsData,
22
- } from '../types';
23
- import { _getCompoundV3AccountData, _getCompoundV3MarketsData } from '../compoundV3';
24
- import { _getSparkAccountData, _getSparkMarketsData } from '../spark';
25
- import { _getEulerV2AccountData, _getEulerV2MarketsData } from '../eulerV2';
26
- import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
27
- import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
28
- import { _getAaveV3AccountData, _getAaveV3MarketData } from '../aaveV3';
29
- import { ZERO_ADDRESS } from '../constants';
30
- import { _getMakerCdpData, _getUserCdps } from '../maker';
31
- import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
32
- import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
33
- import { getViemProvider } from '../services/viem';
34
- import { _getLiquityTroveInfo } from '../liquity';
35
- import { _getLiquityV2MarketData, _getLiquityV2TroveData, _getLiquityV2UserTroveIds } from '../liquityV2';
36
- import { _getUserPositionsPortfolio } from '../fluid';
37
- import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
38
-
39
- export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], summerFiAddresses: EthAddress[]): Promise<PortfolioPositionsData> {
40
- const isMainnet = network === NetworkNumber.Eth;
41
- const isOpt = network === NetworkNumber.Opt;
42
-
43
- const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
44
- const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
45
- const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
46
- const eulerV2Markets = Object.values(EulerV2Markets(network)).filter((market) => market.chainIds.includes(network));
47
- const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
48
- const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
49
- const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
50
- const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
51
- const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
52
-
53
- const client = getViemProvider(provider, network, {
54
- batch: {
55
- multicall: {
56
- batchSize: 2500000,
57
- },
58
- },
59
- });
60
- const defaultClient = getViemProvider(defaultProvider, network, {
61
- batch: {
62
- multicall: {
63
- batchSize: 2500000,
64
- },
65
- },
66
- });
67
-
68
- const morphoMarketsData: Record<string, MorphoBlueMarketInfo> = {};
69
- const compoundV3MarketsData: Record<string, CompoundV3MarketsData> = {};
70
- const sparkMarketsData: Record<string, SparkMarketsData> = {};
71
- const eulerV2MarketsData: Record<string, EulerV2FullMarketData> = {};
72
- const aaveV3MarketsData: Record<string, AaveV3MarketData> = {};
73
- const makerCdps: Record<string, CdpInfo[]> = {};
74
- const aaveV2MarketsData: Record<string, AaveV2MarketData> = {};
75
- const compoundV2MarketsData: Record<string, CompoundV2MarketsData> = {};
76
- const crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData> = {};
77
- const llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData> = {};
78
-
79
- const positions: PortfolioPositionsData = {};
80
- const allAddresses = [...addresses, ...summerFiAddresses];
81
- for (const address of allAddresses) {
82
- positions[address.toLowerCase() as EthAddress] = {
83
- aaveV3: {},
84
- morphoBlue: {},
85
- compoundV3: {},
86
- spark: {},
87
- eulerV2: {},
88
- maker: {},
89
- aaveV2: {},
90
- compoundV2: {},
91
- liquity: {},
92
- crvUsd: {},
93
- llamaLend: {},
94
- fluid: {
95
- error: '',
96
- data: {},
97
- },
98
- };
99
- }
100
-
101
- await Promise.allSettled([
102
- ...morphoMarkets.map(async (market) => {
103
- const marketData = await _getMorphoBlueMarketData(client, network, market);
104
- morphoMarketsData[market.value] = marketData;
105
- }),
106
- ...compoundV3Markets.map(async (market) => {
107
- const marketData = await _getCompoundV3MarketsData(client, network, market, defaultClient);
108
- compoundV3MarketsData[market.value] = marketData;
109
- }),
110
- ...sparkMarkets.map(async (market) => {
111
- const marketData = await _getSparkMarketsData(client, network, market);
112
- sparkMarketsData[market.value] = marketData;
113
- }),
114
- ...eulerV2Markets.map(async (market) => {
115
- const marketData = await _getEulerV2MarketsData(client, network, market);
116
- eulerV2MarketsData[market.value] = marketData;
117
- }),
118
- ...aaveV3Markets.map(async (market) => {
119
- const marketData = await _getAaveV3MarketData(client, network, market);
120
- aaveV3MarketsData[market.value] = marketData;
121
- }),
122
- ...addresses.map(async (address) => {
123
- if (!isMainnet) return; // Maker CDPs are only available on mainnet
124
- const makerCdp = await _getUserCdps(client, network, address);
125
- makerCdps[address.toLowerCase() as EthAddress] = makerCdp;
126
- }),
127
- ...aaveV2Markets.map(async (market) => {
128
- const marketData = await _getAaveV2MarketsData(client, network, market);
129
- aaveV2MarketsData[market.value] = marketData;
130
- }),
131
- ...compoundV2Markets.map(async (market) => {
132
- const marketData = await _getCompoundV2MarketsData(client, network);
133
- compoundV2MarketsData[market.value] = marketData;
134
- }),
135
- ...crvUsdMarkets.map(async (market) => {
136
- const marketData = await _getCurveUsdGlobalData(client, network, market);
137
- crvUsdMarketsData[market.value] = marketData;
138
- }),
139
- ...llamaLendMarkets.map(async (market) => {
140
- const marketData = await _getLlamaLendGlobalData(client, network, market);
141
- llamaLendMarketsData[market.value] = marketData;
142
- }),
143
- ...addresses.map(async (address) => {
144
- try {
145
- if (isOpt) return; // Fluid is not available on Optimism
146
- const userPositions = await _getUserPositionsPortfolio(client, network, address);
147
- for (const position of userPositions) {
148
- if (new Dec(position.userData.suppliedUsd).gt(0)) {
149
- positions[address.toLowerCase() as EthAddress].fluid.data[position.userData.nftId] = position.userData;
150
- }
151
- }
152
- } catch (error) {
153
- console.error(`Error fetching Fluid positions for address ${address}:`, error);
154
- positions[address.toLowerCase() as EthAddress].fluid = {
155
- error: `Error fetching Fluid positions for address ${address}`,
156
- data: {},
157
- };
158
- }
159
- }),
160
- ]);
161
-
162
-
163
- await Promise.all([
164
- ...aaveV3Markets.map((market) => allAddresses.map(async (address) => {
165
- try {
166
- const accData = await _getAaveV3AccountData(client, network, address, { selectedMarket: market, ...aaveV3MarketsData[market.value] });
167
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: accData };
168
- } catch (error) {
169
- console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
170
- positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
171
- }
172
- })).flat(),
173
- ...morphoMarkets.map((market) => addresses.map(async (address) => {
174
- try {
175
- const accData = await _getMorphoBlueAccountData(client, network, address, market, morphoMarketsData[market.value]);
176
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: '', data: accData };
177
- } catch (error) {
178
- console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
179
- positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
180
- }
181
- })).flat(),
182
- ...compoundV3Markets.map((market) => addresses.map(async (address) => {
183
- try {
184
- const accData = await _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: compoundV3MarketsData[market.value].assetsData });
185
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: '', data: accData };
186
- } catch (error) {
187
- console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
188
- positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
189
- }
190
- })).flat(),
191
- ...sparkMarkets.map((market) => allAddresses.map(async (address) => {
192
- try {
193
- const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: sparkMarketsData[market.value].assetsData });
194
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: accData };
195
- } catch (error) {
196
- console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
197
- positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
198
- }
199
- })).flat(),
200
- ...eulerV2Markets.map((market) => addresses.map((address) => {
201
- const eulerV2SubAccounts = getEulerV2SubAccounts(address);
202
- const eulerV2Addresses = [address, ...eulerV2SubAccounts];
203
- return Promise.all(eulerV2Addresses.map(async (eulerAddress) => {
204
- try {
205
- const accData = await _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
206
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
207
- if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
208
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
209
- }
210
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: '', data: accData };
211
- }
212
- } catch (error) {
213
- console.error(`Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}:`, error);
214
- if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
215
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
216
- }
217
- 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 };
218
- }
219
- }));
220
- }).flat()).flat(),
221
- ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
222
- try {
223
- const cdpData = await _getMakerCdpData(client, network, cdpInfo);
224
- if (cdpData) {
225
- positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: '', data: cdpData };
226
- }
227
- } catch (error) {
228
- console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
229
- positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
230
- }
231
- })).flat(),
232
- ...aaveV2Markets.map((market) => addresses.map(async (address) => {
233
- try {
234
- const accData = await _getAaveV2AccountData(client, network, address, aaveV2MarketsData[market.value].assetsData, market);
235
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: '', data: accData };
236
- } catch (error) {
237
- console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
238
- positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
239
- }
240
- })).flat(),
241
- ...compoundV2Markets.map((market) => addresses.map(async (address) => {
242
- try {
243
- const accData = await _getCompoundV2AccountData(client, network, address, compoundV2MarketsData[market.value].assetsData);
244
- if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: '', data: accData };
245
- } catch (error) {
246
- console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
247
- positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
248
- }
249
- })).flat(),
250
- ...addresses.map(async (address) => {
251
- try {
252
- if (!isMainnet) return; // Liquity trove info is only available on mainnet
253
- const troveInfo = await _getLiquityTroveInfo(client, network, address);
254
- if (new Dec(troveInfo.collateral).gt(0)) positions[address.toLowerCase() as EthAddress].liquity = { error: '', data: troveInfo };
255
- } catch (error) {
256
- console.error(`Error fetching Liquity trove info for address ${address}:`, error);
257
- positions[address.toLowerCase() as EthAddress].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
258
- }
259
- }),
260
- ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
261
- try {
262
- const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
263
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
264
- positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: { ...accData, borrowRate: crvUsdMarketsData[market.value].borrowRate } };
265
- }
266
- } catch (error) {
267
- console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
268
- positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
269
- }
270
- })).flat(),
271
- ...llamaLendMarkets.map((market) => addresses.map(async (address) => {
272
- try {
273
- const accData = await _getLlamaLendUserData(client, network, address, market, llamaLendMarketsData[market.value]);
274
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
275
- positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: '', data: { ...accData, borrowRate: llamaLendMarketsData[market.value].borrowRate } };
276
- }
277
- } catch (error) {
278
- console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
279
- positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
280
- }
281
- })).flat(),
282
- ]);
283
-
284
- return positions;
285
- }
1
+ import Dec from 'decimal.js';
2
+ import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
3
+ import {
4
+ AaveMarkets,
5
+ CompoundMarkets, CrvUsdMarkets, EulerV2Markets, LlamaLendMarkets, MorphoBlueMarkets, SparkMarkets,
6
+ } from '../markets';
7
+ import { _getMorphoBlueAccountData, _getMorphoBlueMarketData } from '../morphoBlue';
8
+ import {
9
+ AaveV2MarketData,
10
+ AaveV3MarketData,
11
+ AaveVersions,
12
+ CdpInfo,
13
+ CompoundV2MarketsData,
14
+ CompoundV3MarketsData,
15
+ CompoundVersions,
16
+ CrvUSDGlobalMarketData,
17
+ EulerV2FullMarketData,
18
+ LlamaLendGlobalMarketData,
19
+ MorphoBlueMarketInfo,
20
+ PortfolioPositionsData,
21
+ SparkMarketsData,
22
+ } from '../types';
23
+ import { _getCompoundV3AccountData, _getCompoundV3MarketsData } from '../compoundV3';
24
+ import { _getSparkAccountData, _getSparkMarketsData } from '../spark';
25
+ import { _getEulerV2AccountData, _getEulerV2MarketsData } from '../eulerV2';
26
+ import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
27
+ import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
28
+ import { _getAaveV3AccountData, _getAaveV3MarketData } from '../aaveV3';
29
+ import { ZERO_ADDRESS } from '../constants';
30
+ import { _getMakerCdpData, _getUserCdps } from '../maker';
31
+ import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
32
+ import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
33
+ import { getViemProvider } from '../services/viem';
34
+ import { _getLiquityTroveInfo } from '../liquity';
35
+ import { _getLiquityV2MarketData, _getLiquityV2TroveData, _getLiquityV2UserTroveIds } from '../liquityV2';
36
+ import { _getUserPositionsPortfolio } from '../fluid';
37
+ import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
38
+
39
+ export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], summerFiAddresses: EthAddress[]): Promise<PortfolioPositionsData> {
40
+ const isMainnet = network === NetworkNumber.Eth;
41
+ const isOpt = network === NetworkNumber.Opt;
42
+
43
+ const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
44
+ const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
45
+ const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
46
+ const eulerV2Markets = Object.values(EulerV2Markets(network)).filter((market) => market.chainIds.includes(network));
47
+ const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
48
+ const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
49
+ const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
50
+ const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
51
+ const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
52
+
53
+ const client = getViemProvider(provider, network, {
54
+ batch: {
55
+ multicall: {
56
+ batchSize: 2500000,
57
+ },
58
+ },
59
+ });
60
+ const defaultClient = getViemProvider(defaultProvider, network, {
61
+ batch: {
62
+ multicall: {
63
+ batchSize: 2500000,
64
+ },
65
+ },
66
+ });
67
+
68
+ const morphoMarketsData: Record<string, MorphoBlueMarketInfo> = {};
69
+ const compoundV3MarketsData: Record<string, CompoundV3MarketsData> = {};
70
+ const sparkMarketsData: Record<string, SparkMarketsData> = {};
71
+ const eulerV2MarketsData: Record<string, EulerV2FullMarketData> = {};
72
+ const aaveV3MarketsData: Record<string, AaveV3MarketData> = {};
73
+ const makerCdps: Record<string, CdpInfo[]> = {};
74
+ const aaveV2MarketsData: Record<string, AaveV2MarketData> = {};
75
+ const compoundV2MarketsData: Record<string, CompoundV2MarketsData> = {};
76
+ const crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData> = {};
77
+ const llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData> = {};
78
+
79
+ const positions: PortfolioPositionsData = {};
80
+ const allAddresses = [...addresses, ...summerFiAddresses];
81
+ for (const address of allAddresses) {
82
+ positions[address.toLowerCase() as EthAddress] = {
83
+ aaveV3: {},
84
+ morphoBlue: {},
85
+ compoundV3: {},
86
+ spark: {},
87
+ eulerV2: {},
88
+ maker: {},
89
+ aaveV2: {},
90
+ compoundV2: {},
91
+ liquity: {},
92
+ crvUsd: {},
93
+ llamaLend: {},
94
+ fluid: {
95
+ error: '',
96
+ data: {},
97
+ },
98
+ };
99
+ }
100
+
101
+ await Promise.allSettled([
102
+ ...morphoMarkets.map(async (market) => {
103
+ const marketData = await _getMorphoBlueMarketData(client, network, market);
104
+ morphoMarketsData[market.value] = marketData;
105
+ }),
106
+ ...compoundV3Markets.map(async (market) => {
107
+ const marketData = await _getCompoundV3MarketsData(client, network, market, defaultClient);
108
+ compoundV3MarketsData[market.value] = marketData;
109
+ }),
110
+ ...sparkMarkets.map(async (market) => {
111
+ const marketData = await _getSparkMarketsData(client, network, market);
112
+ sparkMarketsData[market.value] = marketData;
113
+ }),
114
+ ...eulerV2Markets.map(async (market) => {
115
+ const marketData = await _getEulerV2MarketsData(client, network, market);
116
+ eulerV2MarketsData[market.value] = marketData;
117
+ }),
118
+ ...aaveV3Markets.map(async (market) => {
119
+ const marketData = await _getAaveV3MarketData(client, network, market);
120
+ aaveV3MarketsData[market.value] = marketData;
121
+ }),
122
+ ...addresses.map(async (address) => {
123
+ if (!isMainnet) return; // Maker CDPs are only available on mainnet
124
+ const makerCdp = await _getUserCdps(client, network, address);
125
+ makerCdps[address.toLowerCase() as EthAddress] = makerCdp;
126
+ }),
127
+ ...aaveV2Markets.map(async (market) => {
128
+ const marketData = await _getAaveV2MarketsData(client, network, market);
129
+ aaveV2MarketsData[market.value] = marketData;
130
+ }),
131
+ ...compoundV2Markets.map(async (market) => {
132
+ const marketData = await _getCompoundV2MarketsData(client, network);
133
+ compoundV2MarketsData[market.value] = marketData;
134
+ }),
135
+ ...crvUsdMarkets.map(async (market) => {
136
+ const marketData = await _getCurveUsdGlobalData(client, network, market);
137
+ crvUsdMarketsData[market.value] = marketData;
138
+ }),
139
+ ...llamaLendMarkets.map(async (market) => {
140
+ const marketData = await _getLlamaLendGlobalData(client, network, market);
141
+ llamaLendMarketsData[market.value] = marketData;
142
+ }),
143
+ ...addresses.map(async (address) => {
144
+ try {
145
+ if (isOpt) return; // Fluid is not available on Optimism
146
+ const userPositions = await _getUserPositionsPortfolio(client, network, address);
147
+ for (const position of userPositions) {
148
+ if (new Dec(position.userData.suppliedUsd).gt(0)) {
149
+ positions[address.toLowerCase() as EthAddress].fluid.data[position.userData.nftId] = position.userData;
150
+ }
151
+ }
152
+ } catch (error) {
153
+ console.error(`Error fetching Fluid positions for address ${address}:`, error);
154
+ positions[address.toLowerCase() as EthAddress].fluid = {
155
+ error: `Error fetching Fluid positions for address ${address}`,
156
+ data: {},
157
+ };
158
+ }
159
+ }),
160
+ ]);
161
+
162
+
163
+ await Promise.all([
164
+ ...aaveV3Markets.map((market) => allAddresses.map(async (address) => {
165
+ try {
166
+ const accData = await _getAaveV3AccountData(client, network, address, { selectedMarket: market, ...aaveV3MarketsData[market.value] });
167
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: accData };
168
+ } catch (error) {
169
+ console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
170
+ positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
171
+ }
172
+ })).flat(),
173
+ ...morphoMarkets.map((market) => addresses.map(async (address) => {
174
+ try {
175
+ const accData = await _getMorphoBlueAccountData(client, network, address, market, morphoMarketsData[market.value]);
176
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: '', data: accData };
177
+ } catch (error) {
178
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
179
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
180
+ }
181
+ })).flat(),
182
+ ...compoundV3Markets.map((market) => addresses.map(async (address) => {
183
+ try {
184
+ const accData = await _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: compoundV3MarketsData[market.value].assetsData });
185
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: '', data: accData };
186
+ } catch (error) {
187
+ console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
188
+ positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
189
+ }
190
+ })).flat(),
191
+ ...sparkMarkets.map((market) => allAddresses.map(async (address) => {
192
+ try {
193
+ const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: sparkMarketsData[market.value].assetsData });
194
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: accData };
195
+ } catch (error) {
196
+ console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
197
+ positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
198
+ }
199
+ })).flat(),
200
+ ...eulerV2Markets.map((market) => addresses.map((address) => {
201
+ const eulerV2SubAccounts = getEulerV2SubAccounts(address);
202
+ const eulerV2Addresses = [address, ...eulerV2SubAccounts];
203
+ return Promise.all(eulerV2Addresses.map(async (eulerAddress) => {
204
+ try {
205
+ const accData = await _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
206
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
207
+ if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
208
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
209
+ }
210
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = { error: '', data: accData };
211
+ }
212
+ } catch (error) {
213
+ console.error(`Error fetching EulerV2 account data for address ${eulerAddress} on market ${market.value}:`, error);
214
+ if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
215
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
216
+ }
217
+ 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 };
218
+ }
219
+ }));
220
+ }).flat()).flat(),
221
+ ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
222
+ try {
223
+ const cdpData = await _getMakerCdpData(client, network, cdpInfo);
224
+ if (cdpData) {
225
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: '', data: cdpData };
226
+ }
227
+ } catch (error) {
228
+ console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
229
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
230
+ }
231
+ })).flat(),
232
+ ...aaveV2Markets.map((market) => addresses.map(async (address) => {
233
+ try {
234
+ const accData = await _getAaveV2AccountData(client, network, address, aaveV2MarketsData[market.value].assetsData, market);
235
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: '', data: accData };
236
+ } catch (error) {
237
+ console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
238
+ positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
239
+ }
240
+ })).flat(),
241
+ ...compoundV2Markets.map((market) => addresses.map(async (address) => {
242
+ try {
243
+ const accData = await _getCompoundV2AccountData(client, network, address, compoundV2MarketsData[market.value].assetsData);
244
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: '', data: accData };
245
+ } catch (error) {
246
+ console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
247
+ positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
248
+ }
249
+ })).flat(),
250
+ ...addresses.map(async (address) => {
251
+ try {
252
+ if (!isMainnet) return; // Liquity trove info is only available on mainnet
253
+ const troveInfo = await _getLiquityTroveInfo(client, network, address);
254
+ if (new Dec(troveInfo.collateral).gt(0)) positions[address.toLowerCase() as EthAddress].liquity = { error: '', data: troveInfo };
255
+ } catch (error) {
256
+ console.error(`Error fetching Liquity trove info for address ${address}:`, error);
257
+ positions[address.toLowerCase() as EthAddress].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
258
+ }
259
+ }),
260
+ ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
261
+ try {
262
+ const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
263
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
264
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: { ...accData, borrowRate: crvUsdMarketsData[market.value].borrowRate } };
265
+ }
266
+ } catch (error) {
267
+ console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
268
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
269
+ }
270
+ })).flat(),
271
+ ...llamaLendMarkets.map((market) => addresses.map(async (address) => {
272
+ try {
273
+ const accData = await _getLlamaLendUserData(client, network, address, market, llamaLendMarketsData[market.value]);
274
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
275
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: '', data: { ...accData, borrowRate: llamaLendMarketsData[market.value].borrowRate } };
276
+ }
277
+ } catch (error) {
278
+ console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
279
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
280
+ }
281
+ })).flat(),
282
+ ]);
283
+
284
+ return positions;
285
+ }