@morpho-org/blue-sdk 2.0.0-next.9 → 2.0.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 (56) hide show
  1. package/lib/addresses.d.ts +1 -1
  2. package/lib/addresses.js +63 -58
  3. package/lib/chain.js +9 -6
  4. package/lib/constants.js +12 -9
  5. package/lib/errors.d.ts +5 -0
  6. package/lib/errors.js +37 -13
  7. package/lib/holding/AssetBalances.d.ts +2 -2
  8. package/lib/holding/AssetBalances.js +5 -1
  9. package/lib/holding/Holding.d.ts +5 -5
  10. package/lib/holding/Holding.js +9 -5
  11. package/lib/holding/index.js +18 -2
  12. package/lib/index.js +28 -12
  13. package/lib/market/Market.d.ts +37 -25
  14. package/lib/market/Market.js +72 -51
  15. package/lib/market/MarketParams.d.ts +4 -3
  16. package/lib/market/MarketParams.js +16 -14
  17. package/lib/market/MarketUtils.d.ts +81 -30
  18. package/lib/market/MarketUtils.js +135 -51
  19. package/lib/market/index.js +19 -3
  20. package/lib/math/AdaptiveCurveIrmLib.js +25 -22
  21. package/lib/math/MathLib.d.ts +1 -16
  22. package/lib/math/MathLib.js +6 -29
  23. package/lib/math/SharesMath.js +8 -5
  24. package/lib/math/index.js +19 -3
  25. package/lib/position/Position.d.ts +28 -19
  26. package/lib/position/Position.js +49 -43
  27. package/lib/position/index.js +17 -1
  28. package/lib/token/ConstantWrappedToken.d.ts +2 -2
  29. package/lib/token/ConstantWrappedToken.js +10 -6
  30. package/lib/token/ExchangeRateWrappedToken.d.ts +2 -2
  31. package/lib/token/ExchangeRateWrappedToken.js +9 -5
  32. package/lib/token/Token.d.ts +15 -15
  33. package/lib/token/Token.js +27 -25
  34. package/lib/token/VaultToken.d.ts +4 -4
  35. package/lib/token/VaultToken.js +9 -5
  36. package/lib/token/WrappedToken.d.ts +2 -2
  37. package/lib/token/WrappedToken.js +11 -7
  38. package/lib/token/index.js +21 -5
  39. package/lib/types.js +9 -4
  40. package/lib/user/User.js +5 -1
  41. package/lib/user/index.js +17 -1
  42. package/lib/vault/Vault.d.ts +20 -12
  43. package/lib/vault/Vault.js +26 -21
  44. package/lib/vault/VaultConfig.d.ts +3 -4
  45. package/lib/vault/VaultConfig.js +9 -5
  46. package/lib/vault/VaultMarketAllocation.d.ts +3 -3
  47. package/lib/vault/VaultMarketAllocation.js +8 -4
  48. package/lib/vault/VaultMarketConfig.d.ts +3 -3
  49. package/lib/vault/VaultMarketConfig.js +5 -1
  50. package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +3 -3
  51. package/lib/vault/VaultMarketPublicAllocatorConfig.js +5 -1
  52. package/lib/vault/VaultUser.d.ts +3 -3
  53. package/lib/vault/VaultUser.js +5 -1
  54. package/lib/vault/VaultUtils.js +9 -6
  55. package/lib/vault/index.js +23 -7
  56. package/package.json +9 -19
@@ -1,10 +1,13 @@
1
- import { ZERO_ADDRESS } from "@morpho-org/morpho-ts";
2
- import { UnknownMarketParamsError } from "../errors.js";
3
- import { MarketUtils } from "./MarketUtils.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MarketParams = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const errors_js_1 = require("../errors.js");
6
+ const MarketUtils_js_1 = require("./MarketUtils.js");
4
7
  /**
5
8
  * Represents a market's configuration (also called market params).
6
9
  */
7
- export class MarketParams {
10
+ class MarketParams {
8
11
  static _CACHE = {};
9
12
  /**
10
13
  * Returns the previously cached market config for the given id, if any.
@@ -13,7 +16,7 @@ export class MarketParams {
13
16
  static get(id) {
14
17
  const marketParams = MarketParams._CACHE[id];
15
18
  if (!marketParams)
16
- throw new UnknownMarketParamsError(id);
19
+ throw new errors_js_1.UnknownMarketParamsError(id);
17
20
  return marketParams;
18
21
  }
19
22
  /**
@@ -21,10 +24,10 @@ export class MarketParams {
21
24
  */
22
25
  static idle(token) {
23
26
  return new MarketParams({
24
- collateralToken: ZERO_ADDRESS,
27
+ collateralToken: morpho_ts_1.ZERO_ADDRESS,
25
28
  loanToken: token,
26
- oracle: ZERO_ADDRESS,
27
- irm: ZERO_ADDRESS,
29
+ oracle: morpho_ts_1.ZERO_ADDRESS,
30
+ irm: morpho_ts_1.ZERO_ADDRESS,
28
31
  lltv: 0n,
29
32
  });
30
33
  }
@@ -51,13 +54,11 @@ export class MarketParams {
51
54
  /**
52
55
  * The market's hex-encoded id, defined as the hash of the market params.
53
56
  */
54
- // Cached because params are readonly.
55
- id;
57
+ id; // Cached because params are readonly.
56
58
  /**
57
59
  * The market's liquidation incentive factor.
58
60
  */
59
- // Cached because lltv is readonly.
60
- liquidationIncentiveFactor;
61
+ liquidationIncentiveFactor; // Cached because lltv is readonly.
61
62
  constructor(params) {
62
63
  const { collateralToken, loanToken, oracle, irm, lltv } = params;
63
64
  this.collateralToken = collateralToken;
@@ -65,9 +66,10 @@ export class MarketParams {
65
66
  this.oracle = oracle;
66
67
  this.irm = irm;
67
68
  this.lltv = BigInt(lltv);
68
- this.id = MarketUtils.getMarketId(params);
69
+ this.id = MarketUtils_js_1.MarketUtils.getMarketId(params);
69
70
  this.liquidationIncentiveFactor =
70
- MarketUtils.getLiquidationIncentiveFactor(params);
71
+ MarketUtils_js_1.MarketUtils.getLiquidationIncentiveFactor(params);
71
72
  MarketParams._CACHE[this.id] = this;
72
73
  }
73
74
  }
75
+ exports.MarketParams = MarketParams;
@@ -1,6 +1,6 @@
1
1
  import { type RoundingDirection } from "../math/index.js";
2
2
  import type { BigIntish, MarketId } from "../types.js";
3
- import type { InputMarketParams } from "./MarketParams.js";
3
+ import type { IMarketParams } from "./MarketParams.js";
4
4
  /**
5
5
  * Namespace of utility functions to ease market-related calculations.
6
6
  */
@@ -9,7 +9,7 @@ export declare namespace MarketUtils {
9
9
  * Returns the id of a market based on its params.
10
10
  * @param market The market params.
11
11
  */
12
- function getMarketId(market: InputMarketParams): MarketId;
12
+ function getMarketId(market: IMarketParams): MarketId;
13
13
  /**
14
14
  * Returns the liquidation incentive factor for a given market params.
15
15
  * @param config The market params.
@@ -26,7 +26,7 @@ export declare namespace MarketUtils {
26
26
  totalBorrowAssets: BigIntish;
27
27
  }): bigint;
28
28
  /**
29
- * Returns the rate at which interest accrued on average for suppliers on the corresponding market,
29
+ * Returns the rate at which interest accrued for suppliers on the corresponding market,
30
30
  * since the last time the market was updated (scaled by WAD).
31
31
  * @param borrowRate The average borrow rate since the last market update (scaled by WAD).
32
32
  * @param market The market state.
@@ -36,10 +36,12 @@ export declare namespace MarketUtils {
36
36
  fee: BigIntish;
37
37
  }): bigint;
38
38
  /**
39
- * Returns the Annual Percentage Yield (APY) from an average rate, as calculated in Morpho Blue.
40
- * @param rate The average rate to convert to APY (scaled by WAD).
39
+ * Returns the per-second rate continuously compounded over the given period, as calculated in Morpho Blue (scaled by WAD).
40
+ * If the period is 1 year, the compounded rate correspond to the Annual Percentage Yield (APY)
41
+ * @param rate The per-second rate to compound (scaled by WAD).
42
+ * @param period The period to compound the rate over (in seconds). Defaults to 1 year.
41
43
  */
42
- function getApy(rate: BigIntish): bigint;
44
+ function compoundRate(rate: BigIntish, period?: BigIntish): bigint;
43
45
  /**
44
46
  * Returns the interest accrued on both sides of the given market
45
47
  * as well as the supply shares minted to the fee recipient.
@@ -95,68 +97,102 @@ export declare namespace MarketUtils {
95
97
  function getCollateralPower(collateral: BigIntish, { lltv }: {
96
98
  lltv: BigIntish;
97
99
  }): bigint;
100
+ /**
101
+ * Returns the value of a given amount of collateral quoted in loan assets.
102
+ * Return `undefined` iff the market's price is undefined.
103
+ */
98
104
  function getCollateralValue(collateral: BigIntish, { price }: {
99
- price: BigIntish;
100
- }): bigint;
105
+ price?: BigIntish;
106
+ }): bigint | undefined;
107
+ /**
108
+ * Returns the maximum debt allowed given a certain amount of collateral.
109
+ * Return `undefined` iff the market's price is undefined.
110
+ * To calculate the amount of loan assets that can be borrowed, use `getMaxBorrowableAssets`.
111
+ */
101
112
  function getMaxBorrowAssets(collateral: BigIntish, market: {
102
- price: BigIntish;
113
+ price?: BigIntish;
103
114
  }, { lltv }: {
104
115
  lltv: BigIntish;
105
- }): bigint;
116
+ }): bigint | undefined;
117
+ /**
118
+ * Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
119
+ * Return `undefined` iff the market's price is undefined.
120
+ */
106
121
  function getMaxBorrowableAssets({ collateral, borrowShares, }: {
107
122
  collateral: BigIntish;
108
123
  borrowShares: BigIntish;
109
124
  }, market: {
110
125
  totalBorrowAssets: BigIntish;
111
126
  totalBorrowShares: BigIntish;
112
- price: BigIntish;
127
+ price?: BigIntish;
113
128
  }, marketParams: {
114
129
  lltv: BigIntish;
115
- }): bigint;
130
+ }): bigint | undefined;
131
+ /**
132
+ * Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
133
+ * Return `undefined` iff the market's price is undefined.
134
+ */
116
135
  function getLiquidationSeizedAssets(repaidShares: BigIntish, market: {
117
136
  totalBorrowAssets: BigIntish;
118
137
  totalBorrowShares: BigIntish;
119
- price: BigIntish;
138
+ price?: BigIntish;
120
139
  }, config: {
121
140
  lltv: BigIntish;
122
- }): bigint;
141
+ }): bigint | undefined;
142
+ /**
143
+ * Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
144
+ * Return `undefined` iff the market's price is undefined.
145
+ */
123
146
  function getLiquidationRepaidShares(seizedAssets: BigIntish, market: {
124
147
  totalBorrowAssets: BigIntish;
125
148
  totalBorrowShares: BigIntish;
126
- price: BigIntish;
149
+ price?: BigIntish;
127
150
  }, config: {
128
151
  lltv: BigIntish;
129
- }): bigint;
152
+ }): bigint | undefined;
153
+ /**
154
+ * Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
155
+ * Return `undefined` iff the market's price is undefined.
156
+ */
130
157
  function getSeizableCollateral(position: {
131
158
  collateral: BigIntish;
132
159
  borrowShares: BigIntish;
133
160
  }, market: {
134
161
  totalBorrowAssets: BigIntish;
135
162
  totalBorrowShares: BigIntish;
136
- price: BigIntish;
163
+ price?: BigIntish;
137
164
  }, config: {
138
165
  lltv: BigIntish;
139
- }): bigint;
166
+ }): bigint | undefined;
167
+ /**
168
+ * Returns the amount of collateral that can be withdrawn given a certain borrow position.
169
+ * Return `undefined` iff the market's price is undefined.
170
+ */
140
171
  function getWithdrawableCollateral({ collateral, borrowShares, }: {
141
172
  collateral: BigIntish;
142
173
  borrowShares: BigIntish;
143
174
  }, market: {
144
175
  totalBorrowAssets: BigIntish;
145
176
  totalBorrowShares: BigIntish;
146
- price: BigIntish;
177
+ price?: BigIntish;
147
178
  }, { lltv }: {
148
179
  lltv: BigIntish;
149
- }): bigint;
180
+ }): bigint | undefined;
181
+ /**
182
+ * Returns whether a given borrow position is healthy.
183
+ * Return `undefined` iff the market's price is undefined.
184
+ * @param position The borrow position to check.
185
+ */
150
186
  function isHealthy({ collateral, borrowShares, }: {
151
187
  collateral: BigIntish;
152
188
  borrowShares: BigIntish;
153
189
  }, market: {
154
190
  totalBorrowAssets: BigIntish;
155
191
  totalBorrowShares: BigIntish;
156
- price: BigIntish;
192
+ price?: BigIntish;
157
193
  }, marketParams: {
158
194
  lltv: BigIntish;
159
- }): boolean;
195
+ }): boolean | undefined;
160
196
  /**
161
197
  * Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
162
198
  * that set the user's position to be liquidatable.
@@ -174,6 +210,7 @@ export declare namespace MarketUtils {
174
210
  /**
175
211
  * Returns the price variation required for the given position to reach its liquidation threshold (scaled by WAD).
176
212
  * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
213
+ * Returns `undefined` iff the market's price is undefined.
177
214
  * Returns null if the position is not a borrow.
178
215
  */
179
216
  function getPriceVariationToLiquidationPrice(position: {
@@ -182,38 +219,52 @@ export declare namespace MarketUtils {
182
219
  }, market: {
183
220
  totalBorrowAssets: BigIntish;
184
221
  totalBorrowShares: BigIntish;
185
- price: BigIntish;
222
+ price?: BigIntish;
186
223
  }, marketParams: {
187
224
  lltv: BigIntish;
188
- }): bigint | null;
225
+ }): bigint | null | undefined;
226
+ /**
227
+ * Returns the health factor of a given borrow position (scaled by WAD).
228
+ * Returns `undefined` iff the market's price is undefined.
229
+ * Returns null if the position is not a borrow.
230
+ */
189
231
  function getHealthFactor({ collateral, borrowShares, }: {
190
232
  collateral: BigIntish;
191
233
  borrowShares: BigIntish;
192
234
  }, market: {
193
235
  totalBorrowAssets: BigIntish;
194
236
  totalBorrowShares: BigIntish;
195
- price: BigIntish;
237
+ price?: BigIntish;
196
238
  }, marketParams: {
197
239
  lltv: BigIntish;
198
- }): bigint | null;
240
+ }): bigint | null | undefined;
241
+ /**
242
+ * Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
243
+ * Returns `undefined` iff the market's price is undefined.
244
+ * Returns null if the position is not a borrow.
245
+ */
199
246
  function getLtv({ collateral, borrowShares, }: {
200
247
  collateral: BigIntish;
201
248
  borrowShares: BigIntish;
202
249
  }, market: {
203
250
  totalBorrowAssets: BigIntish;
204
251
  totalBorrowShares: BigIntish;
205
- price: BigIntish;
206
- }): bigint | null;
252
+ price?: BigIntish;
253
+ }): bigint | null | undefined;
254
+ /**
255
+ * Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
256
+ * Returns `undefined` iff the market's price is undefined.
257
+ */
207
258
  function getBorrowCapacityUsage(position: {
208
259
  collateral: BigIntish;
209
260
  borrowShares: BigIntish;
210
261
  }, market: {
211
262
  totalBorrowAssets: BigIntish;
212
263
  totalBorrowShares: BigIntish;
213
- price: BigIntish;
264
+ price?: BigIntish;
214
265
  }, marketParams: {
215
266
  lltv: BigIntish;
216
- }): bigint | null;
267
+ }): bigint | undefined;
217
268
  function toSupplyAssets(shares: BigIntish, market: {
218
269
  totalSupplyAssets: BigIntish;
219
270
  totalSupplyShares: BigIntish;
@@ -1,21 +1,28 @@
1
- import keccak256 from "keccak256";
2
- import { LIQUIDATION_CURSOR, MAX_LIQUIDATION_INCENTIVE_FACTOR, ORACLE_PRICE_SCALE, SECONDS_PER_YEAR, } from "../constants.js";
3
- import { MathLib, SharesMath } from "../math/index.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MarketUtils = void 0;
4
+ const sha3_1 = require("@noble/hashes/sha3");
5
+ const utils_1 = require("@noble/hashes/utils");
6
+ const constants_js_1 = require("../constants.js");
7
+ const index_js_1 = require("../math/index.js");
4
8
  /**
5
9
  * Namespace of utility functions to ease market-related calculations.
6
10
  */
7
- export var MarketUtils;
11
+ var MarketUtils;
8
12
  (function (MarketUtils) {
9
13
  /**
10
14
  * Returns the id of a market based on its params.
11
15
  * @param market The market params.
12
16
  */
13
17
  function getMarketId(market) {
14
- return `0x${keccak256(`0x${market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
15
- market.collateralToken.substring(2).toLowerCase().padStart(64, "0") +
18
+ return `0x${(0, utils_1.bytesToHex)((0, sha3_1.keccak_256)((0, utils_1.hexToBytes)(`${market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
19
+ market.collateralToken
20
+ .substring(2)
21
+ .toLowerCase()
22
+ .padStart(64, "0") +
16
23
  market.oracle.substring(2).padStart(64, "0") +
17
24
  market.irm.substring(2).toLowerCase().padStart(64, "0") +
18
- BigInt(market.lltv).toString(16).padStart(64, "0")}`).toString("hex")}`;
25
+ BigInt(market.lltv).toString(16).padStart(64, "0")}`)))}`;
19
26
  }
20
27
  MarketUtils.getMarketId = getMarketId;
21
28
  /**
@@ -23,8 +30,8 @@ export var MarketUtils;
23
30
  * @param config The market params.
24
31
  */
25
32
  function getLiquidationIncentiveFactor({ lltv }) {
26
- return MathLib.min(MAX_LIQUIDATION_INCENTIVE_FACTOR, MathLib.wDivDown(MathLib.WAD, MathLib.WAD -
27
- MathLib.wMulDown(LIQUIDATION_CURSOR, MathLib.WAD - BigInt(lltv))));
33
+ 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 -
34
+ index_js_1.MathLib.wMulDown(constants_js_1.LIQUIDATION_CURSOR, index_js_1.MathLib.WAD - BigInt(lltv))));
28
35
  }
29
36
  MarketUtils.getLiquidationIncentiveFactor = getLiquidationIncentiveFactor;
30
37
  /**
@@ -36,31 +43,33 @@ export var MarketUtils;
36
43
  totalBorrowAssets = BigInt(totalBorrowAssets);
37
44
  if (totalSupplyAssets === 0n) {
38
45
  if (totalBorrowAssets > 0n)
39
- return MathLib.MAX_UINT_256;
46
+ return index_js_1.MathLib.MAX_UINT_256;
40
47
  return 0n;
41
48
  }
42
- return MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
49
+ return index_js_1.MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
43
50
  }
44
51
  MarketUtils.getUtilization = getUtilization;
45
52
  /**
46
- * Returns the rate at which interest accrued on average for suppliers on the corresponding market,
53
+ * Returns the rate at which interest accrued for suppliers on the corresponding market,
47
54
  * since the last time the market was updated (scaled by WAD).
48
55
  * @param borrowRate The average borrow rate since the last market update (scaled by WAD).
49
56
  * @param market The market state.
50
57
  */
51
58
  function getSupplyRate(borrowRate, { utilization, fee }) {
52
- const borrowRateWithoutFees = MathLib.wMulUp(borrowRate, utilization);
53
- return MathLib.wMulUp(borrowRateWithoutFees, MathLib.WAD - BigInt(fee));
59
+ const borrowRateWithoutFees = index_js_1.MathLib.wMulUp(borrowRate, utilization);
60
+ return index_js_1.MathLib.wMulUp(borrowRateWithoutFees, index_js_1.MathLib.WAD - BigInt(fee));
54
61
  }
55
62
  MarketUtils.getSupplyRate = getSupplyRate;
56
63
  /**
57
- * Returns the Annual Percentage Yield (APY) from an average rate, as calculated in Morpho Blue.
58
- * @param rate The average rate to convert to APY (scaled by WAD).
64
+ * Returns the per-second rate continuously compounded over the given period, as calculated in Morpho Blue (scaled by WAD).
65
+ * If the period is 1 year, the compounded rate correspond to the Annual Percentage Yield (APY)
66
+ * @param rate The per-second rate to compound (scaled by WAD).
67
+ * @param period The period to compound the rate over (in seconds). Defaults to 1 year.
59
68
  */
60
- function getApy(rate) {
61
- return MathLib.wTaylorCompounded(rate, SECONDS_PER_YEAR);
69
+ function compoundRate(rate, period = constants_js_1.SECONDS_PER_YEAR) {
70
+ return index_js_1.MathLib.wTaylorCompounded(rate, period);
62
71
  }
63
- MarketUtils.getApy = getApy;
72
+ MarketUtils.compoundRate = compoundRate;
64
73
  /**
65
74
  * Returns the interest accrued on both sides of the given market
66
75
  * as well as the supply shares minted to the fee recipient.
@@ -69,8 +78,8 @@ export var MarketUtils;
69
78
  * @param elapsed The time elapsed since the last market update (in seconds).
70
79
  */
71
80
  function getAccruedInterest(borrowRate, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }, elapsed = 0n) {
72
- const interest = MathLib.wMulDown(totalBorrowAssets, MathLib.wTaylorCompounded(borrowRate, elapsed));
73
- const feeAmount = MathLib.wMulDown(interest, fee);
81
+ const interest = index_js_1.MathLib.wMulDown(totalBorrowAssets, index_js_1.MathLib.wTaylorCompounded(borrowRate, elapsed));
82
+ const feeAmount = index_js_1.MathLib.wMulDown(interest, fee);
74
83
  const feeShares = toSupplyShares(feeAmount, {
75
84
  totalSupplyAssets: BigInt(totalSupplyAssets) - feeAmount,
76
85
  totalSupplyShares,
@@ -88,9 +97,9 @@ export var MarketUtils;
88
97
  if (utilization === 0n) {
89
98
  if (getUtilization(market) === 0n)
90
99
  return 0n;
91
- return MathLib.MAX_UINT_256;
100
+ return index_js_1.MathLib.MAX_UINT_256;
92
101
  }
93
- return MathLib.zeroFloorSub(MathLib.wDivUp(market.totalBorrowAssets, utilization), market.totalSupplyAssets);
102
+ return index_js_1.MathLib.zeroFloorSub(index_js_1.MathLib.wDivUp(market.totalBorrowAssets, utilization), market.totalSupplyAssets);
94
103
  }
95
104
  MarketUtils.getSupplyToUtilization = getSupplyToUtilization;
96
105
  /**
@@ -107,7 +116,7 @@ export var MarketUtils;
107
116
  return totalSupplyAssets;
108
117
  return 0n;
109
118
  }
110
- return MathLib.zeroFloorSub(totalSupplyAssets, MathLib.wDivUp(totalBorrowAssets, utilization));
119
+ return index_js_1.MathLib.zeroFloorSub(totalSupplyAssets, index_js_1.MathLib.wDivUp(totalBorrowAssets, utilization));
111
120
  }
112
121
  MarketUtils.getWithdrawToUtilization = getWithdrawToUtilization;
113
122
  /**
@@ -116,7 +125,7 @@ export var MarketUtils;
116
125
  * @param utilization The target utilization rate (scaled by WAD).
117
126
  */
118
127
  function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
119
- return MathLib.zeroFloorSub(MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
128
+ return index_js_1.MathLib.zeroFloorSub(index_js_1.MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
120
129
  }
121
130
  MarketUtils.getBorrowToUtilization = getBorrowToUtilization;
122
131
  /**
@@ -125,53 +134,105 @@ export var MarketUtils;
125
134
  * @param utilization The target utilization rate (scaled by WAD).
126
135
  */
127
136
  function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
128
- return MathLib.zeroFloorSub(totalBorrowAssets, MathLib.wMulDown(totalSupplyAssets, utilization));
137
+ return index_js_1.MathLib.zeroFloorSub(totalBorrowAssets, index_js_1.MathLib.wMulDown(totalSupplyAssets, utilization));
129
138
  }
130
139
  MarketUtils.getRepayToUtilization = getRepayToUtilization;
131
140
  function getCollateralPower(collateral, { lltv }) {
132
- return MathLib.wMulDown(collateral, lltv);
141
+ return index_js_1.MathLib.wMulDown(collateral, lltv);
133
142
  }
134
143
  MarketUtils.getCollateralPower = getCollateralPower;
144
+ /**
145
+ * Returns the value of a given amount of collateral quoted in loan assets.
146
+ * Return `undefined` iff the market's price is undefined.
147
+ */
135
148
  function getCollateralValue(collateral, { price }) {
136
- return MathLib.mulDivDown(collateral, price, ORACLE_PRICE_SCALE);
149
+ if (price == null)
150
+ return;
151
+ return index_js_1.MathLib.mulDivDown(collateral, price, constants_js_1.ORACLE_PRICE_SCALE);
137
152
  }
138
153
  MarketUtils.getCollateralValue = getCollateralValue;
154
+ /**
155
+ * Returns the maximum debt allowed given a certain amount of collateral.
156
+ * Return `undefined` iff the market's price is undefined.
157
+ * To calculate the amount of loan assets that can be borrowed, use `getMaxBorrowableAssets`.
158
+ */
139
159
  function getMaxBorrowAssets(collateral, market, { lltv }) {
140
- return MathLib.wMulDown(getCollateralValue(collateral, market), lltv);
160
+ const collateralValue = getCollateralValue(collateral, market);
161
+ if (collateralValue == null)
162
+ return;
163
+ return index_js_1.MathLib.wMulDown(collateralValue, lltv);
141
164
  }
142
165
  MarketUtils.getMaxBorrowAssets = getMaxBorrowAssets;
166
+ /**
167
+ * Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
168
+ * Return `undefined` iff the market's price is undefined.
169
+ */
143
170
  function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketParams) {
144
- return MathLib.zeroFloorSub(getMaxBorrowAssets(collateral, market, marketParams), toBorrowAssets(borrowShares, market));
171
+ const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
172
+ if (maxBorrowAssets == null)
173
+ return;
174
+ return index_js_1.MathLib.zeroFloorSub(maxBorrowAssets, toBorrowAssets(borrowShares, market));
145
175
  }
146
176
  MarketUtils.getMaxBorrowableAssets = getMaxBorrowableAssets;
177
+ /**
178
+ * Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
179
+ * Return `undefined` iff the market's price is undefined.
180
+ */
147
181
  function getLiquidationSeizedAssets(repaidShares, market, config) {
182
+ if (market.price == null)
183
+ return;
148
184
  market.price = BigInt(market.price);
149
185
  if (market.price === 0n)
150
186
  return 0n;
151
- return MathLib.mulDivDown(MathLib.wMulDown(toBorrowAssets(repaidShares, market, "Down"), getLiquidationIncentiveFactor(config)), ORACLE_PRICE_SCALE, market.price);
187
+ return index_js_1.MathLib.mulDivDown(index_js_1.MathLib.wMulDown(toBorrowAssets(repaidShares, market, "Down"), getLiquidationIncentiveFactor(config)), constants_js_1.ORACLE_PRICE_SCALE, market.price);
152
188
  }
153
189
  MarketUtils.getLiquidationSeizedAssets = getLiquidationSeizedAssets;
190
+ /**
191
+ * Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
192
+ * Return `undefined` iff the market's price is undefined.
193
+ */
154
194
  function getLiquidationRepaidShares(seizedAssets, market, config) {
155
- return toBorrowShares(MathLib.wDivUp(MathLib.mulDivUp(seizedAssets, market.price, ORACLE_PRICE_SCALE), getLiquidationIncentiveFactor(config)), market, "Up");
195
+ if (market.price == null)
196
+ return;
197
+ return toBorrowShares(index_js_1.MathLib.wDivUp(index_js_1.MathLib.mulDivUp(seizedAssets, market.price, constants_js_1.ORACLE_PRICE_SCALE), getLiquidationIncentiveFactor(config)), market, "Up");
156
198
  }
157
199
  MarketUtils.getLiquidationRepaidShares = getLiquidationRepaidShares;
200
+ /**
201
+ * Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
202
+ * Return `undefined` iff the market's price is undefined.
203
+ */
158
204
  function getSeizableCollateral(position, market, config) {
205
+ if (market.price == null)
206
+ return; // Must be checked before calling `isHealthy`.
159
207
  market.price = BigInt(market.price);
160
208
  if (market.price === 0n || isHealthy(position, market, config))
161
209
  return 0n;
162
- return MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
210
+ return index_js_1.MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
163
211
  }
164
212
  MarketUtils.getSeizableCollateral = getSeizableCollateral;
213
+ /**
214
+ * Returns the amount of collateral that can be withdrawn given a certain borrow position.
215
+ * Return `undefined` iff the market's price is undefined.
216
+ */
165
217
  function getWithdrawableCollateral({ collateral, borrowShares, }, market, { lltv }) {
218
+ if (market.price == null)
219
+ return;
166
220
  market.price = BigInt(market.price);
167
221
  if (market.price === 0n)
168
222
  return 0n;
169
- return MathLib.zeroFloorSub(collateral, MathLib.wDivUp(MathLib.mulDivUp(toBorrowAssets(borrowShares, market), ORACLE_PRICE_SCALE, market.price), lltv));
223
+ return index_js_1.MathLib.zeroFloorSub(collateral, index_js_1.MathLib.wDivUp(index_js_1.MathLib.mulDivUp(toBorrowAssets(borrowShares, market), constants_js_1.ORACLE_PRICE_SCALE, market.price), lltv));
170
224
  }
171
225
  MarketUtils.getWithdrawableCollateral = getWithdrawableCollateral;
226
+ /**
227
+ * Returns whether a given borrow position is healthy.
228
+ * Return `undefined` iff the market's price is undefined.
229
+ * @param position The borrow position to check.
230
+ */
172
231
  function isHealthy({ collateral, borrowShares, }, market, marketParams) {
173
- return (getMaxBorrowAssets(collateral, market, marketParams) >=
174
- toBorrowAssets(borrowShares, market));
232
+ const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
233
+ if (maxBorrowAssets == null)
234
+ return;
235
+ return maxBorrowAssets >= toBorrowAssets(borrowShares, market);
175
236
  }
176
237
  MarketUtils.isHealthy = isHealthy;
177
238
  /**
@@ -186,26 +247,34 @@ export var MarketUtils;
186
247
  return null;
187
248
  const collateralPower = getCollateralPower(collateral, marketParams);
188
249
  if (collateralPower === 0n)
189
- return MathLib.MAX_UINT_256;
250
+ return index_js_1.MathLib.MAX_UINT_256;
190
251
  const borrowAssets = toBorrowAssets(borrowShares, market);
191
- return MathLib.mulDivUp(borrowAssets, ORACLE_PRICE_SCALE, collateralPower);
252
+ return index_js_1.MathLib.mulDivUp(borrowAssets, constants_js_1.ORACLE_PRICE_SCALE, collateralPower);
192
253
  }
193
254
  MarketUtils.getLiquidationPrice = getLiquidationPrice;
194
255
  /**
195
256
  * Returns the price variation required for the given position to reach its liquidation threshold (scaled by WAD).
196
257
  * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
258
+ * Returns `undefined` iff the market's price is undefined.
197
259
  * Returns null if the position is not a borrow.
198
260
  */
199
261
  function getPriceVariationToLiquidationPrice(position, market, marketParams) {
262
+ if (market.price == null)
263
+ return;
200
264
  market.price = BigInt(market.price);
201
265
  if (market.price === 0n)
202
266
  return null;
203
267
  const liquidationPrice = getLiquidationPrice(position, market, marketParams);
204
268
  if (liquidationPrice == null)
205
269
  return null;
206
- return MathLib.wDivUp(liquidationPrice, market.price) - MathLib.WAD;
270
+ return index_js_1.MathLib.wDivUp(liquidationPrice, market.price) - index_js_1.MathLib.WAD;
207
271
  }
208
272
  MarketUtils.getPriceVariationToLiquidationPrice = getPriceVariationToLiquidationPrice;
273
+ /**
274
+ * Returns the health factor of a given borrow position (scaled by WAD).
275
+ * Returns `undefined` iff the market's price is undefined.
276
+ * Returns null if the position is not a borrow.
277
+ */
209
278
  function getHealthFactor({ collateral, borrowShares, }, market, marketParams) {
210
279
  borrowShares = BigInt(borrowShares);
211
280
  market.totalBorrowShares = BigInt(market.totalBorrowShares);
@@ -213,45 +282,60 @@ export var MarketUtils;
213
282
  return null;
214
283
  const borrowAssets = toBorrowAssets(borrowShares, market);
215
284
  if (borrowAssets === 0n)
216
- return MathLib.MAX_UINT_256;
285
+ return index_js_1.MathLib.MAX_UINT_256;
217
286
  const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
218
- return MathLib.wDivDown(maxBorrowAssets, borrowAssets);
287
+ if (maxBorrowAssets == null)
288
+ return;
289
+ return index_js_1.MathLib.wDivDown(maxBorrowAssets, borrowAssets);
219
290
  }
220
291
  MarketUtils.getHealthFactor = getHealthFactor;
292
+ /**
293
+ * Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
294
+ * Returns `undefined` iff the market's price is undefined.
295
+ * Returns null if the position is not a borrow.
296
+ */
221
297
  function getLtv({ collateral, borrowShares, }, market) {
222
298
  borrowShares = BigInt(borrowShares);
223
299
  market.totalBorrowShares = BigInt(market.totalBorrowShares);
224
300
  if (borrowShares === 0n || market.totalBorrowShares === 0n)
225
301
  return null;
226
302
  const collateralValue = getCollateralValue(collateral, market);
303
+ if (collateralValue == null)
304
+ return;
227
305
  if (collateralValue === 0n)
228
- return MathLib.MAX_UINT_256;
229
- return MathLib.wDivUp(toBorrowAssets(borrowShares, market), collateralValue);
306
+ return index_js_1.MathLib.MAX_UINT_256;
307
+ return index_js_1.MathLib.wDivUp(toBorrowAssets(borrowShares, market), collateralValue);
230
308
  }
231
309
  MarketUtils.getLtv = getLtv;
310
+ /**
311
+ * Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
312
+ * Returns `undefined` iff the market's price is undefined.
313
+ */
232
314
  function getBorrowCapacityUsage(position, market, marketParams) {
233
315
  const hf = getHealthFactor(position, market, marketParams);
316
+ if (hf === undefined)
317
+ return;
234
318
  if (hf === null)
235
- return null;
319
+ return 0n;
236
320
  if (hf === 0n)
237
- return MathLib.MAX_UINT_256;
238
- return MathLib.wDivUp(MathLib.WAD, hf);
321
+ return index_js_1.MathLib.MAX_UINT_256;
322
+ return index_js_1.MathLib.wDivUp(index_js_1.MathLib.WAD, hf);
239
323
  }
240
324
  MarketUtils.getBorrowCapacityUsage = getBorrowCapacityUsage;
241
325
  function toSupplyAssets(shares, market, rounding = "Down") {
242
- return SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
326
+ return index_js_1.SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
243
327
  }
244
328
  MarketUtils.toSupplyAssets = toSupplyAssets;
245
329
  function toSupplyShares(assets, market, rounding = "Up") {
246
- return SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
330
+ return index_js_1.SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
247
331
  }
248
332
  MarketUtils.toSupplyShares = toSupplyShares;
249
333
  function toBorrowAssets(shares, market, rounding = "Up") {
250
- return SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
334
+ return index_js_1.SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
251
335
  }
252
336
  MarketUtils.toBorrowAssets = toBorrowAssets;
253
337
  function toBorrowShares(assets, market, rounding = "Down") {
254
- return SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
338
+ return index_js_1.SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
255
339
  }
256
340
  MarketUtils.toBorrowShares = toBorrowShares;
257
- })(MarketUtils || (MarketUtils = {}));
341
+ })(MarketUtils || (exports.MarketUtils = MarketUtils = {}));