@morpho-org/blue-sdk 1.4.0 → 1.4.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.
@@ -162,15 +162,25 @@ export declare class Market implements InputMarket {
162
162
  */
163
163
  toBorrowShares(assets: bigint, rounding?: RoundingDirection): bigint;
164
164
  /**
165
- * Returns the liquidity available to borrow until the market reach the given utilization rate.
165
+ * Returns the volume to supply until the market gets the closest to the given utilization rate.
166
166
  * @param utilization The target utilization rate (scaled by WAD).
167
167
  */
168
- getSupplyLiquidityToUtilization(utilization: bigint): BigIntish;
168
+ getSupplyToUtilization(utilization: bigint): BigIntish;
169
169
  /**
170
- * Returns the liquidity available to borrow until the market reach the given utilization rate.
170
+ * Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
171
171
  * @param utilization The target utilization rate (scaled by WAD).
172
172
  */
173
- getBorrowLiquidityToUtilization(utilization: bigint): bigint;
173
+ getWithdrawToUtilization(utilization: bigint): BigIntish;
174
+ /**
175
+ * Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
176
+ * @param utilization The target utilization rate (scaled by WAD).
177
+ */
178
+ getBorrowToUtilization(utilization: bigint): bigint;
179
+ /**
180
+ * Returns the volume to repay until the market gets the closest to the given utilization rate.
181
+ * @param utilization The target utilization rate (scaled by WAD).
182
+ */
183
+ getRepayToUtilization(utilization: bigint): bigint;
174
184
  /**
175
185
  * Returns the value of a given amount of collateral quoted in loan assets.
176
186
  * @param collateral The amount of collateral to quote.
@@ -140,8 +140,8 @@ class Market {
140
140
  return this;
141
141
  let borrowRate = 0n;
142
142
  let { rateAtTarget } = this;
143
- if (this.rateAtTarget != null) {
144
- const { avgBorrowRate, endRateAtTarget } = maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, elapsed);
143
+ if (rateAtTarget != null) {
144
+ const { avgBorrowRate, endRateAtTarget } = maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, rateAtTarget, elapsed);
145
145
  borrowRate = avgBorrowRate;
146
146
  rateAtTarget = endRateAtTarget;
147
147
  }
@@ -240,18 +240,32 @@ class Market {
240
240
  return MarketUtils_1.MarketUtils.toBorrowShares(assets, this, rounding);
241
241
  }
242
242
  /**
243
- * Returns the liquidity available to borrow until the market reach the given utilization rate.
243
+ * Returns the volume to supply until the market gets the closest to the given utilization rate.
244
244
  * @param utilization The target utilization rate (scaled by WAD).
245
245
  */
246
- getSupplyLiquidityToUtilization(utilization) {
247
- return MarketUtils_1.MarketUtils.getSupplyLiquidityToUtilization(this, utilization);
246
+ getSupplyToUtilization(utilization) {
247
+ return MarketUtils_1.MarketUtils.getSupplyToUtilization(this, utilization);
248
248
  }
249
249
  /**
250
- * Returns the liquidity available to borrow until the market reach the given utilization rate.
250
+ * Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
251
251
  * @param utilization The target utilization rate (scaled by WAD).
252
252
  */
253
- getBorrowLiquidityToUtilization(utilization) {
254
- return MarketUtils_1.MarketUtils.getBorrowLiquidityToUtilization(this, utilization);
253
+ getWithdrawToUtilization(utilization) {
254
+ return MarketUtils_1.MarketUtils.getWithdrawToUtilization(this, utilization);
255
+ }
256
+ /**
257
+ * Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
258
+ * @param utilization The target utilization rate (scaled by WAD).
259
+ */
260
+ getBorrowToUtilization(utilization) {
261
+ return MarketUtils_1.MarketUtils.getBorrowToUtilization(this, utilization);
262
+ }
263
+ /**
264
+ * Returns the volume to repay until the market gets the closest to the given utilization rate.
265
+ * @param utilization The target utilization rate (scaled by WAD).
266
+ */
267
+ getRepayToUtilization(utilization) {
268
+ return MarketUtils_1.MarketUtils.getRepayToUtilization(this, utilization);
255
269
  }
256
270
  /**
257
271
  * Returns the value of a given amount of collateral quoted in loan assets.
@@ -57,20 +57,38 @@ export declare namespace MarketUtils {
57
57
  feeShares: bigint;
58
58
  };
59
59
  /**
60
- * Returns the liquidity available to withdraw until the market reach the given utilization rate.
60
+ * Returns the volume to supply until the market gets the closest to the given utilization rate.
61
61
  * @param market The market state.
62
62
  * @param utilization The target utilization rate (scaled by WAD).
63
63
  */
64
- function getSupplyLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
64
+ function getSupplyToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
65
65
  totalSupplyAssets: BigIntish;
66
66
  totalBorrowAssets: BigIntish;
67
67
  }, utilization: BigIntish): BigIntish;
68
68
  /**
69
- * Returns the liquidity available to borrow until the market reach the given utilization rate.
69
+ * Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
70
70
  * @param market The market state.
71
71
  * @param utilization The target utilization rate (scaled by WAD).
72
72
  */
73
- function getBorrowLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
73
+ function getWithdrawToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
74
+ totalSupplyAssets: BigIntish;
75
+ totalBorrowAssets: BigIntish;
76
+ }, utilization: BigIntish): BigIntish;
77
+ /**
78
+ * Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
79
+ * @param market The market state.
80
+ * @param utilization The target utilization rate (scaled by WAD).
81
+ */
82
+ function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
83
+ totalSupplyAssets: BigIntish;
84
+ totalBorrowAssets: BigIntish;
85
+ }, utilization: BigIntish): bigint;
86
+ /**
87
+ * Returns the volume to repay until the market gets the closest to the given utilization rate.
88
+ * @param market The market state.
89
+ * @param utilization The target utilization rate (scaled by WAD).
90
+ */
91
+ function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
74
92
  totalSupplyAssets: BigIntish;
75
93
  totalBorrowAssets: BigIntish;
76
94
  }, utilization: BigIntish): bigint;
@@ -86,11 +86,27 @@ var MarketUtils;
86
86
  }
87
87
  MarketUtils.getAccruedInterest = getAccruedInterest;
88
88
  /**
89
- * Returns the liquidity available to withdraw until the market reach the given utilization rate.
89
+ * Returns the volume to supply until the market gets the closest to the given utilization rate.
90
90
  * @param market The market state.
91
91
  * @param utilization The target utilization rate (scaled by WAD).
92
92
  */
93
- function getSupplyLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
93
+ function getSupplyToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
94
+ utilization = BigInt(utilization);
95
+ totalBorrowAssets = BigInt(totalBorrowAssets);
96
+ if (utilization === 0n) {
97
+ if (totalBorrowAssets === 0n)
98
+ return totalSupplyAssets;
99
+ return 0n;
100
+ }
101
+ return maths_1.MathLib.zeroFloorSub(maths_1.MathLib.wDivUp(totalBorrowAssets, utilization), totalSupplyAssets);
102
+ }
103
+ MarketUtils.getSupplyToUtilization = getSupplyToUtilization;
104
+ /**
105
+ * Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
106
+ * @param market The market state.
107
+ * @param utilization The target utilization rate (scaled by WAD).
108
+ */
109
+ function getWithdrawToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
94
110
  utilization = BigInt(utilization);
95
111
  totalBorrowAssets = BigInt(totalBorrowAssets);
96
112
  if (utilization === 0n) {
@@ -100,16 +116,25 @@ var MarketUtils;
100
116
  }
101
117
  return maths_1.MathLib.zeroFloorSub(totalSupplyAssets, maths_1.MathLib.wDivUp(totalBorrowAssets, utilization));
102
118
  }
103
- MarketUtils.getSupplyLiquidityToUtilization = getSupplyLiquidityToUtilization;
119
+ MarketUtils.getWithdrawToUtilization = getWithdrawToUtilization;
104
120
  /**
105
- * Returns the liquidity available to borrow until the market reach the given utilization rate.
121
+ * Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
106
122
  * @param market The market state.
107
123
  * @param utilization The target utilization rate (scaled by WAD).
108
124
  */
109
- function getBorrowLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
125
+ function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
110
126
  return maths_1.MathLib.zeroFloorSub(maths_1.MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
111
127
  }
112
- MarketUtils.getBorrowLiquidityToUtilization = getBorrowLiquidityToUtilization;
128
+ MarketUtils.getBorrowToUtilization = getBorrowToUtilization;
129
+ /**
130
+ * Returns the volume to repay until the market gets the closest to the given utilization rate.
131
+ * @param market The market state.
132
+ * @param utilization The target utilization rate (scaled by WAD).
133
+ */
134
+ function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
135
+ return maths_1.MathLib.zeroFloorSub(totalBorrowAssets, maths_1.MathLib.wMulDown(totalSupplyAssets, utilization));
136
+ }
137
+ MarketUtils.getRepayToUtilization = getRepayToUtilization;
113
138
  function getCollateralPower(collateral, { lltv }) {
114
139
  return maths_1.MathLib.wMulDown(collateral, lltv);
115
140
  }
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const maths_1 = require("../maths");
3
4
  const MarketUtils_1 = require("./MarketUtils");
4
5
  const market = {
5
6
  loanToken: "0x0000000000000000000000000000000000000001",
@@ -15,4 +16,21 @@ describe("MarketUtils", () => {
15
16
  it("should calculate the correct liquidation incentive factor", () => {
16
17
  expect(MarketUtils_1.MarketUtils.getLiquidationIncentiveFactor(market)).toEqual(1043841336116910229n);
17
18
  });
19
+ it("should calculate the supply volume to reach utilization", () => {
20
+ expect(MarketUtils_1.MarketUtils.getSupplyToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(111111111111111112n);
21
+ expect(MarketUtils_1.MarketUtils.getSupplyToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(0n);
22
+ });
23
+ it("should calculate the withdraw volume to reach utilization", () => {
24
+ expect(MarketUtils_1.MarketUtils.getWithdrawToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(0n);
25
+ expect(MarketUtils_1.MarketUtils.getWithdrawToUtilization({ totalSupplyAssets: 2n * maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(888888888888888888n);
26
+ expect(MarketUtils_1.MarketUtils.getWithdrawToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(maths_1.MathLib.WAD);
27
+ });
28
+ it("should calculate the borrow volume to reach utilization", () => {
29
+ expect(MarketUtils_1.MarketUtils.getBorrowToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(0n);
30
+ expect(MarketUtils_1.MarketUtils.getBorrowToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(900000000000000000n);
31
+ });
32
+ it("should calculate the repay volume to reach utilization", () => {
33
+ expect(MarketUtils_1.MarketUtils.getRepayToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(100000000000000000n);
34
+ expect(MarketUtils_1.MarketUtils.getRepayToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(0n);
35
+ });
18
36
  });
@@ -35,4 +35,5 @@ export declare namespace AdaptiveCurveIrmLib {
35
35
  avgBorrowRate: bigint;
36
36
  endRateAtTarget: bigint;
37
37
  };
38
+ function getUtilizationAtBorrowRate(borrowRate: BigIntish, rateAtTarget: BigIntish): bigint;
38
39
  }
@@ -116,4 +116,16 @@ var AdaptiveCurveIrmLib;
116
116
  };
117
117
  }
118
118
  AdaptiveCurveIrmLib.getBorrowRate = getBorrowRate;
119
+ function getUtilizationAtBorrowRate(borrowRate, rateAtTarget) {
120
+ borrowRate = BigInt(borrowRate);
121
+ rateAtTarget = BigInt(rateAtTarget);
122
+ if (borrowRate >= rateAtTarget) {
123
+ const maxBorrowRate = MathLib_1.MathLib.wMulDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
124
+ 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));
126
+ }
127
+ const minBorrowRate = MathLib_1.MathLib.wDivDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
128
+ return MathLib_1.MathLib.max(0n, MathLib_1.MathLib.mulDivDown(AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - minBorrowRate, rateAtTarget - minBorrowRate));
129
+ }
130
+ AdaptiveCurveIrmLib.getUtilizationAtBorrowRate = getUtilizationAtBorrowRate;
119
131
  })(AdaptiveCurveIrmLib || (exports.AdaptiveCurveIrmLib = AdaptiveCurveIrmLib = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@morpho-org/blue-sdk",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "author": "Morpho Association <contact@morpho.org>",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -13,17 +13,20 @@
13
13
  "test": "jest"
14
14
  },
15
15
  "dependencies": {
16
- "@morpho-org/morpho-ts": "^1.4.0",
17
16
  "keccak256": "^1.0.6"
18
17
  },
19
18
  "devDependencies": {
20
- "@morpho-org/morpho-test": "^1.4.0",
19
+ "@morpho-org/morpho-test": "^1.4.2",
20
+ "@morpho-org/morpho-ts": "^1.4.2",
21
21
  "@types/jest": "^29.5.12",
22
22
  "@types/node": "^22.0.0",
23
23
  "jest": "^29.7.0",
24
24
  "ts-jest": "^29.2.2",
25
25
  "typescript": "^5.4.5"
26
26
  },
27
+ "peerDependencies": {
28
+ "@morpho-org/morpho-ts": "^1.4.2"
29
+ },
27
30
  "publishConfig": {
28
31
  "access": "public"
29
32
  },
@@ -46,5 +49,5 @@
46
49
  ],
47
50
  "preset": "ts-jest"
48
51
  },
49
- "gitHead": "a58c35ff1cf51728fe72db7eb4542bafc0a6d8ac"
52
+ "gitHead": "b11ee6671da97837dd8356b3fb5e8867375a2a19"
50
53
  }