@morpho-org/blue-sdk 6.0.0 → 6.1.0
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/lib/cjs/addresses.d.ts +105 -0
- package/lib/cjs/addresses.js +74 -0
- package/lib/cjs/chain.d.ts +84 -1
- package/lib/cjs/chain.js +78 -0
- package/lib/cjs/errors.d.ts +34 -0
- package/lib/cjs/errors.js +28 -0
- package/lib/cjs/holding/AssetBalances.d.ts +3 -0
- package/lib/cjs/holding/AssetBalances.js +1 -0
- package/lib/cjs/holding/Holding.d.ts +6 -0
- package/lib/cjs/holding/Holding.js +2 -0
- package/lib/cjs/market/Market.d.ts +4 -0
- package/lib/cjs/market/MarketParams.d.ts +3 -0
- package/lib/cjs/market/MarketParams.js +1 -0
- package/lib/cjs/market/MarketUtils.d.ts +397 -0
- package/lib/cjs/market/MarketUtils.js +397 -2
- package/lib/cjs/math/AdaptiveCurveIrmLib.d.ts +51 -1
- package/lib/cjs/math/AdaptiveCurveIrmLib.js +51 -1
- package/lib/cjs/math/MathLib.d.ts +152 -4
- package/lib/cjs/math/MathLib.js +151 -4
- package/lib/cjs/math/SharesMath.d.ts +34 -0
- package/lib/cjs/math/SharesMath.js +34 -0
- package/lib/cjs/position/Position.d.ts +4 -0
- package/lib/cjs/position/Position.js +2 -0
- package/lib/cjs/position/PreLiquidationPosition.d.ts +4 -0
- package/lib/cjs/position/PreLiquidationPosition.js +2 -0
- package/lib/cjs/preLiquidation.d.ts +16 -0
- package/lib/cjs/preLiquidation.js +16 -0
- package/lib/cjs/token/ConstantWrappedToken.d.ts +1 -0
- package/lib/cjs/token/ConstantWrappedToken.js +1 -0
- package/lib/cjs/token/Eip5267Domain.d.ts +4 -0
- package/lib/cjs/token/Eip5267Domain.js +2 -0
- package/lib/cjs/token/ExchangeRateWrappedToken.d.ts +1 -0
- package/lib/cjs/token/ExchangeRateWrappedToken.js +1 -0
- package/lib/cjs/token/Token.d.ts +2 -0
- package/lib/cjs/token/Token.js +1 -0
- package/lib/cjs/token/VaultToken.d.ts +2 -0
- package/lib/cjs/token/VaultToken.js +1 -0
- package/lib/cjs/token/WrappedToken.d.ts +1 -0
- package/lib/cjs/token/WrappedToken.js +1 -0
- package/lib/cjs/types.d.ts +17 -0
- package/lib/cjs/types.js +13 -0
- package/lib/cjs/user/User.d.ts +1 -0
- package/lib/cjs/user/User.js +1 -0
- package/lib/cjs/utils.d.ts +2 -0
- package/lib/cjs/utils.js +1 -0
- package/lib/cjs/vault/Vault.d.ts +7 -0
- package/lib/cjs/vault/Vault.js +2 -0
- package/lib/cjs/vault/VaultConfig.d.ts +2 -0
- package/lib/cjs/vault/VaultConfig.js +1 -0
- package/lib/cjs/vault/VaultMarketAllocation.d.ts +2 -0
- package/lib/cjs/vault/VaultMarketAllocation.js +1 -0
- package/lib/cjs/vault/VaultMarketConfig.d.ts +2 -0
- package/lib/cjs/vault/VaultMarketConfig.js +1 -0
- package/lib/cjs/vault/VaultMarketPublicAllocatorConfig.d.ts +1 -0
- package/lib/cjs/vault/VaultMarketPublicAllocatorConfig.js +1 -0
- package/lib/cjs/vault/VaultUser.d.ts +2 -0
- package/lib/cjs/vault/VaultUser.js +1 -0
- package/lib/cjs/vault/VaultUtils.d.ts +49 -0
- package/lib/cjs/vault/VaultUtils.js +49 -0
- package/lib/cjs/vault/v2/VaultV2.d.ts +5 -0
- package/lib/cjs/vault/v2/VaultV2.js +2 -0
- package/lib/cjs/vault/v2/VaultV2Adapter.d.ts +3 -0
- package/lib/cjs/vault/v2/VaultV2Adapter.js +1 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1Adapter.d.ts +4 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1Adapter.js +2 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +4 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1AdapterV2.js +2 -0
- package/lib/cjs/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +4 -0
- package/lib/cjs/vault/v2/VaultV2MorphoVaultV1Adapter.js +2 -0
- package/lib/esm/addresses.d.ts +105 -0
- package/lib/esm/addresses.js +74 -0
- package/lib/esm/chain.d.ts +84 -1
- package/lib/esm/chain.js +78 -0
- package/lib/esm/errors.d.ts +34 -0
- package/lib/esm/errors.js +28 -0
- package/lib/esm/holding/AssetBalances.d.ts +3 -0
- package/lib/esm/holding/AssetBalances.js +1 -0
- package/lib/esm/holding/Holding.d.ts +6 -0
- package/lib/esm/holding/Holding.js +2 -0
- package/lib/esm/market/Market.d.ts +4 -0
- package/lib/esm/market/MarketParams.d.ts +3 -0
- package/lib/esm/market/MarketParams.js +1 -0
- package/lib/esm/market/MarketUtils.d.ts +397 -0
- package/lib/esm/market/MarketUtils.js +397 -2
- package/lib/esm/math/AdaptiveCurveIrmLib.d.ts +51 -1
- package/lib/esm/math/AdaptiveCurveIrmLib.js +51 -1
- package/lib/esm/math/MathLib.d.ts +152 -4
- package/lib/esm/math/MathLib.js +151 -4
- package/lib/esm/math/SharesMath.d.ts +34 -0
- package/lib/esm/math/SharesMath.js +34 -0
- package/lib/esm/position/Position.d.ts +4 -0
- package/lib/esm/position/Position.js +2 -0
- package/lib/esm/position/PreLiquidationPosition.d.ts +4 -0
- package/lib/esm/position/PreLiquidationPosition.js +2 -0
- package/lib/esm/preLiquidation.d.ts +16 -0
- package/lib/esm/preLiquidation.js +16 -0
- package/lib/esm/token/ConstantWrappedToken.d.ts +1 -0
- package/lib/esm/token/ConstantWrappedToken.js +1 -0
- package/lib/esm/token/Eip5267Domain.d.ts +4 -0
- package/lib/esm/token/Eip5267Domain.js +2 -0
- package/lib/esm/token/ExchangeRateWrappedToken.d.ts +1 -0
- package/lib/esm/token/ExchangeRateWrappedToken.js +1 -0
- package/lib/esm/token/Token.d.ts +2 -0
- package/lib/esm/token/Token.js +1 -0
- package/lib/esm/token/VaultToken.d.ts +2 -0
- package/lib/esm/token/VaultToken.js +1 -0
- package/lib/esm/token/WrappedToken.d.ts +1 -0
- package/lib/esm/token/WrappedToken.js +1 -0
- package/lib/esm/types.d.ts +17 -0
- package/lib/esm/types.js +13 -0
- package/lib/esm/user/User.d.ts +1 -0
- package/lib/esm/user/User.js +1 -0
- package/lib/esm/utils.d.ts +2 -0
- package/lib/esm/utils.js +1 -0
- package/lib/esm/vault/Vault.d.ts +7 -0
- package/lib/esm/vault/Vault.js +2 -0
- package/lib/esm/vault/VaultConfig.d.ts +2 -0
- package/lib/esm/vault/VaultConfig.js +1 -0
- package/lib/esm/vault/VaultMarketAllocation.d.ts +2 -0
- package/lib/esm/vault/VaultMarketAllocation.js +1 -0
- package/lib/esm/vault/VaultMarketConfig.d.ts +2 -0
- package/lib/esm/vault/VaultMarketConfig.js +1 -0
- package/lib/esm/vault/VaultMarketPublicAllocatorConfig.d.ts +1 -0
- package/lib/esm/vault/VaultMarketPublicAllocatorConfig.js +1 -0
- package/lib/esm/vault/VaultUser.d.ts +2 -0
- package/lib/esm/vault/VaultUser.js +1 -0
- package/lib/esm/vault/VaultUtils.d.ts +49 -0
- package/lib/esm/vault/VaultUtils.js +49 -0
- package/lib/esm/vault/v2/VaultV2.d.ts +5 -0
- package/lib/esm/vault/v2/VaultV2.js +2 -0
- package/lib/esm/vault/v2/VaultV2Adapter.d.ts +3 -0
- package/lib/esm/vault/v2/VaultV2Adapter.js +1 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.d.ts +4 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.js +2 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +4 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.js +2 -0
- package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +4 -0
- package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.js +2 -0
- package/package.json +10 -6
|
@@ -14,6 +14,22 @@ var MarketUtils;
|
|
|
14
14
|
/**
|
|
15
15
|
* Returns the id of a market based on its params.
|
|
16
16
|
* @param market The market params.
|
|
17
|
+
* @returns The deterministic Morpho Blue market id.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
21
|
+
*
|
|
22
|
+
* const marketParams = {
|
|
23
|
+
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
24
|
+
* collateralToken: "0x0000000000000000000000000000000000000002",
|
|
25
|
+
* oracle: "0x0000000000000000000000000000000000000003",
|
|
26
|
+
* irm: "0x0000000000000000000000000000000000000004",
|
|
27
|
+
* lltv: 860_000_000_000_000_000n,
|
|
28
|
+
* } as const;
|
|
29
|
+
*
|
|
30
|
+
* const id = MarketUtils.getMarketId(marketParams);
|
|
31
|
+
* // id satisfies MarketId
|
|
32
|
+
* ```
|
|
17
33
|
*/
|
|
18
34
|
function getMarketId(market) {
|
|
19
35
|
return `0x${(0, utils_js_1.bytesToHex)((0, sha3_js_1.keccak_256)((0, utils_js_1.hexToBytes)(`${market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
|
|
@@ -29,6 +45,14 @@ var MarketUtils;
|
|
|
29
45
|
/**
|
|
30
46
|
* Returns the liquidation incentive factor for a given market params.
|
|
31
47
|
* @param config The market params.
|
|
48
|
+
* @returns The liquidation incentive factor, scaled by WAD.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
52
|
+
*
|
|
53
|
+
* const lif = MarketUtils.getLiquidationIncentiveFactor({ lltv: 86_0000000000000000n });
|
|
54
|
+
* // lif satisfies bigint
|
|
55
|
+
* ```
|
|
32
56
|
*/
|
|
33
57
|
function getLiquidationIncentiveFactor({ lltv }) {
|
|
34
58
|
return index_js_1.MathLib.min(constants_js_1.MAX_LIQUIDATION_INCENTIVE_FACTOR, index_js_1.MathLib.wDivDown(index_js_1.MathLib.WAD, index_js_1.MathLib.WAD -
|
|
@@ -38,6 +62,17 @@ var MarketUtils;
|
|
|
38
62
|
/**
|
|
39
63
|
* Returns the market's utilization rate (scaled by WAD).
|
|
40
64
|
* @param market The market state.
|
|
65
|
+
* @returns The market utilization rate, scaled by WAD.
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
69
|
+
*
|
|
70
|
+
* const utilization = MarketUtils.getUtilization({
|
|
71
|
+
* totalSupplyAssets: 100n,
|
|
72
|
+
* totalBorrowAssets: 50n,
|
|
73
|
+
* });
|
|
74
|
+
* // utilization === 500000000000000000n
|
|
75
|
+
* ```
|
|
41
76
|
*/
|
|
42
77
|
function getUtilization({ totalSupplyAssets, totalBorrowAssets, }) {
|
|
43
78
|
totalSupplyAssets = BigInt(totalSupplyAssets);
|
|
@@ -54,6 +89,14 @@ var MarketUtils;
|
|
|
54
89
|
* Returns the per-second rate continuously compounded over a year,
|
|
55
90
|
* as calculated in Morpho Blue assuming the market is frequently accrued onchain.
|
|
56
91
|
* @param rate The per-second rate to compound annually.
|
|
92
|
+
* @returns The annual percentage yield as a JavaScript number.
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
96
|
+
*
|
|
97
|
+
* const apy = MarketUtils.rateToApy(1n);
|
|
98
|
+
* // apy satisfies number
|
|
99
|
+
* ```
|
|
57
100
|
*/
|
|
58
101
|
function rateToApy(rate) {
|
|
59
102
|
return Math.expm1(+(0, viem_1.formatEther)(BigInt(rate) * constants_js_1.SECONDS_PER_YEAR));
|
|
@@ -106,6 +149,17 @@ var MarketUtils;
|
|
|
106
149
|
* Returns the smallest volume to supply until the market gets the closest to the given utilization rate.
|
|
107
150
|
* @param market The market state.
|
|
108
151
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
152
|
+
* @returns The amount to supply to approach the target utilization.
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
|
|
156
|
+
*
|
|
157
|
+
* const assets = MarketUtils.getSupplyToUtilization(
|
|
158
|
+
* { totalSupplyAssets: 100n, totalBorrowAssets: 80n },
|
|
159
|
+
* MathLib.WAD / 2n,
|
|
160
|
+
* );
|
|
161
|
+
* // assets satisfies bigint
|
|
162
|
+
* ```
|
|
109
163
|
*/
|
|
110
164
|
function getSupplyToUtilization(market, utilization) {
|
|
111
165
|
// biome-ignore lint/style/noParameterAssign: TODO refactor to avoid mutating parameter
|
|
@@ -122,6 +176,17 @@ var MarketUtils;
|
|
|
122
176
|
* Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
|
|
123
177
|
* @param market The market state.
|
|
124
178
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
179
|
+
* @returns The amount withdrawable before reaching the target utilization.
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
|
|
183
|
+
*
|
|
184
|
+
* const assets = MarketUtils.getWithdrawToUtilization(
|
|
185
|
+
* { totalSupplyAssets: 100n, totalBorrowAssets: 50n },
|
|
186
|
+
* MathLib.WAD,
|
|
187
|
+
* );
|
|
188
|
+
* // assets satisfies bigint
|
|
189
|
+
* ```
|
|
125
190
|
*/
|
|
126
191
|
function getWithdrawToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
|
|
127
192
|
// biome-ignore lint/style/noParameterAssign: TODO refactor to avoid mutating parameter
|
|
@@ -140,6 +205,17 @@ var MarketUtils;
|
|
|
140
205
|
* Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
|
|
141
206
|
* @param market The market state.
|
|
142
207
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
208
|
+
* @returns The amount borrowable before reaching the target utilization.
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts
|
|
211
|
+
* import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
|
|
212
|
+
*
|
|
213
|
+
* const assets = MarketUtils.getBorrowToUtilization(
|
|
214
|
+
* { totalSupplyAssets: 100n, totalBorrowAssets: 50n },
|
|
215
|
+
* MathLib.WAD,
|
|
216
|
+
* );
|
|
217
|
+
* // assets satisfies bigint
|
|
218
|
+
* ```
|
|
143
219
|
*/
|
|
144
220
|
function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
|
|
145
221
|
return index_js_1.MathLib.zeroFloorSub(index_js_1.MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
|
|
@@ -149,11 +225,36 @@ var MarketUtils;
|
|
|
149
225
|
* Returns the smallest volume to repay until the market gets the closest to the given utilization rate.
|
|
150
226
|
* @param market The market state.
|
|
151
227
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
228
|
+
* @returns The amount to repay before reaching the target utilization.
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
|
|
232
|
+
*
|
|
233
|
+
* const assets = MarketUtils.getRepayToUtilization(
|
|
234
|
+
* { totalSupplyAssets: 100n, totalBorrowAssets: 80n },
|
|
235
|
+
* MathLib.WAD / 2n,
|
|
236
|
+
* );
|
|
237
|
+
* // assets satisfies bigint
|
|
238
|
+
* ```
|
|
152
239
|
*/
|
|
153
240
|
function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
|
|
154
241
|
return index_js_1.MathLib.zeroFloorSub(totalBorrowAssets, index_js_1.MathLib.wMulDown(totalSupplyAssets, utilization));
|
|
155
242
|
}
|
|
156
243
|
MarketUtils.getRepayToUtilization = getRepayToUtilization;
|
|
244
|
+
/**
|
|
245
|
+
* Returns the borrow power of a collateral amount before oracle pricing.
|
|
246
|
+
*
|
|
247
|
+
* @param collateral - The collateral amount.
|
|
248
|
+
* @param marketParams.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
249
|
+
* @returns The collateral amount multiplied by LLTV.
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
253
|
+
*
|
|
254
|
+
* const power = MarketUtils.getCollateralPower(100n, { lltv: 50_0000000000000000n });
|
|
255
|
+
* // power === 50n
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
157
258
|
function getCollateralPower(collateral, { lltv }) {
|
|
158
259
|
return index_js_1.MathLib.wMulDown(collateral, lltv);
|
|
159
260
|
}
|
|
@@ -161,6 +262,17 @@ var MarketUtils;
|
|
|
161
262
|
/**
|
|
162
263
|
* Returns the value of a given amount of collateral quoted in loan assets.
|
|
163
264
|
* Return `undefined` iff the market's price is undefined.
|
|
265
|
+
*
|
|
266
|
+
* @param collateral - The collateral amount.
|
|
267
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
268
|
+
* @returns The collateral value in loan assets, or `undefined` when price is unavailable.
|
|
269
|
+
* @example
|
|
270
|
+
* ```ts
|
|
271
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
272
|
+
*
|
|
273
|
+
* const value = MarketUtils.getCollateralValue(2n, { price: ORACLE_PRICE_SCALE });
|
|
274
|
+
* // value === 2n
|
|
275
|
+
* ```
|
|
164
276
|
*/
|
|
165
277
|
function getCollateralValue(collateral, { price }) {
|
|
166
278
|
if (price == null)
|
|
@@ -172,6 +284,22 @@ var MarketUtils;
|
|
|
172
284
|
* Returns the maximum debt allowed given a certain amount of collateral.
|
|
173
285
|
* Return `undefined` iff the market's price is undefined.
|
|
174
286
|
* To calculate the amount of loan assets that can be borrowed, use `getMaxBorrowableAssets`.
|
|
287
|
+
*
|
|
288
|
+
* @param collateral - The collateral amount.
|
|
289
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
290
|
+
* @param marketParams.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
291
|
+
* @returns The maximum borrow assets allowed, or `undefined` when price is unavailable.
|
|
292
|
+
* @example
|
|
293
|
+
* ```ts
|
|
294
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
295
|
+
*
|
|
296
|
+
* const maxBorrow = MarketUtils.getMaxBorrowAssets(
|
|
297
|
+
* 2n,
|
|
298
|
+
* { price: ORACLE_PRICE_SCALE },
|
|
299
|
+
* { lltv: 50_0000000000000000n },
|
|
300
|
+
* );
|
|
301
|
+
* // maxBorrow === 1n
|
|
302
|
+
* ```
|
|
175
303
|
*/
|
|
176
304
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
177
305
|
function getMaxBorrowAssets(collateral, market, { lltv }) {
|
|
@@ -184,6 +312,25 @@ var MarketUtils;
|
|
|
184
312
|
/**
|
|
185
313
|
* Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
|
|
186
314
|
* Return `undefined` iff the market's price is undefined.
|
|
315
|
+
*
|
|
316
|
+
* @param position.collateral - The collateral amount in the position.
|
|
317
|
+
* @param position.borrowShares - The borrow shares in the position.
|
|
318
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
319
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
320
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
321
|
+
* @param marketParams.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
322
|
+
* @returns The additional borrowable loan assets, or `undefined` when price is unavailable.
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
326
|
+
*
|
|
327
|
+
* const borrowable = MarketUtils.getMaxBorrowableAssets(
|
|
328
|
+
* { collateral: 2n, borrowShares: 0n },
|
|
329
|
+
* { totalBorrowAssets: 0n, totalBorrowShares: 0n, price: ORACLE_PRICE_SCALE },
|
|
330
|
+
* { lltv: 50_0000000000000000n },
|
|
331
|
+
* );
|
|
332
|
+
* // borrowable satisfies bigint | undefined
|
|
333
|
+
* ```
|
|
187
334
|
*/
|
|
188
335
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
189
336
|
function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketParams) {
|
|
@@ -196,6 +343,24 @@ var MarketUtils;
|
|
|
196
343
|
/**
|
|
197
344
|
* Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
|
|
198
345
|
* Return `undefined` iff the market's price is undefined.
|
|
346
|
+
*
|
|
347
|
+
* @param repaidShares - The borrow shares repaid by the liquidation.
|
|
348
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
349
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
350
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
351
|
+
* @param config.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
352
|
+
* @returns The seized collateral amount, or `undefined` when price is unavailable.
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
356
|
+
*
|
|
357
|
+
* const seized = MarketUtils.getLiquidationSeizedAssets(
|
|
358
|
+
* 1n,
|
|
359
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n, price: ORACLE_PRICE_SCALE },
|
|
360
|
+
* { lltv: 86_0000000000000000n },
|
|
361
|
+
* );
|
|
362
|
+
* // seized satisfies bigint | undefined
|
|
363
|
+
* ```
|
|
199
364
|
*/
|
|
200
365
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
201
366
|
function getLiquidationSeizedAssets(repaidShares, market, config) {
|
|
@@ -210,6 +375,24 @@ var MarketUtils;
|
|
|
210
375
|
/**
|
|
211
376
|
* Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
|
|
212
377
|
* Return `undefined` iff the market's price is undefined.
|
|
378
|
+
*
|
|
379
|
+
* @param seizedAssets - The collateral amount seized by the liquidation.
|
|
380
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
381
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
382
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
383
|
+
* @param config.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
384
|
+
* @returns The borrow shares repaid, or `undefined` when price is unavailable.
|
|
385
|
+
* @example
|
|
386
|
+
* ```ts
|
|
387
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
388
|
+
*
|
|
389
|
+
* const shares = MarketUtils.getLiquidationRepaidShares(
|
|
390
|
+
* 1n,
|
|
391
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n, price: ORACLE_PRICE_SCALE },
|
|
392
|
+
* { lltv: 86_0000000000000000n },
|
|
393
|
+
* );
|
|
394
|
+
* // shares satisfies bigint | undefined
|
|
395
|
+
* ```
|
|
213
396
|
*/
|
|
214
397
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
215
398
|
function getLiquidationRepaidShares(seizedAssets, market, config) {
|
|
@@ -221,6 +404,25 @@ var MarketUtils;
|
|
|
221
404
|
/**
|
|
222
405
|
* Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
|
|
223
406
|
* Return `undefined` iff the market's price is undefined.
|
|
407
|
+
*
|
|
408
|
+
* @param position.collateral - The collateral amount in the position.
|
|
409
|
+
* @param position.borrowShares - The borrow shares in the position.
|
|
410
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
411
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
412
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
413
|
+
* @param config.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
414
|
+
* @returns The maximum seizable collateral, or `undefined` when price is unavailable.
|
|
415
|
+
* @example
|
|
416
|
+
* ```ts
|
|
417
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
418
|
+
*
|
|
419
|
+
* const collateral = MarketUtils.getSeizableCollateral(
|
|
420
|
+
* { collateral: 1n, borrowShares: 1n },
|
|
421
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n, price: ORACLE_PRICE_SCALE },
|
|
422
|
+
* { lltv: 50_0000000000000000n },
|
|
423
|
+
* );
|
|
424
|
+
* // collateral satisfies bigint | undefined
|
|
425
|
+
* ```
|
|
224
426
|
*/
|
|
225
427
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
226
428
|
function getSeizableCollateral(position, market, config) {
|
|
@@ -235,6 +437,25 @@ var MarketUtils;
|
|
|
235
437
|
/**
|
|
236
438
|
* Returns the amount of collateral that can be withdrawn given a certain borrow position.
|
|
237
439
|
* Return `undefined` iff the market's price is undefined.
|
|
440
|
+
*
|
|
441
|
+
* @param position.collateral - The collateral amount in the position.
|
|
442
|
+
* @param position.borrowShares - The borrow shares in the position.
|
|
443
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
444
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
445
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
446
|
+
* @param marketParams.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
447
|
+
* @returns The withdrawable collateral amount, or `undefined` when price is unavailable.
|
|
448
|
+
* @example
|
|
449
|
+
* ```ts
|
|
450
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
451
|
+
*
|
|
452
|
+
* const collateral = MarketUtils.getWithdrawableCollateral(
|
|
453
|
+
* { collateral: 2n, borrowShares: 0n },
|
|
454
|
+
* { totalBorrowAssets: 0n, totalBorrowShares: 0n, price: ORACLE_PRICE_SCALE },
|
|
455
|
+
* { lltv: 50_0000000000000000n },
|
|
456
|
+
* );
|
|
457
|
+
* // collateral satisfies bigint | undefined
|
|
458
|
+
* ```
|
|
238
459
|
*/
|
|
239
460
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
240
461
|
function getWithdrawableCollateral({ collateral, borrowShares, }, market, { lltv }) {
|
|
@@ -250,6 +471,20 @@ var MarketUtils;
|
|
|
250
471
|
* Returns whether a given borrow position is healthy.
|
|
251
472
|
* Return `undefined` iff the market's price is undefined.
|
|
252
473
|
* @param position The borrow position to check.
|
|
474
|
+
* @param market The market state used to value the position.
|
|
475
|
+
* @param marketParams The market params containing LLTV.
|
|
476
|
+
* @returns Whether the position is healthy, or `undefined` when price is unavailable.
|
|
477
|
+
* @example
|
|
478
|
+
* ```ts
|
|
479
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
480
|
+
*
|
|
481
|
+
* const healthy = MarketUtils.isHealthy(
|
|
482
|
+
* { collateral: 2n, borrowShares: 0n },
|
|
483
|
+
* { totalBorrowAssets: 0n, totalBorrowShares: 0n, price: ORACLE_PRICE_SCALE },
|
|
484
|
+
* { lltv: 50_0000000000000000n },
|
|
485
|
+
* );
|
|
486
|
+
* // healthy satisfies boolean | undefined
|
|
487
|
+
* ```
|
|
253
488
|
*/
|
|
254
489
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
255
490
|
function isHealthy({ collateral, borrowShares, }, market, marketParams) {
|
|
@@ -263,6 +498,24 @@ var MarketUtils;
|
|
|
263
498
|
* Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
|
|
264
499
|
* that set the user's position to be liquidatable.
|
|
265
500
|
* Returns null if the position is not a borrow.
|
|
501
|
+
*
|
|
502
|
+
* @param position.collateral - The collateral amount in the position.
|
|
503
|
+
* @param position.borrowShares - The borrow shares in the position.
|
|
504
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
505
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
506
|
+
* @param marketParams.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
507
|
+
* @returns The liquidation price, or `null` when the position has no borrow.
|
|
508
|
+
* @example
|
|
509
|
+
* ```ts
|
|
510
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
511
|
+
*
|
|
512
|
+
* const price = MarketUtils.getLiquidationPrice(
|
|
513
|
+
* { collateral: 2n, borrowShares: 1n },
|
|
514
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n },
|
|
515
|
+
* { lltv: 50_0000000000000000n },
|
|
516
|
+
* );
|
|
517
|
+
* // price satisfies bigint | null
|
|
518
|
+
* ```
|
|
266
519
|
*/
|
|
267
520
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
268
521
|
function getLiquidationPrice({ collateral, borrowShares, }, market, marketParams) {
|
|
@@ -282,6 +535,22 @@ var MarketUtils;
|
|
|
282
535
|
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
283
536
|
* Returns `undefined` iff the market's price is undefined.
|
|
284
537
|
* Returns null if the position is not a borrow.
|
|
538
|
+
*
|
|
539
|
+
* @param position - The borrow position to evaluate.
|
|
540
|
+
* @param market - The market state used to value the position.
|
|
541
|
+
* @param marketParams - The market params containing LLTV.
|
|
542
|
+
* @returns The WAD-scaled price variation, `undefined`, or `null` when the position has no borrow.
|
|
543
|
+
* @example
|
|
544
|
+
* ```ts
|
|
545
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
546
|
+
*
|
|
547
|
+
* const variation = MarketUtils.getPriceVariationToLiquidationPrice(
|
|
548
|
+
* { collateral: 2n, borrowShares: 1n },
|
|
549
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n, price: ORACLE_PRICE_SCALE },
|
|
550
|
+
* { lltv: 50_0000000000000000n },
|
|
551
|
+
* );
|
|
552
|
+
* // variation satisfies bigint | null | undefined
|
|
553
|
+
* ```
|
|
285
554
|
*/
|
|
286
555
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
287
556
|
function getPriceVariationToLiquidationPrice(position, market, marketParams) {
|
|
@@ -300,6 +569,25 @@ var MarketUtils;
|
|
|
300
569
|
* Returns the health factor of a given borrow position (scaled by WAD).
|
|
301
570
|
* If the debt is 0, health factor is `MaxUint256`.
|
|
302
571
|
* Returns `undefined` iff the market's price is undefined.
|
|
572
|
+
*
|
|
573
|
+
* @param position.collateral - The collateral amount in the position.
|
|
574
|
+
* @param position.borrowShares - The borrow shares in the position.
|
|
575
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
576
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
577
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
578
|
+
* @param marketParams.lltv - The market liquidation loan-to-value, scaled by WAD.
|
|
579
|
+
* @returns The WAD-scaled health factor, or `undefined` when price is unavailable.
|
|
580
|
+
* @example
|
|
581
|
+
* ```ts
|
|
582
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
583
|
+
*
|
|
584
|
+
* const healthFactor = MarketUtils.getHealthFactor(
|
|
585
|
+
* { collateral: 2n, borrowShares: 1n },
|
|
586
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n, price: ORACLE_PRICE_SCALE },
|
|
587
|
+
* { lltv: 50_0000000000000000n },
|
|
588
|
+
* );
|
|
589
|
+
* // healthFactor satisfies bigint | undefined
|
|
590
|
+
* ```
|
|
303
591
|
*/
|
|
304
592
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
305
593
|
function getHealthFactor({ collateral, borrowShares, }, market, marketParams) {
|
|
@@ -316,6 +604,23 @@ var MarketUtils;
|
|
|
316
604
|
* Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
|
|
317
605
|
* Returns `undefined` iff the market's price is undefined.
|
|
318
606
|
* Returns null if the position is not a borrow.
|
|
607
|
+
*
|
|
608
|
+
* @param position.collateral - The collateral amount in the position.
|
|
609
|
+
* @param position.borrowShares - The borrow shares in the position.
|
|
610
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
611
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
612
|
+
* @param market.price - The oracle price, scaled by `ORACLE_PRICE_SCALE`.
|
|
613
|
+
* @returns The WAD-scaled loan-to-value ratio, `undefined`, or `null` when the position has no borrow.
|
|
614
|
+
* @example
|
|
615
|
+
* ```ts
|
|
616
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
617
|
+
*
|
|
618
|
+
* const ltv = MarketUtils.getLtv(
|
|
619
|
+
* { collateral: 2n, borrowShares: 1n },
|
|
620
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n, price: ORACLE_PRICE_SCALE },
|
|
621
|
+
* );
|
|
622
|
+
* // ltv satisfies bigint | null | undefined
|
|
623
|
+
* ```
|
|
319
624
|
*/
|
|
320
625
|
function getLtv({ collateral, borrowShares, }, market) {
|
|
321
626
|
borrowShares = BigInt(borrowShares);
|
|
@@ -333,34 +638,124 @@ var MarketUtils;
|
|
|
333
638
|
/**
|
|
334
639
|
* Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
|
|
335
640
|
* Returns `undefined` iff the market's price is undefined.
|
|
641
|
+
*
|
|
642
|
+
* @param position - The borrow position to evaluate.
|
|
643
|
+
* @param market - The market state used to value the position.
|
|
644
|
+
* @param marketParams - The market params containing LLTV.
|
|
645
|
+
* @returns The WAD-scaled borrow capacity usage, or `undefined` when price is unavailable.
|
|
646
|
+
* @example
|
|
647
|
+
* ```ts
|
|
648
|
+
* import { MarketUtils, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
649
|
+
*
|
|
650
|
+
* const usage = MarketUtils.getBorrowCapacityUsage(
|
|
651
|
+
* { collateral: 2n, borrowShares: 1n },
|
|
652
|
+
* { totalBorrowAssets: 1n, totalBorrowShares: 1n, price: ORACLE_PRICE_SCALE },
|
|
653
|
+
* { lltv: 50_0000000000000000n },
|
|
654
|
+
* );
|
|
655
|
+
* // usage satisfies bigint | undefined
|
|
656
|
+
* ```
|
|
336
657
|
*/
|
|
337
658
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
338
659
|
function getBorrowCapacityUsage(position, market, marketParams) {
|
|
339
660
|
const hf = getHealthFactor(position, market, marketParams);
|
|
340
661
|
if (hf === undefined)
|
|
341
662
|
return;
|
|
342
|
-
if (hf === null)
|
|
343
|
-
return 0n;
|
|
344
663
|
if (hf === 0n)
|
|
345
664
|
return index_js_1.MathLib.MAX_UINT_256;
|
|
346
665
|
return index_js_1.MathLib.wDivUp(index_js_1.MathLib.WAD, hf);
|
|
347
666
|
}
|
|
348
667
|
MarketUtils.getBorrowCapacityUsage = getBorrowCapacityUsage;
|
|
668
|
+
/**
|
|
669
|
+
* Converts market supply shares to loan assets.
|
|
670
|
+
*
|
|
671
|
+
* @param shares - The supply shares to convert.
|
|
672
|
+
* @param market.totalSupplyAssets - The market's total supplied assets.
|
|
673
|
+
* @param market.totalSupplyShares - The market's total supply shares.
|
|
674
|
+
* @param rounding - Optional rounding direction. Defaults to `"Down"`.
|
|
675
|
+
* @returns The equivalent amount of supplied loan assets.
|
|
676
|
+
* @example
|
|
677
|
+
* ```ts
|
|
678
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
679
|
+
*
|
|
680
|
+
* const assets = MarketUtils.toSupplyAssets(100n, {
|
|
681
|
+
* totalSupplyAssets: 1_000n,
|
|
682
|
+
* totalSupplyShares: 100n,
|
|
683
|
+
* });
|
|
684
|
+
* // assets satisfies bigint
|
|
685
|
+
* ```
|
|
686
|
+
*/
|
|
349
687
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
350
688
|
function toSupplyAssets(shares, market, rounding = "Down") {
|
|
351
689
|
return index_js_1.SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
|
|
352
690
|
}
|
|
353
691
|
MarketUtils.toSupplyAssets = toSupplyAssets;
|
|
692
|
+
/**
|
|
693
|
+
* Converts market supply assets to supply shares.
|
|
694
|
+
*
|
|
695
|
+
* @param assets - The supplied loan assets to convert.
|
|
696
|
+
* @param market.totalSupplyAssets - The market's total supplied assets.
|
|
697
|
+
* @param market.totalSupplyShares - The market's total supply shares.
|
|
698
|
+
* @param rounding - Optional rounding direction. Defaults to `"Up"`.
|
|
699
|
+
* @returns The equivalent amount of supply shares.
|
|
700
|
+
* @example
|
|
701
|
+
* ```ts
|
|
702
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
703
|
+
*
|
|
704
|
+
* const shares = MarketUtils.toSupplyShares(100n, {
|
|
705
|
+
* totalSupplyAssets: 1_000n,
|
|
706
|
+
* totalSupplyShares: 100n,
|
|
707
|
+
* });
|
|
708
|
+
* // shares satisfies bigint
|
|
709
|
+
* ```
|
|
710
|
+
*/
|
|
354
711
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
355
712
|
function toSupplyShares(assets, market, rounding = "Up") {
|
|
356
713
|
return index_js_1.SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
|
|
357
714
|
}
|
|
358
715
|
MarketUtils.toSupplyShares = toSupplyShares;
|
|
716
|
+
/**
|
|
717
|
+
* Converts market borrow shares to loan assets.
|
|
718
|
+
*
|
|
719
|
+
* @param shares - The borrow shares to convert.
|
|
720
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
721
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
722
|
+
* @param rounding - Optional rounding direction. Defaults to `"Up"`.
|
|
723
|
+
* @returns The equivalent amount of borrowed loan assets.
|
|
724
|
+
* @example
|
|
725
|
+
* ```ts
|
|
726
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
727
|
+
*
|
|
728
|
+
* const assets = MarketUtils.toBorrowAssets(100n, {
|
|
729
|
+
* totalBorrowAssets: 1_000n,
|
|
730
|
+
* totalBorrowShares: 100n,
|
|
731
|
+
* });
|
|
732
|
+
* // assets satisfies bigint
|
|
733
|
+
* ```
|
|
734
|
+
*/
|
|
359
735
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
360
736
|
function toBorrowAssets(shares, market, rounding = "Up") {
|
|
361
737
|
return index_js_1.SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
|
|
362
738
|
}
|
|
363
739
|
MarketUtils.toBorrowAssets = toBorrowAssets;
|
|
740
|
+
/**
|
|
741
|
+
* Converts market borrow assets to borrow shares.
|
|
742
|
+
*
|
|
743
|
+
* @param assets - The borrowed loan assets to convert.
|
|
744
|
+
* @param market.totalBorrowAssets - The market's total borrowed assets.
|
|
745
|
+
* @param market.totalBorrowShares - The market's total borrow shares.
|
|
746
|
+
* @param rounding - Optional rounding direction. Defaults to `"Down"`.
|
|
747
|
+
* @returns The equivalent amount of borrow shares.
|
|
748
|
+
* @example
|
|
749
|
+
* ```ts
|
|
750
|
+
* import { MarketUtils } from "@morpho-org/blue-sdk";
|
|
751
|
+
*
|
|
752
|
+
* const shares = MarketUtils.toBorrowShares(100n, {
|
|
753
|
+
* totalBorrowAssets: 1_000n,
|
|
754
|
+
* totalBorrowShares: 100n,
|
|
755
|
+
* });
|
|
756
|
+
* // shares satisfies bigint
|
|
757
|
+
* ```
|
|
758
|
+
*/
|
|
364
759
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
365
760
|
function toBorrowShares(assets, market, rounding = "Down") {
|
|
366
761
|
return index_js_1.SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
|
|
@@ -3,11 +3,17 @@ import type { BigIntish } from "../types.js";
|
|
|
3
3
|
* JS implementation of {@link https://github.com/morpho-org/morpho-blue-irm/blob/main/src/libraries/adaptive-curve/ExpLib.sol ExpLib} used by the Adaptive Curve IRM.
|
|
4
4
|
*/
|
|
5
5
|
export declare namespace AdaptiveCurveIrmLib {
|
|
6
|
+
/** Curve steepness constant, scaled by WAD. */
|
|
6
7
|
const CURVE_STEEPNESS = 4000000000000000000n;
|
|
8
|
+
/** Target utilization for the Adaptive Curve IRM, scaled by WAD. */
|
|
7
9
|
const TARGET_UTILIZATION = 900000000000000000n;
|
|
10
|
+
/** Initial per-second rate at target utilization, scaled by WAD. */
|
|
8
11
|
const INITIAL_RATE_AT_TARGET: bigint;
|
|
12
|
+
/** Per-second speed used to adjust the rate at target utilization. */
|
|
9
13
|
const ADJUSTMENT_SPEED: bigint;
|
|
14
|
+
/** Minimum per-second rate at target utilization, scaled by WAD. */
|
|
10
15
|
const MIN_RATE_AT_TARGET: bigint;
|
|
16
|
+
/** Maximum per-second rate at target utilization, scaled by WAD. */
|
|
11
17
|
const MAX_RATE_AT_TARGET: bigint;
|
|
12
18
|
/**
|
|
13
19
|
* ln(2), scaled by WAD.
|
|
@@ -28,13 +34,57 @@ export declare namespace AdaptiveCurveIrmLib {
|
|
|
28
34
|
const WEXP_UPPER_VALUE = 57716089161558943949701069502944508345128422502756744429568n;
|
|
29
35
|
/**
|
|
30
36
|
* Returns an approximation of exp(x) used by the Adaptive Curve IRM.
|
|
31
|
-
* @param x
|
|
37
|
+
* @param x - The WAD-scaled exponent.
|
|
38
|
+
* @returns The WAD-scaled exponential approximation.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { AdaptiveCurveIrmLib } from "@morpho-org/blue-sdk";
|
|
42
|
+
*
|
|
43
|
+
* const value = AdaptiveCurveIrmLib.wExp(0n);
|
|
44
|
+
* // value === 1000000000000000000n
|
|
45
|
+
* ```
|
|
32
46
|
*/
|
|
33
47
|
function wExp(x: BigIntish): bigint;
|
|
48
|
+
/**
|
|
49
|
+
* Computes Adaptive Curve IRM borrow rates from utilization and elapsed time.
|
|
50
|
+
*
|
|
51
|
+
* @param startUtilization - The market utilization at the start of the period, scaled by WAD.
|
|
52
|
+
* @param startRateAtTarget - The rate at target utilization at the start of the period, scaled by WAD.
|
|
53
|
+
* @param elapsed - The elapsed time in seconds.
|
|
54
|
+
* @returns The average borrow rate, end borrow rate, and end rate at target, all scaled by WAD.
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { AdaptiveCurveIrmLib } from "@morpho-org/blue-sdk";
|
|
58
|
+
*
|
|
59
|
+
* const rates = AdaptiveCurveIrmLib.getBorrowRate(
|
|
60
|
+
* AdaptiveCurveIrmLib.TARGET_UTILIZATION,
|
|
61
|
+
* AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET,
|
|
62
|
+
* 12n,
|
|
63
|
+
* );
|
|
64
|
+
* // rates satisfies { avgBorrowRate: bigint; endBorrowRate: bigint; endRateAtTarget: bigint }
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
34
67
|
function getBorrowRate(startUtilization: BigIntish, startRateAtTarget: BigIntish, elapsed: BigIntish): {
|
|
35
68
|
avgBorrowRate: bigint;
|
|
36
69
|
endBorrowRate: bigint;
|
|
37
70
|
endRateAtTarget: bigint;
|
|
38
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* Finds the utilization corresponding to a borrow rate and rate at target.
|
|
74
|
+
*
|
|
75
|
+
* @param borrowRate - The borrow rate to invert, scaled by WAD.
|
|
76
|
+
* @param rateAtTarget - The rate at target utilization, scaled by WAD.
|
|
77
|
+
* @returns The utilization corresponding to `borrowRate`, scaled by WAD.
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { AdaptiveCurveIrmLib } from "@morpho-org/blue-sdk";
|
|
81
|
+
*
|
|
82
|
+
* const utilization = AdaptiveCurveIrmLib.getUtilizationAtBorrowRate(
|
|
83
|
+
* AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET,
|
|
84
|
+
* AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET,
|
|
85
|
+
* );
|
|
86
|
+
* // utilization === AdaptiveCurveIrmLib.TARGET_UTILIZATION
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
39
89
|
function getUtilizationAtBorrowRate(borrowRate: BigIntish, rateAtTarget: BigIntish): bigint;
|
|
40
90
|
}
|