@morpho-org/blue-sdk 2.0.0-test.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -26
- package/lib/addresses.d.ts +59 -55
- package/lib/addresses.js +42 -43
- package/lib/chain.d.ts +1 -1
- package/lib/chain.js +3 -3
- package/lib/constants.d.ts +0 -9
- package/lib/constants.js +1 -10
- package/lib/errors.d.ts +7 -8
- package/lib/errors.js +21 -19
- package/lib/holding/AssetBalances.d.ts +3 -3
- package/lib/holding/Holding.d.ts +10 -5
- package/lib/holding/Holding.js +14 -4
- package/lib/holding/index.d.ts +2 -2
- package/lib/holding/index.js +2 -2
- package/lib/index.d.ts +12 -25
- package/lib/index.js +12 -38
- package/lib/market/Market.d.ts +48 -34
- package/lib/market/Market.js +76 -57
- package/lib/market/{MarketConfig.d.ts → MarketParams.d.ts} +8 -7
- package/lib/market/{MarketConfig.js → MarketParams.js} +20 -21
- package/lib/market/MarketUtils.d.ts +94 -42
- package/lib/market/MarketUtils.js +145 -67
- package/lib/market/index.d.ts +3 -3
- package/lib/market/index.js +3 -3
- package/lib/{maths → math}/AdaptiveCurveIrmLib.d.ts +1 -1
- package/lib/{maths → math}/AdaptiveCurveIrmLib.js +20 -20
- package/lib/{maths → math}/MathLib.d.ts +24 -42
- package/lib/{maths → math}/MathLib.js +41 -54
- package/lib/{maths → math}/SharesMath.d.ts +2 -2
- package/lib/{maths → math}/SharesMath.js +3 -3
- package/lib/math/index.d.ts +3 -0
- package/lib/{helpers/format → math}/index.js +3 -1
- package/lib/position/Position.d.ts +35 -25
- package/lib/position/Position.js +46 -44
- package/lib/position/index.d.ts +1 -1
- package/lib/position/index.js +1 -1
- package/lib/token/ConstantWrappedToken.d.ts +6 -7
- package/lib/token/ConstantWrappedToken.js +8 -10
- package/lib/token/ExchangeRateWrappedToken.d.ts +5 -5
- package/lib/token/ExchangeRateWrappedToken.js +5 -5
- package/lib/token/Token.d.ts +18 -18
- package/lib/token/Token.js +24 -27
- package/lib/token/VaultToken.d.ts +20 -11
- package/lib/token/VaultToken.js +15 -7
- package/lib/token/WrappedToken.d.ts +4 -4
- package/lib/token/WrappedToken.js +7 -7
- package/lib/token/index.d.ts +5 -5
- package/lib/token/index.js +5 -5
- package/lib/types.d.ts +3 -6
- package/lib/user/User.d.ts +1 -1
- package/lib/user/index.d.ts +1 -1
- package/lib/user/index.js +1 -1
- package/lib/vault/Vault.d.ts +22 -28
- package/lib/vault/Vault.js +43 -51
- package/lib/vault/VaultConfig.d.ts +6 -7
- package/lib/vault/VaultConfig.js +5 -5
- package/lib/vault/VaultMarketAllocation.d.ts +8 -8
- package/lib/vault/VaultMarketAllocation.js +3 -3
- package/lib/vault/VaultMarketConfig.d.ts +10 -10
- package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +6 -6
- package/lib/vault/VaultUser.d.ts +26 -0
- package/lib/vault/VaultUser.js +28 -0
- package/lib/vault/VaultUtils.d.ts +4 -6
- package/lib/vault/VaultUtils.js +6 -6
- package/lib/vault/index.d.ts +7 -6
- package/lib/vault/index.js +7 -6
- package/package.json +25 -56
- package/lib/chain.test.d.ts +0 -1
- package/lib/chain.test.js +0 -21
- package/lib/helpers/format/format.d.ts +0 -122
- package/lib/helpers/format/format.js +0 -286
- package/lib/helpers/format/format.test.d.ts +0 -1
- package/lib/helpers/format/format.test.js +0 -255
- package/lib/helpers/format/index.d.ts +0 -1
- package/lib/helpers/index.d.ts +0 -2
- package/lib/helpers/index.js +0 -18
- package/lib/helpers/locale.d.ts +0 -46
- package/lib/helpers/locale.js +0 -96
- package/lib/market/MarketUtils.test.d.ts +0 -1
- package/lib/market/MarketUtils.test.js +0 -38
- package/lib/maths/index.d.ts +0 -3
- package/lib/maths/index.js +0 -19
- package/lib/tests/mocks/markets.d.ts +0 -19
- package/lib/tests/mocks/markets.js +0 -121
package/lib/market/Market.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Market = exports.CapacityLimitReason = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
|
+
const errors_js_1 = require("../errors.js");
|
|
6
|
+
const index_js_1 = require("../math/index.js");
|
|
7
|
+
const MarketUtils_js_1 = require("./MarketUtils.js");
|
|
7
8
|
var CapacityLimitReason;
|
|
8
9
|
(function (CapacityLimitReason) {
|
|
9
10
|
CapacityLimitReason["liquidity"] = "Liquidity";
|
|
@@ -17,9 +18,9 @@ var CapacityLimitReason;
|
|
|
17
18
|
*/
|
|
18
19
|
class Market {
|
|
19
20
|
/**
|
|
20
|
-
* The market's
|
|
21
|
+
* The market's params.
|
|
21
22
|
*/
|
|
22
|
-
|
|
23
|
+
params;
|
|
23
24
|
/**
|
|
24
25
|
* The amount of loan assets supplied in total on the market.
|
|
25
26
|
*/
|
|
@@ -46,6 +47,7 @@ class Market {
|
|
|
46
47
|
fee;
|
|
47
48
|
/**
|
|
48
49
|
* The price as returned by the market's oracle.
|
|
50
|
+
* `undefined` if the oracle is undefined or reverts.
|
|
49
51
|
*/
|
|
50
52
|
price;
|
|
51
53
|
/**
|
|
@@ -53,8 +55,8 @@ class Market {
|
|
|
53
55
|
* Undefined otherwise.
|
|
54
56
|
*/
|
|
55
57
|
rateAtTarget;
|
|
56
|
-
constructor({
|
|
57
|
-
this.
|
|
58
|
+
constructor({ params, totalSupplyAssets, totalBorrowAssets, totalSupplyShares, totalBorrowShares, lastUpdate, fee, price, rateAtTarget, }) {
|
|
59
|
+
this.params = params;
|
|
58
60
|
this.totalSupplyAssets = totalSupplyAssets;
|
|
59
61
|
this.totalBorrowAssets = totalBorrowAssets;
|
|
60
62
|
this.totalSupplyShares = totalSupplyShares;
|
|
@@ -69,14 +71,13 @@ class Market {
|
|
|
69
71
|
* The market's hex-encoded id, defined as the hash of the market params.
|
|
70
72
|
*/
|
|
71
73
|
get id() {
|
|
72
|
-
return this.
|
|
74
|
+
return this.params.id;
|
|
73
75
|
}
|
|
74
76
|
/**
|
|
75
77
|
* Whether the market satisfies the canonical definition of an idle market (i.e. collateral token is the zero address).
|
|
76
78
|
*/
|
|
77
79
|
get isIdle() {
|
|
78
|
-
return
|
|
79
|
-
"0x0000000000000000000000000000000000000000");
|
|
80
|
+
return this.params.collateralToken === morpho_ts_1.ZERO_ADDRESS;
|
|
80
81
|
}
|
|
81
82
|
/**
|
|
82
83
|
* @warning Cannot be used to calculate the liquidity available inside a callback,
|
|
@@ -89,7 +90,7 @@ class Market {
|
|
|
89
90
|
* The market's utilization rate (scaled by WAD).
|
|
90
91
|
*/
|
|
91
92
|
get utilization() {
|
|
92
|
-
return
|
|
93
|
+
return MarketUtils_js_1.MarketUtils.getUtilization(this);
|
|
93
94
|
}
|
|
94
95
|
/**
|
|
95
96
|
* The market's Annual Percentage Yield (APY) at the IRM's target utilization rate, if applicable (scaled by WAD).
|
|
@@ -97,35 +98,35 @@ class Market {
|
|
|
97
98
|
get apyAtTarget() {
|
|
98
99
|
if (this.rateAtTarget == null)
|
|
99
100
|
return;
|
|
100
|
-
return
|
|
101
|
+
return MarketUtils_js_1.MarketUtils.compoundRate(this.rateAtTarget);
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
103
|
-
* Returns the rate at which interest accrued
|
|
104
|
+
* Returns the rate at which interest accrued for suppliers of this market,
|
|
104
105
|
* since the last time the market was updated (scaled by WAD).
|
|
105
106
|
*/
|
|
106
107
|
get supplyRate() {
|
|
107
|
-
return
|
|
108
|
+
return MarketUtils_js_1.MarketUtils.getSupplyRate(this.borrowRate, this);
|
|
108
109
|
}
|
|
109
110
|
/**
|
|
110
|
-
* Returns the rate at which interest accrued
|
|
111
|
+
* Returns the rate at which interest accrued for borrowers of this market,
|
|
111
112
|
* since the last time the market was updated (scaled by WAD).
|
|
112
113
|
*/
|
|
113
114
|
get borrowRate() {
|
|
114
115
|
if (this.rateAtTarget == null)
|
|
115
116
|
return 0n;
|
|
116
|
-
return
|
|
117
|
+
return index_js_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, 0n).avgBorrowRate;
|
|
117
118
|
}
|
|
118
119
|
/**
|
|
119
|
-
* The market's supply Annual Percentage Yield (APY) (scaled by WAD).
|
|
120
|
+
* The market's supply-side Annual Percentage Yield (APY) (scaled by WAD).
|
|
120
121
|
*/
|
|
121
122
|
get supplyApy() {
|
|
122
|
-
return
|
|
123
|
+
return MarketUtils_js_1.MarketUtils.compoundRate(this.supplyRate);
|
|
123
124
|
}
|
|
124
125
|
/**
|
|
125
|
-
* The market's borrow Annual Percentage Yield (APY) (scaled by WAD).
|
|
126
|
+
* The market's borrow-side Annual Percentage Yield (APY) (scaled by WAD).
|
|
126
127
|
*/
|
|
127
128
|
get borrowApy() {
|
|
128
|
-
return
|
|
129
|
+
return MarketUtils_js_1.MarketUtils.compoundRate(this.borrowRate);
|
|
129
130
|
}
|
|
130
131
|
/**
|
|
131
132
|
* Returns a new market derived from this market, whose interest has been accrued up to the given timestamp.
|
|
@@ -135,17 +136,17 @@ class Market {
|
|
|
135
136
|
timestamp = BigInt(timestamp);
|
|
136
137
|
const elapsed = timestamp - this.lastUpdate;
|
|
137
138
|
if (elapsed < 0n)
|
|
138
|
-
throw new
|
|
139
|
+
throw new errors_js_1.BlueErrors.InvalidInterestAccrual(this.id, timestamp, this.lastUpdate);
|
|
139
140
|
if (elapsed === 0n)
|
|
140
141
|
return new Market(this);
|
|
141
142
|
let borrowRate = 0n;
|
|
142
143
|
let { rateAtTarget } = this;
|
|
143
144
|
if (rateAtTarget != null) {
|
|
144
|
-
const { avgBorrowRate, endRateAtTarget } =
|
|
145
|
+
const { avgBorrowRate, endRateAtTarget } = index_js_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, rateAtTarget, elapsed);
|
|
145
146
|
borrowRate = avgBorrowRate;
|
|
146
147
|
rateAtTarget = endRateAtTarget;
|
|
147
148
|
}
|
|
148
|
-
const { interest, feeShares } =
|
|
149
|
+
const { interest, feeShares } = MarketUtils_js_1.MarketUtils.getAccruedInterest(borrowRate, this, elapsed);
|
|
149
150
|
return new Market({
|
|
150
151
|
...this,
|
|
151
152
|
totalSupplyAssets: this.totalSupplyAssets + interest,
|
|
@@ -157,7 +158,7 @@ class Market {
|
|
|
157
158
|
}
|
|
158
159
|
supply(assets, shares, timestamp) {
|
|
159
160
|
if (assets === 0n && shares === 0n)
|
|
160
|
-
throw new
|
|
161
|
+
throw new errors_js_1.BlueErrors.InconsistentInput();
|
|
161
162
|
const market = this.accrueInterest(timestamp);
|
|
162
163
|
if (shares === 0n)
|
|
163
164
|
shares = market.toSupplyShares(assets, "Down");
|
|
@@ -169,7 +170,7 @@ class Market {
|
|
|
169
170
|
}
|
|
170
171
|
withdraw(assets, shares, timestamp) {
|
|
171
172
|
if (assets === 0n && shares === 0n)
|
|
172
|
-
throw new
|
|
173
|
+
throw new errors_js_1.BlueErrors.InconsistentInput();
|
|
173
174
|
const market = this.accrueInterest(timestamp);
|
|
174
175
|
if (shares === 0n)
|
|
175
176
|
shares = market.toSupplyShares(assets, "Up");
|
|
@@ -178,12 +179,12 @@ class Market {
|
|
|
178
179
|
market.totalSupplyAssets -= assets;
|
|
179
180
|
market.totalSupplyShares -= shares;
|
|
180
181
|
if (market.totalBorrowAssets > market.totalSupplyAssets)
|
|
181
|
-
throw new
|
|
182
|
+
throw new errors_js_1.BlueErrors.InsufficientLiquidity(market.id);
|
|
182
183
|
return { market, assets, shares };
|
|
183
184
|
}
|
|
184
185
|
borrow(assets, shares, timestamp) {
|
|
185
186
|
if (assets === 0n && shares === 0n)
|
|
186
|
-
throw new
|
|
187
|
+
throw new errors_js_1.BlueErrors.InconsistentInput();
|
|
187
188
|
const market = this.accrueInterest(timestamp);
|
|
188
189
|
if (shares === 0n)
|
|
189
190
|
shares = market.toBorrowShares(assets, "Up");
|
|
@@ -192,12 +193,12 @@ class Market {
|
|
|
192
193
|
market.totalBorrowAssets += assets;
|
|
193
194
|
market.totalBorrowShares += shares;
|
|
194
195
|
if (market.totalBorrowAssets > market.totalSupplyAssets)
|
|
195
|
-
throw new
|
|
196
|
+
throw new errors_js_1.BlueErrors.InsufficientLiquidity(market.id);
|
|
196
197
|
return { market, assets, shares };
|
|
197
198
|
}
|
|
198
199
|
repay(assets, shares, timestamp) {
|
|
199
200
|
if (assets === 0n && shares === 0n)
|
|
200
|
-
throw new
|
|
201
|
+
throw new errors_js_1.BlueErrors.InconsistentInput();
|
|
201
202
|
const market = this.accrueInterest(timestamp);
|
|
202
203
|
if (shares === 0n)
|
|
203
204
|
shares = market.toBorrowShares(assets, "Down");
|
|
@@ -213,7 +214,7 @@ class Market {
|
|
|
213
214
|
* @param rounding The rounding direction to use (defaults to "Down").
|
|
214
215
|
*/
|
|
215
216
|
toSupplyAssets(shares, rounding) {
|
|
216
|
-
return
|
|
217
|
+
return MarketUtils_js_1.MarketUtils.toSupplyAssets(shares, this, rounding);
|
|
217
218
|
}
|
|
218
219
|
/**
|
|
219
220
|
* Converts a given amount of supply loan assets into supply shares.
|
|
@@ -221,7 +222,7 @@ class Market {
|
|
|
221
222
|
* @param rounding The rounding direction to use (defaults to "Up").
|
|
222
223
|
*/
|
|
223
224
|
toSupplyShares(assets, rounding) {
|
|
224
|
-
return
|
|
225
|
+
return MarketUtils_js_1.MarketUtils.toSupplyShares(assets, this, rounding);
|
|
225
226
|
}
|
|
226
227
|
/**
|
|
227
228
|
* Converts a given amount of borrow shares into borrow loan assets.
|
|
@@ -229,7 +230,7 @@ class Market {
|
|
|
229
230
|
* @param rounding The rounding direction to use (defaults to "Up").
|
|
230
231
|
*/
|
|
231
232
|
toBorrowAssets(shares, rounding) {
|
|
232
|
-
return
|
|
233
|
+
return MarketUtils_js_1.MarketUtils.toBorrowAssets(shares, this, rounding);
|
|
233
234
|
}
|
|
234
235
|
/**
|
|
235
236
|
* Converts a given amount of borrow loan assets into borrow shares.
|
|
@@ -237,140 +238,155 @@ class Market {
|
|
|
237
238
|
* @param rounding The rounding direction to use (defaults to "Down").
|
|
238
239
|
*/
|
|
239
240
|
toBorrowShares(assets, rounding) {
|
|
240
|
-
return
|
|
241
|
+
return MarketUtils_js_1.MarketUtils.toBorrowShares(assets, this, rounding);
|
|
241
242
|
}
|
|
242
243
|
/**
|
|
243
244
|
* Returns the smallest volume to supply until the market gets the closest to the given utilization rate.
|
|
244
245
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
245
246
|
*/
|
|
246
247
|
getSupplyToUtilization(utilization) {
|
|
247
|
-
return
|
|
248
|
+
return MarketUtils_js_1.MarketUtils.getSupplyToUtilization(this, utilization);
|
|
248
249
|
}
|
|
249
250
|
/**
|
|
250
251
|
* Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
|
|
251
252
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
252
253
|
*/
|
|
253
254
|
getWithdrawToUtilization(utilization) {
|
|
254
|
-
return
|
|
255
|
+
return MarketUtils_js_1.MarketUtils.getWithdrawToUtilization(this, utilization);
|
|
255
256
|
}
|
|
256
257
|
/**
|
|
257
258
|
* Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
|
|
258
259
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
259
260
|
*/
|
|
260
261
|
getBorrowToUtilization(utilization) {
|
|
261
|
-
return
|
|
262
|
+
return MarketUtils_js_1.MarketUtils.getBorrowToUtilization(this, utilization);
|
|
262
263
|
}
|
|
263
264
|
/**
|
|
264
265
|
* Returns the smallest volume to repay until the market gets the closest to the given utilization rate.
|
|
265
266
|
* @param utilization The target utilization rate (scaled by WAD).
|
|
266
267
|
*/
|
|
267
268
|
getRepayToUtilization(utilization) {
|
|
268
|
-
return
|
|
269
|
+
return MarketUtils_js_1.MarketUtils.getRepayToUtilization(this, utilization);
|
|
269
270
|
}
|
|
270
271
|
/**
|
|
271
272
|
* Returns the value of a given amount of collateral quoted in loan assets.
|
|
273
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
272
274
|
* @param collateral The amount of collateral to quote.
|
|
273
275
|
*/
|
|
274
276
|
getCollateralValue(collateral) {
|
|
275
|
-
return
|
|
277
|
+
return MarketUtils_js_1.MarketUtils.getCollateralValue(collateral, this);
|
|
276
278
|
}
|
|
277
279
|
/**
|
|
278
280
|
* Returns the maximum debt allowed given a certain amount of collateral.
|
|
281
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
279
282
|
* To calculate the amount of loan assets that can be borrowed, use `getMaxBorrowableAssets`.
|
|
280
283
|
* @param collateral The amount of collateral to consider.
|
|
281
284
|
*/
|
|
282
|
-
getMaxBorrowAssets(collateral, { maxLtv = this.
|
|
283
|
-
return
|
|
284
|
-
lltv:
|
|
285
|
+
getMaxBorrowAssets(collateral, { maxLtv = this.params.lltv } = {}) {
|
|
286
|
+
return MarketUtils_js_1.MarketUtils.getMaxBorrowAssets(collateral, this, {
|
|
287
|
+
lltv: index_js_1.MathLib.min(maxLtv, this.params.lltv),
|
|
285
288
|
});
|
|
286
289
|
}
|
|
287
290
|
/**
|
|
288
291
|
* Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
|
|
292
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
289
293
|
* @param position The borrow position to consider.
|
|
290
294
|
*/
|
|
291
295
|
getMaxBorrowableAssets(position) {
|
|
292
|
-
return
|
|
296
|
+
return MarketUtils_js_1.MarketUtils.getMaxBorrowableAssets(position, this, this.params);
|
|
293
297
|
}
|
|
294
298
|
/**
|
|
295
299
|
* Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
|
|
300
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
296
301
|
* @param repaidShares The amount of shares hypothetically repaid.
|
|
297
302
|
*/
|
|
298
303
|
getLiquidationSeizedAssets(repaidShares) {
|
|
299
|
-
return
|
|
304
|
+
return MarketUtils_js_1.MarketUtils.getLiquidationSeizedAssets(repaidShares, this, this.params);
|
|
300
305
|
}
|
|
301
306
|
/**
|
|
302
307
|
* Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
|
|
308
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
303
309
|
* @param seizedAssets The amount of collateral hypothetically seized.
|
|
304
310
|
*/
|
|
305
311
|
getLiquidationRepaidShares(seizedAssets) {
|
|
306
|
-
return
|
|
312
|
+
return MarketUtils_js_1.MarketUtils.getLiquidationRepaidShares(seizedAssets, this, this.params);
|
|
307
313
|
}
|
|
308
314
|
/**
|
|
309
315
|
* Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
|
|
316
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
310
317
|
* @param position The borrow position to consider.
|
|
311
318
|
*/
|
|
312
319
|
getSeizableCollateral(position) {
|
|
313
|
-
return
|
|
320
|
+
return MarketUtils_js_1.MarketUtils.getSeizableCollateral(position, this, this.params);
|
|
314
321
|
}
|
|
315
322
|
/**
|
|
316
323
|
* Returns the amount of collateral that can be withdrawn given a certain borrow position.
|
|
324
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
317
325
|
* @param position The borrow position to consider.
|
|
318
326
|
*/
|
|
319
|
-
getWithdrawableCollateral(position, { maxLtv = this.
|
|
320
|
-
return
|
|
321
|
-
lltv:
|
|
327
|
+
getWithdrawableCollateral(position, { maxLtv = this.params.lltv } = {}) {
|
|
328
|
+
return MarketUtils_js_1.MarketUtils.getWithdrawableCollateral(position, this, {
|
|
329
|
+
lltv: index_js_1.MathLib.min(maxLtv, this.params.lltv),
|
|
322
330
|
});
|
|
323
331
|
}
|
|
324
332
|
/**
|
|
325
333
|
* Returns whether a given borrow position is healthy.
|
|
334
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
326
335
|
* @param position The borrow position to check.
|
|
327
336
|
*/
|
|
328
337
|
isHealthy(position) {
|
|
329
|
-
return
|
|
338
|
+
return MarketUtils_js_1.MarketUtils.isHealthy(position, this, this.params);
|
|
330
339
|
}
|
|
331
340
|
/**
|
|
332
341
|
* Returns the liquidation price of a given borrow position.
|
|
333
342
|
* @param position The borrow position to consider.
|
|
334
343
|
*/
|
|
335
344
|
getLiquidationPrice(position) {
|
|
336
|
-
return
|
|
345
|
+
return MarketUtils_js_1.MarketUtils.getLiquidationPrice(position, this, this.params);
|
|
337
346
|
}
|
|
338
347
|
/**
|
|
339
|
-
* Returns the price
|
|
348
|
+
* Returns the price variation required for the given position to reach its liquidation threshold (scaled by WAD).
|
|
349
|
+
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
350
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
351
|
+
* Returns null if the position is not a borrow.
|
|
340
352
|
* @param position The borrow position to consider.
|
|
341
353
|
*/
|
|
342
|
-
|
|
343
|
-
return
|
|
354
|
+
getPriceVariationToLiquidationPrice(position) {
|
|
355
|
+
return MarketUtils_js_1.MarketUtils.getPriceVariationToLiquidationPrice(position, this, this.params);
|
|
344
356
|
}
|
|
345
357
|
/**
|
|
346
358
|
* Returns the health factor of a given borrow position (scaled by WAD).
|
|
347
359
|
* @param position The borrow position to consider.
|
|
348
360
|
*/
|
|
349
361
|
getHealthFactor(position) {
|
|
350
|
-
return
|
|
362
|
+
return MarketUtils_js_1.MarketUtils.getHealthFactor(position, this, this.params);
|
|
351
363
|
}
|
|
352
364
|
/**
|
|
353
365
|
* Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
|
|
354
366
|
* @param position The borrow position to consider.
|
|
355
367
|
*/
|
|
356
368
|
getLtv(position) {
|
|
357
|
-
return
|
|
369
|
+
return MarketUtils_js_1.MarketUtils.getLtv(position, this);
|
|
358
370
|
}
|
|
359
371
|
/**
|
|
360
372
|
* Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
|
|
361
373
|
* @param position The borrow position to consider.
|
|
362
374
|
*/
|
|
363
375
|
getBorrowCapacityUsage(position) {
|
|
364
|
-
return
|
|
376
|
+
return MarketUtils_js_1.MarketUtils.getBorrowCapacityUsage(position, this, this.params);
|
|
365
377
|
}
|
|
366
378
|
/**
|
|
367
379
|
* Returns the maximum amount of loan assets that can be borrowed given a certain borrow position
|
|
368
380
|
* and the reason for the limit.
|
|
381
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
369
382
|
* @param position The borrow position to consider.
|
|
370
383
|
*/
|
|
371
384
|
getBorrowCapacityLimit({ collateral, borrowShares = 0n, }, options) {
|
|
385
|
+
const maxBorrowAssets = this.getMaxBorrowAssets(collateral, options);
|
|
386
|
+
if (maxBorrowAssets == null)
|
|
387
|
+
return;
|
|
372
388
|
// handle edge cases when the user is liquidatable (maxBorrow < borrow)
|
|
373
|
-
const maxBorrowableAssets =
|
|
389
|
+
const maxBorrowableAssets = index_js_1.MathLib.zeroFloorSub(maxBorrowAssets, this.toBorrowAssets(borrowShares));
|
|
374
390
|
const { liquidity } = this;
|
|
375
391
|
if (maxBorrowableAssets > liquidity)
|
|
376
392
|
return {
|
|
@@ -420,10 +436,13 @@ class Market {
|
|
|
420
436
|
/**
|
|
421
437
|
* Returns the maximum amount of collateral assets that can be withdrawn given a certain borrow position
|
|
422
438
|
* and the reason for the limit.
|
|
439
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
423
440
|
* @param position The borrow position to consider.
|
|
424
441
|
*/
|
|
425
442
|
getWithdrawCollateralCapacityLimit(position, options) {
|
|
426
443
|
const withdrawableCollateral = this.getWithdrawableCollateral(position, options);
|
|
444
|
+
if (withdrawableCollateral == null)
|
|
445
|
+
return;
|
|
427
446
|
if (position.collateral > withdrawableCollateral)
|
|
428
447
|
return {
|
|
429
448
|
value: withdrawableCollateral,
|
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
import { Address, BigIntish, MarketId } from "../types";
|
|
2
|
-
export interface
|
|
1
|
+
import type { Address, BigIntish, MarketId } from "../types.js";
|
|
2
|
+
export interface IMarketParams {
|
|
3
3
|
loanToken: Address;
|
|
4
4
|
collateralToken: Address;
|
|
5
5
|
oracle: Address;
|
|
6
6
|
irm: Address;
|
|
7
7
|
lltv: BigIntish;
|
|
8
8
|
}
|
|
9
|
+
export type InputMarketParams = Pick<MarketParams, "loanToken" | "collateralToken" | "oracle" | "irm" | "lltv">;
|
|
9
10
|
/**
|
|
10
11
|
* Represents a market's configuration (also called market params).
|
|
11
12
|
*/
|
|
12
|
-
export declare class
|
|
13
|
+
export declare class MarketParams implements IMarketParams {
|
|
13
14
|
private static readonly _CACHE;
|
|
14
15
|
/**
|
|
15
16
|
* Returns the previously cached market config for the given id, if any.
|
|
16
|
-
* @throws {
|
|
17
|
+
* @throws {UnknownMarketParamsError} If no market config is cached.
|
|
17
18
|
*/
|
|
18
|
-
static get(id: MarketId):
|
|
19
|
+
static get(id: MarketId): MarketParams;
|
|
19
20
|
/**
|
|
20
21
|
* Returns the canonical idle market configuration for the given loan token.
|
|
21
22
|
*/
|
|
22
|
-
static idle(token: Address):
|
|
23
|
+
static idle(token: Address): MarketParams;
|
|
23
24
|
/**
|
|
24
25
|
* The market's collateral token address.
|
|
25
26
|
*/
|
|
@@ -48,5 +49,5 @@ export declare class MarketConfig implements MarketParams {
|
|
|
48
49
|
* The market's liquidation incentive factor.
|
|
49
50
|
*/
|
|
50
51
|
readonly liquidationIncentiveFactor: bigint;
|
|
51
|
-
constructor(params:
|
|
52
|
+
constructor(params: IMarketParams);
|
|
52
53
|
}
|
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
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");
|
|
6
7
|
/**
|
|
7
8
|
* Represents a market's configuration (also called market params).
|
|
8
9
|
*/
|
|
9
|
-
class
|
|
10
|
+
class MarketParams {
|
|
10
11
|
static _CACHE = {};
|
|
11
12
|
/**
|
|
12
13
|
* Returns the previously cached market config for the given id, if any.
|
|
13
|
-
* @throws {
|
|
14
|
+
* @throws {UnknownMarketParamsError} If no market config is cached.
|
|
14
15
|
*/
|
|
15
16
|
static get(id) {
|
|
16
|
-
const
|
|
17
|
-
if (!
|
|
18
|
-
throw new
|
|
19
|
-
return
|
|
17
|
+
const marketParams = MarketParams._CACHE[id];
|
|
18
|
+
if (!marketParams)
|
|
19
|
+
throw new errors_js_1.UnknownMarketParamsError(id);
|
|
20
|
+
return marketParams;
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
22
23
|
* Returns the canonical idle market configuration for the given loan token.
|
|
23
24
|
*/
|
|
24
25
|
static idle(token) {
|
|
25
|
-
return new
|
|
26
|
-
collateralToken:
|
|
26
|
+
return new MarketParams({
|
|
27
|
+
collateralToken: morpho_ts_1.ZERO_ADDRESS,
|
|
27
28
|
loanToken: token,
|
|
28
|
-
oracle:
|
|
29
|
-
irm:
|
|
29
|
+
oracle: morpho_ts_1.ZERO_ADDRESS,
|
|
30
|
+
irm: morpho_ts_1.ZERO_ADDRESS,
|
|
30
31
|
lltv: 0n,
|
|
31
32
|
});
|
|
32
33
|
}
|
|
@@ -53,13 +54,11 @@ class MarketConfig {
|
|
|
53
54
|
/**
|
|
54
55
|
* The market's hex-encoded id, defined as the hash of the market params.
|
|
55
56
|
*/
|
|
56
|
-
// Cached because params are readonly.
|
|
57
|
-
id;
|
|
57
|
+
id; // Cached because params are readonly.
|
|
58
58
|
/**
|
|
59
59
|
* The market's liquidation incentive factor.
|
|
60
60
|
*/
|
|
61
|
-
// Cached because lltv is readonly.
|
|
62
|
-
liquidationIncentiveFactor;
|
|
61
|
+
liquidationIncentiveFactor; // Cached because lltv is readonly.
|
|
63
62
|
constructor(params) {
|
|
64
63
|
const { collateralToken, loanToken, oracle, irm, lltv } = params;
|
|
65
64
|
this.collateralToken = collateralToken;
|
|
@@ -67,10 +66,10 @@ class MarketConfig {
|
|
|
67
66
|
this.oracle = oracle;
|
|
68
67
|
this.irm = irm;
|
|
69
68
|
this.lltv = BigInt(lltv);
|
|
70
|
-
this.id =
|
|
69
|
+
this.id = MarketUtils_js_1.MarketUtils.getMarketId(params);
|
|
71
70
|
this.liquidationIncentiveFactor =
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
MarketUtils_js_1.MarketUtils.getLiquidationIncentiveFactor(params);
|
|
72
|
+
MarketParams._CACHE[this.id] = this;
|
|
74
73
|
}
|
|
75
74
|
}
|
|
76
|
-
exports.
|
|
75
|
+
exports.MarketParams = MarketParams;
|