@morpho-org/blue-sdk 1.0.0-integ-650.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 (86) hide show
  1. package/README.md +106 -0
  2. package/lib/addresses.d.ts +113 -0
  3. package/lib/addresses.js +162 -0
  4. package/lib/chain.d.ts +29 -0
  5. package/lib/chain.js +289 -0
  6. package/lib/chain.test.d.ts +1 -0
  7. package/lib/chain.test.js +21 -0
  8. package/lib/constants.d.ts +29 -0
  9. package/lib/constants.js +33 -0
  10. package/lib/errors.d.ts +58 -0
  11. package/lib/errors.js +119 -0
  12. package/lib/helpers/format/format.d.ts +122 -0
  13. package/lib/helpers/format/format.js +286 -0
  14. package/lib/helpers/format/format.test.d.ts +1 -0
  15. package/lib/helpers/format/format.test.js +255 -0
  16. package/lib/helpers/format/index.d.ts +1 -0
  17. package/lib/helpers/format/index.js +17 -0
  18. package/lib/helpers/index.d.ts +2 -0
  19. package/lib/helpers/index.js +18 -0
  20. package/lib/helpers/locale.d.ts +46 -0
  21. package/lib/helpers/locale.js +96 -0
  22. package/lib/holding/AssetBalances.d.ts +48 -0
  23. package/lib/holding/AssetBalances.js +42 -0
  24. package/lib/holding/Holding.d.ts +59 -0
  25. package/lib/holding/Holding.js +59 -0
  26. package/lib/holding/index.d.ts +2 -0
  27. package/lib/holding/index.js +18 -0
  28. package/lib/index.d.ts +25 -0
  29. package/lib/index.js +54 -0
  30. package/lib/market/Market.d.ts +329 -0
  31. package/lib/market/Market.js +460 -0
  32. package/lib/market/MarketConfig.d.ts +52 -0
  33. package/lib/market/MarketConfig.js +76 -0
  34. package/lib/market/MarketUtils.d.ts +232 -0
  35. package/lib/market/MarketUtils.js +263 -0
  36. package/lib/market/MarketUtils.test.d.ts +1 -0
  37. package/lib/market/MarketUtils.test.js +38 -0
  38. package/lib/market/index.d.ts +3 -0
  39. package/lib/market/index.js +19 -0
  40. package/lib/maths/AdaptiveCurveIrmLib.d.ts +39 -0
  41. package/lib/maths/AdaptiveCurveIrmLib.js +134 -0
  42. package/lib/maths/MathLib.d.ts +114 -0
  43. package/lib/maths/MathLib.js +175 -0
  44. package/lib/maths/SharesMath.d.ts +12 -0
  45. package/lib/maths/SharesMath.js +21 -0
  46. package/lib/maths/index.d.ts +3 -0
  47. package/lib/maths/index.js +19 -0
  48. package/lib/position/Position.d.ts +126 -0
  49. package/lib/position/Position.js +203 -0
  50. package/lib/position/index.d.ts +1 -0
  51. package/lib/position/index.js +17 -0
  52. package/lib/tests/mocks/markets.d.ts +19 -0
  53. package/lib/tests/mocks/markets.js +121 -0
  54. package/lib/token/ConstantWrappedToken.d.ts +18 -0
  55. package/lib/token/ConstantWrappedToken.js +36 -0
  56. package/lib/token/ExchangeRateWrappedToken.d.ts +11 -0
  57. package/lib/token/ExchangeRateWrappedToken.js +21 -0
  58. package/lib/token/Token.d.ts +46 -0
  59. package/lib/token/Token.js +64 -0
  60. package/lib/token/VaultToken.d.ts +14 -0
  61. package/lib/token/VaultToken.js +23 -0
  62. package/lib/token/WrappedToken.d.ts +17 -0
  63. package/lib/token/WrappedToken.js +33 -0
  64. package/lib/token/index.d.ts +5 -0
  65. package/lib/token/index.js +21 -0
  66. package/lib/types.d.ts +30 -0
  67. package/lib/types.js +22 -0
  68. package/lib/user/User.d.ts +20 -0
  69. package/lib/user/User.js +23 -0
  70. package/lib/user/index.d.ts +1 -0
  71. package/lib/user/index.js +17 -0
  72. package/lib/vault/Vault.d.ts +166 -0
  73. package/lib/vault/Vault.js +234 -0
  74. package/lib/vault/VaultConfig.d.ts +22 -0
  75. package/lib/vault/VaultConfig.js +32 -0
  76. package/lib/vault/VaultMarketAllocation.d.ts +20 -0
  77. package/lib/vault/VaultMarketAllocation.js +30 -0
  78. package/lib/vault/VaultMarketConfig.d.ts +43 -0
  79. package/lib/vault/VaultMarketConfig.js +43 -0
  80. package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
  81. package/lib/vault/VaultMarketPublicAllocatorConfig.js +28 -0
  82. package/lib/vault/VaultUtils.d.ts +18 -0
  83. package/lib/vault/VaultUtils.js +20 -0
  84. package/lib/vault/index.d.ts +6 -0
  85. package/lib/vault/index.js +22 -0
  86. package/package.json +53 -0
@@ -0,0 +1,460 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Market = exports.CapacityLimitReason = void 0;
4
+ const errors_1 = require("../errors");
5
+ const maths_1 = require("../maths");
6
+ const MarketUtils_1 = require("./MarketUtils");
7
+ var CapacityLimitReason;
8
+ (function (CapacityLimitReason) {
9
+ CapacityLimitReason["liquidity"] = "Liquidity";
10
+ CapacityLimitReason["balance"] = "Balance";
11
+ CapacityLimitReason["position"] = "Position";
12
+ CapacityLimitReason["collateral"] = "Collateral";
13
+ CapacityLimitReason["cap"] = "Cap";
14
+ })(CapacityLimitReason || (exports.CapacityLimitReason = CapacityLimitReason = {}));
15
+ /**
16
+ * Represents a lending market on Morpho Blue.
17
+ */
18
+ class Market {
19
+ /**
20
+ * The market's config.
21
+ */
22
+ config;
23
+ /**
24
+ * The amount of loan assets supplied in total on the market.
25
+ */
26
+ totalSupplyAssets;
27
+ /**
28
+ * The amount of loan assets supplied in total on the market.
29
+ */
30
+ totalBorrowAssets;
31
+ /**
32
+ * The amount of loan assets supplied in total on the market.
33
+ */
34
+ totalSupplyShares;
35
+ /**
36
+ * The amount of loan assets supplied in total on the market.
37
+ */
38
+ totalBorrowShares;
39
+ /**
40
+ * The block timestamp (in __seconds__) when the interest was last accrued.
41
+ */
42
+ lastUpdate;
43
+ /**
44
+ * The fee percentage of the market, scaled by WAD.
45
+ */
46
+ fee;
47
+ /**
48
+ * The price as returned by the market's oracle.
49
+ */
50
+ price;
51
+ /**
52
+ * If the market uses the Adaptive Curve IRM, the rate at target utilization.
53
+ * Undefined otherwise.
54
+ */
55
+ rateAtTarget;
56
+ constructor({ config, totalSupplyAssets, totalBorrowAssets, totalSupplyShares, totalBorrowShares, lastUpdate, fee, price, rateAtTarget, }) {
57
+ this.config = config;
58
+ this.totalSupplyAssets = totalSupplyAssets;
59
+ this.totalBorrowAssets = totalBorrowAssets;
60
+ this.totalSupplyShares = totalSupplyShares;
61
+ this.totalBorrowShares = totalBorrowShares;
62
+ this.lastUpdate = lastUpdate;
63
+ this.fee = fee;
64
+ this.price = price;
65
+ if (rateAtTarget != null)
66
+ this.rateAtTarget = rateAtTarget;
67
+ }
68
+ /**
69
+ * The market's hex-encoded id, defined as the hash of the market params.
70
+ */
71
+ get id() {
72
+ return this.config.id;
73
+ }
74
+ /**
75
+ * Whether the market satisfies the canonical definition of an idle market (i.e. collateral token is the zero address).
76
+ */
77
+ get isIdle() {
78
+ return (this.config.collateralToken ===
79
+ "0x0000000000000000000000000000000000000000");
80
+ }
81
+ /**
82
+ * @warning Cannot be used to calculate the liquidity available inside a callback,
83
+ * because the balance of Blue may be lower than the market's liquidity due to assets being transferred out prior to the callback.
84
+ */
85
+ get liquidity() {
86
+ return this.totalSupplyAssets - this.totalBorrowAssets;
87
+ }
88
+ /**
89
+ * The market's utilization rate (scaled by WAD).
90
+ */
91
+ get utilization() {
92
+ return MarketUtils_1.MarketUtils.getUtilization(this);
93
+ }
94
+ /**
95
+ * The market's Annual Percentage Yield (APY) at the IRM's target utilization rate, if applicable (scaled by WAD).
96
+ */
97
+ get apyAtTarget() {
98
+ if (this.rateAtTarget == null)
99
+ return;
100
+ return MarketUtils_1.MarketUtils.getApy(this.rateAtTarget);
101
+ }
102
+ /**
103
+ * Returns the rate at which interest accrued on average for suppliers of this market,
104
+ * since the last time the market was updated (scaled by WAD).
105
+ */
106
+ get supplyRate() {
107
+ return MarketUtils_1.MarketUtils.getSupplyRate(this.borrowRate, this);
108
+ }
109
+ /**
110
+ * Returns the rate at which interest accrued on average for borrowers of this market,
111
+ * since the last time the market was updated (scaled by WAD).
112
+ */
113
+ get borrowRate() {
114
+ if (this.rateAtTarget == null)
115
+ return 0n;
116
+ return maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, 0n).avgBorrowRate;
117
+ }
118
+ /**
119
+ * The market's supply Annual Percentage Yield (APY) (scaled by WAD).
120
+ */
121
+ get supplyApy() {
122
+ return MarketUtils_1.MarketUtils.getApy(this.supplyRate);
123
+ }
124
+ /**
125
+ * The market's borrow Annual Percentage Yield (APY) (scaled by WAD).
126
+ */
127
+ get borrowApy() {
128
+ return MarketUtils_1.MarketUtils.getApy(this.borrowRate);
129
+ }
130
+ /**
131
+ * Returns a new market derived from this market, whose interest has been accrued up to the given timestamp.
132
+ * @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to `lastUpdate`. Defaults to `lastUpdate` (returns a copy of the market).
133
+ */
134
+ accrueInterest(timestamp = this.lastUpdate) {
135
+ timestamp = BigInt(timestamp);
136
+ const elapsed = timestamp - this.lastUpdate;
137
+ if (elapsed < 0n)
138
+ throw new errors_1.BlueErrors.InvalidInterestAccrual(this.id, timestamp, this.lastUpdate);
139
+ if (elapsed === 0n)
140
+ return new Market(this);
141
+ let borrowRate = 0n;
142
+ let { rateAtTarget } = this;
143
+ if (rateAtTarget != null) {
144
+ const { avgBorrowRate, endRateAtTarget } = maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, rateAtTarget, elapsed);
145
+ borrowRate = avgBorrowRate;
146
+ rateAtTarget = endRateAtTarget;
147
+ }
148
+ const { interest, feeShares } = MarketUtils_1.MarketUtils.getAccruedInterest(borrowRate, this, elapsed);
149
+ return new Market({
150
+ ...this,
151
+ totalSupplyAssets: this.totalSupplyAssets + interest,
152
+ totalBorrowAssets: this.totalBorrowAssets + interest,
153
+ totalSupplyShares: this.totalSupplyShares + feeShares,
154
+ lastUpdate: timestamp,
155
+ rateAtTarget,
156
+ });
157
+ }
158
+ supply(assets, shares, timestamp) {
159
+ if (assets === 0n && shares === 0n)
160
+ throw new errors_1.BlueErrors.InconsistentInput();
161
+ const market = this.accrueInterest(timestamp);
162
+ if (shares === 0n)
163
+ shares = market.toSupplyShares(assets, "Down");
164
+ else
165
+ assets = market.toSupplyAssets(shares, "Up");
166
+ market.totalSupplyAssets += assets;
167
+ market.totalSupplyShares += shares;
168
+ return { market, assets, shares };
169
+ }
170
+ withdraw(assets, shares, timestamp) {
171
+ if (assets === 0n && shares === 0n)
172
+ throw new errors_1.BlueErrors.InconsistentInput();
173
+ const market = this.accrueInterest(timestamp);
174
+ if (shares === 0n)
175
+ shares = market.toSupplyShares(assets, "Up");
176
+ else
177
+ assets = market.toSupplyAssets(shares, "Down");
178
+ market.totalSupplyAssets -= assets;
179
+ market.totalSupplyShares -= shares;
180
+ if (market.totalBorrowAssets > market.totalSupplyAssets)
181
+ throw new errors_1.BlueErrors.InsufficientLiquidity(market.id);
182
+ return { market, assets, shares };
183
+ }
184
+ borrow(assets, shares, timestamp) {
185
+ if (assets === 0n && shares === 0n)
186
+ throw new errors_1.BlueErrors.InconsistentInput();
187
+ const market = this.accrueInterest(timestamp);
188
+ if (shares === 0n)
189
+ shares = market.toBorrowShares(assets, "Up");
190
+ else
191
+ assets = market.toBorrowAssets(shares, "Down");
192
+ market.totalBorrowAssets += assets;
193
+ market.totalBorrowShares += shares;
194
+ if (market.totalBorrowAssets > market.totalSupplyAssets)
195
+ throw new errors_1.BlueErrors.InsufficientLiquidity(market.id);
196
+ return { market, assets, shares };
197
+ }
198
+ repay(assets, shares, timestamp) {
199
+ if (assets === 0n && shares === 0n)
200
+ throw new errors_1.BlueErrors.InconsistentInput();
201
+ const market = this.accrueInterest(timestamp);
202
+ if (shares === 0n)
203
+ shares = market.toBorrowShares(assets, "Down");
204
+ else
205
+ assets = market.toBorrowAssets(shares, "Up");
206
+ market.totalBorrowAssets -= assets;
207
+ market.totalBorrowShares -= shares;
208
+ return { market, assets, shares };
209
+ }
210
+ /**
211
+ * Converts a given amount of supply shares into supply loan assets.
212
+ * @param shares The amount of shares to convert.
213
+ * @param rounding The rounding direction to use (defaults to "Down").
214
+ */
215
+ toSupplyAssets(shares, rounding) {
216
+ return MarketUtils_1.MarketUtils.toSupplyAssets(shares, this, rounding);
217
+ }
218
+ /**
219
+ * Converts a given amount of supply loan assets into supply shares.
220
+ * @param shares The amount of assets to convert.
221
+ * @param rounding The rounding direction to use (defaults to "Up").
222
+ */
223
+ toSupplyShares(assets, rounding) {
224
+ return MarketUtils_1.MarketUtils.toSupplyShares(assets, this, rounding);
225
+ }
226
+ /**
227
+ * Converts a given amount of borrow shares into borrow loan assets.
228
+ * @param shares The amount of shares to convert.
229
+ * @param rounding The rounding direction to use (defaults to "Up").
230
+ */
231
+ toBorrowAssets(shares, rounding) {
232
+ return MarketUtils_1.MarketUtils.toBorrowAssets(shares, this, rounding);
233
+ }
234
+ /**
235
+ * Converts a given amount of borrow loan assets into borrow shares.
236
+ * @param shares The amount of assets to convert.
237
+ * @param rounding The rounding direction to use (defaults to "Down").
238
+ */
239
+ toBorrowShares(assets, rounding) {
240
+ return MarketUtils_1.MarketUtils.toBorrowShares(assets, this, rounding);
241
+ }
242
+ /**
243
+ * Returns the smallest volume to supply until the market gets the closest to the given utilization rate.
244
+ * @param utilization The target utilization rate (scaled by WAD).
245
+ */
246
+ getSupplyToUtilization(utilization) {
247
+ return MarketUtils_1.MarketUtils.getSupplyToUtilization(this, utilization);
248
+ }
249
+ /**
250
+ * Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
251
+ * @param utilization The target utilization rate (scaled by WAD).
252
+ */
253
+ getWithdrawToUtilization(utilization) {
254
+ return MarketUtils_1.MarketUtils.getWithdrawToUtilization(this, utilization);
255
+ }
256
+ /**
257
+ * Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
258
+ * @param utilization The target utilization rate (scaled by WAD).
259
+ */
260
+ getBorrowToUtilization(utilization) {
261
+ return MarketUtils_1.MarketUtils.getBorrowToUtilization(this, utilization);
262
+ }
263
+ /**
264
+ * Returns the smallest volume to repay until the market gets the closest to the given utilization rate.
265
+ * @param utilization The target utilization rate (scaled by WAD).
266
+ */
267
+ getRepayToUtilization(utilization) {
268
+ return MarketUtils_1.MarketUtils.getRepayToUtilization(this, utilization);
269
+ }
270
+ /**
271
+ * Returns the value of a given amount of collateral quoted in loan assets.
272
+ * @param collateral The amount of collateral to quote.
273
+ */
274
+ getCollateralValue(collateral) {
275
+ return MarketUtils_1.MarketUtils.getCollateralValue(collateral, this);
276
+ }
277
+ /**
278
+ * Returns the maximum amount of loan assets that can be borrowed given a certain amount of collateral.
279
+ * @param collateral The amount of collateral to consider.
280
+ */
281
+ getMaxBorrowAssets(collateral, { maxLtv = this.config.lltv } = {}) {
282
+ return MarketUtils_1.MarketUtils.getMaxBorrowAssets(collateral, this, {
283
+ lltv: maths_1.MathLib.min(maxLtv, this.config.lltv),
284
+ });
285
+ }
286
+ /**
287
+ * Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
288
+ * @param position The borrow position to consider.
289
+ */
290
+ getMaxBorrowableAssets(position) {
291
+ return MarketUtils_1.MarketUtils.getMaxBorrowableAssets(position, this, this.config);
292
+ }
293
+ /**
294
+ * Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
295
+ * @param repaidShares The amount of shares hypothetically repaid.
296
+ */
297
+ getLiquidationSeizedAssets(repaidShares) {
298
+ return MarketUtils_1.MarketUtils.getLiquidationSeizedAssets(repaidShares, this, this.config);
299
+ }
300
+ /**
301
+ * Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
302
+ * @param seizedAssets The amount of collateral hypothetically seized.
303
+ */
304
+ getLiquidationRepaidShares(seizedAssets) {
305
+ return MarketUtils_1.MarketUtils.getLiquidationRepaidShares(seizedAssets, this, this.config);
306
+ }
307
+ /**
308
+ * Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
309
+ * @param position The borrow position to consider.
310
+ */
311
+ getSeizableCollateral(position) {
312
+ return MarketUtils_1.MarketUtils.getSeizableCollateral(position, this, this.config);
313
+ }
314
+ /**
315
+ * Returns the amount of collateral that can be withdrawn given a certain borrow position.
316
+ * @param position The borrow position to consider.
317
+ */
318
+ getWithdrawableCollateral(position, { maxLtv = this.config.lltv } = {}) {
319
+ return MarketUtils_1.MarketUtils.getWithdrawableCollateral(position, this, {
320
+ lltv: maths_1.MathLib.min(maxLtv, this.config.lltv),
321
+ });
322
+ }
323
+ /**
324
+ * Returns whether a given borrow position is healthy.
325
+ * @param position The borrow position to check.
326
+ */
327
+ isHealthy(position) {
328
+ return MarketUtils_1.MarketUtils.isHealthy(position, this, this.config);
329
+ }
330
+ /**
331
+ * Returns the liquidation price of a given borrow position.
332
+ * @param position The borrow position to consider.
333
+ */
334
+ getLiquidationPrice(position) {
335
+ return MarketUtils_1.MarketUtils.getLiquidationPrice(position, this, this.config);
336
+ }
337
+ /**
338
+ * Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
339
+ * @param position The borrow position to consider.
340
+ */
341
+ getPriceVariationToLiquidation(position) {
342
+ return MarketUtils_1.MarketUtils.getPriceVariationToLiquidation(position, this, this.config);
343
+ }
344
+ /**
345
+ * Returns the health factor of a given borrow position (scaled by WAD).
346
+ * @param position The borrow position to consider.
347
+ */
348
+ getHealthFactor(position) {
349
+ return MarketUtils_1.MarketUtils.getHealthFactor(position, this, this.config);
350
+ }
351
+ /**
352
+ * Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
353
+ * @param position The borrow position to consider.
354
+ */
355
+ getLtv(position) {
356
+ return MarketUtils_1.MarketUtils.getLtv(position, this);
357
+ }
358
+ /**
359
+ * Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
360
+ * @param position The borrow position to consider.
361
+ */
362
+ getBorrowCapacityUsage(position) {
363
+ return MarketUtils_1.MarketUtils.getBorrowCapacityUsage(position, this, this.config);
364
+ }
365
+ /**
366
+ * Returns the maximum amount of loan assets that can be borrowed given a certain borrow position
367
+ * and the reason for the limit.
368
+ * @param position The borrow position to consider.
369
+ */
370
+ getBorrowCapacityLimit({ collateral, borrowShares = 0n, }, options) {
371
+ // handle edge cases when the user is liquidatable (maxBorrow < borrow)
372
+ const maxBorrowableAssets = maths_1.MathLib.zeroFloorSub(this.getMaxBorrowAssets(collateral, options), this.toBorrowAssets(borrowShares));
373
+ const { liquidity } = this;
374
+ if (maxBorrowableAssets > liquidity)
375
+ return {
376
+ value: liquidity,
377
+ limiter: CapacityLimitReason.liquidity,
378
+ };
379
+ return {
380
+ value: maxBorrowableAssets,
381
+ limiter: CapacityLimitReason.collateral,
382
+ };
383
+ }
384
+ /**
385
+ * Returns the maximum amount of loan assets that can be repaid given a certain borrow position
386
+ * and a balance of loan assets, and the reason for the limit.
387
+ * @param position The borrow position to consider.
388
+ */
389
+ getRepayCapacityLimit(borrowShares, loanTokenBalance) {
390
+ const borrowAssets = this.toBorrowAssets(borrowShares);
391
+ if (borrowAssets > loanTokenBalance)
392
+ return {
393
+ value: loanTokenBalance,
394
+ limiter: CapacityLimitReason.balance,
395
+ };
396
+ return {
397
+ value: borrowAssets,
398
+ limiter: CapacityLimitReason.position,
399
+ };
400
+ }
401
+ /**
402
+ * Returns the maximum amount of loan assets that can be withdrawn given a certain supply position
403
+ * and a balance of loan assets, and the reason for the limit.
404
+ * @param position The supply position to consider.
405
+ */
406
+ getWithdrawCapacityLimit({ supplyShares, }) {
407
+ const supplyAssets = this.toSupplyAssets(supplyShares);
408
+ const { liquidity } = this;
409
+ if (supplyAssets > liquidity)
410
+ return {
411
+ value: liquidity,
412
+ limiter: CapacityLimitReason.liquidity,
413
+ };
414
+ return {
415
+ value: supplyAssets,
416
+ limiter: CapacityLimitReason.position,
417
+ };
418
+ }
419
+ /**
420
+ * Returns the maximum amount of collateral assets that can be withdrawn given a certain borrow position
421
+ * and the reason for the limit.
422
+ * @param position The borrow position to consider.
423
+ */
424
+ getWithdrawCollateralCapacityLimit(position, options) {
425
+ const withdrawableCollateral = this.getWithdrawableCollateral(position, options);
426
+ if (position.collateral > withdrawableCollateral)
427
+ return {
428
+ value: withdrawableCollateral,
429
+ limiter: CapacityLimitReason.collateral,
430
+ };
431
+ return {
432
+ value: position.collateral,
433
+ limiter: CapacityLimitReason.position,
434
+ };
435
+ }
436
+ /**
437
+ * Returns the maximum capacity for all interactions with Morpho Blue given a certain position
438
+ * and loan and collateral balances.
439
+ * @param position The position to consider.
440
+ * @param loanTokenBalance The balance of loan assets.
441
+ * @param collateralTokenBalance The balance of collateral assets.
442
+ */
443
+ getMaxCapacities(position, loanTokenBalance, collateralTokenBalance, options) {
444
+ return {
445
+ supply: {
446
+ value: loanTokenBalance,
447
+ limiter: CapacityLimitReason.balance,
448
+ },
449
+ withdraw: this.getWithdrawCapacityLimit(position),
450
+ borrow: this.getBorrowCapacityLimit(position, options?.borrow),
451
+ repay: this.getRepayCapacityLimit(position.borrowShares, loanTokenBalance),
452
+ supplyCollateral: {
453
+ value: collateralTokenBalance,
454
+ limiter: CapacityLimitReason.balance,
455
+ },
456
+ withdrawCollateral: this.getWithdrawCollateralCapacityLimit(position, options?.withdrawCollateral),
457
+ };
458
+ }
459
+ }
460
+ exports.Market = Market;
@@ -0,0 +1,52 @@
1
+ import { Address, BigIntish, MarketId } from "../types";
2
+ export interface MarketParams {
3
+ loanToken: Address;
4
+ collateralToken: Address;
5
+ oracle: Address;
6
+ irm: Address;
7
+ lltv: BigIntish;
8
+ }
9
+ /**
10
+ * Represents a market's configuration (also called market params).
11
+ */
12
+ export declare class MarketConfig implements MarketParams {
13
+ private static readonly _CACHE;
14
+ /**
15
+ * Returns the previously cached market config for the given id, if any.
16
+ * @throws {UnknownMarketConfigError} If no market config is cached.
17
+ */
18
+ static get(id: MarketId): MarketConfig;
19
+ /**
20
+ * Returns the canonical idle market configuration for the given loan token.
21
+ */
22
+ static idle(token: Address): MarketConfig;
23
+ /**
24
+ * The market's collateral token address.
25
+ */
26
+ readonly collateralToken: Address;
27
+ /**
28
+ * The market's loan token address.
29
+ */
30
+ readonly loanToken: Address;
31
+ /**
32
+ * The market's oracle address.
33
+ */
34
+ readonly oracle: Address;
35
+ /**
36
+ * The market's interest rate model address.
37
+ */
38
+ readonly irm: Address;
39
+ /**
40
+ * The market's liquidation Loan-To-Value (scaled by WAD).
41
+ */
42
+ readonly lltv: bigint;
43
+ /**
44
+ * The market's hex-encoded id, defined as the hash of the market params.
45
+ */
46
+ readonly id: MarketId;
47
+ /**
48
+ * The market's liquidation incentive factor.
49
+ */
50
+ readonly liquidationIncentiveFactor: bigint;
51
+ constructor(params: MarketParams);
52
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MarketConfig = void 0;
4
+ const errors_1 = require("../errors");
5
+ const MarketUtils_1 = require("./MarketUtils");
6
+ /**
7
+ * Represents a market's configuration (also called market params).
8
+ */
9
+ class MarketConfig {
10
+ static _CACHE = {};
11
+ /**
12
+ * Returns the previously cached market config for the given id, if any.
13
+ * @throws {UnknownMarketConfigError} If no market config is cached.
14
+ */
15
+ static get(id) {
16
+ const marketConfig = MarketConfig._CACHE[id];
17
+ if (!marketConfig)
18
+ throw new errors_1.UnknownMarketConfigError(id);
19
+ return marketConfig;
20
+ }
21
+ /**
22
+ * Returns the canonical idle market configuration for the given loan token.
23
+ */
24
+ static idle(token) {
25
+ return new MarketConfig({
26
+ collateralToken: "0x0000000000000000000000000000000000000000",
27
+ loanToken: token,
28
+ oracle: "0x0000000000000000000000000000000000000000",
29
+ irm: "0x0000000000000000000000000000000000000000",
30
+ lltv: 0n,
31
+ });
32
+ }
33
+ /**
34
+ * The market's collateral token address.
35
+ */
36
+ collateralToken;
37
+ /**
38
+ * The market's loan token address.
39
+ */
40
+ loanToken;
41
+ /**
42
+ * The market's oracle address.
43
+ */
44
+ oracle;
45
+ /**
46
+ * The market's interest rate model address.
47
+ */
48
+ irm;
49
+ /**
50
+ * The market's liquidation Loan-To-Value (scaled by WAD).
51
+ */
52
+ lltv;
53
+ /**
54
+ * The market's hex-encoded id, defined as the hash of the market params.
55
+ */
56
+ // Cached because params are readonly.
57
+ id;
58
+ /**
59
+ * The market's liquidation incentive factor.
60
+ */
61
+ // Cached because lltv is readonly.
62
+ liquidationIncentiveFactor;
63
+ constructor(params) {
64
+ const { collateralToken, loanToken, oracle, irm, lltv } = params;
65
+ this.collateralToken = collateralToken;
66
+ this.loanToken = loanToken;
67
+ this.oracle = oracle;
68
+ this.irm = irm;
69
+ this.lltv = BigInt(lltv);
70
+ this.id = MarketUtils_1.MarketUtils.getMarketId(params);
71
+ this.liquidationIncentiveFactor =
72
+ MarketUtils_1.MarketUtils.getLiquidationIncentiveFactor(params);
73
+ MarketConfig._CACHE[this.id] = this;
74
+ }
75
+ }
76
+ exports.MarketConfig = MarketConfig;