@morpho-org/blue-sdk 1.7.2 → 1.7.4

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.
@@ -12,6 +12,12 @@ export interface CapacityLimit {
12
12
  value: bigint;
13
13
  limiter: CapacityLimitReason;
14
14
  }
15
+ export interface MaxBorrowOptions {
16
+ maxLtv?: bigint;
17
+ }
18
+ export interface MaxWithdrawCollateralOptions {
19
+ maxLtv?: bigint;
20
+ }
15
21
  export interface MaxPositionCapacities {
16
22
  supply: CapacityLimit;
17
23
  withdraw: CapacityLimit;
@@ -190,7 +196,7 @@ export declare class Market implements InputMarket {
190
196
  * Returns the maximum amount of loan assets that can be borrowed given a certain amount of collateral.
191
197
  * @param collateral The amount of collateral to consider.
192
198
  */
193
- getMaxBorrowAssets(collateral: bigint): bigint;
199
+ getMaxBorrowAssets(collateral: bigint, { maxLtv }?: MaxBorrowOptions): bigint;
194
200
  /**
195
201
  * Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
196
202
  * @param position The borrow position to consider.
@@ -224,7 +230,7 @@ export declare class Market implements InputMarket {
224
230
  getWithdrawableCollateral(position: {
225
231
  collateral: bigint;
226
232
  borrowShares: bigint;
227
- }): bigint;
233
+ }, { maxLtv }?: MaxWithdrawCollateralOptions): bigint;
228
234
  /**
229
235
  * Returns whether a given borrow position is healthy.
230
236
  * @param position The borrow position to check.
@@ -281,7 +287,7 @@ export declare class Market implements InputMarket {
281
287
  getBorrowCapacityLimit({ collateral, borrowShares, }: {
282
288
  collateral: bigint;
283
289
  borrowShares?: bigint;
284
- }): CapacityLimit;
290
+ }, options?: MaxBorrowOptions): CapacityLimit;
285
291
  /**
286
292
  * Returns the maximum amount of loan assets that can be repaid given a certain borrow position
287
293
  * and a balance of loan assets, and the reason for the limit.
@@ -304,7 +310,7 @@ export declare class Market implements InputMarket {
304
310
  getWithdrawCollateralCapacityLimit(position: {
305
311
  collateral: bigint;
306
312
  borrowShares: bigint;
307
- }): CapacityLimit;
313
+ }, options?: MaxWithdrawCollateralOptions): CapacityLimit;
308
314
  /**
309
315
  * Returns the maximum capacity for all interactions with Morpho Blue given a certain position
310
316
  * and loan and collateral balances.
@@ -316,5 +322,8 @@ export declare class Market implements InputMarket {
316
322
  collateral: bigint;
317
323
  supplyShares: bigint;
318
324
  borrowShares: bigint;
319
- }, loanTokenBalance: bigint, collateralTokenBalance: bigint): MaxPositionCapacities;
325
+ }, loanTokenBalance: bigint, collateralTokenBalance: bigint, options?: {
326
+ borrow?: MaxBorrowOptions;
327
+ withdrawCollateral?: MaxWithdrawCollateralOptions;
328
+ }): MaxPositionCapacities;
320
329
  }
@@ -278,8 +278,10 @@ class Market {
278
278
  * Returns the maximum amount of loan assets that can be borrowed given a certain amount of collateral.
279
279
  * @param collateral The amount of collateral to consider.
280
280
  */
281
- getMaxBorrowAssets(collateral) {
282
- return MarketUtils_1.MarketUtils.getMaxBorrowAssets(collateral, this, this.config);
281
+ getMaxBorrowAssets(collateral, { maxLtv = this.config.lltv } = {}) {
282
+ return MarketUtils_1.MarketUtils.getMaxBorrowAssets(collateral, this, {
283
+ lltv: maths_1.MathLib.min(maxLtv, this.config.lltv),
284
+ });
283
285
  }
284
286
  /**
285
287
  * Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
@@ -313,8 +315,10 @@ class Market {
313
315
  * Returns the amount of collateral that can be withdrawn given a certain borrow position.
314
316
  * @param position The borrow position to consider.
315
317
  */
316
- getWithdrawableCollateral(position) {
317
- return MarketUtils_1.MarketUtils.getWithdrawableCollateral(position, this, this.config);
318
+ getWithdrawableCollateral(position, { maxLtv = this.config.lltv } = {}) {
319
+ return MarketUtils_1.MarketUtils.getWithdrawableCollateral(position, this, {
320
+ lltv: maths_1.MathLib.min(maxLtv, this.config.lltv),
321
+ });
318
322
  }
319
323
  /**
320
324
  * Returns whether a given borrow position is healthy.
@@ -363,9 +367,9 @@ class Market {
363
367
  * and the reason for the limit.
364
368
  * @param position The borrow position to consider.
365
369
  */
366
- getBorrowCapacityLimit({ collateral, borrowShares = 0n, }) {
370
+ getBorrowCapacityLimit({ collateral, borrowShares = 0n, }, options) {
367
371
  // handle edge cases when the user is liquidatable (maxBorrow < borrow)
368
- const maxBorrowableAssets = maths_1.MathLib.zeroFloorSub(this.getMaxBorrowAssets(collateral), this.toBorrowAssets(borrowShares));
372
+ const maxBorrowableAssets = maths_1.MathLib.zeroFloorSub(this.getMaxBorrowAssets(collateral, options), this.toBorrowAssets(borrowShares));
369
373
  const { liquidity } = this;
370
374
  if (maxBorrowableAssets > liquidity)
371
375
  return {
@@ -417,8 +421,8 @@ class Market {
417
421
  * and the reason for the limit.
418
422
  * @param position The borrow position to consider.
419
423
  */
420
- getWithdrawCollateralCapacityLimit(position) {
421
- const withdrawableCollateral = this.getWithdrawableCollateral(position);
424
+ getWithdrawCollateralCapacityLimit(position, options) {
425
+ const withdrawableCollateral = this.getWithdrawableCollateral(position, options);
422
426
  if (position.collateral > withdrawableCollateral)
423
427
  return {
424
428
  value: withdrawableCollateral,
@@ -436,20 +440,20 @@ class Market {
436
440
  * @param loanTokenBalance The balance of loan assets.
437
441
  * @param collateralTokenBalance The balance of collateral assets.
438
442
  */
439
- getMaxCapacities(position, loanTokenBalance, collateralTokenBalance) {
443
+ getMaxCapacities(position, loanTokenBalance, collateralTokenBalance, options) {
440
444
  return {
441
445
  supply: {
442
446
  value: loanTokenBalance,
443
447
  limiter: CapacityLimitReason.balance,
444
448
  },
445
449
  withdraw: this.getWithdrawCapacityLimit(position),
446
- borrow: this.getBorrowCapacityLimit(position),
450
+ borrow: this.getBorrowCapacityLimit(position, options?.borrow),
447
451
  repay: this.getRepayCapacityLimit(position.borrowShares, loanTokenBalance),
448
452
  supplyCollateral: {
449
453
  value: collateralTokenBalance,
450
454
  limiter: CapacityLimitReason.balance,
451
455
  },
452
- withdrawCollateral: this.getWithdrawCollateralCapacityLimit(position),
456
+ withdrawCollateral: this.getWithdrawCollateralCapacityLimit(position, options?.withdrawCollateral),
453
457
  };
454
458
  }
455
459
  }
@@ -22,7 +22,7 @@ var MarketUtils;
22
22
  market.collateralToken.substring(2).toLowerCase().padStart(64, "0") +
23
23
  market.oracle.substring(2).padStart(64, "0") +
24
24
  market.irm.substring(2).toLowerCase().padStart(64, "0") +
25
- market.lltv.toString(16).padStart(64, "0")).toString("hex")}`;
25
+ BigInt(market.lltv).toString(16).padStart(64, "0")).toString("hex")}`;
26
26
  }
27
27
  MarketUtils.getMarketId = getMarketId;
28
28
  /**
@@ -121,8 +121,11 @@ var AdaptiveCurveIrmLib;
121
121
  rateAtTarget = BigInt(rateAtTarget);
122
122
  if (borrowRate >= rateAtTarget) {
123
123
  const maxBorrowRate = MathLib_1.MathLib.wMulDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
124
+ const diffToMaxBorrowRate = maxBorrowRate - rateAtTarget;
125
+ if (diffToMaxBorrowRate === 0n)
126
+ return MathLib_1.MathLib.WAD;
124
127
  return MathLib_1.MathLib.min(MathLib_1.MathLib.WAD, AdaptiveCurveIrmLib.TARGET_UTILIZATION +
125
- MathLib_1.MathLib.mulDivDown(MathLib_1.MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - rateAtTarget, maxBorrowRate - rateAtTarget));
128
+ MathLib_1.MathLib.mulDivDown(MathLib_1.MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - rateAtTarget, diffToMaxBorrowRate));
126
129
  }
127
130
  const minBorrowRate = MathLib_1.MathLib.wDivDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
128
131
  return MathLib_1.MathLib.max(0n, MathLib_1.MathLib.mulDivDown(AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - minBorrowRate, rateAtTarget - minBorrowRate));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@morpho-org/blue-sdk",
3
- "version": "1.7.2",
3
+ "version": "1.7.4",
4
4
  "author": "Morpho Association <contact@morpho.org>",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -16,8 +16,8 @@
16
16
  "keccak256": "^1.0.6"
17
17
  },
18
18
  "devDependencies": {
19
- "@morpho-org/morpho-test": "^1.7.2",
20
- "@morpho-org/morpho-ts": "^1.7.2",
19
+ "@morpho-org/morpho-test": "^1.7.4",
20
+ "@morpho-org/morpho-ts": "^1.7.4",
21
21
  "@types/jest": "^29.5.12",
22
22
  "@types/node": "^22.1.0",
23
23
  "jest": "^29.7.0",
@@ -25,7 +25,7 @@
25
25
  "typescript": "^5.4.5"
26
26
  },
27
27
  "peerDependencies": {
28
- "@morpho-org/morpho-ts": "^1.7.2"
28
+ "@morpho-org/morpho-ts": "^1.7.4"
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public"
@@ -49,5 +49,5 @@
49
49
  ],
50
50
  "preset": "ts-jest"
51
51
  },
52
- "gitHead": "deea0629f694c66ea05202f357a495775144b839"
52
+ "gitHead": "09517429d483381a758182323547e8a6fe75f2c9"
53
53
  }