@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
package/src/aaveV4/index.ts
CHANGED
|
@@ -1,237 +1,239 @@
|
|
|
1
|
-
import { Client } from 'viem';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
-
import { getViemProvider } from '../services/viem';
|
|
5
|
-
import {
|
|
6
|
-
AaveV4AccountData,
|
|
7
|
-
AaveV4HubAssetOnChainData,
|
|
8
|
-
AaveV4HubOnChainData,
|
|
9
|
-
AaveV4ReserveAssetData,
|
|
10
|
-
AaveV4ReserveAssetOnChain,
|
|
11
|
-
AaveV4SpokeData,
|
|
12
|
-
AaveV4SpokeInfo,
|
|
13
|
-
AaveV4UsedReserveAssets,
|
|
14
|
-
EthAddress,
|
|
15
|
-
EthereumProvider,
|
|
16
|
-
IncentiveData,
|
|
17
|
-
IncentiveKind,
|
|
18
|
-
NetworkNumber,
|
|
19
|
-
} from '../types';
|
|
20
|
-
import { AaveV4ViewContractViem } from '../contracts';
|
|
21
|
-
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
22
|
-
import { isMaxUint, wethToEth } from '../services/utils';
|
|
23
|
-
import { aaveV4GetAggregatedPositionData, calcUserRiskPremiumBps } from '../helpers/aaveV4Helpers';
|
|
24
|
-
import { getAaveV4HubByAddress } from '../markets/aaveV4';
|
|
25
|
-
import { aprToApy } from '../moneymarket';
|
|
26
|
-
|
|
27
|
-
export * as lend from './lend';
|
|
28
|
-
|
|
29
|
-
const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractViem>, hubAddress: EthAddress): Promise<AaveV4HubOnChainData> => {
|
|
30
|
-
const hubData = await viewContract.read.getHubAllAssetsData([hubAddress]);
|
|
31
|
-
return {
|
|
32
|
-
assets: hubData.reduce((acc: Record<number, AaveV4HubAssetOnChainData>, assetOnChainData) => {
|
|
33
|
-
acc[assetOnChainData.assetId] = {
|
|
34
|
-
assetId: assetOnChainData.assetId,
|
|
35
|
-
drawnRate: assetOnChainData.drawnRate,
|
|
36
|
-
liquidity: assetOnChainData.liquidity,
|
|
37
|
-
liquidityFee: assetOnChainData.liquidityFee,
|
|
38
|
-
swept: assetOnChainData.swept,
|
|
39
|
-
totalDrawn: assetOnChainData.totalDrawn,
|
|
40
|
-
totalDrawnShares: assetOnChainData.totalDrawnShares,
|
|
41
|
-
totalPremiumShares: assetOnChainData.totalPremiumShares,
|
|
42
|
-
};
|
|
43
|
-
return acc;
|
|
44
|
-
}, {}),
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAsset: AaveV4HubAssetOnChainData, reserveId: number, oracleDecimals: number, network: NetworkNumber): Promise<AaveV4ReserveAssetData> => {
|
|
50
|
-
const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
|
|
51
|
-
const symbol = wethToEth(assetInfo.symbol);
|
|
52
|
-
const hubInfo = getAaveV4HubByAddress(network, reserveAsset.hub);
|
|
53
|
-
if (!hubInfo) {
|
|
54
|
-
throw new Error(`Hub not found with address: ${reserveAsset.hub}`);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const isStakingAsset = STAKING_ASSETS.includes(symbol);
|
|
58
|
-
const supplyIncentives: IncentiveData[] = [];
|
|
59
|
-
const borrowIncentives: IncentiveData[] = [];
|
|
60
|
-
|
|
61
|
-
if (isStakingAsset) {
|
|
62
|
-
const yieldApy = await getStakingApy(symbol, network as NetworkNumber);
|
|
63
|
-
supplyIncentives.push({
|
|
64
|
-
apy: yieldApy,
|
|
65
|
-
token: symbol,
|
|
66
|
-
incentiveKind: IncentiveKind.Staking,
|
|
67
|
-
description: `Native ${symbol} yield.`,
|
|
68
|
-
});
|
|
69
|
-
if (reserveAsset.borrowable) {
|
|
70
|
-
// When borrowing assets whose value increases over time
|
|
71
|
-
borrowIncentives.push({
|
|
72
|
-
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
73
|
-
token: symbol,
|
|
74
|
-
incentiveKind: IncentiveKind.Reward,
|
|
75
|
-
description: `Due to the native yield of ${symbol}, the value of the debt would increase over time.`,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const totalSuppliedRaw = reserveAsset.totalSupplied ?? 0;
|
|
81
|
-
const totalDrawnRaw = reserveAsset.totalDrawn ?? 0;
|
|
82
|
-
const totalPremiumRaw = reserveAsset.totalPremium ?? 0;
|
|
83
|
-
const totalDebtRaw = reserveAsset.totalDebt ?? 0;
|
|
84
|
-
const supplyCapRaw = reserveAsset.supplyCap ?? 0;
|
|
85
|
-
const borrowCapRaw = reserveAsset.borrowCap ?? 0;
|
|
86
|
-
|
|
87
|
-
/** @DEV Hub related calculations */
|
|
88
|
-
const drawnRate = new Dec(hubAsset.drawnRate.toString()).div(new Dec(10).pow(27));
|
|
89
|
-
const borrowApr = drawnRate.mul(100);
|
|
90
|
-
const totalDrawn = new Dec(hubAsset.totalDrawn.toString());
|
|
91
|
-
const liquidity = new Dec(hubAsset.liquidity.toString());
|
|
92
|
-
const swept = new Dec(hubAsset.swept.toString());
|
|
93
|
-
const hubUtilizationDenominator = totalDrawn.add(swept).add(liquidity);
|
|
94
|
-
const hubUtilization = hubUtilizationDenominator.isZero() ? new Dec(0) : totalDrawn.div(hubUtilizationDenominator);
|
|
95
|
-
const liquidityFee = new Dec(hubAsset.liquidityFee.toString()).div(new Dec(10).pow(4));
|
|
96
|
-
const totalDrawnShares = new Dec(hubAsset.totalDrawnShares.toString());
|
|
97
|
-
const totalPremiumShares = new Dec(hubAsset.totalPremiumShares.toString());
|
|
98
|
-
const premiumMultiplier = totalDrawnShares.isZero() ? new Dec(1) : totalDrawnShares.add(totalPremiumShares).div(totalDrawnShares);
|
|
99
|
-
const supplyApr = borrowApr.mul(hubUtilization).mul(premiumMultiplier).mul(new Dec(1).minus(liquidityFee));
|
|
100
|
-
const utilization = hubUtilization.times(100).toString();
|
|
101
|
-
|
|
102
|
-
const hubLiquidityRaw = hubAsset.liquidity;
|
|
103
|
-
const hubLiquidity = isMaxUint(hubLiquidityRaw.toString()) ? hubLiquidityRaw.toString() : assetAmountInEth(hubLiquidityRaw.toString(), symbol);
|
|
104
|
-
|
|
105
|
-
return ({
|
|
106
|
-
symbol,
|
|
107
|
-
underlying: reserveAsset.underlying,
|
|
108
|
-
hub: hubInfo.address,
|
|
109
|
-
hubName: hubInfo?.label,
|
|
110
|
-
assetId: reserveAsset.assetId,
|
|
111
|
-
reserveId,
|
|
112
|
-
paused: reserveAsset.paused,
|
|
113
|
-
frozen: reserveAsset.frozen,
|
|
114
|
-
borrowable: reserveAsset.borrowable,
|
|
115
|
-
collateralRisk: new Dec(reserveAsset.collateralRisk).div(10000).toNumber(),
|
|
116
|
-
collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
|
|
117
|
-
liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
|
|
118
|
-
maxLiquidationBonus: new Dec(reserveAsset.maxLiquidationBonus).div(10000).toNumber(),
|
|
119
|
-
price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
|
|
120
|
-
totalSupplied: isMaxUint(totalSuppliedRaw.toString()) ? totalSuppliedRaw.toString() : assetAmountInEth(totalSuppliedRaw.toString(), symbol),
|
|
121
|
-
totalDrawn: isMaxUint(totalDrawnRaw.toString()) ? totalDrawnRaw.toString() : assetAmountInEth(totalDrawnRaw.toString(), symbol),
|
|
122
|
-
totalPremium: isMaxUint(totalPremiumRaw.toString()) ? totalPremiumRaw.toString() : assetAmountInEth(totalPremiumRaw.toString(), symbol),
|
|
123
|
-
totalDebt: isMaxUint(totalDebtRaw.toString()) ? totalDebtRaw.toString() : assetAmountInEth(totalDebtRaw.toString(), symbol),
|
|
124
|
-
supplyCap: isMaxUint(supplyCapRaw.toString()) ? supplyCapRaw.toString() : assetAmountInEth(supplyCapRaw.toString(), symbol),
|
|
125
|
-
borrowCap: isMaxUint(borrowCapRaw.toString()) ? borrowCapRaw.toString() : assetAmountInEth(borrowCapRaw.toString(), symbol),
|
|
126
|
-
spokeActive: reserveAsset.spokeActive,
|
|
127
|
-
spokeHalted: reserveAsset.spokeHalted,
|
|
128
|
-
drawnRate: drawnRate.toString(),
|
|
129
|
-
borrowRate: aprToApy(borrowApr.toString()),
|
|
130
|
-
supplyRate: aprToApy(supplyApr.toString()),
|
|
131
|
-
supplyIncentives,
|
|
132
|
-
borrowIncentives,
|
|
133
|
-
canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen && reserveAsset.borrowable,
|
|
134
|
-
canBeSupplied: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
|
|
135
|
-
canBeWithdrawn: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
136
|
-
canBePayBacked: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
137
|
-
utilization,
|
|
138
|
-
hubLiquidity,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
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
|
-
const
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
const
|
|
182
|
-
const
|
|
183
|
-
const
|
|
184
|
-
const
|
|
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
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
1
|
+
import { Client } from 'viem';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
+
import { getViemProvider } from '../services/viem';
|
|
5
|
+
import {
|
|
6
|
+
AaveV4AccountData,
|
|
7
|
+
AaveV4HubAssetOnChainData,
|
|
8
|
+
AaveV4HubOnChainData,
|
|
9
|
+
AaveV4ReserveAssetData,
|
|
10
|
+
AaveV4ReserveAssetOnChain,
|
|
11
|
+
AaveV4SpokeData,
|
|
12
|
+
AaveV4SpokeInfo,
|
|
13
|
+
AaveV4UsedReserveAssets,
|
|
14
|
+
EthAddress,
|
|
15
|
+
EthereumProvider,
|
|
16
|
+
IncentiveData,
|
|
17
|
+
IncentiveKind,
|
|
18
|
+
NetworkNumber,
|
|
19
|
+
} from '../types';
|
|
20
|
+
import { AaveV4ViewContractViem } from '../contracts';
|
|
21
|
+
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
22
|
+
import { isMaxUint, wethToEth } from '../services/utils';
|
|
23
|
+
import { aaveV4GetAggregatedPositionData, calcUserRiskPremiumBps } from '../helpers/aaveV4Helpers';
|
|
24
|
+
import { getAaveV4HubByAddress } from '../markets/aaveV4';
|
|
25
|
+
import { aprToApy } from '../moneymarket';
|
|
26
|
+
|
|
27
|
+
export * as lend from './lend';
|
|
28
|
+
|
|
29
|
+
const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractViem>, hubAddress: EthAddress): Promise<AaveV4HubOnChainData> => {
|
|
30
|
+
const hubData = await viewContract.read.getHubAllAssetsData([hubAddress]);
|
|
31
|
+
return {
|
|
32
|
+
assets: hubData.reduce((acc: Record<number, AaveV4HubAssetOnChainData>, assetOnChainData) => {
|
|
33
|
+
acc[assetOnChainData.assetId] = {
|
|
34
|
+
assetId: assetOnChainData.assetId,
|
|
35
|
+
drawnRate: assetOnChainData.drawnRate,
|
|
36
|
+
liquidity: assetOnChainData.liquidity,
|
|
37
|
+
liquidityFee: assetOnChainData.liquidityFee,
|
|
38
|
+
swept: assetOnChainData.swept,
|
|
39
|
+
totalDrawn: assetOnChainData.totalDrawn,
|
|
40
|
+
totalDrawnShares: assetOnChainData.totalDrawnShares,
|
|
41
|
+
totalPremiumShares: assetOnChainData.totalPremiumShares,
|
|
42
|
+
};
|
|
43
|
+
return acc;
|
|
44
|
+
}, {}),
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAsset: AaveV4HubAssetOnChainData, reserveId: number, oracleDecimals: number, network: NetworkNumber): Promise<AaveV4ReserveAssetData> => {
|
|
50
|
+
const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
|
|
51
|
+
const symbol = wethToEth(assetInfo.symbol);
|
|
52
|
+
const hubInfo = getAaveV4HubByAddress(network, reserveAsset.hub);
|
|
53
|
+
if (!hubInfo) {
|
|
54
|
+
throw new Error(`Hub not found with address: ${reserveAsset.hub}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const isStakingAsset = STAKING_ASSETS.includes(symbol);
|
|
58
|
+
const supplyIncentives: IncentiveData[] = [];
|
|
59
|
+
const borrowIncentives: IncentiveData[] = [];
|
|
60
|
+
|
|
61
|
+
if (isStakingAsset) {
|
|
62
|
+
const yieldApy = await getStakingApy(symbol, network as NetworkNumber);
|
|
63
|
+
supplyIncentives.push({
|
|
64
|
+
apy: yieldApy,
|
|
65
|
+
token: symbol,
|
|
66
|
+
incentiveKind: IncentiveKind.Staking,
|
|
67
|
+
description: `Native ${symbol} yield.`,
|
|
68
|
+
});
|
|
69
|
+
if (reserveAsset.borrowable) {
|
|
70
|
+
// When borrowing assets whose value increases over time
|
|
71
|
+
borrowIncentives.push({
|
|
72
|
+
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
73
|
+
token: symbol,
|
|
74
|
+
incentiveKind: IncentiveKind.Reward,
|
|
75
|
+
description: `Due to the native yield of ${symbol}, the value of the debt would increase over time.`,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const totalSuppliedRaw = reserveAsset.totalSupplied ?? 0;
|
|
81
|
+
const totalDrawnRaw = reserveAsset.totalDrawn ?? 0;
|
|
82
|
+
const totalPremiumRaw = reserveAsset.totalPremium ?? 0;
|
|
83
|
+
const totalDebtRaw = reserveAsset.totalDebt ?? 0;
|
|
84
|
+
const supplyCapRaw = reserveAsset.supplyCap ?? 0;
|
|
85
|
+
const borrowCapRaw = reserveAsset.borrowCap ?? 0;
|
|
86
|
+
|
|
87
|
+
/** @DEV Hub related calculations */
|
|
88
|
+
const drawnRate = new Dec(hubAsset.drawnRate.toString()).div(new Dec(10).pow(27));
|
|
89
|
+
const borrowApr = drawnRate.mul(100);
|
|
90
|
+
const totalDrawn = new Dec(hubAsset.totalDrawn.toString());
|
|
91
|
+
const liquidity = new Dec(hubAsset.liquidity.toString());
|
|
92
|
+
const swept = new Dec(hubAsset.swept.toString());
|
|
93
|
+
const hubUtilizationDenominator = totalDrawn.add(swept).add(liquidity);
|
|
94
|
+
const hubUtilization = hubUtilizationDenominator.isZero() ? new Dec(0) : totalDrawn.div(hubUtilizationDenominator);
|
|
95
|
+
const liquidityFee = new Dec(hubAsset.liquidityFee.toString()).div(new Dec(10).pow(4));
|
|
96
|
+
const totalDrawnShares = new Dec(hubAsset.totalDrawnShares.toString());
|
|
97
|
+
const totalPremiumShares = new Dec(hubAsset.totalPremiumShares.toString());
|
|
98
|
+
const premiumMultiplier = totalDrawnShares.isZero() ? new Dec(1) : totalDrawnShares.add(totalPremiumShares).div(totalDrawnShares);
|
|
99
|
+
const supplyApr = borrowApr.mul(hubUtilization).mul(premiumMultiplier).mul(new Dec(1).minus(liquidityFee));
|
|
100
|
+
const utilization = hubUtilization.times(100).toString();
|
|
101
|
+
|
|
102
|
+
const hubLiquidityRaw = hubAsset.liquidity;
|
|
103
|
+
const hubLiquidity = isMaxUint(hubLiquidityRaw.toString()) ? hubLiquidityRaw.toString() : assetAmountInEth(hubLiquidityRaw.toString(), symbol);
|
|
104
|
+
|
|
105
|
+
return ({
|
|
106
|
+
symbol,
|
|
107
|
+
underlying: reserveAsset.underlying,
|
|
108
|
+
hub: hubInfo.address,
|
|
109
|
+
hubName: hubInfo?.label,
|
|
110
|
+
assetId: reserveAsset.assetId,
|
|
111
|
+
reserveId,
|
|
112
|
+
paused: reserveAsset.paused,
|
|
113
|
+
frozen: reserveAsset.frozen,
|
|
114
|
+
borrowable: reserveAsset.borrowable,
|
|
115
|
+
collateralRisk: new Dec(reserveAsset.collateralRisk).div(10000).toNumber(),
|
|
116
|
+
collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
|
|
117
|
+
liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
|
|
118
|
+
maxLiquidationBonus: new Dec(reserveAsset.maxLiquidationBonus).div(10000).toNumber(),
|
|
119
|
+
price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
|
|
120
|
+
totalSupplied: isMaxUint(totalSuppliedRaw.toString()) ? totalSuppliedRaw.toString() : assetAmountInEth(totalSuppliedRaw.toString(), symbol),
|
|
121
|
+
totalDrawn: isMaxUint(totalDrawnRaw.toString()) ? totalDrawnRaw.toString() : assetAmountInEth(totalDrawnRaw.toString(), symbol),
|
|
122
|
+
totalPremium: isMaxUint(totalPremiumRaw.toString()) ? totalPremiumRaw.toString() : assetAmountInEth(totalPremiumRaw.toString(), symbol),
|
|
123
|
+
totalDebt: isMaxUint(totalDebtRaw.toString()) ? totalDebtRaw.toString() : assetAmountInEth(totalDebtRaw.toString(), symbol),
|
|
124
|
+
supplyCap: isMaxUint(supplyCapRaw.toString()) ? supplyCapRaw.toString() : assetAmountInEth(supplyCapRaw.toString(), symbol),
|
|
125
|
+
borrowCap: isMaxUint(borrowCapRaw.toString()) ? borrowCapRaw.toString() : assetAmountInEth(borrowCapRaw.toString(), symbol),
|
|
126
|
+
spokeActive: reserveAsset.spokeActive,
|
|
127
|
+
spokeHalted: reserveAsset.spokeHalted,
|
|
128
|
+
drawnRate: drawnRate.toString(),
|
|
129
|
+
borrowRate: aprToApy(borrowApr.toString()),
|
|
130
|
+
supplyRate: aprToApy(supplyApr.toString()),
|
|
131
|
+
supplyIncentives,
|
|
132
|
+
borrowIncentives,
|
|
133
|
+
canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen && reserveAsset.borrowable,
|
|
134
|
+
canBeSupplied: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
|
|
135
|
+
canBeWithdrawn: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
136
|
+
canBePayBacked: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
137
|
+
utilization,
|
|
138
|
+
hubLiquidity,
|
|
139
|
+
premiumMultiplier: premiumMultiplier.toString(),
|
|
140
|
+
liquidityFee: liquidityFee.toString(),
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export async function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
145
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
146
|
+
|
|
147
|
+
const hubsData: Record<EthAddress, AaveV4HubOnChainData> = {};
|
|
148
|
+
const [spokeData] = await Promise.all([
|
|
149
|
+
viewContract.read.getSpokeDataFull([market.address]),
|
|
150
|
+
...market.hubs.map(async (hubAddress) => {
|
|
151
|
+
hubsData[hubAddress] = await fetchHubData(viewContract, hubAddress);
|
|
152
|
+
}),
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
const reserveAssetsArray = await Promise.all(spokeData[1].map(async (reserveAssetOnChain: AaveV4ReserveAssetOnChain, index: number) => formatReserveAsset(reserveAssetOnChain, hubsData[reserveAssetOnChain.hub].assets[reserveAssetOnChain.assetId], index, +spokeData[0].oracleDecimals.toString(), network)));
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
assetsData: reserveAssetsArray.reduce((acc: Record<string, AaveV4ReserveAssetData>, reserveAsset: AaveV4ReserveAssetData) => {
|
|
159
|
+
acc[`${reserveAsset.symbol}-${reserveAsset.reserveId}`] = reserveAsset;
|
|
160
|
+
return acc;
|
|
161
|
+
}, {}),
|
|
162
|
+
oracle: spokeData[0].oracle,
|
|
163
|
+
oracleDecimals: +spokeData[0].oracleDecimals.toString(),
|
|
164
|
+
address: market.address,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export async function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
169
|
+
return _getAaveV4SpokeData(getViemProvider(provider, network), network, spoke, blockNumber);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export async function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4AccountData> {
|
|
173
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
174
|
+
|
|
175
|
+
const loanData = await viewContract.read.getLoanData([spokeData.address, address]);
|
|
176
|
+
|
|
177
|
+
const healthFactorFromContract = new Dec(loanData.healthFactor.toString());
|
|
178
|
+
const healthFactor = isMaxUint(healthFactorFromContract.toString()) ? 'Infinity' : healthFactorFromContract.div(1e18).toString();
|
|
179
|
+
const usedAssets = loanData.reserves.reduce((acc: AaveV4UsedReserveAssets, usedReserveAsset) => {
|
|
180
|
+
const identifier = `${wethToEth(getAssetInfoByAddress(usedReserveAsset.underlying, network).symbol)}-${+usedReserveAsset.reserveId.toString()}`;
|
|
181
|
+
const reserveData = spokeData.assetsData[identifier];
|
|
182
|
+
const price = reserveData.price;
|
|
183
|
+
const supplied = isMaxUint(usedReserveAsset.supplied.toString()) ? usedReserveAsset.supplied.toString() : assetAmountInEth(usedReserveAsset.supplied.toString(), reserveData.symbol);
|
|
184
|
+
const drawn = isMaxUint(usedReserveAsset.drawn.toString()) ? usedReserveAsset.drawn.toString() : assetAmountInEth(usedReserveAsset.drawn.toString(), reserveData.symbol);
|
|
185
|
+
const premium = isMaxUint(usedReserveAsset.premium.toString()) ? usedReserveAsset.premium.toString() : assetAmountInEth(usedReserveAsset.premium.toString(), reserveData.symbol);
|
|
186
|
+
const borrowed = isMaxUint(usedReserveAsset.totalDebt.toString()) ? usedReserveAsset.totalDebt.toString() : assetAmountInEth(usedReserveAsset.totalDebt.toString(), reserveData.symbol);
|
|
187
|
+
acc[identifier] = {
|
|
188
|
+
symbol: reserveData.symbol,
|
|
189
|
+
hubName: reserveData.hubName,
|
|
190
|
+
assetId: reserveData.assetId,
|
|
191
|
+
reserveId: +usedReserveAsset.reserveId.toString(),
|
|
192
|
+
supplied,
|
|
193
|
+
suppliedUsd: new Dec(supplied).mul(price).toString(),
|
|
194
|
+
drawn,
|
|
195
|
+
drawnUsd: new Dec(drawn).mul(price).toString(),
|
|
196
|
+
premium,
|
|
197
|
+
premiumUsd: new Dec(premium).mul(price).toString(),
|
|
198
|
+
borrowed,
|
|
199
|
+
borrowedUsd: new Dec(borrowed).mul(price).toString(),
|
|
200
|
+
isSupplied: !new Dec(supplied).eq(0),
|
|
201
|
+
isBorrowed: usedReserveAsset.isBorrowing,
|
|
202
|
+
collateral: usedReserveAsset.isUsingAsCollateral,
|
|
203
|
+
collateralFactor: new Dec(usedReserveAsset.collateralFactor).div(10000).toNumber(),
|
|
204
|
+
};
|
|
205
|
+
return acc;
|
|
206
|
+
}, {});
|
|
207
|
+
|
|
208
|
+
const aggregated = aaveV4GetAggregatedPositionData({
|
|
209
|
+
usedAssets,
|
|
210
|
+
assetsData: spokeData.assetsData,
|
|
211
|
+
network,
|
|
212
|
+
useUserCollateralFactor: true,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const riskPremiumBps = calcUserRiskPremiumBps(usedAssets, spokeData.assetsData);
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
...aggregated,
|
|
219
|
+
usedAssets,
|
|
220
|
+
healthFactor,
|
|
221
|
+
riskPremiumBps,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export async function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<any> {
|
|
226
|
+
return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const _getAaveV4UnderlyingFromReserveId = async (provider: Client, network: NetworkNumber, spoke: EthAddress, reserveId: number): Promise<any> => {
|
|
230
|
+
const viewContract = AaveV4ViewContractViem(provider, network);
|
|
231
|
+
|
|
232
|
+
const reserveData = await viewContract.read.getReserveData([spoke, BigInt(reserveId)]);
|
|
233
|
+
|
|
234
|
+
return reserveData.underlying;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
export async function getAaveV4UnderlyingFromReserveId(provider: EthereumProvider, network: NetworkNumber, spoke: EthAddress, reserveId: number): Promise<any> {
|
|
238
|
+
return _getAaveV4UnderlyingFromReserveId(getViemProvider(provider, network), network, spoke, reserveId);
|
|
237
239
|
}
|