@morpho-org/blue-sdk 2.0.0-test.1 → 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 (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +46 -26
  3. package/lib/addresses.d.ts +59 -55
  4. package/lib/addresses.js +42 -43
  5. package/lib/chain.d.ts +1 -1
  6. package/lib/chain.js +3 -3
  7. package/lib/errors.d.ts +7 -8
  8. package/lib/errors.js +21 -19
  9. package/lib/holding/AssetBalances.d.ts +3 -3
  10. package/lib/holding/Holding.d.ts +10 -5
  11. package/lib/holding/Holding.js +14 -4
  12. package/lib/holding/index.d.ts +2 -2
  13. package/lib/holding/index.js +2 -2
  14. package/lib/index.d.ts +12 -25
  15. package/lib/index.js +12 -38
  16. package/lib/market/Market.d.ts +48 -34
  17. package/lib/market/Market.js +76 -57
  18. package/lib/market/{MarketConfig.d.ts → MarketParams.d.ts} +8 -7
  19. package/lib/market/{MarketConfig.js → MarketParams.js} +20 -21
  20. package/lib/market/MarketUtils.d.ts +94 -42
  21. package/lib/market/MarketUtils.js +145 -67
  22. package/lib/market/index.d.ts +3 -3
  23. package/lib/market/index.js +3 -3
  24. package/lib/{maths → math}/AdaptiveCurveIrmLib.d.ts +1 -1
  25. package/lib/{maths → math}/AdaptiveCurveIrmLib.js +20 -20
  26. package/lib/{maths → math}/MathLib.d.ts +24 -42
  27. package/lib/{maths → math}/MathLib.js +41 -54
  28. package/lib/{maths → math}/SharesMath.d.ts +2 -2
  29. package/lib/{maths → math}/SharesMath.js +3 -3
  30. package/lib/math/index.d.ts +3 -0
  31. package/lib/{helpers/format → math}/index.js +3 -1
  32. package/lib/position/Position.d.ts +35 -25
  33. package/lib/position/Position.js +46 -44
  34. package/lib/position/index.d.ts +1 -1
  35. package/lib/position/index.js +1 -1
  36. package/lib/token/ConstantWrappedToken.d.ts +6 -7
  37. package/lib/token/ConstantWrappedToken.js +8 -10
  38. package/lib/token/ExchangeRateWrappedToken.d.ts +5 -5
  39. package/lib/token/ExchangeRateWrappedToken.js +5 -5
  40. package/lib/token/Token.d.ts +18 -18
  41. package/lib/token/Token.js +24 -27
  42. package/lib/token/VaultToken.d.ts +20 -11
  43. package/lib/token/VaultToken.js +15 -7
  44. package/lib/token/WrappedToken.d.ts +4 -4
  45. package/lib/token/WrappedToken.js +7 -7
  46. package/lib/token/index.d.ts +5 -5
  47. package/lib/token/index.js +5 -5
  48. package/lib/types.d.ts +3 -6
  49. package/lib/user/User.d.ts +1 -1
  50. package/lib/user/index.d.ts +1 -1
  51. package/lib/user/index.js +1 -1
  52. package/lib/vault/Vault.d.ts +22 -28
  53. package/lib/vault/Vault.js +43 -51
  54. package/lib/vault/VaultConfig.d.ts +6 -7
  55. package/lib/vault/VaultConfig.js +5 -5
  56. package/lib/vault/VaultMarketAllocation.d.ts +8 -8
  57. package/lib/vault/VaultMarketAllocation.js +3 -3
  58. package/lib/vault/VaultMarketConfig.d.ts +10 -10
  59. package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +6 -6
  60. package/lib/vault/VaultUser.d.ts +26 -0
  61. package/lib/vault/VaultUser.js +28 -0
  62. package/lib/vault/VaultUtils.d.ts +4 -6
  63. package/lib/vault/VaultUtils.js +6 -6
  64. package/lib/vault/index.d.ts +7 -6
  65. package/lib/vault/index.js +7 -6
  66. package/package.json +25 -56
  67. package/lib/chain.test.d.ts +0 -1
  68. package/lib/chain.test.js +0 -21
  69. package/lib/helpers/format/format.d.ts +0 -122
  70. package/lib/helpers/format/format.js +0 -286
  71. package/lib/helpers/format/format.test.d.ts +0 -1
  72. package/lib/helpers/format/format.test.js +0 -255
  73. package/lib/helpers/format/index.d.ts +0 -1
  74. package/lib/helpers/index.d.ts +0 -2
  75. package/lib/helpers/index.js +0 -18
  76. package/lib/helpers/locale.d.ts +0 -46
  77. package/lib/helpers/locale.js +0 -96
  78. package/lib/market/MarketUtils.test.d.ts +0 -1
  79. package/lib/market/MarketUtils.test.js +0 -38
  80. package/lib/maths/index.d.ts +0 -3
  81. package/lib/maths/index.js +0 -19
  82. package/lib/tests/mocks/markets.d.ts +0 -19
  83. package/lib/tests/mocks/markets.js +0 -121
@@ -1,6 +1,6 @@
1
- import { RoundingDirection } from "../maths";
2
- import { BigIntish, MarketId } from "../types";
3
- import { MarketParams } from "./MarketConfig";
1
+ import { type RoundingDirection } from "../math/index.js";
2
+ import type { BigIntish, MarketId } from "../types.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: MarketParams): 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,72 +97,106 @@ 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;
113
- }, marketConfig: {
127
+ price?: BigIntish;
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;
157
- }, marketConfig: {
192
+ price?: BigIntish;
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.
163
- * Returns null if the user is not a borrower
199
+ * Returns null if the position is not a borrow.
164
200
  */
165
201
  function getLiquidationPrice({ collateral, borrowShares, }: {
166
202
  collateral: BigIntish;
@@ -168,51 +204,67 @@ export declare namespace MarketUtils {
168
204
  }, market: {
169
205
  totalBorrowAssets: BigIntish;
170
206
  totalBorrowShares: BigIntish;
171
- }, marketConfig: {
207
+ }, marketParams: {
172
208
  lltv: BigIntish;
173
209
  }): bigint | null;
174
210
  /**
175
- * Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
176
- * @param position The borrow position to consider.
211
+ * Returns the price variation required for the given position to reach its liquidation threshold (scaled by WAD).
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.
214
+ * Returns null if the position is not a borrow.
177
215
  */
178
- function getPriceVariationToLiquidation(position: {
216
+ function getPriceVariationToLiquidationPrice(position: {
179
217
  collateral: BigIntish;
180
218
  borrowShares: BigIntish;
181
219
  }, market: {
182
220
  totalBorrowAssets: BigIntish;
183
221
  totalBorrowShares: BigIntish;
184
- price: BigIntish;
185
- }, marketConfig: {
222
+ price?: BigIntish;
223
+ }, marketParams: {
186
224
  lltv: BigIntish;
187
- }): 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
+ */
188
231
  function getHealthFactor({ collateral, borrowShares, }: {
189
232
  collateral: BigIntish;
190
233
  borrowShares: BigIntish;
191
234
  }, market: {
192
235
  totalBorrowAssets: BigIntish;
193
236
  totalBorrowShares: BigIntish;
194
- price: BigIntish;
195
- }, marketConfig: {
237
+ price?: BigIntish;
238
+ }, marketParams: {
196
239
  lltv: BigIntish;
197
- }): 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
+ */
198
246
  function getLtv({ collateral, borrowShares, }: {
199
247
  collateral: BigIntish;
200
248
  borrowShares: BigIntish;
201
249
  }, market: {
202
250
  totalBorrowAssets: BigIntish;
203
251
  totalBorrowShares: BigIntish;
204
- price: BigIntish;
205
- }): 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
+ */
206
258
  function getBorrowCapacityUsage(position: {
207
259
  collateral: BigIntish;
208
260
  borrowShares: BigIntish;
209
261
  }, market: {
210
262
  totalBorrowAssets: BigIntish;
211
263
  totalBorrowShares: BigIntish;
212
- price: BigIntish;
213
- }, marketConfig: {
264
+ price?: BigIntish;
265
+ }, marketParams: {
214
266
  lltv: BigIntish;
215
- }): bigint | null;
267
+ }): bigint | undefined;
216
268
  function toSupplyAssets(shares: BigIntish, market: {
217
269
  totalSupplyAssets: BigIntish;
218
270
  totalSupplyShares: BigIntish;
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.MarketUtils = void 0;
7
- const keccak256_1 = __importDefault(require("keccak256"));
8
- const constants_1 = require("../constants");
9
- const maths_1 = require("../maths");
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");
10
8
  /**
11
9
  * Namespace of utility functions to ease market-related calculations.
12
10
  */
@@ -17,12 +15,14 @@ var MarketUtils;
17
15
  * @param market The market params.
18
16
  */
19
17
  function getMarketId(market) {
20
- return `0x${(0, keccak256_1.default)("0x" +
21
- market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
22
- 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") +
23
23
  market.oracle.substring(2).padStart(64, "0") +
24
24
  market.irm.substring(2).toLowerCase().padStart(64, "0") +
25
- BigInt(market.lltv).toString(16).padStart(64, "0")).toString("hex")}`;
25
+ BigInt(market.lltv).toString(16).padStart(64, "0")}`)))}`;
26
26
  }
27
27
  MarketUtils.getMarketId = getMarketId;
28
28
  /**
@@ -30,8 +30,8 @@ var MarketUtils;
30
30
  * @param config The market params.
31
31
  */
32
32
  function getLiquidationIncentiveFactor({ lltv }) {
33
- return maths_1.MathLib.min(constants_1.MAX_LIQUIDATION_INCENTIVE_FACTOR, maths_1.MathLib.wDivDown(maths_1.MathLib.WAD, maths_1.MathLib.WAD -
34
- maths_1.MathLib.wMulDown(constants_1.LIQUIDATION_CURSOR, maths_1.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))));
35
35
  }
36
36
  MarketUtils.getLiquidationIncentiveFactor = getLiquidationIncentiveFactor;
37
37
  /**
@@ -43,31 +43,33 @@ var MarketUtils;
43
43
  totalBorrowAssets = BigInt(totalBorrowAssets);
44
44
  if (totalSupplyAssets === 0n) {
45
45
  if (totalBorrowAssets > 0n)
46
- return maths_1.MathLib.MAX_UINT_256;
46
+ return index_js_1.MathLib.MAX_UINT_256;
47
47
  return 0n;
48
48
  }
49
- return maths_1.MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
49
+ return index_js_1.MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
50
50
  }
51
51
  MarketUtils.getUtilization = getUtilization;
52
52
  /**
53
- * 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,
54
54
  * since the last time the market was updated (scaled by WAD).
55
55
  * @param borrowRate The average borrow rate since the last market update (scaled by WAD).
56
56
  * @param market The market state.
57
57
  */
58
58
  function getSupplyRate(borrowRate, { utilization, fee }) {
59
- const borrowRateWithoutFees = maths_1.MathLib.wMulUp(borrowRate, utilization);
60
- return maths_1.MathLib.wMulUp(borrowRateWithoutFees, maths_1.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));
61
61
  }
62
62
  MarketUtils.getSupplyRate = getSupplyRate;
63
63
  /**
64
- * Returns the Annual Percentage Yield (APY) from an average rate, as calculated in Morpho Blue.
65
- * @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.
66
68
  */
67
- function getApy(rate) {
68
- return maths_1.MathLib.wTaylorCompounded(rate, constants_1.SECONDS_PER_YEAR);
69
+ function compoundRate(rate, period = constants_js_1.SECONDS_PER_YEAR) {
70
+ return index_js_1.MathLib.wTaylorCompounded(rate, period);
69
71
  }
70
- MarketUtils.getApy = getApy;
72
+ MarketUtils.compoundRate = compoundRate;
71
73
  /**
72
74
  * Returns the interest accrued on both sides of the given market
73
75
  * as well as the supply shares minted to the fee recipient.
@@ -76,8 +78,8 @@ var MarketUtils;
76
78
  * @param elapsed The time elapsed since the last market update (in seconds).
77
79
  */
78
80
  function getAccruedInterest(borrowRate, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }, elapsed = 0n) {
79
- const interest = maths_1.MathLib.wMulDown(totalBorrowAssets, maths_1.MathLib.wTaylorCompounded(borrowRate, elapsed));
80
- const feeAmount = maths_1.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);
81
83
  const feeShares = toSupplyShares(feeAmount, {
82
84
  totalSupplyAssets: BigInt(totalSupplyAssets) - feeAmount,
83
85
  totalSupplyShares,
@@ -95,9 +97,9 @@ var MarketUtils;
95
97
  if (utilization === 0n) {
96
98
  if (getUtilization(market) === 0n)
97
99
  return 0n;
98
- return maths_1.MathLib.MAX_UINT_256;
100
+ return index_js_1.MathLib.MAX_UINT_256;
99
101
  }
100
- return maths_1.MathLib.zeroFloorSub(maths_1.MathLib.wDivUp(market.totalBorrowAssets, utilization), market.totalSupplyAssets);
102
+ return index_js_1.MathLib.zeroFloorSub(index_js_1.MathLib.wDivUp(market.totalBorrowAssets, utilization), market.totalSupplyAssets);
101
103
  }
102
104
  MarketUtils.getSupplyToUtilization = getSupplyToUtilization;
103
105
  /**
@@ -114,7 +116,7 @@ var MarketUtils;
114
116
  return totalSupplyAssets;
115
117
  return 0n;
116
118
  }
117
- return maths_1.MathLib.zeroFloorSub(totalSupplyAssets, maths_1.MathLib.wDivUp(totalBorrowAssets, utilization));
119
+ return index_js_1.MathLib.zeroFloorSub(totalSupplyAssets, index_js_1.MathLib.wDivUp(totalBorrowAssets, utilization));
118
120
  }
119
121
  MarketUtils.getWithdrawToUtilization = getWithdrawToUtilization;
120
122
  /**
@@ -123,7 +125,7 @@ var MarketUtils;
123
125
  * @param utilization The target utilization rate (scaled by WAD).
124
126
  */
125
127
  function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
126
- return maths_1.MathLib.zeroFloorSub(maths_1.MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
128
+ return index_js_1.MathLib.zeroFloorSub(index_js_1.MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
127
129
  }
128
130
  MarketUtils.getBorrowToUtilization = getBorrowToUtilization;
129
131
  /**
@@ -132,132 +134,208 @@ var MarketUtils;
132
134
  * @param utilization The target utilization rate (scaled by WAD).
133
135
  */
134
136
  function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
135
- return maths_1.MathLib.zeroFloorSub(totalBorrowAssets, maths_1.MathLib.wMulDown(totalSupplyAssets, utilization));
137
+ return index_js_1.MathLib.zeroFloorSub(totalBorrowAssets, index_js_1.MathLib.wMulDown(totalSupplyAssets, utilization));
136
138
  }
137
139
  MarketUtils.getRepayToUtilization = getRepayToUtilization;
138
140
  function getCollateralPower(collateral, { lltv }) {
139
- return maths_1.MathLib.wMulDown(collateral, lltv);
141
+ return index_js_1.MathLib.wMulDown(collateral, lltv);
140
142
  }
141
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
+ */
142
148
  function getCollateralValue(collateral, { price }) {
143
- return maths_1.MathLib.mulDivDown(collateral, price, constants_1.ORACLE_PRICE_SCALE);
149
+ if (price == null)
150
+ return;
151
+ return index_js_1.MathLib.mulDivDown(collateral, price, constants_js_1.ORACLE_PRICE_SCALE);
144
152
  }
145
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
+ */
146
159
  function getMaxBorrowAssets(collateral, market, { lltv }) {
147
- return maths_1.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);
148
164
  }
149
165
  MarketUtils.getMaxBorrowAssets = getMaxBorrowAssets;
150
- function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketConfig) {
151
- return maths_1.MathLib.zeroFloorSub(getMaxBorrowAssets(collateral, market, marketConfig), toBorrowAssets(borrowShares, market));
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
+ */
170
+ function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketParams) {
171
+ const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
172
+ if (maxBorrowAssets == null)
173
+ return;
174
+ return index_js_1.MathLib.zeroFloorSub(maxBorrowAssets, toBorrowAssets(borrowShares, market));
152
175
  }
153
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
+ */
154
181
  function getLiquidationSeizedAssets(repaidShares, market, config) {
182
+ if (market.price == null)
183
+ return;
155
184
  market.price = BigInt(market.price);
156
185
  if (market.price === 0n)
157
186
  return 0n;
158
- return maths_1.MathLib.mulDivDown(maths_1.MathLib.wMulDown(toBorrowAssets(repaidShares, market, "Down"), getLiquidationIncentiveFactor(config)), constants_1.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);
159
188
  }
160
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
+ */
161
194
  function getLiquidationRepaidShares(seizedAssets, market, config) {
162
- return toBorrowShares(maths_1.MathLib.wDivUp(maths_1.MathLib.mulDivUp(seizedAssets, market.price, constants_1.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");
163
198
  }
164
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
+ */
165
204
  function getSeizableCollateral(position, market, config) {
205
+ if (market.price == null)
206
+ return; // Must be checked before calling `isHealthy`.
166
207
  market.price = BigInt(market.price);
167
208
  if (market.price === 0n || isHealthy(position, market, config))
168
209
  return 0n;
169
- return maths_1.MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
210
+ return index_js_1.MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
170
211
  }
171
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
+ */
172
217
  function getWithdrawableCollateral({ collateral, borrowShares, }, market, { lltv }) {
218
+ if (market.price == null)
219
+ return;
173
220
  market.price = BigInt(market.price);
174
221
  if (market.price === 0n)
175
222
  return 0n;
176
- return maths_1.MathLib.zeroFloorSub(collateral, maths_1.MathLib.wDivUp(maths_1.MathLib.mulDivUp(toBorrowAssets(borrowShares, market), constants_1.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));
177
224
  }
178
225
  MarketUtils.getWithdrawableCollateral = getWithdrawableCollateral;
179
- function isHealthy({ collateral, borrowShares, }, market, marketConfig) {
180
- return (getMaxBorrowAssets(collateral, market, marketConfig) >=
181
- toBorrowAssets(borrowShares, market));
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
+ */
231
+ function isHealthy({ collateral, borrowShares, }, market, marketParams) {
232
+ const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
233
+ if (maxBorrowAssets == null)
234
+ return;
235
+ return maxBorrowAssets >= toBorrowAssets(borrowShares, market);
182
236
  }
183
237
  MarketUtils.isHealthy = isHealthy;
184
238
  /**
185
239
  * Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
186
240
  * that set the user's position to be liquidatable.
187
- * Returns null if the user is not a borrower
241
+ * Returns null if the position is not a borrow.
188
242
  */
189
- function getLiquidationPrice({ collateral, borrowShares, }, market, marketConfig) {
243
+ function getLiquidationPrice({ collateral, borrowShares, }, market, marketParams) {
190
244
  borrowShares = BigInt(borrowShares);
191
245
  market.totalBorrowShares = BigInt(market.totalBorrowShares);
192
246
  if (borrowShares === 0n || market.totalBorrowShares === 0n)
193
247
  return null;
194
- const collateralPower = getCollateralPower(collateral, marketConfig);
248
+ const collateralPower = getCollateralPower(collateral, marketParams);
195
249
  if (collateralPower === 0n)
196
- return maths_1.MathLib.MAX_UINT_256;
250
+ return index_js_1.MathLib.MAX_UINT_256;
197
251
  const borrowAssets = toBorrowAssets(borrowShares, market);
198
- return maths_1.MathLib.mulDivUp(borrowAssets, constants_1.ORACLE_PRICE_SCALE, collateralPower);
252
+ return index_js_1.MathLib.mulDivUp(borrowAssets, constants_js_1.ORACLE_PRICE_SCALE, collateralPower);
199
253
  }
200
254
  MarketUtils.getLiquidationPrice = getLiquidationPrice;
201
255
  /**
202
- * Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
203
- * @param position The borrow position to consider.
256
+ * Returns the price variation required for the given position to reach its liquidation threshold (scaled by WAD).
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.
259
+ * Returns null if the position is not a borrow.
204
260
  */
205
- function getPriceVariationToLiquidation(position, market, marketConfig) {
261
+ function getPriceVariationToLiquidationPrice(position, market, marketParams) {
262
+ if (market.price == null)
263
+ return;
206
264
  market.price = BigInt(market.price);
207
265
  if (market.price === 0n)
208
266
  return null;
209
- const liquidationPrice = getLiquidationPrice(position, market, marketConfig);
267
+ const liquidationPrice = getLiquidationPrice(position, market, marketParams);
210
268
  if (liquidationPrice == null)
211
269
  return null;
212
- return maths_1.MathLib.WAD - maths_1.MathLib.wDivUp(liquidationPrice, market.price);
270
+ return index_js_1.MathLib.wDivUp(liquidationPrice, market.price) - index_js_1.MathLib.WAD;
213
271
  }
214
- MarketUtils.getPriceVariationToLiquidation = getPriceVariationToLiquidation;
215
- function getHealthFactor({ collateral, borrowShares, }, market, marketConfig) {
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
+ */
278
+ function getHealthFactor({ collateral, borrowShares, }, market, marketParams) {
216
279
  borrowShares = BigInt(borrowShares);
217
280
  market.totalBorrowShares = BigInt(market.totalBorrowShares);
218
281
  if (borrowShares === 0n || market.totalBorrowShares === 0n)
219
282
  return null;
220
283
  const borrowAssets = toBorrowAssets(borrowShares, market);
221
284
  if (borrowAssets === 0n)
222
- return maths_1.MathLib.MAX_UINT_256;
223
- const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketConfig);
224
- return maths_1.MathLib.wDivDown(maxBorrowAssets, borrowAssets);
285
+ return index_js_1.MathLib.MAX_UINT_256;
286
+ const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
287
+ if (maxBorrowAssets == null)
288
+ return;
289
+ return index_js_1.MathLib.wDivDown(maxBorrowAssets, borrowAssets);
225
290
  }
226
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
+ */
227
297
  function getLtv({ collateral, borrowShares, }, market) {
228
298
  borrowShares = BigInt(borrowShares);
229
299
  market.totalBorrowShares = BigInt(market.totalBorrowShares);
230
300
  if (borrowShares === 0n || market.totalBorrowShares === 0n)
231
301
  return null;
232
302
  const collateralValue = getCollateralValue(collateral, market);
303
+ if (collateralValue == null)
304
+ return;
233
305
  if (collateralValue === 0n)
234
- return maths_1.MathLib.MAX_UINT_256;
235
- return maths_1.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);
236
308
  }
237
309
  MarketUtils.getLtv = getLtv;
238
- function getBorrowCapacityUsage(position, market, marketConfig) {
239
- const hf = getHealthFactor(position, market, marketConfig);
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
+ */
314
+ function getBorrowCapacityUsage(position, market, marketParams) {
315
+ const hf = getHealthFactor(position, market, marketParams);
316
+ if (hf === undefined)
317
+ return;
240
318
  if (hf === null)
241
- return null;
319
+ return 0n;
242
320
  if (hf === 0n)
243
- return maths_1.MathLib.MAX_UINT_256;
244
- return maths_1.MathLib.wDivUp(maths_1.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);
245
323
  }
246
324
  MarketUtils.getBorrowCapacityUsage = getBorrowCapacityUsage;
247
325
  function toSupplyAssets(shares, market, rounding = "Down") {
248
- return maths_1.SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
326
+ return index_js_1.SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
249
327
  }
250
328
  MarketUtils.toSupplyAssets = toSupplyAssets;
251
329
  function toSupplyShares(assets, market, rounding = "Up") {
252
- return maths_1.SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
330
+ return index_js_1.SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
253
331
  }
254
332
  MarketUtils.toSupplyShares = toSupplyShares;
255
333
  function toBorrowAssets(shares, market, rounding = "Up") {
256
- return maths_1.SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
334
+ return index_js_1.SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
257
335
  }
258
336
  MarketUtils.toBorrowAssets = toBorrowAssets;
259
337
  function toBorrowShares(assets, market, rounding = "Down") {
260
- return maths_1.SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
338
+ return index_js_1.SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
261
339
  }
262
340
  MarketUtils.toBorrowShares = toBorrowShares;
263
341
  })(MarketUtils || (exports.MarketUtils = MarketUtils = {}));
@@ -1,3 +1,3 @@
1
- export * from "./MarketUtils";
2
- export * from "./MarketConfig";
3
- export * from "./Market";
1
+ export * from "./MarketUtils.js";
2
+ export * from "./MarketParams.js";
3
+ export * from "./Market.js";
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./MarketUtils"), exports);
18
- __exportStar(require("./MarketConfig"), exports);
19
- __exportStar(require("./Market"), exports);
17
+ __exportStar(require("./MarketUtils.js"), exports);
18
+ __exportStar(require("./MarketParams.js"), exports);
19
+ __exportStar(require("./Market.js"), exports);