@morpho-org/blue-sdk 2.0.0-next.2 → 2.0.0-next.21

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