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