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