@defisaver/positions-sdk 2.0.15-dev → 2.0.15-dev-2
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/aaveV3/index.ts
CHANGED
|
@@ -1,493 +1,511 @@
|
|
|
1
|
-
import { assetAmountInEth, assetAmountInWei, getAssetInfo } from '@defisaver/tokens';
|
|
2
|
-
import { Client } from 'viem';
|
|
3
|
-
import Dec from 'decimal.js';
|
|
4
|
-
import {
|
|
5
|
-
AaveIncentiveDataProviderV3ContractViem,
|
|
6
|
-
AaveV3ViewContractViem,
|
|
7
|
-
createViemContractFromConfigFunc,
|
|
8
|
-
} from '../contracts';
|
|
9
|
-
import { aaveAnyGetAggregatedPositionData, aaveV3IsInIsolationMode, aaveV3IsInSiloedMode } from '../helpers/aaveHelpers';
|
|
10
|
-
import { AAVE_V3 } from '../markets/aave';
|
|
11
|
-
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
12
|
-
import {
|
|
13
|
-
ethToWeth, isEnabledOnBitmap, isLayer2Network, wethToEth, wethToEthByAddress,
|
|
14
|
-
} from '../services/utils';
|
|
15
|
-
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
16
|
-
import {
|
|
17
|
-
AaveMarketInfo,
|
|
18
|
-
AaveV3AssetData,
|
|
19
|
-
AaveV3AssetsData,
|
|
20
|
-
AaveV3MarketData,
|
|
21
|
-
AaveV3PositionData,
|
|
22
|
-
AaveV3UsedAsset,
|
|
23
|
-
AaveV3UsedAssets,
|
|
24
|
-
EModeCategoriesData,
|
|
25
|
-
EModeCategoryData,
|
|
26
|
-
EModeCategoryDataMapping,
|
|
27
|
-
} from '../types/aave';
|
|
28
|
-
import {
|
|
29
|
-
Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
|
|
30
|
-
} from '../types/common';
|
|
31
|
-
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
32
|
-
import {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
loanInfoContract.read.
|
|
77
|
-
loanInfoContract.read.
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
_market.borrowIncentives.push({
|
|
192
|
-
apy:
|
|
193
|
-
token: _market.
|
|
194
|
-
incentiveKind:
|
|
195
|
-
description: `Due to the native yield of ${_market.symbol}, the value of the debt would increase over time.`,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const aTokenAddress = (_market as any).aTokenAddress.toLowerCase(); // DEV: Should aTokenAddress be in AaveV3AssetData type?
|
|
201
|
-
if (merkleRewardsMap[aTokenAddress]?.supply) {
|
|
202
|
-
const {
|
|
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
|
-
// @ts-ignore
|
|
250
|
-
rewardForMarket.
|
|
251
|
-
if (
|
|
252
|
-
if (+(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
.mul(
|
|
260
|
-
.
|
|
261
|
-
.div(_market.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
const
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
payload.
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
1
|
+
import { assetAmountInEth, assetAmountInWei, getAssetInfo } from '@defisaver/tokens';
|
|
2
|
+
import { Client } from 'viem';
|
|
3
|
+
import Dec from 'decimal.js';
|
|
4
|
+
import {
|
|
5
|
+
AaveIncentiveDataProviderV3ContractViem,
|
|
6
|
+
AaveV3ViewContractViem,
|
|
7
|
+
createViemContractFromConfigFunc,
|
|
8
|
+
} from '../contracts';
|
|
9
|
+
import { aaveAnyGetAggregatedPositionData, aaveV3IsInIsolationMode, aaveV3IsInSiloedMode } from '../helpers/aaveHelpers';
|
|
10
|
+
import { AAVE_V3 } from '../markets/aave';
|
|
11
|
+
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
12
|
+
import {
|
|
13
|
+
ethToWeth, isEnabledOnBitmap, isLayer2Network, wethToEth, wethToEthByAddress,
|
|
14
|
+
} from '../services/utils';
|
|
15
|
+
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
16
|
+
import {
|
|
17
|
+
AaveMarketInfo,
|
|
18
|
+
AaveV3AssetData,
|
|
19
|
+
AaveV3AssetsData,
|
|
20
|
+
AaveV3MarketData,
|
|
21
|
+
AaveV3PositionData,
|
|
22
|
+
AaveV3UsedAsset,
|
|
23
|
+
AaveV3UsedAssets,
|
|
24
|
+
EModeCategoriesData,
|
|
25
|
+
EModeCategoryData,
|
|
26
|
+
EModeCategoryDataMapping,
|
|
27
|
+
} from '../types/aave';
|
|
28
|
+
import {
|
|
29
|
+
Blockish, EthAddress, EthereumProvider, IncentiveEligibilityId, IncentiveKind, NetworkNumber, PositionBalances,
|
|
30
|
+
} from '../types/common';
|
|
31
|
+
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
32
|
+
import { getMeritCampaigns } from './merit';
|
|
33
|
+
import { getAaveUnderlyingSymbol, getMerkleCampaigns } from './merkl';
|
|
34
|
+
|
|
35
|
+
export const aaveV3EmodeCategoriesMapping = (extractedState: any, usedAssets: AaveV3UsedAssets) => {
|
|
36
|
+
const { eModeCategoriesData }: { assetsData: AaveV3AssetsData, eModeCategoriesData: EModeCategoriesData } = extractedState;
|
|
37
|
+
const usedAssetsValues = Object.values(usedAssets);
|
|
38
|
+
|
|
39
|
+
const categoriesMapping: { [key: number]: EModeCategoryDataMapping } = {};
|
|
40
|
+
|
|
41
|
+
Object.values(eModeCategoriesData).forEach((e: EModeCategoryData) => {
|
|
42
|
+
const borrowingOnlyFromCategory = e.id === 0
|
|
43
|
+
? true
|
|
44
|
+
: !usedAssetsValues.filter(u => u.isBorrowed && !e.borrowAssets.includes(u.symbol)).length;
|
|
45
|
+
const afterEnteringCategory = aaveAnyGetAggregatedPositionData({
|
|
46
|
+
...extractedState, usedAssets, eModeCategory: e.id,
|
|
47
|
+
});
|
|
48
|
+
const willStayOverCollateralized = new Dec(afterEnteringCategory.ratio).eq(0) || new Dec(afterEnteringCategory.ratio).gt(afterEnteringCategory.liqPercent);
|
|
49
|
+
const enteringTerms = [borrowingOnlyFromCategory, willStayOverCollateralized];
|
|
50
|
+
categoriesMapping[e.id] = {
|
|
51
|
+
enteringTerms,
|
|
52
|
+
canEnterCategory: !enteringTerms.includes(false),
|
|
53
|
+
id: e.id,
|
|
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 async function _getAaveV3MarketData(provider: Client, network: NetworkNumber, market: AaveMarketInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3MarketData> {
|
|
66
|
+
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a), network).address);
|
|
67
|
+
|
|
68
|
+
const isL2 = isLayer2Network(network);
|
|
69
|
+
const loanInfoContract = AaveV3ViewContractViem(provider, network);
|
|
70
|
+
const aaveIncentivesContract = AaveIncentiveDataProviderV3ContractViem(provider, network);
|
|
71
|
+
const marketAddress = market.providerAddress;
|
|
72
|
+
const networksWithIncentives = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Opt, NetworkNumber.Linea];
|
|
73
|
+
|
|
74
|
+
// eslint-disable-next-line prefer-const
|
|
75
|
+
let [loanInfo, eModesInfo, isBorrowAllowed, rewardInfo, merkleRewardsMap, meritRewardsMap] = await Promise.all([
|
|
76
|
+
loanInfoContract.read.getFullTokensInfo([marketAddress, _addresses as EthAddress[]], setViemBlockNumber(blockNumber)),
|
|
77
|
+
loanInfoContract.read.getAllEmodes([marketAddress], setViemBlockNumber(blockNumber)),
|
|
78
|
+
loanInfoContract.read.isBorrowAllowed([marketAddress], setViemBlockNumber(blockNumber)), // Used on L2s check for PriceOracleSentinel (mainnet will always return true)
|
|
79
|
+
networksWithIncentives.includes(network) ? aaveIncentivesContract.read.getReservesIncentivesData([marketAddress], setViemBlockNumber(blockNumber)) : null,
|
|
80
|
+
getMerkleCampaigns(network),
|
|
81
|
+
getMeritCampaigns(network, market.value),
|
|
82
|
+
]);
|
|
83
|
+
isBorrowAllowed = isLayer2Network(network) ? isBorrowAllowed : true;
|
|
84
|
+
|
|
85
|
+
const eModeCategoriesData: EModeCategoriesData = {};
|
|
86
|
+
for (let i = 0; i < eModesInfo.length; i++) {
|
|
87
|
+
if (!eModesInfo[i].label) break;
|
|
88
|
+
eModeCategoriesData[i + 1] = {
|
|
89
|
+
label: eModesInfo[i].label,
|
|
90
|
+
id: i + 1,
|
|
91
|
+
liquidationBonus: new Dec(eModesInfo[i].liquidationBonus).div(10000).toString(),
|
|
92
|
+
liquidationRatio: new Dec(eModesInfo[i].liquidationThreshold).div(10000).toString(),
|
|
93
|
+
collateralFactor: new Dec(eModesInfo[i].ltv).div(10000).toString(),
|
|
94
|
+
borrowableBitmap: eModesInfo[i].borrowableBitmap.toString(),
|
|
95
|
+
collateralBitmap: eModesInfo[i].collateralBitmap.toString(),
|
|
96
|
+
borrowAssets: [],
|
|
97
|
+
collateralAssets: [],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (networksWithIncentives.includes(network) && rewardInfo) {
|
|
102
|
+
rewardInfo = rewardInfo.reduce((all: any, _market: any) => {
|
|
103
|
+
// eslint-disable-next-line no-param-reassign
|
|
104
|
+
all[_market.underlyingAsset] = _market;
|
|
105
|
+
return all;
|
|
106
|
+
}, {});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const assetsData: AaveV3AssetData[] = await Promise.all(loanInfo
|
|
110
|
+
.map(async (tokenMarket, i) => {
|
|
111
|
+
const symbol = market.assets[i];
|
|
112
|
+
|
|
113
|
+
// eslint-disable-next-line guard-for-in
|
|
114
|
+
for (const eModeIndex in eModeCategoriesData) {
|
|
115
|
+
if (isEnabledOnBitmap(Number(eModeCategoriesData[eModeIndex].collateralBitmap), Number(tokenMarket.assetId))) eModeCategoriesData[eModeIndex].collateralAssets.push(symbol);
|
|
116
|
+
if (isEnabledOnBitmap(Number(eModeCategoriesData[eModeIndex].borrowableBitmap), Number(tokenMarket.assetId))) eModeCategoriesData[eModeIndex].borrowAssets.push(symbol);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const borrowCap = tokenMarket.borrowCap.toString();
|
|
120
|
+
|
|
121
|
+
const borrowCapInWei = new Dec(assetAmountInWei(borrowCap, symbol));
|
|
122
|
+
let marketLiquidity = borrowCapInWei.lt(new Dec(tokenMarket.totalSupply.toString()))
|
|
123
|
+
? assetAmountInEth(borrowCapInWei
|
|
124
|
+
.sub(tokenMarket.totalBorrow.toString())
|
|
125
|
+
.toString(), symbol)
|
|
126
|
+
: assetAmountInEth(new Dec(tokenMarket.totalSupply.toString())
|
|
127
|
+
.sub(tokenMarket.totalBorrow.toString())
|
|
128
|
+
.toString(), symbol);
|
|
129
|
+
|
|
130
|
+
if (new Dec(marketLiquidity).lt(0)) {
|
|
131
|
+
marketLiquidity = '0';
|
|
132
|
+
}
|
|
133
|
+
return ({
|
|
134
|
+
symbol,
|
|
135
|
+
isIsolated: new Dec(tokenMarket.debtCeilingForIsolationMode.toString()).gt(0),
|
|
136
|
+
debtCeilingForIsolationMode: new Dec(tokenMarket.debtCeilingForIsolationMode.toString()).div(100).toString(),
|
|
137
|
+
isSiloed: tokenMarket.isSiloedForBorrowing,
|
|
138
|
+
isolationModeTotalDebt: new Dec(tokenMarket.isolationModeTotalDebt.toString()).div(100).toString(),
|
|
139
|
+
assetId: Number(tokenMarket.assetId),
|
|
140
|
+
underlyingTokenAddress: tokenMarket.underlyingTokenAddress,
|
|
141
|
+
supplyRate: aprToApy(new Dec(tokenMarket.supplyRate.toString()).div(1e25).toString()),
|
|
142
|
+
borrowRate: aprToApy(new Dec(tokenMarket.borrowRateVariable.toString()).div(1e25).toString()),
|
|
143
|
+
borrowRateStable: aprToApy(new Dec(tokenMarket.borrowRateStable.toString()).div(1e25).toString()),
|
|
144
|
+
collateralFactor: new Dec(tokenMarket.collateralFactor.toString()).div(10000).toString(),
|
|
145
|
+
liquidationBonus: new Dec(tokenMarket.liquidationBonus.toString()).div(10000).toString(),
|
|
146
|
+
liquidationRatio: new Dec(tokenMarket.liquidationRatio.toString()).div(10000).toString(),
|
|
147
|
+
marketLiquidity,
|
|
148
|
+
utilization: new Dec(tokenMarket.totalBorrow.toString()).times(100).div(new Dec(tokenMarket.totalSupply.toString())).toString(),
|
|
149
|
+
usageAsCollateralEnabled: tokenMarket.usageAsCollateralEnabled,
|
|
150
|
+
supplyCap: tokenMarket.supplyCap.toString(),
|
|
151
|
+
borrowCap,
|
|
152
|
+
totalSupply: assetAmountInEth(tokenMarket.totalSupply.toString(), symbol),
|
|
153
|
+
isInactive: !tokenMarket.isActive,
|
|
154
|
+
isFrozen: tokenMarket.isFrozen,
|
|
155
|
+
isPaused: tokenMarket.isPaused,
|
|
156
|
+
canBeBorrowed: tokenMarket.isActive && !tokenMarket.isPaused && !tokenMarket.isFrozen && tokenMarket.borrowingEnabled && isBorrowAllowed,
|
|
157
|
+
canBeSupplied: tokenMarket.isActive && !tokenMarket.isPaused && !tokenMarket.isFrozen,
|
|
158
|
+
canBeWithdrawn: tokenMarket.isActive && !tokenMarket.isPaused,
|
|
159
|
+
canBePayBacked: tokenMarket.isActive && !tokenMarket.isPaused,
|
|
160
|
+
disabledStableBorrowing: !tokenMarket.stableBorrowRateEnabled,
|
|
161
|
+
totalBorrow: assetAmountInEth(tokenMarket.totalBorrow.toString(), symbol),
|
|
162
|
+
totalBorrowVar: assetAmountInEth(tokenMarket.totalBorrowVar.toString(), symbol),
|
|
163
|
+
price: new Dec(tokenMarket.price.toString()).div(1e8).toString(), // is actually price in USD
|
|
164
|
+
isolationModeBorrowingEnabled: tokenMarket.isolationModeBorrowingEnabled,
|
|
165
|
+
isFlashLoanEnabled: tokenMarket.isFlashLoanEnabled,
|
|
166
|
+
aTokenAddress: tokenMarket.aTokenAddress,
|
|
167
|
+
vTokenAddress: tokenMarket.debtTokenAddress,
|
|
168
|
+
supplyIncentives: [],
|
|
169
|
+
borrowIncentives: [],
|
|
170
|
+
});
|
|
171
|
+
}),
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
// Get incentives data
|
|
175
|
+
await Promise.all(assetsData.map(async (_market: AaveV3AssetData, index) => {
|
|
176
|
+
/* eslint-disable no-param-reassign */
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
const rewardForMarket = rewardInfo?.[_market.underlyingTokenAddress];
|
|
179
|
+
const isStakingAsset = STAKING_ASSETS.includes(_market.symbol);
|
|
180
|
+
|
|
181
|
+
if (isStakingAsset) {
|
|
182
|
+
const yieldApy = await getStakingApy(_market.symbol);
|
|
183
|
+
_market.supplyIncentives.push({
|
|
184
|
+
apy: yieldApy,
|
|
185
|
+
token: _market.symbol,
|
|
186
|
+
incentiveKind: IncentiveKind.Staking,
|
|
187
|
+
description: `Native ${_market.symbol} yield.`,
|
|
188
|
+
});
|
|
189
|
+
if (_market.canBeBorrowed) {
|
|
190
|
+
// when borrowing assets whose value increases over time
|
|
191
|
+
_market.borrowIncentives.push({
|
|
192
|
+
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
193
|
+
token: _market.symbol,
|
|
194
|
+
incentiveKind: IncentiveKind.Reward,
|
|
195
|
+
description: `Due to the native yield of ${_market.symbol}, the value of the debt would increase over time.`,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const aTokenAddress = (_market as any).aTokenAddress.toLowerCase(); // DEV: Should aTokenAddress be in AaveV3AssetData type?
|
|
201
|
+
if (merkleRewardsMap[aTokenAddress]?.supply) {
|
|
202
|
+
const {
|
|
203
|
+
apy, rewardTokenSymbol, description, identifier,
|
|
204
|
+
} = merkleRewardsMap[aTokenAddress].supply;
|
|
205
|
+
_market.supplyIncentives.push({
|
|
206
|
+
apy,
|
|
207
|
+
token: rewardTokenSymbol,
|
|
208
|
+
incentiveKind: IncentiveKind.Reward,
|
|
209
|
+
description,
|
|
210
|
+
eligibilityId: identifier as IncentiveEligibilityId,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const vTokenAddress = (_market as any).vTokenAddress.toLowerCase(); // DEV: Should vTokenAddress be in AaveV3AssetData type?
|
|
215
|
+
if (merkleRewardsMap[vTokenAddress]?.borrow) {
|
|
216
|
+
const {
|
|
217
|
+
apy, rewardTokenSymbol, description, identifier,
|
|
218
|
+
} = merkleRewardsMap[vTokenAddress].borrow;
|
|
219
|
+
_market.borrowIncentives.push({
|
|
220
|
+
apy,
|
|
221
|
+
token: rewardTokenSymbol,
|
|
222
|
+
incentiveKind: IncentiveKind.Reward,
|
|
223
|
+
description,
|
|
224
|
+
eligibilityId: identifier as IncentiveEligibilityId,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (meritRewardsMap.supply[_market.symbol]) {
|
|
229
|
+
const { apy, rewardTokenSymbol, description } = meritRewardsMap.supply[_market.symbol];
|
|
230
|
+
_market.supplyIncentives.push({
|
|
231
|
+
apy,
|
|
232
|
+
token: rewardTokenSymbol,
|
|
233
|
+
incentiveKind: IncentiveKind.Reward,
|
|
234
|
+
description,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (meritRewardsMap.borrow[_market.symbol]) {
|
|
239
|
+
const { apy, rewardTokenSymbol, description } = meritRewardsMap.borrow[_market.symbol];
|
|
240
|
+
_market.borrowIncentives.push({
|
|
241
|
+
apy,
|
|
242
|
+
token: rewardTokenSymbol,
|
|
243
|
+
incentiveKind: IncentiveKind.Reward,
|
|
244
|
+
description,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (!rewardForMarket) return;
|
|
249
|
+
// @ts-ignore
|
|
250
|
+
rewardForMarket.aIncentiveData.rewardsTokenInformation.forEach(supplyRewardData => {
|
|
251
|
+
if (supplyRewardData) {
|
|
252
|
+
if (+(supplyRewardData.emissionEndTimestamp.toString()) * 1000 < Date.now()) return;
|
|
253
|
+
// reward token is aave asset
|
|
254
|
+
const supplyEmissionPerSecond = supplyRewardData.emissionPerSecond;
|
|
255
|
+
const supplyRewardPrice = new Dec(supplyRewardData.rewardPriceFeed).div(10 ** +supplyRewardData.priceFeedDecimals)
|
|
256
|
+
.toString();
|
|
257
|
+
const rewardApy = new Dec(supplyEmissionPerSecond).div((10 ** +supplyRewardData.rewardTokenDecimals) / 100)
|
|
258
|
+
.mul(365 * 24 * 3600)
|
|
259
|
+
.mul(supplyRewardPrice)
|
|
260
|
+
.div(_market.price)
|
|
261
|
+
.div(_market.totalSupply)
|
|
262
|
+
.toString();
|
|
263
|
+
_market.supplyIncentives.push({
|
|
264
|
+
token: getAaveUnderlyingSymbol(supplyRewardData.rewardTokenSymbol),
|
|
265
|
+
apy: rewardApy,
|
|
266
|
+
incentiveKind: IncentiveKind.Reward,
|
|
267
|
+
description: 'Eligible for protocol-level incentives.',
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
// @ts-ignore
|
|
272
|
+
rewardForMarket.vIncentiveData.rewardsTokenInformation.forEach(borrowRewardData => {
|
|
273
|
+
if (borrowRewardData) {
|
|
274
|
+
if (+(borrowRewardData.emissionEndTimestamp.toString()) * 1000 < Date.now()) return;
|
|
275
|
+
const supplyEmissionPerSecond = borrowRewardData.emissionPerSecond;
|
|
276
|
+
const supplyRewardPrice = new Dec(borrowRewardData.rewardPriceFeed).div(10 ** +borrowRewardData.priceFeedDecimals)
|
|
277
|
+
.toString();
|
|
278
|
+
const rewardApy = new Dec(supplyEmissionPerSecond).div((10 ** +borrowRewardData.rewardTokenDecimals) / 100)
|
|
279
|
+
.mul(365 * 24 * 3600)
|
|
280
|
+
.mul(supplyRewardPrice)
|
|
281
|
+
.div(_market.price)
|
|
282
|
+
.div(_market.totalBorrowVar)
|
|
283
|
+
.toString();
|
|
284
|
+
_market.borrowIncentives.push({
|
|
285
|
+
token: getAaveUnderlyingSymbol(borrowRewardData.rewardTokenSymbol),
|
|
286
|
+
apy: rewardApy,
|
|
287
|
+
incentiveKind: IncentiveKind.Reward,
|
|
288
|
+
description: 'Eligible for protocol-level incentives.',
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}));
|
|
293
|
+
|
|
294
|
+
const payload: AaveV3AssetsData = {};
|
|
295
|
+
// Sort by market size
|
|
296
|
+
assetsData
|
|
297
|
+
.sort((a, b) => {
|
|
298
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
299
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
300
|
+
|
|
301
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
302
|
+
})
|
|
303
|
+
.forEach((assetData: AaveV3AssetData, i) => {
|
|
304
|
+
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
eModeCategoriesData[0] = {
|
|
308
|
+
id: 0,
|
|
309
|
+
label: '',
|
|
310
|
+
liquidationBonus: '0',
|
|
311
|
+
liquidationRatio: '0',
|
|
312
|
+
collateralFactor: '0',
|
|
313
|
+
collateralAssets: assetsData.map((a) => a.symbol),
|
|
314
|
+
borrowAssets: assetsData.map((a) => a.symbol),
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
return { assetsData: payload, eModeCategoriesData };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export async function getAaveV3MarketData(provider: EthereumProvider, network: NetworkNumber, market: AaveMarketInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3MarketData> {
|
|
321
|
+
return _getAaveV3MarketData(getViemProvider(provider, network), network, market, blockNumber);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export const EMPTY_AAVE_DATA = {
|
|
325
|
+
usedAssets: {},
|
|
326
|
+
suppliedUsd: '0',
|
|
327
|
+
borrowedUsd: '0',
|
|
328
|
+
borrowLimitUsd: '0',
|
|
329
|
+
leftToBorrowUsd: '0',
|
|
330
|
+
ratio: '0',
|
|
331
|
+
minRatio: '0',
|
|
332
|
+
netApy: '0',
|
|
333
|
+
incentiveUsd: '0',
|
|
334
|
+
totalInterestUsd: '0',
|
|
335
|
+
isSubscribedToAutomation: false,
|
|
336
|
+
automationResubscribeRequired: false,
|
|
337
|
+
eModeCategory: 0,
|
|
338
|
+
isInIsolationMode: false,
|
|
339
|
+
isInSiloedMode: false,
|
|
340
|
+
totalSupplied: '0',
|
|
341
|
+
eModeCategories: [],
|
|
342
|
+
collRatio: '0',
|
|
343
|
+
suppliedCollateralUsd: '0',
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
export const _getAaveV3AccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
347
|
+
let balances: PositionBalances = {
|
|
348
|
+
collateral: {},
|
|
349
|
+
debt: {},
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
if (!address) {
|
|
353
|
+
return balances;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const loanInfoContract = AaveV3ViewContractViem(provider, network, block);
|
|
357
|
+
|
|
358
|
+
const market = AAVE_V3(network);
|
|
359
|
+
const marketAddress = market.providerAddress;
|
|
360
|
+
// @ts-ignore
|
|
361
|
+
const protocolDataProviderContract = createViemContractFromConfigFunc(market.protocolData, market.protocolDataAddress)(provider, network);
|
|
362
|
+
|
|
363
|
+
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block));
|
|
364
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
365
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
366
|
+
|
|
367
|
+
// split addresses in half to avoid gas limit by multicall
|
|
368
|
+
const middleAddressIndex = Math.floor(_addresses.length / 2);
|
|
369
|
+
|
|
370
|
+
const [tokenBalances1, tokenBalances2] = await Promise.all([
|
|
371
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(0, middleAddressIndex)], setViemBlockNumber(block)),
|
|
372
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(middleAddressIndex, _addresses.length)], setViemBlockNumber(block)),
|
|
373
|
+
]);
|
|
374
|
+
|
|
375
|
+
const loanInfo = [...tokenBalances1, ...tokenBalances2];
|
|
376
|
+
|
|
377
|
+
loanInfo.forEach((tokenInfo: any, i: number) => {
|
|
378
|
+
const asset = wethToEth(symbols[i]);
|
|
379
|
+
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
380
|
+
|
|
381
|
+
balances = {
|
|
382
|
+
collateral: {
|
|
383
|
+
...balances.collateral,
|
|
384
|
+
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
385
|
+
},
|
|
386
|
+
debt: {
|
|
387
|
+
...balances.debt,
|
|
388
|
+
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
return balances;
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
export const getAaveV3AccountBalances = async (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => _getAaveV3AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
|
|
397
|
+
|
|
398
|
+
export const _getAaveV3AccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, extractedState: any, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3PositionData> => {
|
|
399
|
+
const {
|
|
400
|
+
selectedMarket: market, assetsData,
|
|
401
|
+
} = extractedState;
|
|
402
|
+
let payload: AaveV3PositionData = {
|
|
403
|
+
...EMPTY_AAVE_DATA,
|
|
404
|
+
lastUpdated: Date.now(),
|
|
405
|
+
};
|
|
406
|
+
if (!address) {
|
|
407
|
+
return payload;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const loanInfoContract = AaveV3ViewContractViem(provider, network);
|
|
411
|
+
const lendingPoolContract = createViemContractFromConfigFunc(market.lendingPool, market.lendingPoolAddress)(provider, network);
|
|
412
|
+
const marketAddress = market.providerAddress;
|
|
413
|
+
const _addresses = market.assets.map((a: string[]) => getAssetInfo(ethToWeth(a), network).address);
|
|
414
|
+
|
|
415
|
+
const middleAddressIndex = Math.floor(_addresses.length / 2); // split addresses in half to avoid gas limit by multicall
|
|
416
|
+
|
|
417
|
+
const [eModeCategory, tokenBalances1, tokenBalances2] = await Promise.all([
|
|
418
|
+
lendingPoolContract.read.getUserEMode([address], setViemBlockNumber(blockNumber)),
|
|
419
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(0, middleAddressIndex) as EthAddress[]], setViemBlockNumber(blockNumber)),
|
|
420
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(middleAddressIndex, _addresses.length) as EthAddress[]], setViemBlockNumber(blockNumber)),
|
|
421
|
+
]);
|
|
422
|
+
|
|
423
|
+
const loanInfo = [...tokenBalances1, ...tokenBalances2];
|
|
424
|
+
|
|
425
|
+
const usedAssets = {} as AaveV3UsedAssets;
|
|
426
|
+
loanInfo.map(async (tokenInfo, i) => {
|
|
427
|
+
const asset = market.assets[i];
|
|
428
|
+
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
429
|
+
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
430
|
+
if (!isSupplied && !isBorrowed) return;
|
|
431
|
+
|
|
432
|
+
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
433
|
+
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
434
|
+
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
435
|
+
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
436
|
+
|
|
437
|
+
let interestMode;
|
|
438
|
+
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
439
|
+
interestMode = '1';
|
|
440
|
+
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
441
|
+
interestMode = '2';
|
|
442
|
+
} else {
|
|
443
|
+
interestMode = 'both';
|
|
444
|
+
}
|
|
445
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV3UsedAsset;
|
|
446
|
+
const borrowed = new Dec(borrowedStable).add(borrowedVariable).toString();
|
|
447
|
+
|
|
448
|
+
usedAssets[asset] = {
|
|
449
|
+
...usedAssets[asset],
|
|
450
|
+
symbol: asset,
|
|
451
|
+
supplied,
|
|
452
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
453
|
+
isSupplied,
|
|
454
|
+
collateral: enabledAsCollateral,
|
|
455
|
+
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate.toString()).div(1e25).toString()),
|
|
456
|
+
borrowedStable,
|
|
457
|
+
borrowedVariable,
|
|
458
|
+
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
459
|
+
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
460
|
+
borrowed,
|
|
461
|
+
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
462
|
+
isBorrowed,
|
|
463
|
+
interestMode,
|
|
464
|
+
};
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
payload.eModeCategory = +(eModeCategory as BigInt).toString();
|
|
468
|
+
payload = {
|
|
469
|
+
...payload,
|
|
470
|
+
usedAssets,
|
|
471
|
+
...aaveAnyGetAggregatedPositionData({
|
|
472
|
+
...extractedState, usedAssets, eModeCategory: payload.eModeCategory,
|
|
473
|
+
}),
|
|
474
|
+
};
|
|
475
|
+
payload.eModeCategories = aaveV3EmodeCategoriesMapping(extractedState, usedAssets);
|
|
476
|
+
payload.isInIsolationMode = aaveV3IsInIsolationMode({ usedAssets, assetsData });
|
|
477
|
+
payload.isInSiloedMode = aaveV3IsInSiloedMode({ usedAssets, assetsData });
|
|
478
|
+
|
|
479
|
+
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
480
|
+
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
481
|
+
: '0';
|
|
482
|
+
payload.minRatio = '100';
|
|
483
|
+
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
484
|
+
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
485
|
+
: '0';
|
|
486
|
+
|
|
487
|
+
// Calculate borrow limits per asset
|
|
488
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
489
|
+
if (item.isBorrowed) {
|
|
490
|
+
// eslint-disable-next-line no-param-reassign
|
|
491
|
+
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
492
|
+
// eslint-disable-next-line no-param-reassign
|
|
493
|
+
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
494
|
+
// eslint-disable-next-line no-param-reassign
|
|
495
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
payload.isSubscribedToAutomation = false;
|
|
500
|
+
payload.automationResubscribeRequired = false;
|
|
501
|
+
|
|
502
|
+
return payload;
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
export const getAaveV3AccountData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, extractedState: any, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3PositionData> => _getAaveV3AccountData(getViemProvider(provider, network), network, address, extractedState, blockNumber);
|
|
506
|
+
|
|
507
|
+
export const getAaveV3FullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, market: AaveMarketInfo): Promise<AaveV3PositionData> => {
|
|
508
|
+
const marketData = await getAaveV3MarketData(provider, network, market);
|
|
509
|
+
const positionData = await getAaveV3AccountData(provider, network, address, { assetsData: marketData.assetsData, selectedMarket: market, eModeCategoriesData: marketData.eModeCategoriesData });
|
|
510
|
+
return positionData;
|
|
511
|
+
};
|