@defisaver/positions-sdk 2.1.42 → 2.1.43-dev-stats-test-1-dev
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/aaveV3/index.js +31 -9
- package/cjs/aaveV3/merit.js +5 -1
- package/cjs/aaveV3/merkl.js +1 -0
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/savings/morphoVaults/index.js +17 -17
- package/esm/aaveV3/index.js +31 -9
- package/esm/aaveV3/merit.js +5 -1
- package/esm/aaveV3/merkl.js +1 -0
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/savings/morphoVaults/index.js +17 -17
- package/package.json +48 -48
- package/src/aaveV2/index.ts +240 -240
- package/src/aaveV3/index.ts +648 -625
- package/src/aaveV3/merit.ts +99 -97
- package/src/aaveV3/merkl.ts +75 -74
- package/src/claiming/aaveV3.ts +154 -154
- package/src/claiming/compV3.ts +22 -22
- package/src/claiming/ethena.ts +61 -61
- package/src/claiming/index.ts +12 -12
- package/src/claiming/king.ts +66 -66
- package/src/claiming/morphoBlue.ts +118 -118
- package/src/claiming/spark.ts +225 -225
- package/src/compoundV2/index.ts +244 -244
- package/src/compoundV3/index.ts +274 -274
- package/src/config/contracts.ts +1295 -1295
- package/src/constants/index.ts +10 -10
- package/src/contracts.ts +171 -171
- package/src/curveUsd/index.ts +254 -254
- package/src/eulerV2/index.ts +324 -324
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1800 -1800
- package/src/helpers/aaveHelpers/index.ts +187 -187
- package/src/helpers/compoundHelpers/index.ts +283 -283
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +222 -222
- package/src/helpers/fluidHelpers/index.ts +326 -326
- package/src/helpers/index.ts +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +82 -82
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +396 -396
- package/src/helpers/sparkHelpers/index.ts +158 -158
- package/src/index.ts +49 -49
- package/src/liquity/index.ts +159 -159
- package/src/liquityV2/index.ts +703 -703
- package/src/llamaLend/index.ts +305 -305
- package/src/maker/index.ts +223 -223
- package/src/markets/aave/index.ts +118 -118
- package/src/markets/aave/marketAssets.ts +54 -54
- package/src/markets/compound/index.ts +243 -243
- package/src/markets/compound/marketsAssets.ts +97 -97
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2900 -2900
- 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 +971 -971
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +12 -12
- package/src/moneymarket/moneymarketCommonService.ts +85 -85
- package/src/morphoBlue/index.ts +274 -274
- package/src/portfolio/index.ts +598 -598
- package/src/savings/index.ts +95 -95
- package/src/savings/makerDsr/index.ts +53 -53
- package/src/savings/makerDsr/options.ts +9 -9
- package/src/savings/morphoVaults/index.ts +80 -80
- package/src/savings/morphoVaults/options.ts +193 -193
- package/src/savings/skyOptions/index.ts +95 -95
- package/src/savings/skyOptions/options.ts +10 -10
- package/src/savings/sparkSavingsVaults/index.ts +60 -60
- package/src/savings/sparkSavingsVaults/options.ts +35 -35
- package/src/savings/yearnV3Vaults/index.ts +61 -61
- package/src/savings/yearnV3Vaults/options.ts +55 -55
- package/src/savings/yearnVaults/index.ts +73 -73
- package/src/savings/yearnVaults/options.ts +32 -32
- package/src/services/priceService.ts +278 -278
- package/src/services/utils.ts +115 -115
- package/src/services/viem.ts +34 -34
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +458 -458
- package/src/staking/eligibility.ts +53 -53
- package/src/staking/index.ts +1 -1
- package/src/staking/staking.ts +186 -186
- package/src/types/aave.ts +196 -196
- package/src/types/claiming.ts +114 -114
- package/src/types/common.ts +107 -107
- package/src/types/compound.ts +144 -144
- package/src/types/curveUsd.ts +123 -123
- package/src/types/euler.ts +175 -175
- package/src/types/fluid.ts +483 -483
- package/src/types/index.ts +14 -14
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +126 -126
- package/src/types/llamaLend.ts +159 -159
- package/src/types/maker.ts +63 -63
- package/src/types/merit.ts +1 -1
- package/src/types/merkl.ts +70 -70
- package/src/types/morphoBlue.ts +200 -200
- package/src/types/portfolio.ts +60 -60
- package/src/types/savings/index.ts +23 -23
- package/src/types/savings/makerDsr.ts +13 -13
- package/src/types/savings/morphoVaults.ts +32 -32
- package/src/types/savings/sky.ts +14 -14
- package/src/types/savings/sparkSavingsVaults.ts +15 -15
- package/src/types/savings/yearnV3Vaults.ts +17 -17
- package/src/types/savings/yearnVaults.ts +14 -14
- package/src/types/spark.ts +134 -134
- package/src/umbrella/index.ts +69 -69
- package/src/umbrella/umbrellaUtils.ts +29 -29
package/src/aaveV3/index.ts
CHANGED
|
@@ -1,625 +1,648 @@
|
|
|
1
|
-
import { assetAmountInEth, assetAmountInWei, getAssetInfo } from '@defisaver/tokens';
|
|
2
|
-
import { Client } from 'viem';
|
|
3
|
-
import Dec from 'decimal.js';
|
|
4
|
-
import {
|
|
5
|
-
AaveIncentiveDataProviderV3ContractViem,
|
|
6
|
-
AaveIncentivesControllerViem,
|
|
7
|
-
AaveV3ViewContractViem,
|
|
8
|
-
createViemContractFromConfigFunc,
|
|
9
|
-
StkAAVEViem,
|
|
10
|
-
} from '../contracts';
|
|
11
|
-
import { aaveAnyGetAggregatedPositionData, aaveV3IsInIsolationMode, aaveV3IsInSiloedMode } from '../helpers/aaveHelpers';
|
|
12
|
-
import { AAVE_V3 } from '../markets/aave';
|
|
13
|
-
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
14
|
-
import {
|
|
15
|
-
getWrappedNativeAssetFromUnwrapped,
|
|
16
|
-
isEnabledOnBitmap,
|
|
17
|
-
isLayer2Network,
|
|
18
|
-
wethToEth,
|
|
19
|
-
wethToEthByAddress,
|
|
20
|
-
} from '../services/utils';
|
|
21
|
-
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
22
|
-
import {
|
|
23
|
-
AaveMarketInfo,
|
|
24
|
-
AaveV3AssetData,
|
|
25
|
-
AaveV3AssetsData,
|
|
26
|
-
AaveV3MarketData,
|
|
27
|
-
AaveV3PositionData,
|
|
28
|
-
AaveV3UsedAsset,
|
|
29
|
-
AaveV3UsedAssets,
|
|
30
|
-
EModeCategoriesData,
|
|
31
|
-
EModeCategoryData,
|
|
32
|
-
EModeCategoryDataMapping,
|
|
33
|
-
} from '../types/aave';
|
|
34
|
-
import {
|
|
35
|
-
Blockish,
|
|
36
|
-
EthAddress,
|
|
37
|
-
EthereumProvider,
|
|
38
|
-
IncentiveEligibilityId,
|
|
39
|
-
IncentiveKind,
|
|
40
|
-
NetworkNumber,
|
|
41
|
-
PositionBalances,
|
|
42
|
-
HexString,
|
|
43
|
-
} from '../types/common';
|
|
44
|
-
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
45
|
-
import { getMeritCampaigns } from './merit';
|
|
46
|
-
import { getAaveUnderlyingSymbol, getMerkleCampaigns } from './merkl';
|
|
47
|
-
import { SECONDS_PER_YEAR } from '../constants';
|
|
48
|
-
|
|
49
|
-
export const aaveV3EmodeCategoriesMapping = (extractedState: any, usedAssets: AaveV3UsedAssets) => {
|
|
50
|
-
const { eModeCategoriesData }: { assetsData: AaveV3AssetsData, eModeCategoriesData: EModeCategoriesData } = extractedState;
|
|
51
|
-
const usedAssetsValues = Object.values(usedAssets);
|
|
52
|
-
|
|
53
|
-
const categoriesMapping: { [key: number]: EModeCategoryDataMapping } = {};
|
|
54
|
-
|
|
55
|
-
Object.values(eModeCategoriesData).forEach((e: EModeCategoryData) => {
|
|
56
|
-
const borrowingOnlyFromCategory = e.id === 0
|
|
57
|
-
? true
|
|
58
|
-
: !usedAssetsValues.filter(u => u.isBorrowed && !e.borrowAssets.includes(u.symbol)).length;
|
|
59
|
-
const afterEnteringCategory = aaveAnyGetAggregatedPositionData({
|
|
60
|
-
...extractedState, usedAssets, eModeCategory: e.id,
|
|
61
|
-
});
|
|
62
|
-
const willStayOverCollateralized = new Dec(afterEnteringCategory.ratio).eq(0) || new Dec(afterEnteringCategory.ratio).gt(afterEnteringCategory.liqPercent);
|
|
63
|
-
const enteringTerms = [borrowingOnlyFromCategory, willStayOverCollateralized];
|
|
64
|
-
categoriesMapping[e.id] = {
|
|
65
|
-
enteringTerms,
|
|
66
|
-
canEnterCategory: !enteringTerms.includes(false),
|
|
67
|
-
id: e.id,
|
|
68
|
-
enabledData: {
|
|
69
|
-
ratio: afterEnteringCategory.ratio,
|
|
70
|
-
liqRatio: afterEnteringCategory.liqRatio,
|
|
71
|
-
liqPercent: afterEnteringCategory.liqPercent,
|
|
72
|
-
collRatio: afterEnteringCategory.collRatio,
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
});
|
|
76
|
-
return categoriesMapping;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export async function _getAaveV3MarketData(provider: Client, network: NetworkNumber, market: AaveMarketInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3MarketData> {
|
|
80
|
-
const _addresses = market.assets.map(a => getAssetInfo(getWrappedNativeAssetFromUnwrapped(a), network).address);
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
const
|
|
86
|
-
const
|
|
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
|
-
|
|
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
|
-
symbol
|
|
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
|
-
|
|
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
|
-
apy
|
|
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
|
-
// @ts-ignore
|
|
286
|
-
rewardForMarket.
|
|
287
|
-
if (
|
|
288
|
-
if (+(
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
.mul(
|
|
295
|
-
.
|
|
296
|
-
.div(_market.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
...
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
payload.
|
|
515
|
-
payload.
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
//
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
'
|
|
554
|
-
'
|
|
555
|
-
'
|
|
556
|
-
'
|
|
557
|
-
'
|
|
558
|
-
'
|
|
559
|
-
'
|
|
560
|
-
'
|
|
561
|
-
'
|
|
562
|
-
'
|
|
563
|
-
'
|
|
564
|
-
'
|
|
565
|
-
'
|
|
566
|
-
'
|
|
567
|
-
'
|
|
568
|
-
'
|
|
569
|
-
'
|
|
570
|
-
'
|
|
571
|
-
'
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
1
|
+
import { assetAmountInEth, assetAmountInWei, getAssetInfo } from '@defisaver/tokens';
|
|
2
|
+
import { Client } from 'viem';
|
|
3
|
+
import Dec from 'decimal.js';
|
|
4
|
+
import {
|
|
5
|
+
AaveIncentiveDataProviderV3ContractViem,
|
|
6
|
+
AaveIncentivesControllerViem,
|
|
7
|
+
AaveV3ViewContractViem,
|
|
8
|
+
createViemContractFromConfigFunc,
|
|
9
|
+
StkAAVEViem,
|
|
10
|
+
} from '../contracts';
|
|
11
|
+
import { aaveAnyGetAggregatedPositionData, aaveV3IsInIsolationMode, aaveV3IsInSiloedMode } from '../helpers/aaveHelpers';
|
|
12
|
+
import { AAVE_V3 } from '../markets/aave';
|
|
13
|
+
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
14
|
+
import {
|
|
15
|
+
getWrappedNativeAssetFromUnwrapped,
|
|
16
|
+
isEnabledOnBitmap,
|
|
17
|
+
isLayer2Network,
|
|
18
|
+
wethToEth,
|
|
19
|
+
wethToEthByAddress,
|
|
20
|
+
} from '../services/utils';
|
|
21
|
+
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
22
|
+
import {
|
|
23
|
+
AaveMarketInfo,
|
|
24
|
+
AaveV3AssetData,
|
|
25
|
+
AaveV3AssetsData,
|
|
26
|
+
AaveV3MarketData,
|
|
27
|
+
AaveV3PositionData,
|
|
28
|
+
AaveV3UsedAsset,
|
|
29
|
+
AaveV3UsedAssets,
|
|
30
|
+
EModeCategoriesData,
|
|
31
|
+
EModeCategoryData,
|
|
32
|
+
EModeCategoryDataMapping,
|
|
33
|
+
} from '../types/aave';
|
|
34
|
+
import {
|
|
35
|
+
Blockish,
|
|
36
|
+
EthAddress,
|
|
37
|
+
EthereumProvider,
|
|
38
|
+
IncentiveEligibilityId,
|
|
39
|
+
IncentiveKind,
|
|
40
|
+
NetworkNumber,
|
|
41
|
+
PositionBalances,
|
|
42
|
+
HexString,
|
|
43
|
+
} from '../types/common';
|
|
44
|
+
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
45
|
+
import { getMeritCampaigns } from './merit';
|
|
46
|
+
import { getAaveUnderlyingSymbol, getMerkleCampaigns } from './merkl';
|
|
47
|
+
import { SECONDS_PER_YEAR } from '../constants';
|
|
48
|
+
|
|
49
|
+
export const aaveV3EmodeCategoriesMapping = (extractedState: any, usedAssets: AaveV3UsedAssets) => {
|
|
50
|
+
const { eModeCategoriesData }: { assetsData: AaveV3AssetsData, eModeCategoriesData: EModeCategoriesData } = extractedState;
|
|
51
|
+
const usedAssetsValues = Object.values(usedAssets);
|
|
52
|
+
|
|
53
|
+
const categoriesMapping: { [key: number]: EModeCategoryDataMapping } = {};
|
|
54
|
+
|
|
55
|
+
Object.values(eModeCategoriesData).forEach((e: EModeCategoryData) => {
|
|
56
|
+
const borrowingOnlyFromCategory = e.id === 0
|
|
57
|
+
? true
|
|
58
|
+
: !usedAssetsValues.filter(u => u.isBorrowed && !e.borrowAssets.includes(u.symbol)).length;
|
|
59
|
+
const afterEnteringCategory = aaveAnyGetAggregatedPositionData({
|
|
60
|
+
...extractedState, usedAssets, eModeCategory: e.id,
|
|
61
|
+
});
|
|
62
|
+
const willStayOverCollateralized = new Dec(afterEnteringCategory.ratio).eq(0) || new Dec(afterEnteringCategory.ratio).gt(afterEnteringCategory.liqPercent);
|
|
63
|
+
const enteringTerms = [borrowingOnlyFromCategory, willStayOverCollateralized];
|
|
64
|
+
categoriesMapping[e.id] = {
|
|
65
|
+
enteringTerms,
|
|
66
|
+
canEnterCategory: !enteringTerms.includes(false),
|
|
67
|
+
id: e.id,
|
|
68
|
+
enabledData: {
|
|
69
|
+
ratio: afterEnteringCategory.ratio,
|
|
70
|
+
liqRatio: afterEnteringCategory.liqRatio,
|
|
71
|
+
liqPercent: afterEnteringCategory.liqPercent,
|
|
72
|
+
collRatio: afterEnteringCategory.collRatio,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
return categoriesMapping;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export async function _getAaveV3MarketData(provider: Client, network: NetworkNumber, market: AaveMarketInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3MarketData> {
|
|
80
|
+
const _addresses = market.assets.map(a => getAssetInfo(getWrappedNativeAssetFromUnwrapped(a), network).address);
|
|
81
|
+
|
|
82
|
+
console.log('Fetched addresses');
|
|
83
|
+
const isL2 = isLayer2Network(network);
|
|
84
|
+
const loanInfoContract = AaveV3ViewContractViem(provider, network);
|
|
85
|
+
const aaveIncentivesContract = AaveIncentiveDataProviderV3ContractViem(provider, network);
|
|
86
|
+
const marketAddress = market.providerAddress;
|
|
87
|
+
const networksWithIncentives = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Opt, NetworkNumber.Linea, NetworkNumber.Plasma];
|
|
88
|
+
|
|
89
|
+
console.log({ marketAddress, blockNumber });
|
|
90
|
+
console.log('Fetching loan info...');
|
|
91
|
+
const loanInfo = await loanInfoContract.read.getFullTokensInfo([marketAddress, _addresses as EthAddress[]], setViemBlockNumber(blockNumber));
|
|
92
|
+
console.log('Fetched loan info');
|
|
93
|
+
|
|
94
|
+
console.log('Fetching eModes info...');
|
|
95
|
+
const eModesInfo = await loanInfoContract.read.getAllEmodes([marketAddress], setViemBlockNumber(blockNumber));
|
|
96
|
+
console.log('Fetched eModes info');
|
|
97
|
+
|
|
98
|
+
console.log('Fetching borrow allowed status...');
|
|
99
|
+
let isBorrowAllowed = await loanInfoContract.read.isBorrowAllowed([marketAddress], setViemBlockNumber(blockNumber)); // Used on L2s check for PriceOracleSentinel (mainnet will always return true)
|
|
100
|
+
console.log('Fetched borrow allowed status');
|
|
101
|
+
|
|
102
|
+
console.log('Fetching reward info...');
|
|
103
|
+
let rewardInfo = networksWithIncentives.includes(network) ? await aaveIncentivesContract.read.getReservesIncentivesData([marketAddress], setViemBlockNumber(blockNumber)) : null;
|
|
104
|
+
console.log('Fetched reward info');
|
|
105
|
+
|
|
106
|
+
console.log('Fetching Merkle campaigns...');
|
|
107
|
+
const merkleRewardsMap = await getMerkleCampaigns(network);
|
|
108
|
+
console.log('Fetched Merkle campaigns');
|
|
109
|
+
|
|
110
|
+
console.log('Fetching Merit campaigns...');
|
|
111
|
+
const meritRewardsMap = await getMeritCampaigns(network, market.value);
|
|
112
|
+
console.log('Fetched Merit campaigns');
|
|
113
|
+
isBorrowAllowed = isLayer2Network(network) ? isBorrowAllowed : true;
|
|
114
|
+
|
|
115
|
+
const eModeCategoriesData: EModeCategoriesData = {};
|
|
116
|
+
for (let i = 0; i < eModesInfo.length; i++) {
|
|
117
|
+
if (!eModesInfo[i].label) break;
|
|
118
|
+
eModeCategoriesData[i + 1] = {
|
|
119
|
+
label: eModesInfo[i].label,
|
|
120
|
+
id: i + 1,
|
|
121
|
+
liquidationBonus: new Dec(eModesInfo[i].liquidationBonus).div(10000).toString(),
|
|
122
|
+
liquidationRatio: new Dec(eModesInfo[i].liquidationThreshold).div(10000).toString(),
|
|
123
|
+
collateralFactor: new Dec(eModesInfo[i].ltv).div(10000).toString(),
|
|
124
|
+
borrowableBitmap: eModesInfo[i].borrowableBitmap.toString(),
|
|
125
|
+
collateralBitmap: eModesInfo[i].collateralBitmap.toString(),
|
|
126
|
+
borrowAssets: [],
|
|
127
|
+
collateralAssets: [],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
console.log('Parsed e modes info');
|
|
132
|
+
if (networksWithIncentives.includes(network) && rewardInfo) {
|
|
133
|
+
rewardInfo = rewardInfo.reduce((all: any, _market: any) => {
|
|
134
|
+
// eslint-disable-next-line no-param-reassign
|
|
135
|
+
all[_market.underlyingAsset] = _market;
|
|
136
|
+
return all;
|
|
137
|
+
}, {});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
console.log('Parsed reward info');
|
|
141
|
+
const assetsData: AaveV3AssetData[] = await Promise.all(loanInfo
|
|
142
|
+
.map(async (tokenMarket, i) => {
|
|
143
|
+
const symbol = market.assets[i];
|
|
144
|
+
|
|
145
|
+
// eslint-disable-next-line guard-for-in
|
|
146
|
+
for (const eModeIndex in eModeCategoriesData) {
|
|
147
|
+
if (isEnabledOnBitmap(Number(eModeCategoriesData[eModeIndex].collateralBitmap), Number(tokenMarket.assetId))) eModeCategoriesData[eModeIndex].collateralAssets.push(symbol);
|
|
148
|
+
if (isEnabledOnBitmap(Number(eModeCategoriesData[eModeIndex].borrowableBitmap), Number(tokenMarket.assetId))) eModeCategoriesData[eModeIndex].borrowAssets.push(symbol);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const borrowCap = tokenMarket.borrowCap.toString();
|
|
152
|
+
|
|
153
|
+
const borrowCapInWei = new Dec(assetAmountInWei(borrowCap, symbol));
|
|
154
|
+
let marketLiquidity = borrowCapInWei.lt(new Dec(tokenMarket.totalSupply.toString()))
|
|
155
|
+
? assetAmountInEth(borrowCapInWei
|
|
156
|
+
.sub(tokenMarket.totalBorrow.toString())
|
|
157
|
+
.toString(), symbol)
|
|
158
|
+
: assetAmountInEth(new Dec(tokenMarket.totalSupply.toString())
|
|
159
|
+
.sub(tokenMarket.totalBorrow.toString())
|
|
160
|
+
.toString(), symbol);
|
|
161
|
+
|
|
162
|
+
if (new Dec(marketLiquidity).lt(0)) {
|
|
163
|
+
marketLiquidity = '0';
|
|
164
|
+
}
|
|
165
|
+
return ({
|
|
166
|
+
symbol,
|
|
167
|
+
isIsolated: new Dec(tokenMarket.debtCeilingForIsolationMode.toString()).gt(0),
|
|
168
|
+
debtCeilingForIsolationMode: new Dec(tokenMarket.debtCeilingForIsolationMode.toString()).div(100).toString(),
|
|
169
|
+
isSiloed: tokenMarket.isSiloedForBorrowing,
|
|
170
|
+
isolationModeTotalDebt: new Dec(tokenMarket.isolationModeTotalDebt.toString()).div(100).toString(),
|
|
171
|
+
assetId: Number(tokenMarket.assetId),
|
|
172
|
+
underlyingTokenAddress: tokenMarket.underlyingTokenAddress,
|
|
173
|
+
supplyRate: aprToApy(new Dec(tokenMarket.supplyRate.toString()).div(1e25).toString()),
|
|
174
|
+
borrowRate: aprToApy(new Dec(tokenMarket.borrowRateVariable.toString()).div(1e25).toString()),
|
|
175
|
+
borrowRateStable: aprToApy(new Dec(tokenMarket.borrowRateStable.toString()).div(1e25).toString()),
|
|
176
|
+
collateralFactor: new Dec(tokenMarket.collateralFactor.toString()).div(10000).toString(),
|
|
177
|
+
liquidationBonus: new Dec(tokenMarket.liquidationBonus.toString()).div(10000).toString(),
|
|
178
|
+
liquidationRatio: new Dec(tokenMarket.liquidationRatio.toString()).div(10000).toString(),
|
|
179
|
+
marketLiquidity,
|
|
180
|
+
utilization: new Dec(tokenMarket.totalBorrow.toString()).times(100).div(new Dec(tokenMarket.totalSupply.toString())).toString(),
|
|
181
|
+
usageAsCollateralEnabled: tokenMarket.usageAsCollateralEnabled,
|
|
182
|
+
supplyCap: tokenMarket.supplyCap.toString(),
|
|
183
|
+
borrowCap,
|
|
184
|
+
totalSupply: assetAmountInEth(tokenMarket.totalSupply.toString(), symbol),
|
|
185
|
+
isInactive: !tokenMarket.isActive,
|
|
186
|
+
isFrozen: tokenMarket.isFrozen,
|
|
187
|
+
isPaused: tokenMarket.isPaused,
|
|
188
|
+
canBeBorrowed: tokenMarket.isActive && !tokenMarket.isPaused && !tokenMarket.isFrozen && tokenMarket.borrowingEnabled && isBorrowAllowed,
|
|
189
|
+
canBeSupplied: tokenMarket.isActive && !tokenMarket.isPaused && !tokenMarket.isFrozen,
|
|
190
|
+
canBeWithdrawn: tokenMarket.isActive && !tokenMarket.isPaused,
|
|
191
|
+
canBePayBacked: tokenMarket.isActive && !tokenMarket.isPaused,
|
|
192
|
+
disabledStableBorrowing: !tokenMarket.stableBorrowRateEnabled,
|
|
193
|
+
totalBorrow: assetAmountInEth(tokenMarket.totalBorrow.toString(), symbol),
|
|
194
|
+
totalBorrowVar: assetAmountInEth(tokenMarket.totalBorrowVar.toString(), symbol),
|
|
195
|
+
price: new Dec(tokenMarket.price.toString()).div(1e8).toString(), // is actually price in USD
|
|
196
|
+
isolationModeBorrowingEnabled: tokenMarket.isolationModeBorrowingEnabled,
|
|
197
|
+
isFlashLoanEnabled: tokenMarket.isFlashLoanEnabled,
|
|
198
|
+
aTokenAddress: tokenMarket.aTokenAddress,
|
|
199
|
+
vTokenAddress: tokenMarket.debtTokenAddress,
|
|
200
|
+
supplyIncentives: [],
|
|
201
|
+
borrowIncentives: [],
|
|
202
|
+
});
|
|
203
|
+
}),
|
|
204
|
+
);
|
|
205
|
+
console.log('Parsed assets data');
|
|
206
|
+
// Get incentives data
|
|
207
|
+
await Promise.all(assetsData.map(async (_market: AaveV3AssetData, index) => {
|
|
208
|
+
/* eslint-disable no-param-reassign */
|
|
209
|
+
// @ts-ignore
|
|
210
|
+
const rewardForMarket = rewardInfo?.[_market.underlyingTokenAddress];
|
|
211
|
+
const isStakingAsset = STAKING_ASSETS.includes(_market.symbol);
|
|
212
|
+
console.log('Processing market', _market.symbol);
|
|
213
|
+
if (isStakingAsset) {
|
|
214
|
+
const yieldApy = await getStakingApy(_market.symbol, network as NetworkNumber);
|
|
215
|
+
console.log('Got yield apy', yieldApy);
|
|
216
|
+
_market.supplyIncentives.push({
|
|
217
|
+
apy: yieldApy,
|
|
218
|
+
token: _market.symbol,
|
|
219
|
+
incentiveKind: IncentiveKind.Staking,
|
|
220
|
+
description: `Native ${_market.symbol} yield.`,
|
|
221
|
+
});
|
|
222
|
+
if (_market.canBeBorrowed) {
|
|
223
|
+
// when borrowing assets whose value increases over time
|
|
224
|
+
_market.borrowIncentives.push({
|
|
225
|
+
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
226
|
+
token: _market.symbol,
|
|
227
|
+
incentiveKind: IncentiveKind.Reward,
|
|
228
|
+
description: `Due to the native yield of ${_market.symbol}, the value of the debt would increase over time.`,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const aTokenAddress = (_market as any).aTokenAddress.toLowerCase(); // DEV: Should aTokenAddress be in AaveV3AssetData type?
|
|
234
|
+
if (merkleRewardsMap[aTokenAddress]?.supply) {
|
|
235
|
+
const {
|
|
236
|
+
apy, rewardTokenSymbol, description, identifier,
|
|
237
|
+
} = merkleRewardsMap[aTokenAddress].supply;
|
|
238
|
+
_market.supplyIncentives.push({
|
|
239
|
+
apy,
|
|
240
|
+
token: rewardTokenSymbol,
|
|
241
|
+
incentiveKind: IncentiveKind.Reward,
|
|
242
|
+
description,
|
|
243
|
+
eligibilityId: identifier as IncentiveEligibilityId,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
console.log('Processed merkle a incentives');
|
|
247
|
+
|
|
248
|
+
const vTokenAddress = (_market as any).vTokenAddress.toLowerCase(); // DEV: Should vTokenAddress be in AaveV3AssetData type?
|
|
249
|
+
if (merkleRewardsMap[vTokenAddress]?.borrow) {
|
|
250
|
+
const {
|
|
251
|
+
apy, rewardTokenSymbol, description, identifier,
|
|
252
|
+
} = merkleRewardsMap[vTokenAddress].borrow;
|
|
253
|
+
_market.borrowIncentives.push({
|
|
254
|
+
apy,
|
|
255
|
+
token: rewardTokenSymbol,
|
|
256
|
+
incentiveKind: IncentiveKind.Reward,
|
|
257
|
+
description,
|
|
258
|
+
eligibilityId: identifier as IncentiveEligibilityId,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
console.log('Processed merkle v incentives');
|
|
262
|
+
|
|
263
|
+
if (meritRewardsMap.supply[_market.symbol]) {
|
|
264
|
+
const { apy, rewardTokenSymbol, description } = meritRewardsMap.supply[_market.symbol];
|
|
265
|
+
_market.supplyIncentives.push({
|
|
266
|
+
apy,
|
|
267
|
+
token: rewardTokenSymbol,
|
|
268
|
+
incentiveKind: IncentiveKind.Reward,
|
|
269
|
+
description,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
console.log('Processed merit supply incentives');
|
|
273
|
+
if (meritRewardsMap.borrow[_market.symbol]) {
|
|
274
|
+
const { apy, rewardTokenSymbol, description } = meritRewardsMap.borrow[_market.symbol];
|
|
275
|
+
_market.borrowIncentives.push({
|
|
276
|
+
apy,
|
|
277
|
+
token: rewardTokenSymbol,
|
|
278
|
+
incentiveKind: IncentiveKind.Reward,
|
|
279
|
+
description,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
console.log('Processed merit borrow incentives');
|
|
283
|
+
if (!rewardForMarket) return;
|
|
284
|
+
console.log('Reward for market', rewardForMarket);
|
|
285
|
+
// @ts-ignore
|
|
286
|
+
rewardForMarket.aIncentiveData.rewardsTokenInformation.forEach(supplyRewardData => {
|
|
287
|
+
if (supplyRewardData) {
|
|
288
|
+
if (+(supplyRewardData.emissionEndTimestamp.toString()) * 1000 < Date.now()) return;
|
|
289
|
+
// reward token is aave asset
|
|
290
|
+
const supplyEmissionPerSecond = supplyRewardData.emissionPerSecond;
|
|
291
|
+
const supplyRewardPrice = new Dec(supplyRewardData.rewardPriceFeed).div(10 ** +supplyRewardData.priceFeedDecimals)
|
|
292
|
+
.toString();
|
|
293
|
+
const rewardApy = new Dec(supplyEmissionPerSecond).div((10 ** +supplyRewardData.rewardTokenDecimals) / 100)
|
|
294
|
+
.mul(365 * 24 * 3600)
|
|
295
|
+
.mul(supplyRewardPrice)
|
|
296
|
+
.div(_market.price)
|
|
297
|
+
.div(_market.totalSupply)
|
|
298
|
+
.toString();
|
|
299
|
+
_market.supplyIncentives.push({
|
|
300
|
+
token: getAaveUnderlyingSymbol(supplyRewardData.rewardTokenSymbol),
|
|
301
|
+
apy: rewardApy,
|
|
302
|
+
incentiveKind: IncentiveKind.Reward,
|
|
303
|
+
description: 'Eligible for protocol-level incentives.',
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
// @ts-ignore
|
|
308
|
+
rewardForMarket.vIncentiveData.rewardsTokenInformation.forEach(borrowRewardData => {
|
|
309
|
+
if (borrowRewardData) {
|
|
310
|
+
if (+(borrowRewardData.emissionEndTimestamp.toString()) * 1000 < Date.now()) return;
|
|
311
|
+
const supplyEmissionPerSecond = borrowRewardData.emissionPerSecond;
|
|
312
|
+
const supplyRewardPrice = new Dec(borrowRewardData.rewardPriceFeed).div(10 ** +borrowRewardData.priceFeedDecimals)
|
|
313
|
+
.toString();
|
|
314
|
+
const rewardApy = new Dec(supplyEmissionPerSecond).div((10 ** +borrowRewardData.rewardTokenDecimals) / 100)
|
|
315
|
+
.mul(365 * 24 * 3600)
|
|
316
|
+
.mul(supplyRewardPrice)
|
|
317
|
+
.div(_market.price)
|
|
318
|
+
.div(_market.totalBorrowVar)
|
|
319
|
+
.toString();
|
|
320
|
+
_market.borrowIncentives.push({
|
|
321
|
+
token: getAaveUnderlyingSymbol(borrowRewardData.rewardTokenSymbol),
|
|
322
|
+
apy: rewardApy,
|
|
323
|
+
incentiveKind: IncentiveKind.Reward,
|
|
324
|
+
description: 'Eligible for protocol-level incentives.',
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}));
|
|
329
|
+
|
|
330
|
+
const payload: AaveV3AssetsData = {};
|
|
331
|
+
// Sort by market size
|
|
332
|
+
assetsData
|
|
333
|
+
.sort((a, b) => {
|
|
334
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
335
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
336
|
+
|
|
337
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
338
|
+
})
|
|
339
|
+
.forEach((assetData: AaveV3AssetData, i) => {
|
|
340
|
+
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
eModeCategoriesData[0] = {
|
|
344
|
+
id: 0,
|
|
345
|
+
label: '',
|
|
346
|
+
liquidationBonus: '0',
|
|
347
|
+
liquidationRatio: '0',
|
|
348
|
+
collateralFactor: '0',
|
|
349
|
+
collateralAssets: assetsData.map((a) => a.symbol),
|
|
350
|
+
borrowAssets: assetsData.map((a) => a.symbol),
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
console.log('Parsed payload');
|
|
354
|
+
return { assetsData: payload, eModeCategoriesData };
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export async function getAaveV3MarketData(provider: EthereumProvider, network: NetworkNumber, market: AaveMarketInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3MarketData> {
|
|
358
|
+
return _getAaveV3MarketData(getViemProvider(provider, network), network, market, blockNumber);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export const EMPTY_AAVE_DATA = {
|
|
362
|
+
usedAssets: {},
|
|
363
|
+
suppliedUsd: '0',
|
|
364
|
+
borrowedUsd: '0',
|
|
365
|
+
borrowLimitUsd: '0',
|
|
366
|
+
leftToBorrowUsd: '0',
|
|
367
|
+
ratio: '0',
|
|
368
|
+
minRatio: '0',
|
|
369
|
+
netApy: '0',
|
|
370
|
+
incentiveUsd: '0',
|
|
371
|
+
totalInterestUsd: '0',
|
|
372
|
+
isSubscribedToAutomation: false,
|
|
373
|
+
automationResubscribeRequired: false,
|
|
374
|
+
eModeCategory: 0,
|
|
375
|
+
isInIsolationMode: false,
|
|
376
|
+
isInSiloedMode: false,
|
|
377
|
+
totalSupplied: '0',
|
|
378
|
+
eModeCategories: [],
|
|
379
|
+
collRatio: '0',
|
|
380
|
+
suppliedCollateralUsd: '0',
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
export const _getAaveV3AccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
384
|
+
let balances: PositionBalances = {
|
|
385
|
+
collateral: {},
|
|
386
|
+
debt: {},
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
if (!address) {
|
|
390
|
+
return balances;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const loanInfoContract = AaveV3ViewContractViem(provider, network, block);
|
|
394
|
+
|
|
395
|
+
const market = AAVE_V3(network);
|
|
396
|
+
const marketAddress = market.providerAddress;
|
|
397
|
+
// @ts-ignore
|
|
398
|
+
const protocolDataProviderContract = createViemContractFromConfigFunc(market.protocolData, market.protocolDataAddress)(provider, network);
|
|
399
|
+
|
|
400
|
+
// @ts-ignore
|
|
401
|
+
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block)) as { symbol: string, tokenAddress: EthAddress }[];
|
|
402
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
403
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
404
|
+
|
|
405
|
+
// split addresses in half to avoid gas limit by multicall
|
|
406
|
+
const middleAddressIndex = Math.floor(_addresses.length / 2);
|
|
407
|
+
|
|
408
|
+
const [tokenBalances1, tokenBalances2] = await Promise.all([
|
|
409
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(0, middleAddressIndex)], setViemBlockNumber(block)),
|
|
410
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(middleAddressIndex, _addresses.length)], setViemBlockNumber(block)),
|
|
411
|
+
]);
|
|
412
|
+
|
|
413
|
+
const loanInfo = [...tokenBalances1, ...tokenBalances2];
|
|
414
|
+
|
|
415
|
+
loanInfo.forEach((tokenInfo: any, i: number) => {
|
|
416
|
+
const asset = wethToEth(symbols[i]);
|
|
417
|
+
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
418
|
+
|
|
419
|
+
balances = {
|
|
420
|
+
collateral: {
|
|
421
|
+
...balances.collateral,
|
|
422
|
+
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
423
|
+
},
|
|
424
|
+
debt: {
|
|
425
|
+
...balances.debt,
|
|
426
|
+
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
427
|
+
},
|
|
428
|
+
};
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
return balances;
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
export const getAaveV3AccountBalances = async (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => _getAaveV3AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
|
|
435
|
+
|
|
436
|
+
export const _getAaveV3AccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, extractedState: any, blockNumber: 'latest' | number = 'latest'): Promise<AaveV3PositionData> => {
|
|
437
|
+
const {
|
|
438
|
+
selectedMarket: market, assetsData,
|
|
439
|
+
} = extractedState;
|
|
440
|
+
let payload: AaveV3PositionData = {
|
|
441
|
+
...EMPTY_AAVE_DATA,
|
|
442
|
+
lastUpdated: Date.now(),
|
|
443
|
+
};
|
|
444
|
+
if (!address) {
|
|
445
|
+
return payload;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const loanInfoContract = AaveV3ViewContractViem(provider, network);
|
|
449
|
+
const lendingPoolContract = createViemContractFromConfigFunc(market.lendingPool, market.lendingPoolAddress)(provider, network);
|
|
450
|
+
const marketAddress = market.providerAddress;
|
|
451
|
+
const _addresses = market.assets.map((a: string) => getAssetInfo(getWrappedNativeAssetFromUnwrapped(a), network).address);
|
|
452
|
+
|
|
453
|
+
const middleAddressIndex = Math.floor(_addresses.length / 2); // split addresses in half to avoid gas limit by multicall
|
|
454
|
+
|
|
455
|
+
const [eModeCategory, tokenBalances1, tokenBalances2] = await Promise.all([
|
|
456
|
+
lendingPoolContract.read.getUserEMode([address], setViemBlockNumber(blockNumber)),
|
|
457
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(0, middleAddressIndex) as EthAddress[]], setViemBlockNumber(blockNumber)),
|
|
458
|
+
loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses.slice(middleAddressIndex, _addresses.length) as EthAddress[]], setViemBlockNumber(blockNumber)),
|
|
459
|
+
]);
|
|
460
|
+
|
|
461
|
+
const loanInfo = [...tokenBalances1, ...tokenBalances2];
|
|
462
|
+
|
|
463
|
+
const usedAssets = {} as AaveV3UsedAssets;
|
|
464
|
+
loanInfo.map(async (tokenInfo, i) => {
|
|
465
|
+
const asset = market.assets[i];
|
|
466
|
+
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
467
|
+
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
468
|
+
if (!isSupplied && !isBorrowed) return;
|
|
469
|
+
|
|
470
|
+
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
471
|
+
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
472
|
+
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
473
|
+
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
474
|
+
|
|
475
|
+
let interestMode;
|
|
476
|
+
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
477
|
+
interestMode = '1';
|
|
478
|
+
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
479
|
+
interestMode = '2';
|
|
480
|
+
} else {
|
|
481
|
+
interestMode = 'both';
|
|
482
|
+
}
|
|
483
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV3UsedAsset;
|
|
484
|
+
const borrowed = new Dec(borrowedStable).add(borrowedVariable).toString();
|
|
485
|
+
|
|
486
|
+
usedAssets[asset] = {
|
|
487
|
+
...usedAssets[asset],
|
|
488
|
+
symbol: asset,
|
|
489
|
+
supplied,
|
|
490
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
491
|
+
isSupplied,
|
|
492
|
+
collateral: enabledAsCollateral,
|
|
493
|
+
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate.toString()).div(1e25).toString()),
|
|
494
|
+
borrowedStable,
|
|
495
|
+
borrowedVariable,
|
|
496
|
+
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
497
|
+
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
498
|
+
borrowed,
|
|
499
|
+
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
500
|
+
isBorrowed,
|
|
501
|
+
interestMode,
|
|
502
|
+
};
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
payload.eModeCategory = +(eModeCategory as BigInt).toString();
|
|
506
|
+
payload = {
|
|
507
|
+
...payload,
|
|
508
|
+
usedAssets,
|
|
509
|
+
...aaveAnyGetAggregatedPositionData({
|
|
510
|
+
...extractedState, usedAssets, eModeCategory: payload.eModeCategory,
|
|
511
|
+
}),
|
|
512
|
+
};
|
|
513
|
+
payload.eModeCategories = aaveV3EmodeCategoriesMapping(extractedState, usedAssets);
|
|
514
|
+
payload.isInIsolationMode = aaveV3IsInIsolationMode({ usedAssets, assetsData });
|
|
515
|
+
payload.isInSiloedMode = aaveV3IsInSiloedMode({ usedAssets, assetsData });
|
|
516
|
+
|
|
517
|
+
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
518
|
+
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
519
|
+
: '0';
|
|
520
|
+
payload.minRatio = '100';
|
|
521
|
+
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
522
|
+
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
523
|
+
: '0';
|
|
524
|
+
|
|
525
|
+
// Calculate borrow limits per asset
|
|
526
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
527
|
+
if (item.isBorrowed) {
|
|
528
|
+
// eslint-disable-next-line no-param-reassign
|
|
529
|
+
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
530
|
+
// eslint-disable-next-line no-param-reassign
|
|
531
|
+
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
532
|
+
// eslint-disable-next-line no-param-reassign
|
|
533
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
payload.isSubscribedToAutomation = false;
|
|
538
|
+
payload.automationResubscribeRequired = false;
|
|
539
|
+
|
|
540
|
+
return payload;
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
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);
|
|
544
|
+
|
|
545
|
+
export const getAaveV3FullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, market: AaveMarketInfo): Promise<AaveV3PositionData> => {
|
|
546
|
+
const marketData = await getAaveV3MarketData(provider, network, market);
|
|
547
|
+
const positionData = await getAaveV3AccountData(provider, network, address, { assetsData: marketData.assetsData, selectedMarket: market, eModeCategoriesData: marketData.eModeCategoriesData });
|
|
548
|
+
return positionData;
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
// aTokens eligible for AAVE rewards
|
|
552
|
+
export const REWARDABLE_ASSETS = [
|
|
553
|
+
'0x028171bCA77440897B824Ca71D1c56caC55b68A3', // DAI
|
|
554
|
+
'0x6C3c78838c761c6Ac7bE9F59fe808ea2A6E4379d',
|
|
555
|
+
'0xD37EE7e4f452C6638c96536e68090De8cBcdb583', // GUSD
|
|
556
|
+
'0x279AF5b99540c1A3A7E3CDd326e19659401eF99e',
|
|
557
|
+
'0xBcca60bB61934080951369a648Fb03DF4F96263C', // USDC
|
|
558
|
+
'0x619beb58998eD2278e08620f97007e1116D5D25b',
|
|
559
|
+
'0x3Ed3B47Dd13EC9a98b44e6204A523E766B225811', // USDT
|
|
560
|
+
'0x531842cEbbdD378f8ee36D171d6cC9C4fcf475Ec',
|
|
561
|
+
'0x9ff58f4fFB29fA2266Ab25e75e2A8b3503311656', // WBTC
|
|
562
|
+
'0x9c39809Dec7F95F5e0713634a4D0701329B3b4d2',
|
|
563
|
+
'0x030bA81f1c18d280636F32af80b9AAd02Cf0854e', // WETH
|
|
564
|
+
'0xF63B34710400CAd3e044cFfDcAb00a0f32E33eCf',
|
|
565
|
+
'0xa06bC25B5805d5F8d82847D191Cb4Af5A3e873E0', // LINK
|
|
566
|
+
'0x0b8f12b1788BFdE65Aa1ca52E3e9F3Ba401be16D',
|
|
567
|
+
'0x6C5024Cd4F8A59110119C56f8933403A539555EB', // SUSD
|
|
568
|
+
'0xdC6a3Ab17299D9C2A412B0e0a4C1f55446AE0817',
|
|
569
|
+
'0x5165d24277cD063F5ac44Efd447B27025e888f37', // YFI
|
|
570
|
+
'0x7EbD09022Be45AD993BAA1CEc61166Fcc8644d97',
|
|
571
|
+
'0xF256CC7847E919FAc9B808cC216cAc87CCF2f47a', // xSUSHI
|
|
572
|
+
'0xfAFEDF95E21184E3d880bd56D4806c4b8d31c69A',
|
|
573
|
+
'0xB9D7CB55f463405CDfBe4E90a6D2Df01C2B92BF1', // UNI
|
|
574
|
+
'0x5BdB050A92CADcCfCDcCCBFC17204a1C9cC0Ab73',
|
|
575
|
+
'0xc713e5E149D5D0715DcD1c156a020976e7E56B88', // MKR
|
|
576
|
+
'0xba728eAd5e496BE00DCF66F650b6d7758eCB50f8',
|
|
577
|
+
'0x101cc05f4A51C0319f570d5E146a8C625198e636', // TUSD
|
|
578
|
+
'0x01C0eb1f8c6F1C1bF74ae028697ce7AA2a8b0E92',
|
|
579
|
+
'0xc9BC48c72154ef3e5425641a3c747242112a46AF', // RAI
|
|
580
|
+
'0xB5385132EE8321977FfF44b60cDE9fE9AB0B4e6b',
|
|
581
|
+
'0x272F97b7a56a387aE942350bBC7Df5700f8a4576', // BAL
|
|
582
|
+
'0x13210D4Fe0d5402bd7Ecbc4B5bC5cFcA3b71adB0',
|
|
583
|
+
'0x2e8f4bdbe3d47d7d7de490437aea9915d930f1a3', // USDP
|
|
584
|
+
'0xfdb93b3b10936cf81fa59a02a7523b6e2149b2b7',
|
|
585
|
+
'0xA361718326c15715591c299427c62086F69923D9', // BUSD
|
|
586
|
+
'0xbA429f7011c9fa04cDd46a2Da24dc0FF0aC6099c',
|
|
587
|
+
'0xd4937682df3C8aEF4FE912A96A74121C0829E664', // FRAX
|
|
588
|
+
'0xfE8F19B17fFeF0fDbfe2671F248903055AFAA8Ca',
|
|
589
|
+
'0x683923dB55Fead99A79Fa01A27EeC3cB19679cC3', // FEI
|
|
590
|
+
'0xC2e10006AccAb7B45D9184FcF5b7EC7763f5BaAe',
|
|
591
|
+
'0x8dAE6Cb04688C62d939ed9B68d32Bc62e49970b1', // CRV
|
|
592
|
+
'0x00ad8eBF64F141f1C81e9f8f792d3d1631c6c684',
|
|
593
|
+
'0x6F634c6135D2EBD550000ac92F494F9CB8183dAe', // DPI
|
|
594
|
+
'0x4dDff5885a67E4EffeC55875a3977D7E60F82ae0',
|
|
595
|
+
] as const;
|
|
596
|
+
|
|
597
|
+
export const fetchYearlyMeritApyForStakingGho = async () => {
|
|
598
|
+
try {
|
|
599
|
+
const response = await fetch('https://apps.aavechan.com/api/merit/aprs', { signal: AbortSignal.timeout(5000) });
|
|
600
|
+
const data = await response.json();
|
|
601
|
+
const apr = data?.currentAPR?.actionsAPR?.['ethereum-stkgho'] || '0' as string;
|
|
602
|
+
const apy = aprToApy(apr);
|
|
603
|
+
const apyWithDFSBonus = new Dec(apy).mul(1.05).toString(); // 5% bonus for DFS users
|
|
604
|
+
return apyWithDFSBonus;
|
|
605
|
+
} catch (e) {
|
|
606
|
+
const message = 'External API Failure: Failed to fetch yearly merit APY for staking GHO';
|
|
607
|
+
console.error(message, e);
|
|
608
|
+
return '0';
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
export const getStakeAaveData = async (provider: Client, network: NetworkNumber, address: EthAddress) => {
|
|
613
|
+
const stkGhoAddress = getAssetInfo('stkGHO').address as HexString;
|
|
614
|
+
const stkAaveAddress = getAssetInfo('stkAAVE').address as HexString;
|
|
615
|
+
|
|
616
|
+
const AaveIncentivesController = AaveIncentivesControllerViem(provider, network);
|
|
617
|
+
const stkAAVE = StkAAVEViem(provider, network);
|
|
618
|
+
const stkGHO = createViemContractFromConfigFunc('Erc20', stkGhoAddress as HexString)(provider, network);
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
const [aaveRewardsBalance, emissionsPerSecond, stkAAVEBalance, stkAAVETotalSupply, stkGHOBalance, ghoMeritApy] = await Promise.all([
|
|
622
|
+
AaveIncentivesController.read.getRewardsBalance([REWARDABLE_ASSETS, address]),
|
|
623
|
+
stkAAVE.read.assets([stkAaveAddress]),
|
|
624
|
+
stkAAVE.read.balanceOf([address]),
|
|
625
|
+
stkAAVE.read.totalSupply(),
|
|
626
|
+
stkGHO.read.balanceOf([address]),
|
|
627
|
+
fetchYearlyMeritApyForStakingGho(),
|
|
628
|
+
]);
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
const stkAaveApy = new Dec(assetAmountInEth(emissionsPerSecond[0].toString(), 'GHO') || 0).mul(SECONDS_PER_YEAR).mul(100).div(assetAmountInEth(stkAAVETotalSupply.toString(), 'stkAAVE'))
|
|
632
|
+
.toString();
|
|
633
|
+
return {
|
|
634
|
+
activatedCooldown: '0',
|
|
635
|
+
activatedCooldownAmount: '0',
|
|
636
|
+
stkAaveRewardsBalance: '0',
|
|
637
|
+
aaveRewardsBalance: assetAmountInEth(aaveRewardsBalance.toString(), 'AAVE'),
|
|
638
|
+
stkAaveBalance: assetAmountInEth(stkAAVEBalance.toString(), 'stkAAVE'),
|
|
639
|
+
stkGhoBalance: assetAmountInEth(stkGHOBalance.toString(), 'GHO'),
|
|
640
|
+
ghoMeritApy,
|
|
641
|
+
stkAaveApy,
|
|
642
|
+
};
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
export {
|
|
646
|
+
getMeritCampaigns,
|
|
647
|
+
getMerkleCampaigns,
|
|
648
|
+
};
|