@defisaver/positions-sdk 2.1.96 → 2.1.98-aave-v4-apy-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/aaveV3/index.js +3 -3
- package/cjs/aaveV4/index.js +2 -0
- package/cjs/config/contracts.d.ts +61 -1
- package/cjs/config/contracts.js +2 -2
- package/cjs/contracts.d.ts +2963 -82
- package/cjs/helpers/aaveV4Helpers/index.d.ts +11 -1
- package/cjs/helpers/aaveV4Helpers/index.js +58 -1
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/savings/morphoVaults/index.js +17 -17
- package/cjs/services/utils.d.ts +1 -1
- package/cjs/services/utils.js +2 -1
- package/cjs/types/aaveV4.d.ts +2 -0
- package/esm/aaveV3/index.js +3 -3
- package/esm/aaveV4/index.js +2 -0
- package/esm/config/contracts.d.ts +61 -1
- package/esm/config/contracts.js +2 -2
- package/esm/contracts.d.ts +2963 -82
- package/esm/helpers/aaveV4Helpers/index.d.ts +11 -1
- package/esm/helpers/aaveV4Helpers/index.js +56 -0
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/savings/morphoVaults/index.js +17 -17
- package/esm/services/utils.d.ts +1 -1
- package/esm/services/utils.js +2 -1
- package/esm/types/aaveV4.d.ts +2 -0
- package/package.json +48 -48
- package/src/aaveV2/index.ts +240 -240
- package/src/aaveV3/index.ts +634 -634
- package/src/aaveV3/merit.ts +97 -97
- package/src/aaveV3/merkl.ts +74 -74
- package/src/aaveV4/index.ts +238 -236
- package/src/aaveV4/lend.ts +185 -185
- 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 +245 -245
- package/src/compoundV3/index.ts +280 -280
- package/src/config/contracts.ts +1357 -1357
- package/src/constants/index.ts +10 -10
- package/src/contracts.ts +183 -183
- package/src/curveUsd/index.ts +254 -254
- package/src/eulerV2/index.ts +330 -330
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1800 -1800
- package/src/helpers/aaveHelpers/index.ts +203 -203
- package/src/helpers/aaveV4Helpers/index.ts +349 -291
- package/src/helpers/compoundHelpers/index.ts +280 -280
- package/src/helpers/curveUsdHelpers/index.ts +44 -44
- package/src/helpers/eulerHelpers/index.ts +230 -230
- package/src/helpers/fluidHelpers/index.ts +338 -338
- package/src/helpers/index.ts +11 -11
- package/src/helpers/liquityV2Helpers/index.ts +85 -85
- package/src/helpers/llamaLendHelpers/index.ts +56 -56
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +406 -406
- package/src/helpers/sparkHelpers/index.ts +170 -170
- package/src/index.ts +51 -51
- package/src/liquity/index.ts +161 -161
- package/src/liquityV2/index.ts +703 -703
- package/src/llamaLend/index.ts +305 -305
- package/src/maker/index.ts +225 -225
- package/src/markets/aave/index.ts +118 -118
- package/src/markets/aave/marketAssets.ts +54 -54
- package/src/markets/aaveV4/index.ts +191 -191
- 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 +29 -29
- 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 +1370 -1370
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +12 -12
- package/src/moneymarket/moneymarketCommonService.ts +91 -91
- package/src/morphoBlue/index.ts +274 -274
- package/src/portfolio/index.ts +606 -606
- package/src/savings/index.ts +111 -111
- 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/summerVaults/index.ts +56 -56
- package/src/savings/summerVaults/options.ts +117 -117
- 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 +122 -117
- package/src/services/viem.ts +57 -57
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +460 -460
- package/src/staking/eligibility.ts +105 -105
- package/src/staking/index.ts +1 -1
- package/src/staking/staking.ts +201 -201
- package/src/types/aave.ts +200 -200
- package/src/types/aaveV4.ts +179 -177
- package/src/types/claiming.ts +114 -114
- package/src/types/common.ts +117 -117
- package/src/types/compound.ts +147 -147
- package/src/types/curveUsd.ts +125 -125
- package/src/types/euler.ts +177 -177
- package/src/types/fluid.ts +486 -486
- package/src/types/index.ts +16 -16
- package/src/types/liquity.ts +31 -31
- package/src/types/liquityV2.ts +130 -130
- package/src/types/llamaLend.ts +163 -163
- package/src/types/maker.ts +64 -64
- package/src/types/merit.ts +1 -1
- package/src/types/merkl.ts +70 -70
- package/src/types/morphoBlue.ts +228 -228
- package/src/types/portfolio.ts +64 -64
- package/src/types/savings/index.ts +25 -25
- 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/summerVaults.ts +24 -24
- package/src/types/savings/yearnV3Vaults.ts +17 -17
- package/src/types/savings/yearnVaults.ts +14 -14
- package/src/types/spark.ts +136 -136
- package/src/umbrella/index.ts +69 -69
- package/src/umbrella/umbrellaUtils.ts +29 -29
|
@@ -1,291 +1,349 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
const
|
|
149
|
-
acc.
|
|
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
|
-
|
|
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
|
-
payload.
|
|
248
|
-
payload.
|
|
249
|
-
payload.
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
payload.
|
|
255
|
-
payload.
|
|
256
|
-
payload.
|
|
257
|
-
payload.
|
|
258
|
-
const
|
|
259
|
-
payload.
|
|
260
|
-
payload.
|
|
261
|
-
payload.
|
|
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
|
-
payload.
|
|
289
|
-
payload.
|
|
290
|
-
|
|
291
|
-
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { assetAmountInWei } from '@defisaver/tokens';
|
|
3
|
+
import { Client } from 'viem';
|
|
4
|
+
import {
|
|
5
|
+
aprToApy,
|
|
6
|
+
calcLeverageLiqPrice,
|
|
7
|
+
getAssetsTotal,
|
|
8
|
+
STABLE_ASSETS,
|
|
9
|
+
} from '../../moneymarket';
|
|
10
|
+
import { calculateInterestEarned } from '../../staking';
|
|
11
|
+
import {
|
|
12
|
+
AaveV4AggregatedPositionData,
|
|
13
|
+
AaveV4AssetsData,
|
|
14
|
+
AaveV4ReserveAssetData,
|
|
15
|
+
AaveV4SpokeInfo,
|
|
16
|
+
AaveV4UsedReserveAsset,
|
|
17
|
+
AaveV4UsedReserveAssets,
|
|
18
|
+
EthereumProvider,
|
|
19
|
+
LeverageType,
|
|
20
|
+
NetworkNumber,
|
|
21
|
+
} from '../../types';
|
|
22
|
+
import { borrowOperations } from '../../constants';
|
|
23
|
+
import { AaveV4ViewContractViem } from '../../contracts';
|
|
24
|
+
import { getViemProvider } from '../../services/viem';
|
|
25
|
+
|
|
26
|
+
export const calcUserRiskPremiumBps = (usedAssets: AaveV4UsedReserveAssets, assetsData: AaveV4AssetsData): number => {
|
|
27
|
+
type CollateralInfo = { riskBps: number; valueUsd: Dec };
|
|
28
|
+
type DebtInfo = { valueUsd: Dec };
|
|
29
|
+
|
|
30
|
+
const collaterals: CollateralInfo[] = [];
|
|
31
|
+
const debts: DebtInfo[] = [];
|
|
32
|
+
|
|
33
|
+
Object.entries(usedAssets).forEach(([identifier, asset]) => {
|
|
34
|
+
const reserveData = assetsData[identifier];
|
|
35
|
+
if (!reserveData) return;
|
|
36
|
+
|
|
37
|
+
const borrowedUsdDec = new Dec(asset.borrowedUsd || '0');
|
|
38
|
+
if (asset.isBorrowed && borrowedUsdDec.gt(0)) {
|
|
39
|
+
debts.push({ valueUsd: borrowedUsdDec });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const suppliedUsdDec = new Dec(asset.suppliedUsd || '0');
|
|
43
|
+
const isActiveCollateral = asset.collateral
|
|
44
|
+
&& asset.isSupplied
|
|
45
|
+
&& asset.collateralFactor > 0
|
|
46
|
+
&& suppliedUsdDec.gt(0);
|
|
47
|
+
|
|
48
|
+
if (isActiveCollateral) {
|
|
49
|
+
// collateralRisk is stored as a fraction (e.g. 0.25), convert back to bps
|
|
50
|
+
const riskBps = new Dec(reserveData.collateralRisk).mul(10000).toNumber();
|
|
51
|
+
collaterals.push({
|
|
52
|
+
riskBps,
|
|
53
|
+
valueUsd: suppliedUsdDec,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const totalDebtUsd = debts.reduce((sum, d) => sum.add(d.valueUsd), new Dec(0));
|
|
59
|
+
|
|
60
|
+
if (totalDebtUsd.lte(0)) {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// sort by risk ASC, value DESC
|
|
65
|
+
collaterals.sort((a, b) => {
|
|
66
|
+
if (a.riskBps !== b.riskBps) return a.riskBps - b.riskBps;
|
|
67
|
+
return b.valueUsd.comparedTo(a.valueUsd);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
let debtLeftToCover = totalDebtUsd;
|
|
71
|
+
let numerator = new Dec(0); // sum(coveredUsd * riskBps)
|
|
72
|
+
let coveredDebt = new Dec(0); // sum(coveredUsd)
|
|
73
|
+
|
|
74
|
+
collaterals.forEach(({ riskBps, valueUsd }) => {
|
|
75
|
+
if (debtLeftToCover.lte(0)) return;
|
|
76
|
+
|
|
77
|
+
const coveredUsd = Dec.min(valueUsd, debtLeftToCover);
|
|
78
|
+
|
|
79
|
+
numerator = numerator.add(coveredUsd.mul(riskBps));
|
|
80
|
+
coveredDebt = coveredDebt.add(coveredUsd);
|
|
81
|
+
|
|
82
|
+
debtLeftToCover = debtLeftToCover.sub(coveredUsd);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
if (coveredDebt.lte(0)) {
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const riskPremiumBps = numerator.div(coveredDebt);
|
|
90
|
+
return riskPremiumBps.toNumber();
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const getApyAfterValuesEstimation = (
|
|
94
|
+
usedAssets: AaveV4UsedReserveAssets,
|
|
95
|
+
assetsData: AaveV4AssetsData,
|
|
96
|
+
): Record<string, { borrowRate: string; supplyRate: string }> => {
|
|
97
|
+
const riskPremiumBps = calcUserRiskPremiumBps(usedAssets, assetsData);
|
|
98
|
+
const riskPremiumFraction = new Dec(riskPremiumBps).div(10000); // bps to fraction
|
|
99
|
+
|
|
100
|
+
const result: Record<string, { borrowRate: string; supplyRate: string }> = {};
|
|
101
|
+
|
|
102
|
+
Object.entries(assetsData).forEach(([identifier, assetData]) => {
|
|
103
|
+
const drawnRate = new Dec(assetData.drawnRate);
|
|
104
|
+
const baseBorrowApr = drawnRate.mul(100);
|
|
105
|
+
// finalBorrowRate = baseBorrowRate * (1 + riskPremiumFraction)
|
|
106
|
+
const userBorrowApr = baseBorrowApr.mul(new Dec(1).add(riskPremiumFraction));
|
|
107
|
+
|
|
108
|
+
result[identifier] = {
|
|
109
|
+
borrowRate: aprToApy(userBorrowApr.toString()),
|
|
110
|
+
// Supply rate is market-level (not user-specific), use existing value
|
|
111
|
+
supplyRate: assetData.supplyRate,
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return result;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const calculateNetApyAaveV4 = ({
|
|
119
|
+
usedAssets,
|
|
120
|
+
assetsData,
|
|
121
|
+
}: {
|
|
122
|
+
usedAssets: AaveV4UsedReserveAssets,
|
|
123
|
+
assetsData: AaveV4AssetsData,
|
|
124
|
+
}) => {
|
|
125
|
+
const riskPremiumBps = calcUserRiskPremiumBps(usedAssets, assetsData);
|
|
126
|
+
const riskPremiumFraction = new Dec(riskPremiumBps).div(10000);
|
|
127
|
+
|
|
128
|
+
const sumValues = Object.entries(usedAssets).reduce((_acc, [identifier, usedAsset]) => {
|
|
129
|
+
const acc = { ..._acc };
|
|
130
|
+
const assetData = assetsData[identifier];
|
|
131
|
+
if (!assetData) return acc;
|
|
132
|
+
|
|
133
|
+
if (usedAsset.isSupplied) {
|
|
134
|
+
const amount = usedAsset.suppliedUsd;
|
|
135
|
+
acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
|
|
136
|
+
const supplyInterest = calculateInterestEarned(amount, assetData.supplyRate, 'year', true);
|
|
137
|
+
acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
|
|
138
|
+
|
|
139
|
+
if (assetData.supplyIncentives) {
|
|
140
|
+
for (const supplyIncentive of assetData.supplyIncentives) {
|
|
141
|
+
const incentiveInterest = calculateInterestEarned(amount, supplyIncentive.apy, 'year', true);
|
|
142
|
+
acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (usedAsset.isBorrowed) {
|
|
148
|
+
const amount = usedAsset.borrowedUsd;
|
|
149
|
+
acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
|
|
150
|
+
// User borrow rate = baseRate * (1 + riskPremiumFraction)
|
|
151
|
+
const drawnRate = new Dec(assetData.drawnRate);
|
|
152
|
+
const baseBorrowApr = drawnRate.mul(100);
|
|
153
|
+
const userBorrowApr = baseBorrowApr.mul(new Dec(1).add(riskPremiumFraction));
|
|
154
|
+
const userBorrowRate = aprToApy(userBorrowApr.toString());
|
|
155
|
+
const borrowInterest = calculateInterestEarned(amount, userBorrowRate, 'year', true);
|
|
156
|
+
acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
|
|
157
|
+
|
|
158
|
+
if (assetData.borrowIncentives) {
|
|
159
|
+
for (const borrowIncentive of assetData.borrowIncentives) {
|
|
160
|
+
const incentiveInterest = calculateInterestEarned(amount, borrowIncentive.apy, 'year', true);
|
|
161
|
+
acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return acc;
|
|
167
|
+
}, {
|
|
168
|
+
borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const {
|
|
172
|
+
borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
|
|
173
|
+
} = sumValues;
|
|
174
|
+
|
|
175
|
+
const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
|
|
176
|
+
const balance = new Dec(suppliedUsd).sub(borrowedUsd);
|
|
177
|
+
const netApy = balance.isZero() ? '0' : new Dec(totalInterestUsd).div(balance).times(100).toString();
|
|
178
|
+
|
|
179
|
+
return { netApy, totalInterestUsd, incentiveUsd };
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
export const aaveV4GetCollateralFactor = (assetData: AaveV4ReserveAssetData, usedAssetData: AaveV4UsedReserveAsset, useUserCollateralFactor: boolean = false): number => (useUserCollateralFactor ? usedAssetData.collateralFactor : assetData.collateralFactor);
|
|
183
|
+
|
|
184
|
+
export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLimit = 5) => {
|
|
185
|
+
let borrowUnstable = 0;
|
|
186
|
+
let supplyStable = 0;
|
|
187
|
+
let borrowStable = 0;
|
|
188
|
+
let supplyUnstable = 0;
|
|
189
|
+
let longAsset = '';
|
|
190
|
+
let shortAsset = '';
|
|
191
|
+
Object.values(usedAssets).forEach(({
|
|
192
|
+
symbol, suppliedUsd, borrowedUsd, collateral, reserveId,
|
|
193
|
+
}) => {
|
|
194
|
+
const spokeAsset = `${symbol}-${reserveId}`;
|
|
195
|
+
const isSupplied = (+suppliedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
196
|
+
const isBorrowed = (+borrowedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
197
|
+
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral) supplyStable += 1;
|
|
198
|
+
if (isBorrowed && STABLE_ASSETS.includes(symbol)) borrowStable += 1;
|
|
199
|
+
if (isBorrowed && !STABLE_ASSETS.includes(symbol)) {
|
|
200
|
+
borrowUnstable += 1;
|
|
201
|
+
shortAsset = spokeAsset;
|
|
202
|
+
}
|
|
203
|
+
if (isSupplied && !STABLE_ASSETS.includes(symbol) && collateral) {
|
|
204
|
+
supplyUnstable += 1;
|
|
205
|
+
longAsset = spokeAsset;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
const isLong = borrowStable > 0 && borrowUnstable === 0 && supplyUnstable === 1 && supplyStable === 0;
|
|
209
|
+
const isShort = supplyStable > 0 && supplyUnstable === 0 && borrowUnstable === 1 && borrowStable === 0;
|
|
210
|
+
const isVolatilePair = supplyUnstable === 1 && borrowUnstable === 1 && supplyStable === 0 && borrowStable === 0;
|
|
211
|
+
if (isLong) {
|
|
212
|
+
return {
|
|
213
|
+
leveragedType: LeverageType.Long,
|
|
214
|
+
leveragedAsset: longAsset,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (isShort) {
|
|
218
|
+
return {
|
|
219
|
+
leveragedType: LeverageType.Short,
|
|
220
|
+
leveragedAsset: shortAsset,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
if (isVolatilePair) {
|
|
224
|
+
return {
|
|
225
|
+
leveragedType: LeverageType.VolatilePair,
|
|
226
|
+
leveragedAsset: longAsset,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
leveragedType: LeverageType.None,
|
|
231
|
+
leveragedAsset: '',
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
export const aaveV4GetAggregatedPositionData = ({
|
|
236
|
+
usedAssets,
|
|
237
|
+
assetsData,
|
|
238
|
+
network,
|
|
239
|
+
useUserCollateralFactor = false,
|
|
240
|
+
}: {
|
|
241
|
+
usedAssets: AaveV4UsedReserveAssets,
|
|
242
|
+
assetsData: AaveV4AssetsData,
|
|
243
|
+
network: NetworkNumber,
|
|
244
|
+
useUserCollateralFactor?: boolean,
|
|
245
|
+
}): AaveV4AggregatedPositionData => {
|
|
246
|
+
const payload = {} as AaveV4AggregatedPositionData;
|
|
247
|
+
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
248
|
+
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
249
|
+
payload.borrowLimitUsd = getAssetsTotal(
|
|
250
|
+
usedAssets,
|
|
251
|
+
({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral,
|
|
252
|
+
({ symbol, suppliedUsd, reserveId }: { symbol: string, suppliedUsd: string, reserveId: number }) => new Dec(suppliedUsd).mul(aaveV4GetCollateralFactor(assetsData[`${symbol}-${reserveId}`], usedAssets[`${symbol}-${reserveId}`], useUserCollateralFactor)),
|
|
253
|
+
);
|
|
254
|
+
payload.liquidationLimitUsd = payload.borrowLimitUsd;
|
|
255
|
+
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
256
|
+
payload.drawnUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ drawnUsd }: { drawnUsd: string }) => drawnUsd);
|
|
257
|
+
payload.premiumUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ premiumUsd }: { premiumUsd: string }) => premiumUsd);
|
|
258
|
+
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
259
|
+
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
260
|
+
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
261
|
+
payload.minRatio = '100';
|
|
262
|
+
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
263
|
+
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
264
|
+
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
265
|
+
const { leveragedType, leveragedAsset } = isLeveragedPosAaveV4(usedAssets);
|
|
266
|
+
payload.leveragedType = leveragedType;
|
|
267
|
+
payload.leveragedAsset = leveragedAsset;
|
|
268
|
+
payload.liquidationPrice = '';
|
|
269
|
+
if (leveragedType !== '') {
|
|
270
|
+
const leveragedAssetData = assetsData[leveragedAsset];
|
|
271
|
+
let assetPrice = leveragedAssetData?.price || '0';
|
|
272
|
+
if (leveragedType === LeverageType.VolatilePair) {
|
|
273
|
+
const borrowedAsset = (Object.values(usedAssets) as AaveV4UsedReserveAsset[]).find(({ borrowedUsd }: { borrowedUsd: string }) => +borrowedUsd > 0);
|
|
274
|
+
const borrowedAssetPrice = assetsData[`${borrowedAsset!.symbol}-${borrowedAsset!.reserveId}`].price;
|
|
275
|
+
const leveragedAssetPrice = assetsData[leveragedAsset].price;
|
|
276
|
+
const isReverse = new Dec(leveragedAssetPrice).lt(borrowedAssetPrice);
|
|
277
|
+
if (isReverse) {
|
|
278
|
+
payload.leveragedType = LeverageType.VolatilePairReverse;
|
|
279
|
+
payload.currentVolatilePairRatio = new Dec(borrowedAssetPrice).div(leveragedAssetPrice).toDP(18).toString();
|
|
280
|
+
assetPrice = new Dec(borrowedAssetPrice).div(assetPrice).toString();
|
|
281
|
+
} else {
|
|
282
|
+
assetPrice = new Dec(assetPrice).div(borrowedAssetPrice).toString();
|
|
283
|
+
payload.currentVolatilePairRatio = new Dec(leveragedAssetPrice).div(borrowedAssetPrice).toDP(18).toString();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
payload.liquidationPrice = calcLeverageLiqPrice(payload.leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
287
|
+
}
|
|
288
|
+
payload.minCollRatio = new Dec(payload.suppliedCollateralUsd).div(payload.borrowLimitUsd).mul(100).toString();
|
|
289
|
+
payload.collLiquidationRatio = new Dec(payload.suppliedCollateralUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
290
|
+
payload.healthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowedUsd).toDP(4).toString();
|
|
291
|
+
payload.minHealthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowLimitUsd).toDP(4).toString();
|
|
292
|
+
|
|
293
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApyAaveV4({ usedAssets, assetsData });
|
|
294
|
+
payload.netApy = netApy;
|
|
295
|
+
payload.incentiveUsd = incentiveUsd;
|
|
296
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
297
|
+
return payload;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const getAaveV4ApyAfterValuesEstimationInner = async (selectedSpoke: AaveV4SpokeInfo, assetsData: AaveV4AssetsData, actions: [{ action: string, amount: string, asset: string }], client: Client, network: NetworkNumber) => {
|
|
301
|
+
const params = actions.map(({ action, asset, amount }) => {
|
|
302
|
+
const isDebtAsset = borrowOperations.includes(action);
|
|
303
|
+
const assetData = assetsData[asset];
|
|
304
|
+
const amountInWei = assetAmountInWei(amount, assetData.symbol);
|
|
305
|
+
let liquidityAdded;
|
|
306
|
+
let liquidityTaken;
|
|
307
|
+
if (isDebtAsset) {
|
|
308
|
+
liquidityAdded = action === 'payback' ? amountInWei : '0';
|
|
309
|
+
liquidityTaken = action === 'borrow' ? amountInWei : '0';
|
|
310
|
+
} else {
|
|
311
|
+
liquidityAdded = action === 'collateral' ? amountInWei : '0';
|
|
312
|
+
liquidityTaken = action === 'withdraw' ? amountInWei : '0';
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
reserveId: BigInt(assetData.reserveId),
|
|
316
|
+
liquidityAdded: BigInt(liquidityAdded),
|
|
317
|
+
liquidityTaken: BigInt(liquidityTaken),
|
|
318
|
+
isDebtAsset,
|
|
319
|
+
};
|
|
320
|
+
});
|
|
321
|
+
const viewContract = AaveV4ViewContractViem(client, network);
|
|
322
|
+
const data = await viewContract.read.getApyAfterValuesEstimation([selectedSpoke.address, params]);
|
|
323
|
+
|
|
324
|
+
const rates: { [key: string]: { supplyRate: string, borrowRate: string } } = {};
|
|
325
|
+
data.forEach((item: any) => {
|
|
326
|
+
const {
|
|
327
|
+
hubDrawnRateEstimation, hubTotalDrawnEstimation, hubTotalLiquidityEstimation, hubSwept, liquidityFee, reserveId,
|
|
328
|
+
} = item;
|
|
329
|
+
const drawnRate = new Dec(hubDrawnRateEstimation.toString()).div(new Dec(10).pow(27));
|
|
330
|
+
const borrowApr = drawnRate.mul(100);
|
|
331
|
+
|
|
332
|
+
const assetData = Object.values(assetsData).find(({ reserveId: rId }) => rId === Number(reserveId));
|
|
333
|
+
|
|
334
|
+
const totalDrawn = new Dec(hubTotalDrawnEstimation.toString());
|
|
335
|
+
const swept = new Dec(hubSwept.toString());
|
|
336
|
+
const hubUtilizationDenominator = totalDrawn.add(swept).add(hubTotalLiquidityEstimation.toString());
|
|
337
|
+
const hubUtilization = hubUtilizationDenominator.isZero() ? new Dec(0) : totalDrawn.div(hubUtilizationDenominator);
|
|
338
|
+
|
|
339
|
+
const supplyApr = borrowApr.mul(hubUtilization).mul(assetData?.premiumMultiplier || '0').mul(new Dec(1).minus(assetData?.liquidityFee || '0'));
|
|
340
|
+
|
|
341
|
+
rates[`${assetData?.symbol}-${assetData?.reserveId}`] = {
|
|
342
|
+
borrowRate: aprToApy(borrowApr.toString()),
|
|
343
|
+
supplyRate: aprToApy(supplyApr.toString()),
|
|
344
|
+
};
|
|
345
|
+
});
|
|
346
|
+
return rates;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export const getAaveV4ApyAfterValuesEstimation = async (selectedSpoke: AaveV4SpokeInfo, assetsData: AaveV4AssetsData, actions: [{ action: string, amount: string, asset: string }], provider: EthereumProvider, network: NetworkNumber) => getAaveV4ApyAfterValuesEstimationInner(selectedSpoke, assetsData, actions, getViemProvider(provider, network), network);
|