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