@defisaver/positions-sdk 2.0.15-dev → 2.0.15-dev-3
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.
- package/.mocharc.json +4 -4
- package/.nvmrc +1 -1
- package/README.md +64 -64
- package/cjs/aaveV2/index.js +9 -5
- package/cjs/aaveV3/index.js +50 -40
- package/cjs/aaveV3/merit.d.ts +17 -0
- package/cjs/aaveV3/merit.js +95 -0
- package/cjs/aaveV3/{rewards.d.ts → merkl.d.ts} +7 -2
- package/cjs/aaveV3/{rewards.js → merkl.js} +30 -14
- package/cjs/compoundV2/index.js +13 -7
- package/cjs/compoundV3/index.js +7 -2
- package/cjs/config/contracts.d.ts +6510 -1851
- package/cjs/config/contracts.js +33 -12
- package/cjs/contracts.d.ts +178 -0
- package/cjs/eulerV2/index.js +11 -2
- package/cjs/fluid/index.js +105 -34
- package/cjs/helpers/aaveHelpers/index.js +0 -1
- package/cjs/helpers/compoundHelpers/index.d.ts +3 -5
- package/cjs/helpers/compoundHelpers/index.js +15 -11
- package/cjs/helpers/eulerHelpers/index.d.ts +0 -5
- package/cjs/helpers/eulerHelpers/index.js +2 -31
- package/cjs/helpers/fluidHelpers/index.js +2 -0
- package/cjs/helpers/liquityV2Helpers/index.js +3 -2
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/liquityV2/index.js +10 -2
- package/cjs/llamaLend/index.js +10 -2
- package/cjs/morphoBlue/index.js +20 -6
- package/cjs/spark/index.js +20 -30
- package/cjs/staking/eligibility.d.ts +11 -0
- package/cjs/staking/eligibility.js +43 -0
- package/cjs/staking/index.d.ts +1 -0
- package/cjs/staking/index.js +1 -0
- package/cjs/staking/staking.d.ts +1 -7
- package/cjs/staking/staking.js +29 -55
- package/cjs/types/aave.d.ts +1 -7
- package/cjs/types/common.d.ts +16 -4
- package/cjs/types/common.js +10 -1
- package/cjs/types/euler.d.ts +3 -3
- package/cjs/types/fluid.d.ts +3 -5
- package/cjs/types/liquityV2.d.ts +3 -3
- package/cjs/types/llamaLend.d.ts +3 -1
- package/cjs/types/morphoBlue.d.ts +3 -5
- package/cjs/types/spark.d.ts +0 -3
- package/esm/aaveV2/index.js +9 -5
- package/esm/aaveV3/index.js +48 -38
- package/esm/aaveV3/merit.d.ts +17 -0
- package/esm/aaveV3/merit.js +90 -0
- package/esm/aaveV3/{rewards.d.ts → merkl.d.ts} +7 -2
- package/esm/aaveV3/{rewards.js → merkl.js} +28 -13
- package/esm/compoundV2/index.js +13 -7
- package/esm/compoundV3/index.js +7 -2
- package/esm/config/contracts.d.ts +6510 -1851
- package/esm/config/contracts.js +33 -12
- package/esm/contracts.d.ts +178 -0
- package/esm/eulerV2/index.js +11 -2
- package/esm/fluid/index.js +106 -35
- package/esm/helpers/aaveHelpers/index.js +0 -1
- package/esm/helpers/compoundHelpers/index.d.ts +3 -5
- package/esm/helpers/compoundHelpers/index.js +16 -12
- package/esm/helpers/eulerHelpers/index.d.ts +0 -5
- package/esm/helpers/eulerHelpers/index.js +2 -30
- package/esm/helpers/fluidHelpers/index.js +2 -0
- package/esm/helpers/liquityV2Helpers/index.js +3 -2
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/liquityV2/index.js +11 -3
- package/esm/llamaLend/index.js +11 -3
- package/esm/morphoBlue/index.js +21 -7
- package/esm/spark/index.js +21 -31
- package/esm/staking/eligibility.d.ts +11 -0
- package/esm/staking/eligibility.js +36 -0
- package/esm/staking/index.d.ts +1 -0
- package/esm/staking/index.js +1 -0
- package/esm/staking/staking.d.ts +1 -7
- package/esm/staking/staking.js +28 -53
- package/esm/types/aave.d.ts +1 -7
- package/esm/types/common.d.ts +16 -4
- package/esm/types/common.js +9 -0
- package/esm/types/euler.d.ts +3 -3
- package/esm/types/fluid.d.ts +3 -5
- package/esm/types/liquityV2.d.ts +3 -3
- package/esm/types/llamaLend.d.ts +3 -1
- package/esm/types/morphoBlue.d.ts +3 -5
- package/esm/types/spark.d.ts +0 -3
- package/package.json +47 -47
- package/src/aaveV2/index.ts +239 -236
- package/src/aaveV3/index.ts +511 -493
- package/src/aaveV3/merit.ts +98 -0
- package/src/aaveV3/{rewards.ts → merkl.ts} +141 -125
- package/src/compoundV2/index.ts +244 -240
- package/src/compoundV3/index.ts +274 -270
- package/src/config/contracts.ts +1129 -1108
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +107 -107
- package/src/curveUsd/index.ts +250 -250
- package/src/eulerV2/index.ts +324 -314
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1636 -1568
- package/src/helpers/aaveHelpers/index.ts +169 -170
- package/src/helpers/compoundHelpers/index.ts +267 -261
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +222 -259
- package/src/helpers/fluidHelpers/index.ts +326 -324
- package/src/helpers/index.ts +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +82 -80
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +390 -390
- package/src/helpers/sparkHelpers/index.ts +155 -155
- package/src/index.ts +45 -45
- package/src/liquity/index.ts +104 -104
- package/src/liquityV2/index.ts +418 -408
- package/src/llamaLend/index.ts +305 -296
- package/src/maker/index.ts +223 -223
- package/src/markets/aave/index.ts +116 -116
- package/src/markets/aave/marketAssets.ts +49 -49
- package/src/markets/compound/index.ts +227 -227
- package/src/markets/compound/marketsAssets.ts +90 -90
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2456 -2456
- package/src/markets/index.ts +25 -25
- package/src/markets/liquityV2/index.ts +102 -102
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +895 -895
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +11 -11
- package/src/moneymarket/moneymarketCommonService.ts +80 -80
- package/src/morphoBlue/index.ts +236 -222
- package/src/portfolio/index.ts +285 -285
- package/src/services/priceService.ts +159 -159
- package/src/services/utils.ts +63 -63
- package/src/services/viem.ts +32 -32
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +444 -456
- package/src/staking/eligibility.ts +37 -0
- package/src/staking/index.ts +2 -1
- package/src/staking/staking.ts +169 -194
- package/src/types/aave.ts +189 -195
- package/src/types/common.ts +103 -88
- package/src/types/compound.ts +136 -136
- package/src/types/curveUsd.ts +121 -121
- package/src/types/euler.ts +175 -174
- package/src/types/fluid.ts +448 -450
- package/src/types/index.ts +11 -11
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +126 -126
- package/src/types/llamaLend.ts +159 -157
- package/src/types/maker.ts +63 -63
- package/src/types/morphoBlue.ts +194 -194
- package/src/types/portfolio.ts +60 -60
- package/src/types/spark.ts +135 -137
package/src/spark/index.ts
CHANGED
|
@@ -1,456 +1,444 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import { assetAmountInEth, assetAmountInWei, getAssetInfo } from '@defisaver/tokens';
|
|
3
|
-
import { Client } from 'viem';
|
|
4
|
-
import {
|
|
5
|
-
Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
|
|
6
|
-
} from '../types/common';
|
|
7
|
-
import {
|
|
8
|
-
ethToWeth, wethToEth, wethToEthByAddress,
|
|
9
|
-
} from '../services/utils';
|
|
10
|
-
import {
|
|
11
|
-
calculateNetApy, getStakingApy, STAKING_ASSETS,
|
|
12
|
-
} from '../staking';
|
|
13
|
-
import {
|
|
14
|
-
SparkViewContractViem,
|
|
15
|
-
SparkIncentiveDataProviderContractViem,
|
|
16
|
-
createViemContractFromConfigFunc,
|
|
17
|
-
} from '../contracts';
|
|
18
|
-
import {
|
|
19
|
-
SparkEModeCategoryDataMapping,
|
|
20
|
-
SparkAssetData,
|
|
21
|
-
SparkAssetsData,
|
|
22
|
-
SparkMarketData,
|
|
23
|
-
SparkMarketsData,
|
|
24
|
-
SparkPositionData,
|
|
25
|
-
SparkUsedAsset,
|
|
26
|
-
SparkUsedAssets,
|
|
27
|
-
} from '../types';
|
|
28
|
-
import { sparkGetAggregatedPositionData, sparkIsInIsolationMode } from '../helpers/sparkHelpers';
|
|
29
|
-
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
30
|
-
import { SPARK_V1 } from '../markets/spark';
|
|
31
|
-
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
32
|
-
|
|
33
|
-
export const sparkEmodeCategoriesMapping = (extractedState: { assetsData: SparkAssetsData }, usedAssets: SparkUsedAssets) => {
|
|
34
|
-
const { assetsData } = extractedState;
|
|
35
|
-
const usedAssetsValues = Object.values(usedAssets);
|
|
36
|
-
|
|
37
|
-
const categoriesMapping: { [key: number]: SparkEModeCategoryDataMapping } = {};
|
|
38
|
-
Object.values(assetsData).forEach((a) => {
|
|
39
|
-
const borrowingOnlyFromCategory = a.eModeCategory === 0
|
|
40
|
-
? true
|
|
41
|
-
: !usedAssetsValues.filter(u => u.isBorrowed && u.eModeCategory !== a.eModeCategory).length;
|
|
42
|
-
const afterEnteringCategory = sparkGetAggregatedPositionData({
|
|
43
|
-
...extractedState, usedAssets, eModeCategory: a.eModeCategory,
|
|
44
|
-
});
|
|
45
|
-
const willStayOverCollateralized = new Dec(afterEnteringCategory.ratio).eq(0) || new Dec(afterEnteringCategory.ratio).gt(afterEnteringCategory.liqPercent);
|
|
46
|
-
const enteringTerms = [borrowingOnlyFromCategory, willStayOverCollateralized];
|
|
47
|
-
|
|
48
|
-
categoriesMapping[a.eModeCategory] = {
|
|
49
|
-
enteringTerms,
|
|
50
|
-
canEnterCategory: !enteringTerms.includes(false),
|
|
51
|
-
id: a.eModeCategory,
|
|
52
|
-
data: a.eModeCategoryData,
|
|
53
|
-
assets: a.eModeCategory === 0 ? [] : [...(categoriesMapping[a.eModeCategory]?.assets || []), a.symbol],
|
|
54
|
-
enabledData: {
|
|
55
|
-
ratio: afterEnteringCategory.ratio,
|
|
56
|
-
liqRatio: afterEnteringCategory.liqRatio,
|
|
57
|
-
liqPercent: afterEnteringCategory.liqPercent,
|
|
58
|
-
collRatio: afterEnteringCategory.collRatio,
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
});
|
|
62
|
-
return categoriesMapping;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export const _getSparkMarketsData = async (provider: Client, network: NetworkNumber, selectedMarket: SparkMarketData): Promise<SparkMarketsData> => {
|
|
66
|
-
const marketAddress = selectedMarket.providerAddress;
|
|
67
|
-
|
|
68
|
-
const loanInfoContract = SparkViewContractViem(provider, network);
|
|
69
|
-
const sparkIncentivesContract = SparkIncentiveDataProviderContractViem(provider, network);
|
|
70
|
-
|
|
71
|
-
const [loanInfo, _rewardInfo] = await Promise.all([
|
|
72
|
-
loanInfoContract.read.getFullTokensInfo([marketAddress, selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address) as EthAddress[]]),
|
|
73
|
-
network === NetworkNumber.Opt ? sparkIncentivesContract.read.getReservesIncentivesData([marketAddress]) : [],
|
|
74
|
-
]);
|
|
75
|
-
|
|
76
|
-
let rewardInfo: any[] = [];
|
|
77
|
-
if (network === NetworkNumber.Opt) {
|
|
78
|
-
rewardInfo = rewardInfo.reduce((all, market) => {
|
|
79
|
-
// eslint-disable-next-line no-param-reassign
|
|
80
|
-
all[market.underlyingAsset] = market;
|
|
81
|
-
return all;
|
|
82
|
-
}, {});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const assetsData: SparkAssetData[] = await Promise.all(loanInfo
|
|
86
|
-
.map(async (market, i) => {
|
|
87
|
-
const symbol = selectedMarket.assets[i];
|
|
88
|
-
|
|
89
|
-
const borrowCapInWei = new Dec(assetAmountInWei(market.borrowCap.toString(), symbol));
|
|
90
|
-
let marketLiquidity = borrowCapInWei.lt(new Dec(market.totalSupply.toString()))
|
|
91
|
-
? assetAmountInEth(borrowCapInWei
|
|
92
|
-
.sub(market.totalBorrow.toString())
|
|
93
|
-
.toString(), symbol)
|
|
94
|
-
: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
95
|
-
.sub(market.totalBorrow.toString())
|
|
96
|
-
.toString(), symbol);
|
|
97
|
-
|
|
98
|
-
if (new Dec(marketLiquidity).lt(0)) {
|
|
99
|
-
marketLiquidity = '0';
|
|
100
|
-
}
|
|
101
|
-
return ({
|
|
102
|
-
symbol: selectedMarket.assets[i],
|
|
103
|
-
isIsolated: new Dec(market.debtCeilingForIsolationMode.toString()).gt(0),
|
|
104
|
-
debtCeilingForIsolationMode: new Dec(market.debtCeilingForIsolationMode.toString()).div(100).toString(),
|
|
105
|
-
isSiloed: market.isSiloedForBorrowing,
|
|
106
|
-
eModeCategory: +market.emodeCategory.toString(),
|
|
107
|
-
isolationModeTotalDebt: new Dec(market.isolationModeTotalDebt.toString()).div(100).toString(),
|
|
108
|
-
assetId: Number(market.assetId),
|
|
109
|
-
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
110
|
-
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
111
|
-
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
112
|
-
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
113
|
-
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
114
|
-
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
115
|
-
marketLiquidity,
|
|
116
|
-
utilization: new Dec(market.totalBorrow.toString()).times(100).div(new Dec(market.totalSupply.toString())).toString(),
|
|
117
|
-
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
118
|
-
supplyCap: market.supplyCap.toString(),
|
|
119
|
-
borrowCap: market.borrowCap.toString(),
|
|
120
|
-
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
121
|
-
isInactive: !market.isActive,
|
|
122
|
-
isFrozen: market.isFrozen,
|
|
123
|
-
isPaused: market.isPaused,
|
|
124
|
-
canBeBorrowed: market.isActive && !market.isPaused && !market.isFrozen && market.borrowingEnabled,
|
|
125
|
-
canBeSupplied: market.isActive && !market.isPaused && !market.isFrozen,
|
|
126
|
-
canBeWithdrawn: market.isActive && !market.isPaused,
|
|
127
|
-
canBePayBacked: market.isActive && !market.isPaused,
|
|
128
|
-
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
129
|
-
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
130
|
-
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
131
|
-
price: new Dec(market.price.toString()).div(1e8).toString(), // is actually price in USD
|
|
132
|
-
isolationModeBorrowingEnabled: market.isolationModeBorrowingEnabled,
|
|
133
|
-
isFlashLoanEnabled: market.isFlashLoanEnabled,
|
|
134
|
-
aTokenAddress: market.aTokenAddress,
|
|
135
|
-
eModeCategoryData: {
|
|
136
|
-
label: market.label,
|
|
137
|
-
liquidationBonus: new Dec(market.liquidationBonus).div(10000).toString(),
|
|
138
|
-
liquidationRatio: new Dec(market.liquidationThreshold).div(10000).toString(),
|
|
139
|
-
collateralFactor: new Dec(market.ltv).div(10000).toString(),
|
|
140
|
-
priceSource: market.priceSource,
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
.toString();
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
.
|
|
189
|
-
|
|
190
|
-
.
|
|
191
|
-
.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
payload.
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
payload.
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
export const getSparkAccountData = async (
|
|
446
|
-
provider: EthereumProvider,
|
|
447
|
-
network: NetworkNumber,
|
|
448
|
-
address: EthAddress,
|
|
449
|
-
extractedState: { selectedMarket: SparkMarketData, assetsData: SparkAssetsData },
|
|
450
|
-
) => _getSparkAccountData(getViemProvider(provider, network), network, address, extractedState);
|
|
451
|
-
|
|
452
|
-
export const getSparkFullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, market: SparkMarketData): Promise<SparkPositionData> => {
|
|
453
|
-
const marketData = await getSparkMarketsData(provider, network, market);
|
|
454
|
-
const positionData = await getSparkAccountData(provider, network, address, { assetsData: marketData.assetsData, selectedMarket: market });
|
|
455
|
-
return positionData;
|
|
456
|
-
};
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { assetAmountInEth, assetAmountInWei, getAssetInfo } from '@defisaver/tokens';
|
|
3
|
+
import { Client } from 'viem';
|
|
4
|
+
import {
|
|
5
|
+
Blockish, EthAddress, EthereumProvider, IncentiveKind, NetworkNumber, PositionBalances,
|
|
6
|
+
} from '../types/common';
|
|
7
|
+
import {
|
|
8
|
+
ethToWeth, wethToEth, wethToEthByAddress,
|
|
9
|
+
} from '../services/utils';
|
|
10
|
+
import {
|
|
11
|
+
calculateNetApy, getStakingApy, STAKING_ASSETS,
|
|
12
|
+
} from '../staking';
|
|
13
|
+
import {
|
|
14
|
+
SparkViewContractViem,
|
|
15
|
+
SparkIncentiveDataProviderContractViem,
|
|
16
|
+
createViemContractFromConfigFunc,
|
|
17
|
+
} from '../contracts';
|
|
18
|
+
import {
|
|
19
|
+
SparkEModeCategoryDataMapping,
|
|
20
|
+
SparkAssetData,
|
|
21
|
+
SparkAssetsData,
|
|
22
|
+
SparkMarketData,
|
|
23
|
+
SparkMarketsData,
|
|
24
|
+
SparkPositionData,
|
|
25
|
+
SparkUsedAsset,
|
|
26
|
+
SparkUsedAssets,
|
|
27
|
+
} from '../types';
|
|
28
|
+
import { sparkGetAggregatedPositionData, sparkIsInIsolationMode } from '../helpers/sparkHelpers';
|
|
29
|
+
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
30
|
+
import { SPARK_V1 } from '../markets/spark';
|
|
31
|
+
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
32
|
+
|
|
33
|
+
export const sparkEmodeCategoriesMapping = (extractedState: { assetsData: SparkAssetsData }, usedAssets: SparkUsedAssets) => {
|
|
34
|
+
const { assetsData } = extractedState;
|
|
35
|
+
const usedAssetsValues = Object.values(usedAssets);
|
|
36
|
+
|
|
37
|
+
const categoriesMapping: { [key: number]: SparkEModeCategoryDataMapping } = {};
|
|
38
|
+
Object.values(assetsData).forEach((a) => {
|
|
39
|
+
const borrowingOnlyFromCategory = a.eModeCategory === 0
|
|
40
|
+
? true
|
|
41
|
+
: !usedAssetsValues.filter(u => u.isBorrowed && u.eModeCategory !== a.eModeCategory).length;
|
|
42
|
+
const afterEnteringCategory = sparkGetAggregatedPositionData({
|
|
43
|
+
...extractedState, usedAssets, eModeCategory: a.eModeCategory,
|
|
44
|
+
});
|
|
45
|
+
const willStayOverCollateralized = new Dec(afterEnteringCategory.ratio).eq(0) || new Dec(afterEnteringCategory.ratio).gt(afterEnteringCategory.liqPercent);
|
|
46
|
+
const enteringTerms = [borrowingOnlyFromCategory, willStayOverCollateralized];
|
|
47
|
+
|
|
48
|
+
categoriesMapping[a.eModeCategory] = {
|
|
49
|
+
enteringTerms,
|
|
50
|
+
canEnterCategory: !enteringTerms.includes(false),
|
|
51
|
+
id: a.eModeCategory,
|
|
52
|
+
data: a.eModeCategoryData,
|
|
53
|
+
assets: a.eModeCategory === 0 ? [] : [...(categoriesMapping[a.eModeCategory]?.assets || []), a.symbol],
|
|
54
|
+
enabledData: {
|
|
55
|
+
ratio: afterEnteringCategory.ratio,
|
|
56
|
+
liqRatio: afterEnteringCategory.liqRatio,
|
|
57
|
+
liqPercent: afterEnteringCategory.liqPercent,
|
|
58
|
+
collRatio: afterEnteringCategory.collRatio,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
return categoriesMapping;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const _getSparkMarketsData = async (provider: Client, network: NetworkNumber, selectedMarket: SparkMarketData): Promise<SparkMarketsData> => {
|
|
66
|
+
const marketAddress = selectedMarket.providerAddress;
|
|
67
|
+
|
|
68
|
+
const loanInfoContract = SparkViewContractViem(provider, network);
|
|
69
|
+
const sparkIncentivesContract = SparkIncentiveDataProviderContractViem(provider, network);
|
|
70
|
+
|
|
71
|
+
const [loanInfo, _rewardInfo] = await Promise.all([
|
|
72
|
+
loanInfoContract.read.getFullTokensInfo([marketAddress, selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address) as EthAddress[]]),
|
|
73
|
+
network === NetworkNumber.Opt ? sparkIncentivesContract.read.getReservesIncentivesData([marketAddress]) : [],
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
let rewardInfo: any[] = [];
|
|
77
|
+
if (network === NetworkNumber.Opt) {
|
|
78
|
+
rewardInfo = rewardInfo.reduce((all, market) => {
|
|
79
|
+
// eslint-disable-next-line no-param-reassign
|
|
80
|
+
all[market.underlyingAsset] = market;
|
|
81
|
+
return all;
|
|
82
|
+
}, {});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const assetsData: SparkAssetData[] = await Promise.all(loanInfo
|
|
86
|
+
.map(async (market, i) => {
|
|
87
|
+
const symbol = selectedMarket.assets[i];
|
|
88
|
+
|
|
89
|
+
const borrowCapInWei = new Dec(assetAmountInWei(market.borrowCap.toString(), symbol));
|
|
90
|
+
let marketLiquidity = borrowCapInWei.lt(new Dec(market.totalSupply.toString()))
|
|
91
|
+
? assetAmountInEth(borrowCapInWei
|
|
92
|
+
.sub(market.totalBorrow.toString())
|
|
93
|
+
.toString(), symbol)
|
|
94
|
+
: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
95
|
+
.sub(market.totalBorrow.toString())
|
|
96
|
+
.toString(), symbol);
|
|
97
|
+
|
|
98
|
+
if (new Dec(marketLiquidity).lt(0)) {
|
|
99
|
+
marketLiquidity = '0';
|
|
100
|
+
}
|
|
101
|
+
return ({
|
|
102
|
+
symbol: selectedMarket.assets[i],
|
|
103
|
+
isIsolated: new Dec(market.debtCeilingForIsolationMode.toString()).gt(0),
|
|
104
|
+
debtCeilingForIsolationMode: new Dec(market.debtCeilingForIsolationMode.toString()).div(100).toString(),
|
|
105
|
+
isSiloed: market.isSiloedForBorrowing,
|
|
106
|
+
eModeCategory: +market.emodeCategory.toString(),
|
|
107
|
+
isolationModeTotalDebt: new Dec(market.isolationModeTotalDebt.toString()).div(100).toString(),
|
|
108
|
+
assetId: Number(market.assetId),
|
|
109
|
+
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
110
|
+
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
111
|
+
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
112
|
+
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
113
|
+
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
114
|
+
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
115
|
+
marketLiquidity,
|
|
116
|
+
utilization: new Dec(market.totalBorrow.toString()).times(100).div(new Dec(market.totalSupply.toString())).toString(),
|
|
117
|
+
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
118
|
+
supplyCap: market.supplyCap.toString(),
|
|
119
|
+
borrowCap: market.borrowCap.toString(),
|
|
120
|
+
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
121
|
+
isInactive: !market.isActive,
|
|
122
|
+
isFrozen: market.isFrozen,
|
|
123
|
+
isPaused: market.isPaused,
|
|
124
|
+
canBeBorrowed: market.isActive && !market.isPaused && !market.isFrozen && market.borrowingEnabled,
|
|
125
|
+
canBeSupplied: market.isActive && !market.isPaused && !market.isFrozen,
|
|
126
|
+
canBeWithdrawn: market.isActive && !market.isPaused,
|
|
127
|
+
canBePayBacked: market.isActive && !market.isPaused,
|
|
128
|
+
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
129
|
+
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
130
|
+
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
131
|
+
price: new Dec(market.price.toString()).div(1e8).toString(), // is actually price in USD
|
|
132
|
+
isolationModeBorrowingEnabled: market.isolationModeBorrowingEnabled,
|
|
133
|
+
isFlashLoanEnabled: market.isFlashLoanEnabled,
|
|
134
|
+
aTokenAddress: market.aTokenAddress,
|
|
135
|
+
eModeCategoryData: {
|
|
136
|
+
label: market.label,
|
|
137
|
+
liquidationBonus: new Dec(market.liquidationBonus).div(10000).toString(),
|
|
138
|
+
liquidationRatio: new Dec(market.liquidationThreshold).div(10000).toString(),
|
|
139
|
+
collateralFactor: new Dec(market.ltv).div(10000).toString(),
|
|
140
|
+
priceSource: market.priceSource,
|
|
141
|
+
},
|
|
142
|
+
supplyIncentives: [],
|
|
143
|
+
borrowIncentives: [],
|
|
144
|
+
});
|
|
145
|
+
}));
|
|
146
|
+
|
|
147
|
+
await Promise.all(assetsData.map(async (market) => {
|
|
148
|
+
/* eslint-disable no-param-reassign */
|
|
149
|
+
const rewardForMarket = (rewardInfo as any)[market.underlyingTokenAddress];
|
|
150
|
+
if (STAKING_ASSETS.includes(market.symbol)) {
|
|
151
|
+
const yieldApy = await getStakingApy(market.symbol);
|
|
152
|
+
market.supplyIncentives.push({
|
|
153
|
+
apy: yieldApy,
|
|
154
|
+
token: market.symbol,
|
|
155
|
+
incentiveKind: IncentiveKind.Staking,
|
|
156
|
+
description: `Native ${market.symbol} yield.`,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
if (market.canBeBorrowed) {
|
|
160
|
+
if (!market.borrowIncentives) {
|
|
161
|
+
market.borrowIncentives = [];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
market.borrowIncentives.push({
|
|
165
|
+
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
166
|
+
token: market.symbol,
|
|
167
|
+
incentiveKind: IncentiveKind.Reward,
|
|
168
|
+
description: `Due to the native yield of ${market.symbol}, the value of the debt would increase over time.`,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!rewardForMarket) return;
|
|
174
|
+
(rewardForMarket.aIncentiveData.rewardsTokenInformation as any[]).forEach(supplyRewardData => {
|
|
175
|
+
if (supplyRewardData) {
|
|
176
|
+
if (supplyRewardData.emissionEndTimestamp * 1000 < Date.now()) return;
|
|
177
|
+
const supplyEmissionPerSecond = supplyRewardData.emissionPerSecond;
|
|
178
|
+
const supplyRewardPrice = new Dec(supplyRewardData.rewardPriceFeed).div(10 ** supplyRewardData.priceFeedDecimals)
|
|
179
|
+
.toString();
|
|
180
|
+
const rewardApy = new Dec(supplyEmissionPerSecond).div((10 ** supplyRewardData.rewardTokenDecimals) / 100)
|
|
181
|
+
.mul(365 * 24 * 3600)
|
|
182
|
+
.mul(supplyRewardPrice)
|
|
183
|
+
.div(market.price)
|
|
184
|
+
.div(market.totalSupply)
|
|
185
|
+
.toString();
|
|
186
|
+
|
|
187
|
+
market.supplyIncentives.push({
|
|
188
|
+
token: supplyRewardData.rewardTokenSymbol,
|
|
189
|
+
apy: rewardApy,
|
|
190
|
+
incentiveKind: IncentiveKind.Reward,
|
|
191
|
+
description: 'Eligible for protocol-level incentives.',
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
(rewardForMarket.vIncentiveData.rewardsTokenInformation as any[]).forEach(borrowRewardData => {
|
|
196
|
+
if (borrowRewardData) {
|
|
197
|
+
if (borrowRewardData.emissionEndTimestamp * 1000 < Date.now()) return;
|
|
198
|
+
const supplyEmissionPerSecond = borrowRewardData.emissionPerSecond;
|
|
199
|
+
const supplyRewardPrice = new Dec(borrowRewardData.rewardPriceFeed).div(10 ** borrowRewardData.priceFeedDecimals)
|
|
200
|
+
.toString();
|
|
201
|
+
const rewardApy = new Dec(supplyEmissionPerSecond).div((10 ** borrowRewardData.rewardTokenDecimals) / 100)
|
|
202
|
+
.mul(365 * 24 * 3600)
|
|
203
|
+
.mul(supplyRewardPrice)
|
|
204
|
+
.div(market.price)
|
|
205
|
+
.div(market.totalBorrowVar)
|
|
206
|
+
.toString();
|
|
207
|
+
market.borrowIncentives.push({
|
|
208
|
+
token: borrowRewardData.rewardTokenSymbol,
|
|
209
|
+
apy: rewardApy,
|
|
210
|
+
incentiveKind: IncentiveKind.Reward,
|
|
211
|
+
description: 'Eligible for protocol-level incentives.',
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
/* eslint-enable no-param-reassign */
|
|
216
|
+
}));
|
|
217
|
+
|
|
218
|
+
const payload: SparkAssetsData = {};
|
|
219
|
+
// Sort by market size
|
|
220
|
+
assetsData
|
|
221
|
+
.sort((a, b) => {
|
|
222
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
223
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
224
|
+
|
|
225
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
226
|
+
})
|
|
227
|
+
.forEach((assetData: SparkAssetData, i) => {
|
|
228
|
+
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return { assetsData: payload };
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export const getSparkMarketsData = async (
|
|
235
|
+
provider: EthereumProvider,
|
|
236
|
+
network: NetworkNumber,
|
|
237
|
+
selectedMarket: SparkMarketData,
|
|
238
|
+
): Promise<SparkMarketsData> => _getSparkMarketsData(getViemProvider(provider, network), network, selectedMarket);
|
|
239
|
+
|
|
240
|
+
export const EMPTY_SPARK_DATA = {
|
|
241
|
+
usedAssets: {},
|
|
242
|
+
suppliedUsd: '0',
|
|
243
|
+
borrowedUsd: '0',
|
|
244
|
+
borrowLimitUsd: '0',
|
|
245
|
+
leftToBorrowUsd: '0',
|
|
246
|
+
ratio: '0',
|
|
247
|
+
minRatio: '0',
|
|
248
|
+
netApy: '0',
|
|
249
|
+
incentiveUsd: '0',
|
|
250
|
+
totalInterestUsd: '0',
|
|
251
|
+
eModeCategory: 0,
|
|
252
|
+
isInIsolationMode: false,
|
|
253
|
+
isInSiloedMode: false,
|
|
254
|
+
collRatio: '0',
|
|
255
|
+
suppliedCollateralUsd: '0',
|
|
256
|
+
totalSupplied: '0',
|
|
257
|
+
eModeCategories: [],
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export const _getSparkAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
261
|
+
let balances: PositionBalances = {
|
|
262
|
+
collateral: {},
|
|
263
|
+
debt: {},
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
if (!address) {
|
|
267
|
+
return balances;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const loanInfoContract = SparkViewContractViem(provider, network, block);
|
|
271
|
+
|
|
272
|
+
const market = SPARK_V1(network);
|
|
273
|
+
const marketAddress = market.providerAddress;
|
|
274
|
+
// @ts-ignore
|
|
275
|
+
const protocolDataProviderContract = createViemContractFromConfigFunc(market.protocolData, market.protocolDataAddress)(provider, network);
|
|
276
|
+
|
|
277
|
+
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block));
|
|
278
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
279
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
// split addresses in half to avoid gas limit by multicall
|
|
283
|
+
const middleAddressIndex = Math.floor(_addresses.length / 2);
|
|
284
|
+
|
|
285
|
+
const [tokenBalances1, tokenBalances2] = await Promise.all([
|
|
286
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(0, middleAddressIndex)], setViemBlockNumber(block)),
|
|
287
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(middleAddressIndex, _addresses.length)], setViemBlockNumber(block)),
|
|
288
|
+
]);
|
|
289
|
+
|
|
290
|
+
const loanInfo = [...tokenBalances1, ...tokenBalances2];
|
|
291
|
+
|
|
292
|
+
loanInfo.forEach((tokenInfo: any, i: number) => {
|
|
293
|
+
const asset = wethToEth(symbols[i]);
|
|
294
|
+
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
295
|
+
|
|
296
|
+
balances = {
|
|
297
|
+
collateral: {
|
|
298
|
+
...balances.collateral,
|
|
299
|
+
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
300
|
+
},
|
|
301
|
+
debt: {
|
|
302
|
+
...balances.debt,
|
|
303
|
+
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
return balances;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
export const getSparkAccountBalances = async (
|
|
312
|
+
provider: EthereumProvider,
|
|
313
|
+
network: NetworkNumber,
|
|
314
|
+
block: Blockish,
|
|
315
|
+
addressMapping: boolean,
|
|
316
|
+
address: EthAddress,
|
|
317
|
+
): Promise<PositionBalances> => _getSparkAccountBalances(getViemProvider(provider, network, { batch: { multicall: true } }), network, block, addressMapping, address);
|
|
318
|
+
|
|
319
|
+
export const _getSparkAccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, extractedState: { selectedMarket: SparkMarketData, assetsData: SparkAssetsData }) => {
|
|
320
|
+
const {
|
|
321
|
+
selectedMarket: market, assetsData,
|
|
322
|
+
} = extractedState;
|
|
323
|
+
let payload: SparkPositionData = {
|
|
324
|
+
...EMPTY_SPARK_DATA,
|
|
325
|
+
lastUpdated: Date.now(),
|
|
326
|
+
};
|
|
327
|
+
if (!address) {
|
|
328
|
+
// structure that this function returns is complex and dynamic so i didnt want to hardcode it in EMPTY_SPARK_DATA
|
|
329
|
+
// This case only triggers if user doesnt have proxy and data is refetched once proxy is created
|
|
330
|
+
// TODO when refactoring spark figure out if this is the best solution.
|
|
331
|
+
payload.eModeCategories = sparkEmodeCategoriesMapping(extractedState, {});
|
|
332
|
+
|
|
333
|
+
return payload;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const loanInfoContract = SparkViewContractViem(provider, network);
|
|
337
|
+
const lendingPoolContract = createViemContractFromConfigFunc(market.lendingPool, market.lendingPoolAddress)(provider, network);
|
|
338
|
+
|
|
339
|
+
const marketAddress = market.providerAddress;
|
|
340
|
+
const _addresses = market.assets.map((a) => getAssetInfo(ethToWeth(a)).address);
|
|
341
|
+
|
|
342
|
+
const middleAddressIndex = Math.floor(_addresses.length / 2); // split addresses in half to avoid gas limit by multicall
|
|
343
|
+
|
|
344
|
+
const [userEMode, tokenBalances1, tokenBalances2] = await Promise.all([
|
|
345
|
+
lendingPoolContract.read.getUserEMode([address]),
|
|
346
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(0, middleAddressIndex) as EthAddress[]]),
|
|
347
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(middleAddressIndex, _addresses.length) as EthAddress[]]),
|
|
348
|
+
]);
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
const loanInfo = [...tokenBalances1, ...tokenBalances2];
|
|
352
|
+
|
|
353
|
+
const usedAssets = {} as SparkUsedAssets;
|
|
354
|
+
loanInfo.forEach((tokenInfo, i) => {
|
|
355
|
+
const asset = market.assets[i];
|
|
356
|
+
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
357
|
+
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
358
|
+
if (!isSupplied && !isBorrowed) return;
|
|
359
|
+
|
|
360
|
+
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
361
|
+
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
362
|
+
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
363
|
+
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
364
|
+
let interestMode;
|
|
365
|
+
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
366
|
+
interestMode = '1';
|
|
367
|
+
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
368
|
+
interestMode = '2';
|
|
369
|
+
} else {
|
|
370
|
+
interestMode = 'both';
|
|
371
|
+
}
|
|
372
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as SparkUsedAsset;
|
|
373
|
+
usedAssets[asset] = {
|
|
374
|
+
...usedAssets[asset],
|
|
375
|
+
symbol: asset,
|
|
376
|
+
supplied,
|
|
377
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
378
|
+
isSupplied,
|
|
379
|
+
collateral: enabledAsCollateral,
|
|
380
|
+
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate.toString()).div(1e25).toString()),
|
|
381
|
+
borrowedStable,
|
|
382
|
+
borrowedVariable,
|
|
383
|
+
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
384
|
+
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
385
|
+
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
386
|
+
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
387
|
+
isBorrowed,
|
|
388
|
+
eModeCategory: assetsData[asset].eModeCategory,
|
|
389
|
+
interestMode,
|
|
390
|
+
};
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
payload.eModeCategory = +userEMode.toString();
|
|
394
|
+
payload = {
|
|
395
|
+
...payload,
|
|
396
|
+
usedAssets,
|
|
397
|
+
...sparkGetAggregatedPositionData({
|
|
398
|
+
...extractedState, usedAssets, eModeCategory: payload.eModeCategory,
|
|
399
|
+
}),
|
|
400
|
+
};
|
|
401
|
+
payload.eModeCategories = sparkEmodeCategoriesMapping(extractedState, usedAssets);
|
|
402
|
+
payload.isInIsolationMode = sparkIsInIsolationMode({ usedAssets, assetsData });
|
|
403
|
+
payload.isInSiloedMode = sparkIsInIsolationMode({ usedAssets, assetsData });
|
|
404
|
+
|
|
405
|
+
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
406
|
+
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
407
|
+
: '0';
|
|
408
|
+
payload.minRatio = '100';
|
|
409
|
+
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
410
|
+
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
411
|
+
: '0';
|
|
412
|
+
|
|
413
|
+
// Calculate borrow limits per asset
|
|
414
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
415
|
+
if (item.isBorrowed) {
|
|
416
|
+
// eslint-disable-next-line no-param-reassign
|
|
417
|
+
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
418
|
+
// eslint-disable-next-line no-param-reassign
|
|
419
|
+
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
420
|
+
// eslint-disable-next-line no-param-reassign
|
|
421
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
426
|
+
payload.netApy = netApy;
|
|
427
|
+
payload.incentiveUsd = incentiveUsd;
|
|
428
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
429
|
+
|
|
430
|
+
return payload;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
export const getSparkAccountData = async (
|
|
434
|
+
provider: EthereumProvider,
|
|
435
|
+
network: NetworkNumber,
|
|
436
|
+
address: EthAddress,
|
|
437
|
+
extractedState: { selectedMarket: SparkMarketData, assetsData: SparkAssetsData },
|
|
438
|
+
) => _getSparkAccountData(getViemProvider(provider, network), network, address, extractedState);
|
|
439
|
+
|
|
440
|
+
export const getSparkFullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, market: SparkMarketData): Promise<SparkPositionData> => {
|
|
441
|
+
const marketData = await getSparkMarketsData(provider, network, market);
|
|
442
|
+
const positionData = await getSparkAccountData(provider, network, address, { assetsData: marketData.assetsData, selectedMarket: market });
|
|
443
|
+
return positionData;
|
|
444
|
+
};
|