@defisaver/positions-sdk 2.0.9 → 2.0.10-dev-linea-1

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 (104) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/aaveV3/index.js +1 -1
  5. package/cjs/config/contracts.d.ts +194 -33
  6. package/cjs/config/contracts.js +18 -1
  7. package/cjs/contracts.d.ts +1283 -293
  8. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  9. package/cjs/markets/aave/index.js +1 -1
  10. package/cjs/markets/aave/marketAssets.d.ts +4 -0
  11. package/cjs/markets/aave/marketAssets.js +5 -1
  12. package/cjs/markets/compound/index.js +11 -0
  13. package/cjs/markets/compound/marketsAssets.d.ts +7 -0
  14. package/cjs/markets/compound/marketsAssets.js +7 -0
  15. package/cjs/markets/spark/marketAssets.d.ts +1 -0
  16. package/cjs/markets/spark/marketAssets.js +1 -0
  17. package/cjs/portfolio/index.js +2 -2
  18. package/cjs/services/viem.d.ts +46 -0
  19. package/cjs/services/viem.js +2 -0
  20. package/cjs/types/common.d.ts +2 -1
  21. package/cjs/types/common.js +1 -0
  22. package/esm/aaveV3/index.js +1 -1
  23. package/esm/config/contracts.d.ts +194 -33
  24. package/esm/config/contracts.js +18 -1
  25. package/esm/contracts.d.ts +1283 -293
  26. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  27. package/esm/markets/aave/index.js +1 -1
  28. package/esm/markets/aave/marketAssets.d.ts +4 -0
  29. package/esm/markets/aave/marketAssets.js +4 -0
  30. package/esm/markets/compound/index.js +11 -0
  31. package/esm/markets/compound/marketsAssets.d.ts +7 -0
  32. package/esm/markets/compound/marketsAssets.js +7 -0
  33. package/esm/markets/spark/marketAssets.d.ts +1 -0
  34. package/esm/markets/spark/marketAssets.js +1 -0
  35. package/esm/portfolio/index.js +2 -2
  36. package/esm/services/viem.d.ts +46 -0
  37. package/esm/services/viem.js +3 -1
  38. package/esm/types/common.d.ts +2 -1
  39. package/esm/types/common.js +1 -0
  40. package/package.json +47 -47
  41. package/src/aaveV2/index.ts +236 -236
  42. package/src/aaveV3/index.ts +489 -489
  43. package/src/compoundV2/index.ts +240 -240
  44. package/src/compoundV3/index.ts +270 -270
  45. package/src/config/contracts.ts +1107 -1090
  46. package/src/constants/index.ts +6 -6
  47. package/src/contracts.ts +107 -107
  48. package/src/curveUsd/index.ts +250 -250
  49. package/src/eulerV2/index.ts +314 -314
  50. package/src/exchange/index.ts +25 -25
  51. package/src/fluid/index.ts +1568 -1568
  52. package/src/helpers/aaveHelpers/index.ts +170 -170
  53. package/src/helpers/compoundHelpers/index.ts +261 -261
  54. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  55. package/src/helpers/eulerHelpers/index.ts +259 -259
  56. package/src/helpers/fluidHelpers/index.ts +324 -324
  57. package/src/helpers/index.ts +10 -10
  58. package/src/helpers/liquityV2Helpers/index.ts +80 -80
  59. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  60. package/src/helpers/makerHelpers/index.ts +52 -52
  61. package/src/helpers/morphoBlueHelpers/index.ts +390 -390
  62. package/src/helpers/sparkHelpers/index.ts +155 -155
  63. package/src/index.ts +45 -45
  64. package/src/liquity/index.ts +104 -104
  65. package/src/liquityV2/index.ts +408 -408
  66. package/src/llamaLend/index.ts +296 -296
  67. package/src/maker/index.ts +223 -223
  68. package/src/markets/aave/index.ts +116 -116
  69. package/src/markets/aave/marketAssets.ts +49 -44
  70. package/src/markets/compound/index.ts +227 -216
  71. package/src/markets/compound/marketsAssets.ts +90 -83
  72. package/src/markets/curveUsd/index.ts +69 -69
  73. package/src/markets/euler/index.ts +26 -26
  74. package/src/markets/fluid/index.ts +2456 -2456
  75. package/src/markets/index.ts +25 -25
  76. package/src/markets/liquityV2/index.ts +102 -102
  77. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  78. package/src/markets/llamaLend/index.ts +235 -235
  79. package/src/markets/morphoBlue/index.ts +895 -895
  80. package/src/markets/spark/index.ts +29 -29
  81. package/src/markets/spark/marketAssets.ts +11 -10
  82. package/src/moneymarket/moneymarketCommonService.ts +80 -80
  83. package/src/morphoBlue/index.ts +222 -222
  84. package/src/portfolio/index.ts +285 -285
  85. package/src/services/priceService.ts +159 -159
  86. package/src/services/utils.ts +63 -63
  87. package/src/services/viem.ts +32 -30
  88. package/src/setup.ts +8 -8
  89. package/src/spark/index.ts +456 -456
  90. package/src/staking/staking.ts +192 -192
  91. package/src/types/aave.ts +194 -194
  92. package/src/types/common.ts +88 -87
  93. package/src/types/compound.ts +136 -136
  94. package/src/types/curveUsd.ts +121 -121
  95. package/src/types/euler.ts +174 -174
  96. package/src/types/fluid.ts +450 -450
  97. package/src/types/index.ts +11 -11
  98. package/src/types/liquity.ts +30 -30
  99. package/src/types/liquityV2.ts +126 -126
  100. package/src/types/llamaLend.ts +157 -157
  101. package/src/types/maker.ts +63 -63
  102. package/src/types/morphoBlue.ts +194 -194
  103. package/src/types/portfolio.ts +60 -60
  104. package/src/types/spark.ts +137 -137
@@ -1,240 +1,240 @@
1
- import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
2
- import Dec from 'decimal.js';
3
- import { Client } from 'viem';
4
- import { BLOCKS_IN_A_YEAR } from '../constants';
5
- import { aprToApy } from '../moneymarket';
6
- import { compareAddresses, handleWbtcLegacy, wethToEth } from '../services/utils';
7
- import {
8
- Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
9
- } from '../types/common';
10
- import { CompoundLoanInfoContractViem, ComptrollerContractViem } from '../contracts';
11
- import { compoundV2CollateralAssets } from '../markets';
12
- import {
13
- CompoundV2AssetsData, CompoundV2MarketsData, CompoundV2PositionData, CompoundV2UsedAsset, CompoundV2UsedAssets,
14
- } from '../types';
15
- import { getCompoundV2AggregatedData } from '../helpers/compoundHelpers';
16
- import { getViemProvider, setViemBlockNumber } from '../services/viem';
17
-
18
- const compAddress = '0xc00e94cb662c3520282e6f5717214004a7f26888';
19
-
20
- export const _getCompoundV2MarketsData = async (provider: Client, network: NetworkNumber): Promise<CompoundV2MarketsData> => {
21
- const cAddresses = compoundV2CollateralAssets.map(a => a.address);
22
-
23
- const loanInfoContract = CompoundLoanInfoContractViem(provider, network);
24
- const loanInfo = await loanInfoContract.read.getFullTokensInfo([cAddresses as EthAddress[]]);
25
-
26
- const compPrice = loanInfo.find(m => compareAddresses(m.underlyingTokenAddress, compAddress))!.price.toString();
27
-
28
- const assetsData = loanInfo
29
- .map((market, i) => {
30
- let symbol = getAssetInfoByAddress(cAddresses[i]).underlyingAsset;
31
- let isWbtcLegacy = false;
32
- const totalSupply = new Dec(market.totalSupply.toString()).div(1e18).times(market.exchangeRate.toString());
33
- const totalBorrow = market.totalBorrow.toString();
34
- const borrowCap = market.borrowCap.toString();
35
- const compSupplySpeeds = market.compSupplySpeeds.toString();
36
- const compBorrowSpeeds = market.compBorrowSpeeds.toString();
37
- const assetPrice = market.price.toString();
38
-
39
- const pricePrecisionDiff = 18 - getAssetInfo(getAssetInfoByAddress(cAddresses[i]).underlyingAsset).decimals;
40
-
41
- // compSupplySpeeds/compBorrowSpeeds is per block per market (borrow & supply are separate markets)
42
- const incentiveSupplyApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compSupplySpeeds * +compPrice) / +assetPrice / +totalSupply).toString();
43
- const incentiveBorrowApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compBorrowSpeeds * +compPrice) / +assetPrice / +totalBorrow).toString();
44
-
45
- if (cAddresses[i].toLowerCase() === '0xc11b1268c1a384e55c48c2391d8d480264a3a7f4'.toLowerCase()) {
46
- symbol = 'WBTC Legacy';
47
- isWbtcLegacy = true;
48
- }
49
- return {
50
- symbol,
51
- underlyingTokenAddress: market.underlyingTokenAddress,
52
- supplyRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.supplyRate.toString()).div(1e16).toString()).toString(),
53
- borrowRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.borrowRate.toString()).div(1e16).toString()).toString(),
54
- incentiveSupplyToken: 'COMP',
55
- incentiveBorrowToken: 'COMP',
56
- incentiveSupplyApy,
57
- incentiveBorrowApy,
58
- collateralFactor: new Dec(market.collateralFactor.toString()).div(1e18).toString(),
59
- marketLiquidity: assetAmountInEth(market.marketLiquidity.toString(), handleWbtcLegacy(symbol)),
60
- utilization: new Dec(market.totalBorrow.toString()).div(totalSupply).times(100).toString(),
61
- totalSupply: assetAmountInEth(totalSupply, handleWbtcLegacy(symbol)),
62
- totalBorrow: assetAmountInEth(totalBorrow, handleWbtcLegacy(symbol)),
63
- exchangeRate: new Dec(market.exchangeRate.toString()).div(1e28).toString(),
64
- borrowCap: assetAmountInEth(borrowCap, handleWbtcLegacy(symbol)),
65
- canBeBorrowed: market.canBorrow,
66
- canBeSupplied: market.canMint,
67
- price: new Dec(market.price.toString()).div(10 ** (18 + pricePrecisionDiff)).toString(),
68
- };
69
- });
70
-
71
- const payload = {} as CompoundV2AssetsData;
72
- // Sort by market size
73
- assetsData
74
- .sort((a, b) => {
75
- const aMarket = new Dec(a.price).times(a.totalSupply).toString();
76
- const bMarket = new Dec(b.price).times(b.totalSupply).toString();
77
-
78
- return new Dec(bMarket).minus(aMarket).toNumber();
79
- })
80
- .forEach((market, i) => { payload[market.symbol] = { ...market, sortIndex: i }; });
81
-
82
- return { assetsData: payload };
83
- };
84
-
85
- export const getCompoundV2MarketsData = async (provider: EthereumProvider, network: NetworkNumber): Promise<CompoundV2MarketsData> => _getCompoundV2MarketsData(getViemProvider(provider, network), network);
86
-
87
- export const EMPTY_COMPOUND_DATA = {
88
- usedAssets: {},
89
- suppliedUsd: '0',
90
- borrowedUsd: '0',
91
- borrowLimitUsd: '0',
92
- leftToBorrowUsd: '0',
93
- ratio: '0',
94
- minRatio: '0',
95
- netApy: '0',
96
- incentiveUsd: '0',
97
- totalInterestUsd: '0',
98
- borrowStableSupplyUnstable: false,
99
- };
100
-
101
- const getCollateralAssetsAddresses = async (provider: Client, network: NetworkNumber, account: EthAddress) => {
102
- const contract = ComptrollerContractViem(provider, network);
103
-
104
- return contract.read.getAssetsIn([account]);
105
- };
106
-
107
- const getAllMarketAddresses = async (provider: Client, network: NetworkNumber, block: Blockish) => {
108
- const contract = ComptrollerContractViem(provider, network);
109
-
110
- return contract.read.getAllMarkets(setViemBlockNumber(block));
111
- };
112
-
113
-
114
- export const _getCompoundV2AccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
115
- let balances: PositionBalances = {
116
- collateral: {},
117
- debt: {},
118
- };
119
-
120
- if (!address) {
121
- return balances;
122
- }
123
-
124
- const assets = await getAllMarketAddresses(provider, network, block);
125
- const assetInfo = assets.map(a => getAssetInfoByAddress(a, network));
126
- const loanInfoContract = CompoundLoanInfoContractViem(provider, network, block);
127
- const loanInfo = await loanInfoContract.read.getTokenBalances([address, assets], setViemBlockNumber(block));
128
-
129
- loanInfo[0].forEach((weiAmount: any, i: number) => {
130
- const asset = wethToEth(
131
- assetInfo[i].symbol === 'cWBTC Legacy'
132
- ? `${assetInfo[i].underlyingAsset} Legacy`
133
- : assetInfo[i].underlyingAsset,
134
- );
135
-
136
- balances = {
137
- collateral: {
138
- ...balances.collateral,
139
- [addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
140
- },
141
- };
142
- });
143
- loanInfo[1].forEach((weiAmount: any, i: number) => {
144
- const asset = wethToEth(
145
- assetInfo[i].symbol === 'cWBTC Legacy'
146
- ? `${assetInfo[i].underlyingAsset} Legacy`
147
- : assetInfo[i].underlyingAsset,
148
- );
149
-
150
- balances = {
151
- ...balances,
152
- debt: {
153
- ...balances.debt,
154
- [addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
155
- },
156
- };
157
- });
158
-
159
- return balances;
160
- };
161
-
162
- export const getCompoundV2AccountBalances = async (
163
- provider: EthereumProvider,
164
- network: NetworkNumber,
165
- block: Blockish,
166
- addressMapping: boolean,
167
- address: EthAddress,
168
- ): Promise<PositionBalances> => _getCompoundV2AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
169
-
170
- export const _getCompoundV2AccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, assetsData: CompoundV2AssetsData): Promise<CompoundV2PositionData> => {
171
- if (!address) throw new Error('No address provided');
172
-
173
- let payload = { ...EMPTY_COMPOUND_DATA, lastUpdated: Date.now() };
174
-
175
- const loanInfoContract = CompoundLoanInfoContractViem(provider, network);
176
- const [loanInfo, collateralAssetsAddresses] = await Promise.all([
177
- loanInfoContract.read.getTokenBalances([address, compoundV2CollateralAssets.map(a => a.address) as EthAddress[]]),
178
- getCollateralAssetsAddresses(provider, network, address),
179
- ]);
180
-
181
- const usedAssets = {} as CompoundV2UsedAssets;
182
-
183
- loanInfo[0].forEach((weiAmount: BigInt, i: number) => {
184
- const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
185
- ? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
186
- : compoundV2CollateralAssets[i].underlyingAsset;
187
- const amount = assetAmountInEth(weiAmount.toString(), asset);
188
- const collateral = !!collateralAssetsAddresses.find(a => compareAddresses(a, compoundV2CollateralAssets[i].address));
189
- if (weiAmount.toString() === '0' && !collateral) return;
190
- if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
191
- usedAssets[asset] = {
192
- ...usedAssets[asset],
193
- symbol: asset,
194
- supplied: amount,
195
- suppliedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
196
- isSupplied: +amount > 0,
197
- collateral,
198
- };
199
- });
200
-
201
- loanInfo[1].forEach((weiAmount, i) => {
202
- if (weiAmount.toString() === '0') return;
203
- const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
204
- ? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
205
- : compoundV2CollateralAssets[i].underlyingAsset;
206
- const amount = assetAmountInEth(weiAmount.toString(), asset);
207
- if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
208
- usedAssets[asset] = {
209
- ...usedAssets[asset],
210
- symbol: asset,
211
- borrowed: amount,
212
- borrowedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
213
- isBorrowed: true,
214
- collateral: !!usedAssets[asset].collateral,
215
- };
216
- });
217
-
218
- payload = {
219
- ...payload,
220
- usedAssets,
221
- ...getCompoundV2AggregatedData({
222
- usedAssets, assetsData,
223
- }),
224
- };
225
-
226
- return payload;
227
- };
228
-
229
- export const getCompoundV2AccountData = async (
230
- provider: EthereumProvider,
231
- network: NetworkNumber,
232
- address: EthAddress,
233
- assetsData: CompoundV2AssetsData,
234
- ): Promise<CompoundV2PositionData> => _getCompoundV2AccountData(getViemProvider(provider, network), network, address, assetsData);
235
-
236
- export const getCompoundV2FullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress): Promise<CompoundV2PositionData> => {
237
- const marketData = await getCompoundV2MarketsData(provider, network);
238
- const positionData = await getCompoundV2AccountData(provider, network, address, marketData.assetsData);
239
- return positionData;
240
- };
1
+ import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
2
+ import Dec from 'decimal.js';
3
+ import { Client } from 'viem';
4
+ import { BLOCKS_IN_A_YEAR } from '../constants';
5
+ import { aprToApy } from '../moneymarket';
6
+ import { compareAddresses, handleWbtcLegacy, wethToEth } from '../services/utils';
7
+ import {
8
+ Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
9
+ } from '../types/common';
10
+ import { CompoundLoanInfoContractViem, ComptrollerContractViem } from '../contracts';
11
+ import { compoundV2CollateralAssets } from '../markets';
12
+ import {
13
+ CompoundV2AssetsData, CompoundV2MarketsData, CompoundV2PositionData, CompoundV2UsedAsset, CompoundV2UsedAssets,
14
+ } from '../types';
15
+ import { getCompoundV2AggregatedData } from '../helpers/compoundHelpers';
16
+ import { getViemProvider, setViemBlockNumber } from '../services/viem';
17
+
18
+ const compAddress = '0xc00e94cb662c3520282e6f5717214004a7f26888';
19
+
20
+ export const _getCompoundV2MarketsData = async (provider: Client, network: NetworkNumber): Promise<CompoundV2MarketsData> => {
21
+ const cAddresses = compoundV2CollateralAssets.map(a => a.address);
22
+
23
+ const loanInfoContract = CompoundLoanInfoContractViem(provider, network);
24
+ const loanInfo = await loanInfoContract.read.getFullTokensInfo([cAddresses as EthAddress[]]);
25
+
26
+ const compPrice = loanInfo.find(m => compareAddresses(m.underlyingTokenAddress, compAddress))!.price.toString();
27
+
28
+ const assetsData = loanInfo
29
+ .map((market, i) => {
30
+ let symbol = getAssetInfoByAddress(cAddresses[i]).underlyingAsset;
31
+ let isWbtcLegacy = false;
32
+ const totalSupply = new Dec(market.totalSupply.toString()).div(1e18).times(market.exchangeRate.toString());
33
+ const totalBorrow = market.totalBorrow.toString();
34
+ const borrowCap = market.borrowCap.toString();
35
+ const compSupplySpeeds = market.compSupplySpeeds.toString();
36
+ const compBorrowSpeeds = market.compBorrowSpeeds.toString();
37
+ const assetPrice = market.price.toString();
38
+
39
+ const pricePrecisionDiff = 18 - getAssetInfo(getAssetInfoByAddress(cAddresses[i]).underlyingAsset).decimals;
40
+
41
+ // compSupplySpeeds/compBorrowSpeeds is per block per market (borrow & supply are separate markets)
42
+ const incentiveSupplyApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compSupplySpeeds * +compPrice) / +assetPrice / +totalSupply).toString();
43
+ const incentiveBorrowApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compBorrowSpeeds * +compPrice) / +assetPrice / +totalBorrow).toString();
44
+
45
+ if (cAddresses[i].toLowerCase() === '0xc11b1268c1a384e55c48c2391d8d480264a3a7f4'.toLowerCase()) {
46
+ symbol = 'WBTC Legacy';
47
+ isWbtcLegacy = true;
48
+ }
49
+ return {
50
+ symbol,
51
+ underlyingTokenAddress: market.underlyingTokenAddress,
52
+ supplyRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.supplyRate.toString()).div(1e16).toString()).toString(),
53
+ borrowRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.borrowRate.toString()).div(1e16).toString()).toString(),
54
+ incentiveSupplyToken: 'COMP',
55
+ incentiveBorrowToken: 'COMP',
56
+ incentiveSupplyApy,
57
+ incentiveBorrowApy,
58
+ collateralFactor: new Dec(market.collateralFactor.toString()).div(1e18).toString(),
59
+ marketLiquidity: assetAmountInEth(market.marketLiquidity.toString(), handleWbtcLegacy(symbol)),
60
+ utilization: new Dec(market.totalBorrow.toString()).div(totalSupply).times(100).toString(),
61
+ totalSupply: assetAmountInEth(totalSupply, handleWbtcLegacy(symbol)),
62
+ totalBorrow: assetAmountInEth(totalBorrow, handleWbtcLegacy(symbol)),
63
+ exchangeRate: new Dec(market.exchangeRate.toString()).div(1e28).toString(),
64
+ borrowCap: assetAmountInEth(borrowCap, handleWbtcLegacy(symbol)),
65
+ canBeBorrowed: market.canBorrow,
66
+ canBeSupplied: market.canMint,
67
+ price: new Dec(market.price.toString()).div(10 ** (18 + pricePrecisionDiff)).toString(),
68
+ };
69
+ });
70
+
71
+ const payload = {} as CompoundV2AssetsData;
72
+ // Sort by market size
73
+ assetsData
74
+ .sort((a, b) => {
75
+ const aMarket = new Dec(a.price).times(a.totalSupply).toString();
76
+ const bMarket = new Dec(b.price).times(b.totalSupply).toString();
77
+
78
+ return new Dec(bMarket).minus(aMarket).toNumber();
79
+ })
80
+ .forEach((market, i) => { payload[market.symbol] = { ...market, sortIndex: i }; });
81
+
82
+ return { assetsData: payload };
83
+ };
84
+
85
+ export const getCompoundV2MarketsData = async (provider: EthereumProvider, network: NetworkNumber): Promise<CompoundV2MarketsData> => _getCompoundV2MarketsData(getViemProvider(provider, network), network);
86
+
87
+ export const EMPTY_COMPOUND_DATA = {
88
+ usedAssets: {},
89
+ suppliedUsd: '0',
90
+ borrowedUsd: '0',
91
+ borrowLimitUsd: '0',
92
+ leftToBorrowUsd: '0',
93
+ ratio: '0',
94
+ minRatio: '0',
95
+ netApy: '0',
96
+ incentiveUsd: '0',
97
+ totalInterestUsd: '0',
98
+ borrowStableSupplyUnstable: false,
99
+ };
100
+
101
+ const getCollateralAssetsAddresses = async (provider: Client, network: NetworkNumber, account: EthAddress) => {
102
+ const contract = ComptrollerContractViem(provider, network);
103
+
104
+ return contract.read.getAssetsIn([account]);
105
+ };
106
+
107
+ const getAllMarketAddresses = async (provider: Client, network: NetworkNumber, block: Blockish) => {
108
+ const contract = ComptrollerContractViem(provider, network);
109
+
110
+ return contract.read.getAllMarkets(setViemBlockNumber(block));
111
+ };
112
+
113
+
114
+ export const _getCompoundV2AccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
115
+ let balances: PositionBalances = {
116
+ collateral: {},
117
+ debt: {},
118
+ };
119
+
120
+ if (!address) {
121
+ return balances;
122
+ }
123
+
124
+ const assets = await getAllMarketAddresses(provider, network, block);
125
+ const assetInfo = assets.map(a => getAssetInfoByAddress(a, network));
126
+ const loanInfoContract = CompoundLoanInfoContractViem(provider, network, block);
127
+ const loanInfo = await loanInfoContract.read.getTokenBalances([address, assets], setViemBlockNumber(block));
128
+
129
+ loanInfo[0].forEach((weiAmount: any, i: number) => {
130
+ const asset = wethToEth(
131
+ assetInfo[i].symbol === 'cWBTC Legacy'
132
+ ? `${assetInfo[i].underlyingAsset} Legacy`
133
+ : assetInfo[i].underlyingAsset,
134
+ );
135
+
136
+ balances = {
137
+ collateral: {
138
+ ...balances.collateral,
139
+ [addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
140
+ },
141
+ };
142
+ });
143
+ loanInfo[1].forEach((weiAmount: any, i: number) => {
144
+ const asset = wethToEth(
145
+ assetInfo[i].symbol === 'cWBTC Legacy'
146
+ ? `${assetInfo[i].underlyingAsset} Legacy`
147
+ : assetInfo[i].underlyingAsset,
148
+ );
149
+
150
+ balances = {
151
+ ...balances,
152
+ debt: {
153
+ ...balances.debt,
154
+ [addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
155
+ },
156
+ };
157
+ });
158
+
159
+ return balances;
160
+ };
161
+
162
+ export const getCompoundV2AccountBalances = async (
163
+ provider: EthereumProvider,
164
+ network: NetworkNumber,
165
+ block: Blockish,
166
+ addressMapping: boolean,
167
+ address: EthAddress,
168
+ ): Promise<PositionBalances> => _getCompoundV2AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
169
+
170
+ export const _getCompoundV2AccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, assetsData: CompoundV2AssetsData): Promise<CompoundV2PositionData> => {
171
+ if (!address) throw new Error('No address provided');
172
+
173
+ let payload = { ...EMPTY_COMPOUND_DATA, lastUpdated: Date.now() };
174
+
175
+ const loanInfoContract = CompoundLoanInfoContractViem(provider, network);
176
+ const [loanInfo, collateralAssetsAddresses] = await Promise.all([
177
+ loanInfoContract.read.getTokenBalances([address, compoundV2CollateralAssets.map(a => a.address) as EthAddress[]]),
178
+ getCollateralAssetsAddresses(provider, network, address),
179
+ ]);
180
+
181
+ const usedAssets = {} as CompoundV2UsedAssets;
182
+
183
+ loanInfo[0].forEach((weiAmount: BigInt, i: number) => {
184
+ const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
185
+ ? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
186
+ : compoundV2CollateralAssets[i].underlyingAsset;
187
+ const amount = assetAmountInEth(weiAmount.toString(), asset);
188
+ const collateral = !!collateralAssetsAddresses.find(a => compareAddresses(a, compoundV2CollateralAssets[i].address));
189
+ if (weiAmount.toString() === '0' && !collateral) return;
190
+ if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
191
+ usedAssets[asset] = {
192
+ ...usedAssets[asset],
193
+ symbol: asset,
194
+ supplied: amount,
195
+ suppliedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
196
+ isSupplied: +amount > 0,
197
+ collateral,
198
+ };
199
+ });
200
+
201
+ loanInfo[1].forEach((weiAmount, i) => {
202
+ if (weiAmount.toString() === '0') return;
203
+ const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
204
+ ? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
205
+ : compoundV2CollateralAssets[i].underlyingAsset;
206
+ const amount = assetAmountInEth(weiAmount.toString(), asset);
207
+ if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
208
+ usedAssets[asset] = {
209
+ ...usedAssets[asset],
210
+ symbol: asset,
211
+ borrowed: amount,
212
+ borrowedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
213
+ isBorrowed: true,
214
+ collateral: !!usedAssets[asset].collateral,
215
+ };
216
+ });
217
+
218
+ payload = {
219
+ ...payload,
220
+ usedAssets,
221
+ ...getCompoundV2AggregatedData({
222
+ usedAssets, assetsData,
223
+ }),
224
+ };
225
+
226
+ return payload;
227
+ };
228
+
229
+ export const getCompoundV2AccountData = async (
230
+ provider: EthereumProvider,
231
+ network: NetworkNumber,
232
+ address: EthAddress,
233
+ assetsData: CompoundV2AssetsData,
234
+ ): Promise<CompoundV2PositionData> => _getCompoundV2AccountData(getViemProvider(provider, network), network, address, assetsData);
235
+
236
+ export const getCompoundV2FullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress): Promise<CompoundV2PositionData> => {
237
+ const marketData = await getCompoundV2MarketsData(provider, network);
238
+ const positionData = await getCompoundV2AccountData(provider, network, address, marketData.assetsData);
239
+ return positionData;
240
+ };