@morpho-org/blue-sdk 1.0.6 → 1.2.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/README.md +2 -2
- package/lib/addresses.d.ts +34 -20
- package/lib/addresses.js +50 -13
- package/lib/chain.d.ts +30 -0
- package/lib/chain.js +288 -0
- package/lib/chain.test.js +22 -0
- package/lib/constants.d.ts +27 -6
- package/lib/constants.js +28 -8
- package/lib/errors.d.ts +18 -7
- package/lib/errors.js +38 -10
- package/lib/helpers/format/format.js +21 -60
- package/lib/helpers/index.d.ts +0 -2
- package/lib/helpers/index.js +0 -2
- package/lib/holding/Holding.d.ts +3 -4
- package/lib/holding/Holding.js +32 -4
- package/lib/index.d.ts +0 -8
- package/lib/index.js +1 -9
- package/lib/market/Market.d.ts +202 -54
- package/lib/market/Market.js +263 -62
- package/lib/market/MarketConfig.d.ts +17 -9
- package/lib/market/MarketConfig.js +39 -21
- package/lib/market/MarketUtils.d.ts +151 -102
- package/lib/market/MarketUtils.js +91 -39
- package/lib/market/MarketUtils.test.js +1 -2
- package/lib/maths/AdaptiveCurveIrmLib.d.ts +5 -4
- package/lib/maths/AdaptiveCurveIrmLib.js +10 -7
- package/lib/maths/MathLib.d.ts +35 -15
- package/lib/maths/MathLib.js +53 -33
- package/lib/maths/SharesMath.d.ts +3 -3
- package/lib/maths/SharesMath.js +2 -3
- package/lib/maths/index.d.ts +0 -1
- package/lib/maths/index.js +0 -1
- package/lib/position/Position.d.ts +5 -26
- package/lib/position/Position.js +28 -35
- package/lib/tests/mocks/markets.d.ts +1 -0
- package/lib/tests/mocks/markets.js +35 -29
- package/lib/token/Token.d.ts +4 -3
- package/lib/token/Token.js +28 -3
- package/lib/token/WrappedToken.js +12 -6
- package/lib/token/index.d.ts +1 -1
- package/lib/token/index.js +1 -1
- package/lib/types.d.ts +1 -0
- package/lib/types.js +3 -4
- package/lib/user/User.js +12 -0
- package/lib/vault/Vault.d.ts +12 -12
- package/lib/vault/Vault.js +102 -18
- package/lib/vault/VaultConfig.js +9 -3
- package/lib/vault/VaultMarketAllocation.d.ts +20 -0
- package/lib/vault/VaultMarketAllocation.js +30 -0
- package/lib/vault/VaultMarketConfig.d.ts +43 -0
- package/lib/vault/VaultMarketConfig.js +43 -0
- package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
- package/lib/vault/VaultMarketPublicAllocatorConfig.js +28 -0
- package/lib/vault/VaultUtils.d.ts +8 -9
- package/lib/vault/VaultUtils.js +2 -3
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +3 -1
- package/package.json +3 -6
- package/lib/chain/chain.constants.d.ts +0 -3
- package/lib/chain/chain.constants.js +0 -232
- package/lib/chain/chain.test.js +0 -37
- package/lib/chain/chain.types.d.ts +0 -20
- package/lib/chain/chain.types.js +0 -30
- package/lib/chain/chain.utils.d.ts +0 -14
- package/lib/chain/chain.utils.js +0 -30
- package/lib/chain/index.d.ts +0 -2
- package/lib/chain/index.js +0 -18
- package/lib/ethers/ethers.test.d.ts +0 -1
- package/lib/ethers/ethers.test.js +0 -11
- package/lib/ethers/index.d.ts +0 -2
- package/lib/ethers/index.js +0 -18
- package/lib/ethers/safeGetAddress.d.ts +0 -1
- package/lib/ethers/safeGetAddress.js +0 -6
- package/lib/ethers/safeParseUnits.d.ts +0 -2
- package/lib/ethers/safeParseUnits.js +0 -25
- package/lib/evm.d.ts +0 -36
- package/lib/evm.js +0 -113
- package/lib/helpers/getChecksumedAddress.d.ts +0 -7
- package/lib/helpers/getChecksumedAddress.js +0 -17
- package/lib/helpers/isZeroAddressOrUnset.d.ts +0 -7
- package/lib/helpers/isZeroAddressOrUnset.js +0 -14
- package/lib/maths/MathUtils.d.ts +0 -15
- package/lib/maths/MathUtils.js +0 -33
- package/lib/notifications.d.ts +0 -98
- package/lib/notifications.js +0 -52
- package/lib/signatures/index.d.ts +0 -12
- package/lib/signatures/index.js +0 -39
- package/lib/signatures/manager.d.ts +0 -10
- package/lib/signatures/manager.js +0 -37
- package/lib/signatures/permit.d.ts +0 -21
- package/lib/signatures/permit.js +0 -101
- package/lib/signatures/permit2.d.ts +0 -20
- package/lib/signatures/permit2.js +0 -91
- package/lib/signatures/types.d.ts +0 -13
- package/lib/signatures/types.js +0 -2
- package/lib/signatures/utils.d.ts +0 -6
- package/lib/signatures/utils.js +0 -44
- package/lib/token/ERC20Metadata.d.ts +0 -249
- package/lib/token/ERC20Metadata.js +0 -81
- package/lib/token/TokenNamespace.d.ts +0 -18
- package/lib/token/TokenNamespace.js +0 -55
- package/lib/vault/VaultAllocation.d.ts +0 -38
- package/lib/vault/VaultAllocation.js +0 -18
- /package/lib/{chain/chain.test.d.ts → chain.test.d.ts} +0 -0
package/lib/market/Market.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Market = exports.CapacityLimitReason = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const ethers_types_1 = require("ethers-types");
|
|
6
|
-
const addresses_1 = require("../addresses");
|
|
7
|
-
const chain_1 = require("../chain");
|
|
8
4
|
const errors_1 = require("../errors");
|
|
9
5
|
const maths_1 = require("../maths");
|
|
10
|
-
const MarketConfig_1 = require("./MarketConfig");
|
|
11
6
|
const MarketUtils_1 = require("./MarketUtils");
|
|
12
7
|
var CapacityLimitReason;
|
|
13
8
|
(function (CapacityLimitReason) {
|
|
@@ -17,53 +12,71 @@ var CapacityLimitReason;
|
|
|
17
12
|
CapacityLimitReason["collateral"] = "Collateral";
|
|
18
13
|
CapacityLimitReason["cap"] = "Cap";
|
|
19
14
|
})(CapacityLimitReason || (exports.CapacityLimitReason = CapacityLimitReason = {}));
|
|
15
|
+
/**
|
|
16
|
+
* Represents a lending market on Morpho Blue.
|
|
17
|
+
*/
|
|
20
18
|
class Market {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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;
|
|
50
56
|
constructor({ config, totalSupplyAssets, totalBorrowAssets, totalSupplyShares, totalBorrowShares, lastUpdate, fee, price, rateAtTarget, }) {
|
|
51
57
|
this.config = config;
|
|
52
|
-
this.totalSupplyAssets =
|
|
53
|
-
this.totalBorrowAssets =
|
|
54
|
-
this.totalSupplyShares =
|
|
55
|
-
this.totalBorrowShares =
|
|
56
|
-
this.lastUpdate =
|
|
57
|
-
this.fee =
|
|
58
|
-
this.price =
|
|
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;
|
|
59
65
|
if (rateAtTarget != null)
|
|
60
|
-
this.rateAtTarget =
|
|
66
|
+
this.rateAtTarget = rateAtTarget;
|
|
61
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* The market's hex-encoded id, defined as the hash of the market params.
|
|
70
|
+
*/
|
|
62
71
|
get id() {
|
|
63
72
|
return this.config.id;
|
|
64
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Whether the market satisfies the canonical definition of an idle market (i.e. collateral token is the zero address).
|
|
76
|
+
*/
|
|
65
77
|
get isIdle() {
|
|
66
|
-
return this.config.collateralToken ===
|
|
78
|
+
return (this.config.collateralToken ===
|
|
79
|
+
"0x0000000000000000000000000000000000000000");
|
|
67
80
|
}
|
|
68
81
|
/**
|
|
69
82
|
* @warning Cannot be used to calculate the liquidity available inside a callback,
|
|
@@ -72,33 +85,59 @@ class Market {
|
|
|
72
85
|
get liquidity() {
|
|
73
86
|
return this.totalSupplyAssets - this.totalBorrowAssets;
|
|
74
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* The market's utilization rate (scaled by WAD).
|
|
90
|
+
*/
|
|
75
91
|
get utilization() {
|
|
76
92
|
return MarketUtils_1.MarketUtils.getUtilization(this);
|
|
77
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* The market's Annual Percentage Yield (APY) at the IRM's target utilization rate, if applicable (scaled by WAD).
|
|
96
|
+
*/
|
|
78
97
|
get apyAtTarget() {
|
|
79
98
|
if (this.rateAtTarget == null)
|
|
80
99
|
return;
|
|
81
100
|
return MarketUtils_1.MarketUtils.getApy(this.rateAtTarget);
|
|
82
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
|
+
*/
|
|
83
106
|
get supplyRate() {
|
|
84
107
|
return MarketUtils_1.MarketUtils.getSupplyRate(this.borrowRate, this);
|
|
85
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
|
+
*/
|
|
86
113
|
get borrowRate() {
|
|
87
114
|
if (this.rateAtTarget == null)
|
|
88
115
|
return 0n;
|
|
89
116
|
return maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, 0n).avgBorrowRate;
|
|
90
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* The market's supply Annual Percentage Yield (APY) (scaled by WAD).
|
|
120
|
+
*/
|
|
91
121
|
get supplyApy() {
|
|
92
122
|
return MarketUtils_1.MarketUtils.getApy(this.supplyRate);
|
|
93
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* The market's borrow Annual Percentage Yield (APY) (scaled by WAD).
|
|
126
|
+
*/
|
|
94
127
|
get borrowApy() {
|
|
95
128
|
return MarketUtils_1.MarketUtils.getApy(this.borrowRate);
|
|
96
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`.
|
|
133
|
+
*/
|
|
97
134
|
accrueInterest(timestamp) {
|
|
98
|
-
timestamp = (
|
|
135
|
+
timestamp = BigInt(timestamp);
|
|
99
136
|
const elapsed = timestamp - this.lastUpdate;
|
|
100
137
|
if (elapsed < 0n)
|
|
101
|
-
throw new errors_1.
|
|
138
|
+
throw new errors_1.BlueErrors.InvalidInterestAccrual(this.id, timestamp, this.lastUpdate);
|
|
139
|
+
if (elapsed === 0n)
|
|
140
|
+
return this;
|
|
102
141
|
let borrowRate = 0n;
|
|
103
142
|
let { rateAtTarget } = this;
|
|
104
143
|
if (this.rateAtTarget != null) {
|
|
@@ -116,63 +155,203 @@ class Market {
|
|
|
116
155
|
rateAtTarget,
|
|
117
156
|
});
|
|
118
157
|
}
|
|
119
|
-
|
|
120
|
-
|
|
158
|
+
supply(assets, shares, timestamp = this.lastUpdate) {
|
|
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 = this.lastUpdate) {
|
|
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 = this.lastUpdate) {
|
|
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 = this.lastUpdate) {
|
|
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 liquidity available to borrow until the market reach the given utilization rate.
|
|
244
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
245
|
+
*/
|
|
246
|
+
getSupplyLiquidityToUtilization(utilization) {
|
|
247
|
+
return MarketUtils_1.MarketUtils.getSupplyLiquidityToUtilization(this, utilization);
|
|
121
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Returns the liquidity available to borrow until the market reach the given utilization rate.
|
|
251
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
252
|
+
*/
|
|
253
|
+
getBorrowLiquidityToUtilization(utilization) {
|
|
254
|
+
return MarketUtils_1.MarketUtils.getBorrowLiquidityToUtilization(this, utilization);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Returns the value of a given amount of collateral quoted in loan assets.
|
|
258
|
+
* @param collateral The amount of collateral to quote.
|
|
259
|
+
*/
|
|
122
260
|
getCollateralValue(collateral) {
|
|
123
261
|
return MarketUtils_1.MarketUtils.getCollateralValue(collateral, this);
|
|
124
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Returns the maximum amount of loan assets that can be borrowed given a certain amount of collateral.
|
|
265
|
+
* @param collateral The amount of collateral to consider.
|
|
266
|
+
*/
|
|
125
267
|
getMaxBorrowAssets(collateral) {
|
|
126
268
|
return MarketUtils_1.MarketUtils.getMaxBorrowAssets(collateral, this, this.config);
|
|
127
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
|
|
272
|
+
* @param position The borrow position to consider.
|
|
273
|
+
*/
|
|
128
274
|
getMaxBorrowableAssets(position) {
|
|
129
275
|
return MarketUtils_1.MarketUtils.getMaxBorrowableAssets(position, this, this.config);
|
|
130
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
|
|
279
|
+
* @param repaidShares The amount of shares hypothetically repaid.
|
|
280
|
+
*/
|
|
131
281
|
getLiquidationSeizedAssets(repaidShares) {
|
|
132
282
|
return MarketUtils_1.MarketUtils.getLiquidationSeizedAssets(repaidShares, this, this.config);
|
|
133
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
|
|
286
|
+
* @param seizedAssets The amount of collateral hypothetically seized.
|
|
287
|
+
*/
|
|
134
288
|
getLiquidationRepaidShares(seizedAssets) {
|
|
135
289
|
return MarketUtils_1.MarketUtils.getLiquidationRepaidShares(seizedAssets, this, this.config);
|
|
136
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
|
|
293
|
+
* @param position The borrow position to consider.
|
|
294
|
+
*/
|
|
137
295
|
getSeizableCollateral(position) {
|
|
138
296
|
return MarketUtils_1.MarketUtils.getSeizableCollateral(position, this, this.config);
|
|
139
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Returns the amount of collateral that can be withdrawn given a certain borrow position.
|
|
300
|
+
* @param position The borrow position to consider.
|
|
301
|
+
*/
|
|
140
302
|
getWithdrawableCollateral(position) {
|
|
141
303
|
return MarketUtils_1.MarketUtils.getWithdrawableCollateral(position, this, this.config);
|
|
142
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Returns whether a given borrow position is healthy.
|
|
307
|
+
* @param position The borrow position to check.
|
|
308
|
+
*/
|
|
143
309
|
isHealthy(position) {
|
|
144
310
|
return MarketUtils_1.MarketUtils.isHealthy(position, this, this.config);
|
|
145
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Returns the liquidation price of a given borrow position.
|
|
314
|
+
* @param position The borrow position to consider.
|
|
315
|
+
*/
|
|
146
316
|
getLiquidationPrice(position) {
|
|
147
317
|
return MarketUtils_1.MarketUtils.getLiquidationPrice(position, this, this.config);
|
|
148
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
|
|
321
|
+
* @param position The borrow position to consider.
|
|
322
|
+
*/
|
|
149
323
|
getPriceVariationToLiquidation(position) {
|
|
150
324
|
return MarketUtils_1.MarketUtils.getPriceVariationToLiquidation(position, this, this.config);
|
|
151
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Returns the health factor of a given borrow position (scaled by WAD).
|
|
328
|
+
* @param position The borrow position to consider.
|
|
329
|
+
*/
|
|
152
330
|
getHealthFactor(position) {
|
|
153
331
|
return MarketUtils_1.MarketUtils.getHealthFactor(position, this, this.config);
|
|
154
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
|
|
335
|
+
* @param position The borrow position to consider.
|
|
336
|
+
*/
|
|
155
337
|
getLtv(position) {
|
|
156
338
|
return MarketUtils_1.MarketUtils.getLtv(position, this);
|
|
157
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
|
|
342
|
+
* @param position The borrow position to consider.
|
|
343
|
+
*/
|
|
158
344
|
getBorrowCapacityUsage(position) {
|
|
159
345
|
return MarketUtils_1.MarketUtils.getBorrowCapacityUsage(position, this, this.config);
|
|
160
346
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
toBorrowAssets(shares, rounding) {
|
|
168
|
-
return MarketUtils_1.MarketUtils.toBorrowAssets(shares, this, rounding);
|
|
169
|
-
}
|
|
170
|
-
toBorrowShares(assets, rounding) {
|
|
171
|
-
return MarketUtils_1.MarketUtils.toBorrowShares(assets, this, rounding);
|
|
172
|
-
}
|
|
347
|
+
/**
|
|
348
|
+
* Returns the maximum amount of loan assets that can be borrowed given a certain borrow position
|
|
349
|
+
* and the reason for the limit.
|
|
350
|
+
* @param position The borrow position to consider.
|
|
351
|
+
*/
|
|
173
352
|
getBorrowCapacityLimit({ collateral, borrowShares = 0n, }) {
|
|
174
353
|
// handle edge cases when the user is liquidatable (maxBorrow < borrow)
|
|
175
|
-
const maxBorrowableAssets = maths_1.
|
|
354
|
+
const maxBorrowableAssets = maths_1.MathLib.zeroFloorSub(this.getMaxBorrowAssets(collateral), this.toBorrowAssets(borrowShares));
|
|
176
355
|
const { liquidity } = this;
|
|
177
356
|
if (maxBorrowableAssets > liquidity)
|
|
178
357
|
return {
|
|
@@ -184,6 +363,11 @@ class Market {
|
|
|
184
363
|
limiter: CapacityLimitReason.collateral,
|
|
185
364
|
};
|
|
186
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Returns the maximum amount of loan assets that can be repaid given a certain borrow position
|
|
368
|
+
* and a balance of loan assets, and the reason for the limit.
|
|
369
|
+
* @param position The borrow position to consider.
|
|
370
|
+
*/
|
|
187
371
|
getRepayCapacityLimit(borrowShares, loanTokenBalance) {
|
|
188
372
|
const borrowAssets = this.toBorrowAssets(borrowShares);
|
|
189
373
|
if (borrowAssets > loanTokenBalance)
|
|
@@ -196,7 +380,12 @@ class Market {
|
|
|
196
380
|
limiter: CapacityLimitReason.position,
|
|
197
381
|
};
|
|
198
382
|
}
|
|
199
|
-
|
|
383
|
+
/**
|
|
384
|
+
* Returns the maximum amount of loan assets that can be withdrawn given a certain supply position
|
|
385
|
+
* and a balance of loan assets, and the reason for the limit.
|
|
386
|
+
* @param position The supply position to consider.
|
|
387
|
+
*/
|
|
388
|
+
getWithdrawCapacityLimit({ supplyShares, }) {
|
|
200
389
|
const supplyAssets = this.toSupplyAssets(supplyShares);
|
|
201
390
|
const { liquidity } = this;
|
|
202
391
|
if (supplyAssets > liquidity)
|
|
@@ -209,6 +398,11 @@ class Market {
|
|
|
209
398
|
limiter: CapacityLimitReason.position,
|
|
210
399
|
};
|
|
211
400
|
}
|
|
401
|
+
/**
|
|
402
|
+
* Returns the maximum amount of collateral assets that can be withdrawn given a certain borrow position
|
|
403
|
+
* and the reason for the limit.
|
|
404
|
+
* @param position The borrow position to consider.
|
|
405
|
+
*/
|
|
212
406
|
getWithdrawCollateralCapacityLimit(position) {
|
|
213
407
|
const withdrawableCollateral = this.getWithdrawableCollateral(position);
|
|
214
408
|
if (position.collateral > withdrawableCollateral)
|
|
@@ -221,13 +415,20 @@ class Market {
|
|
|
221
415
|
limiter: CapacityLimitReason.position,
|
|
222
416
|
};
|
|
223
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* Returns the maximum capacity for all interactions with Morpho Blue given a certain position
|
|
420
|
+
* and loan and collateral balances.
|
|
421
|
+
* @param position The position to consider.
|
|
422
|
+
* @param loanTokenBalance The balance of loan assets.
|
|
423
|
+
* @param collateralTokenBalance The balance of collateral assets.
|
|
424
|
+
*/
|
|
224
425
|
getMaxCapacities(position, loanTokenBalance, collateralTokenBalance) {
|
|
225
426
|
return {
|
|
226
427
|
supply: {
|
|
227
428
|
value: loanTokenBalance,
|
|
228
429
|
limiter: CapacityLimitReason.balance,
|
|
229
430
|
},
|
|
230
|
-
withdraw: this.getWithdrawCapacityLimit(position
|
|
431
|
+
withdraw: this.getWithdrawCapacityLimit(position),
|
|
231
432
|
borrow: this.getBorrowCapacityLimit(position),
|
|
232
433
|
repay: this.getRepayCapacityLimit(position.borrowShares, loanTokenBalance),
|
|
233
434
|
supplyCollateral: {
|
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { ChainId } from "../chain";
|
|
4
|
-
import { Address, MarketId } from "../types";
|
|
5
|
-
export interface MarketParams extends MarketParamsStruct {
|
|
1
|
+
import { Address, BigIntish, MarketId } from "../types";
|
|
2
|
+
export interface MarketParams {
|
|
6
3
|
loanToken: Address;
|
|
7
4
|
collateralToken: Address;
|
|
8
5
|
oracle: Address;
|
|
9
6
|
irm: Address;
|
|
7
|
+
lltv: BigIntish;
|
|
10
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Represents a market's configuration (also called market params).
|
|
11
|
+
*/
|
|
11
12
|
export declare class MarketConfig implements MarketParams {
|
|
12
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
|
+
*/
|
|
13
18
|
static get(id: MarketId): MarketConfig;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Returns the canonical idle market configuration for the given loan token.
|
|
21
|
+
*/
|
|
17
22
|
static idle(token: Address): MarketConfig;
|
|
18
23
|
/**
|
|
19
24
|
* The market's collateral token address.
|
|
@@ -37,8 +42,11 @@ export declare class MarketConfig implements MarketParams {
|
|
|
37
42
|
readonly lltv: bigint;
|
|
38
43
|
constructor({ collateralToken, loanToken, oracle, irm, lltv }: MarketParams);
|
|
39
44
|
/**
|
|
40
|
-
* The market's hex-encoded id, defined as the hash of the market
|
|
45
|
+
* The market's hex-encoded id, defined as the hash of the market params.
|
|
41
46
|
*/
|
|
42
47
|
get id(): MarketId;
|
|
48
|
+
/**
|
|
49
|
+
* The market's liquidation incentive factor.
|
|
50
|
+
*/
|
|
43
51
|
get liquidationIncentiveFactor(): bigint;
|
|
44
52
|
}
|
|
@@ -1,56 +1,74 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MarketConfig = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const ethers_types_1 = require("ethers-types");
|
|
6
|
-
const addresses_1 = require("../addresses");
|
|
7
|
-
const chain_1 = require("../chain");
|
|
8
4
|
const errors_1 = require("../errors");
|
|
9
5
|
const MarketUtils_1 = require("./MarketUtils");
|
|
6
|
+
/**
|
|
7
|
+
* Represents a market's configuration (also called market params).
|
|
8
|
+
*/
|
|
10
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
|
+
*/
|
|
11
15
|
static get(id) {
|
|
12
16
|
const marketConfig = MarketConfig._CACHE[id];
|
|
13
17
|
if (!marketConfig)
|
|
14
18
|
throw new errors_1.UnknownMarketConfigError(id);
|
|
15
19
|
return marketConfig;
|
|
16
20
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
21
|
-
const { morpho } = (0, addresses_1.getChainAddresses)(chainId);
|
|
22
|
-
config = new MarketConfig(
|
|
23
|
-
// Always fetch at latest block because config is immutable.
|
|
24
|
-
await ethers_types_1.MorphoBlue__factory.connect(morpho, runner).idToMarketParams(id));
|
|
25
|
-
}
|
|
26
|
-
return config;
|
|
27
|
-
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns the canonical idle market configuration for the given loan token.
|
|
23
|
+
*/
|
|
28
24
|
static idle(token) {
|
|
29
25
|
return new MarketConfig({
|
|
30
|
-
collateralToken:
|
|
26
|
+
collateralToken: "0x0000000000000000000000000000000000000000",
|
|
31
27
|
loanToken: token,
|
|
32
|
-
oracle:
|
|
33
|
-
irm:
|
|
28
|
+
oracle: "0x0000000000000000000000000000000000000000",
|
|
29
|
+
irm: "0x0000000000000000000000000000000000000000",
|
|
34
30
|
lltv: 0n,
|
|
35
31
|
});
|
|
36
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;
|
|
37
53
|
constructor({ collateralToken, loanToken, oracle, irm, lltv }) {
|
|
38
54
|
this.collateralToken = collateralToken;
|
|
39
55
|
this.loanToken = loanToken;
|
|
40
56
|
this.oracle = oracle;
|
|
41
57
|
this.irm = irm;
|
|
42
|
-
this.lltv = (
|
|
58
|
+
this.lltv = BigInt(lltv);
|
|
43
59
|
MarketConfig._CACHE[this.id] = this;
|
|
44
60
|
}
|
|
45
61
|
/**
|
|
46
|
-
* The market's hex-encoded id, defined as the hash of the market
|
|
62
|
+
* The market's hex-encoded id, defined as the hash of the market params.
|
|
47
63
|
*/
|
|
48
64
|
get id() {
|
|
49
65
|
return MarketUtils_1.MarketUtils.getMarketId(this);
|
|
50
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* The market's liquidation incentive factor.
|
|
69
|
+
*/
|
|
51
70
|
get liquidationIncentiveFactor() {
|
|
52
71
|
return MarketUtils_1.MarketUtils.getLiquidationIncentiveFactor(this);
|
|
53
72
|
}
|
|
54
73
|
}
|
|
55
74
|
exports.MarketConfig = MarketConfig;
|
|
56
|
-
MarketConfig._CACHE = {};
|