@morpho-org/blue-sdk 5.1.0 → 5.2.0-next.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/lib/addresses.d.ts +122 -20
- package/lib/addresses.js +48 -10
- package/lib/errors.d.ts +16 -0
- package/lib/errors.js +32 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/market/Market.d.ts +29 -25
- package/lib/market/Market.js +41 -29
- package/lib/market/MarketUtils.d.ts +21 -3
- package/lib/market/MarketUtils.js +27 -3
- package/lib/position/Position.d.ts +4 -4
- package/lib/position/Position.js +3 -2
- package/lib/token/VaultToken.d.ts +3 -3
- package/lib/types.d.ts +1 -0
- package/lib/types.js +5 -0
- package/lib/utils.d.ts +15 -0
- package/lib/utils.js +77 -0
- package/lib/vault/Vault.d.ts +31 -11
- package/lib/vault/Vault.js +48 -25
- package/lib/vault/index.d.ts +1 -0
- package/lib/vault/index.js +1 -0
- package/lib/vault/v2/VaultV2.d.ts +87 -0
- package/lib/vault/v2/VaultV2.js +160 -0
- package/lib/vault/v2/VaultV2Adapter.d.ts +29 -0
- package/lib/vault/v2/VaultV2Adapter.js +16 -0
- package/lib/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +24 -0
- package/lib/vault/v2/VaultV2MorphoVaultV1Adapter.js +41 -0
- package/lib/vault/v2/index.d.ts +3 -0
- package/lib/vault/v2/index.js +19 -0
- package/package.json +4 -4
package/lib/market/Market.js
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Market =
|
|
3
|
+
exports.Market = void 0;
|
|
4
4
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
5
|
const errors_js_1 = require("../errors.js");
|
|
6
6
|
const index_js_1 = require("../math/index.js");
|
|
7
|
-
const
|
|
7
|
+
const utils_js_1 = require("../utils.js");
|
|
8
8
|
const MarketParams_js_1 = require("./MarketParams.js");
|
|
9
9
|
const MarketUtils_js_1 = require("./MarketUtils.js");
|
|
10
|
-
var CapacityLimitReason;
|
|
11
|
-
(function (CapacityLimitReason) {
|
|
12
|
-
CapacityLimitReason["liquidity"] = "Liquidity";
|
|
13
|
-
CapacityLimitReason["balance"] = "Balance";
|
|
14
|
-
CapacityLimitReason["position"] = "Position";
|
|
15
|
-
CapacityLimitReason["collateral"] = "Collateral";
|
|
16
|
-
CapacityLimitReason["cap"] = "Cap";
|
|
17
|
-
})(CapacityLimitReason || (exports.CapacityLimitReason = CapacityLimitReason = {}));
|
|
18
10
|
/**
|
|
19
11
|
* Represents a lending market on Morpho Blue.
|
|
20
12
|
*/
|
|
@@ -95,13 +87,27 @@ class Market {
|
|
|
95
87
|
return MarketUtils_js_1.MarketUtils.getUtilization(this);
|
|
96
88
|
}
|
|
97
89
|
/**
|
|
98
|
-
* The market's Annual Percentage Yield (APY) at the IRM's target utilization rate, if applicable.
|
|
90
|
+
* The market's Annual Percentage Yield (APY) at the IRM's target utilization rate, if applicable (scaled by WAD).
|
|
99
91
|
*/
|
|
100
92
|
get apyAtTarget() {
|
|
101
93
|
if (this.rateAtTarget == null)
|
|
102
94
|
return;
|
|
103
95
|
return MarketUtils_js_1.MarketUtils.rateToApy(this.rateAtTarget);
|
|
104
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Returns the rate at which interest accrued for suppliers of this market,
|
|
99
|
+
* since the last time the market was updated (scaled by WAD).
|
|
100
|
+
* @deprecated There's no such thing as a supply rate in Morpho. Only the supply APY is meaningful.
|
|
101
|
+
*/
|
|
102
|
+
get supplyRate() {
|
|
103
|
+
return MarketUtils_js_1.MarketUtils.getSupplyRate(this.avgBorrowRate, this);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @deprecated Use `avgBorrowRate` instead.
|
|
107
|
+
*/
|
|
108
|
+
get borrowRate() {
|
|
109
|
+
return this.getAccrualBorrowRates().avgBorrowRate;
|
|
110
|
+
}
|
|
105
111
|
/**
|
|
106
112
|
* Returns the instantaneous rate at which interest accrues for borrowers of this market,
|
|
107
113
|
* if `accrueInterest` was called immediately onchain (scaled by WAD).
|
|
@@ -132,19 +138,25 @@ class Market {
|
|
|
132
138
|
return this.getAccrualBorrowRates().avgBorrowRate;
|
|
133
139
|
}
|
|
134
140
|
/**
|
|
135
|
-
* The market's current, instantaneous supply-side Annual Percentage Yield (APY).
|
|
141
|
+
* The market's current, instantaneous supply-side Annual Percentage Yield (APY) (scaled by WAD).
|
|
136
142
|
* If interested in the APY at a specific timestamp, use `getSupplyApy(timestamp)` instead.
|
|
137
143
|
*/
|
|
138
144
|
get supplyApy() {
|
|
139
145
|
return this.getSupplyApy();
|
|
140
146
|
}
|
|
141
147
|
/**
|
|
142
|
-
* The market's current, instantaneous borrow-side Annual Percentage Yield (APY).
|
|
148
|
+
* The market's current, instantaneous borrow-side Annual Percentage Yield (APY) (scaled by WAD).
|
|
143
149
|
* If interested in the APY at a specific timestamp, use `getBorrowApy(timestamp)` instead.
|
|
144
150
|
*/
|
|
145
151
|
get borrowApy() {
|
|
146
152
|
return this.getBorrowApy();
|
|
147
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* @deprecated Use `getEndBorrowRate(timestamp)` instead.
|
|
156
|
+
*/
|
|
157
|
+
getBorrowRate(timestamp = morpho_ts_1.Time.timestamp()) {
|
|
158
|
+
return this.getAccrualBorrowRates(timestamp).endBorrowRate;
|
|
159
|
+
}
|
|
148
160
|
/**
|
|
149
161
|
* Returns the instantaneous rate at which interest accrues for borrowers of this market,
|
|
150
162
|
* at the given timestamp, if the state remains unchanged (not accrued) (scaled by WAD).
|
|
@@ -192,7 +204,7 @@ class Market {
|
|
|
192
204
|
}
|
|
193
205
|
/**
|
|
194
206
|
* The market's instantaneous borrow-side Annual Percentage Yield (APY) at the given timestamp,
|
|
195
|
-
* if the state remains unchanged (not accrued).
|
|
207
|
+
* if the state remains unchanged (not accrued) (scaled by WAD).
|
|
196
208
|
* @param timestamp The timestamp at which to calculate the borrow APY.
|
|
197
209
|
* Must be greater than or equal to `lastUpdate`.
|
|
198
210
|
* Defaults to `Time.timestamp()` (returns the current borrow APY).
|
|
@@ -203,18 +215,18 @@ class Market {
|
|
|
203
215
|
}
|
|
204
216
|
/**
|
|
205
217
|
* The market's instantaneous supply-side Annual Percentage Yield (APY) at the given timestamp,
|
|
206
|
-
* if the state remains unchanged (not accrued).
|
|
218
|
+
* if the state remains unchanged (not accrued) (scaled by WAD).
|
|
207
219
|
* @param timestamp The timestamp at which to calculate the supply APY.
|
|
208
220
|
* Must be greater than or equal to `lastUpdate`.
|
|
209
221
|
* Defaults to `Time.timestamp()` (returns the current supply APY).
|
|
210
222
|
*/
|
|
211
223
|
getSupplyApy(timestamp = morpho_ts_1.Time.timestamp()) {
|
|
212
224
|
const borrowApy = this.getBorrowApy(timestamp);
|
|
213
|
-
return (borrowApy
|
|
225
|
+
return index_js_1.MathLib.wMulUp(index_js_1.MathLib.wMulDown(borrowApy, this.utilization), index_js_1.MathLib.WAD - this.fee);
|
|
214
226
|
}
|
|
215
227
|
/**
|
|
216
228
|
* The market's experienced borrow-side Annual Percentage Yield (APY),
|
|
217
|
-
* if interest was to be accrued at the given timestamp.
|
|
229
|
+
* if interest was to be accrued at the given timestamp (scaled by WAD).
|
|
218
230
|
* @param timestamp The timestamp at which to calculate the borrow APY.
|
|
219
231
|
* Must be greater than or equal to `lastUpdate`.
|
|
220
232
|
* Defaults to `Time.timestamp()` (returns the current borrow APY).
|
|
@@ -225,14 +237,14 @@ class Market {
|
|
|
225
237
|
}
|
|
226
238
|
/**
|
|
227
239
|
* The market's experienced supply-side Annual Percentage Yield (APY),
|
|
228
|
-
* if interest was to be accrued at the given timestamp.
|
|
240
|
+
* if interest was to be accrued at the given timestamp (scaled by WAD).
|
|
229
241
|
* @param timestamp The timestamp at which to calculate the supply APY.
|
|
230
242
|
* Must be greater than or equal to `lastUpdate`.
|
|
231
243
|
* Defaults to `Time.timestamp()` (returns the current supply APY).
|
|
232
244
|
*/
|
|
233
245
|
getAvgSupplyApy(timestamp = morpho_ts_1.Time.timestamp()) {
|
|
234
246
|
const borrowApy = this.getAvgBorrowApy(timestamp);
|
|
235
|
-
return (borrowApy
|
|
247
|
+
return index_js_1.MathLib.wMulUp(index_js_1.MathLib.wMulDown(borrowApy, this.utilization), index_js_1.MathLib.WAD - this.fee);
|
|
236
248
|
}
|
|
237
249
|
/**
|
|
238
250
|
* Returns a new market derived from this market, whose interest has been accrued up to the given timestamp.
|
|
@@ -488,11 +500,11 @@ class Market {
|
|
|
488
500
|
if (maxBorrowableAssets > liquidity)
|
|
489
501
|
return {
|
|
490
502
|
value: liquidity,
|
|
491
|
-
limiter: CapacityLimitReason.liquidity,
|
|
503
|
+
limiter: utils_js_1.CapacityLimitReason.liquidity,
|
|
492
504
|
};
|
|
493
505
|
return {
|
|
494
506
|
value: maxBorrowableAssets,
|
|
495
|
-
limiter: CapacityLimitReason.collateral,
|
|
507
|
+
limiter: utils_js_1.CapacityLimitReason.collateral,
|
|
496
508
|
};
|
|
497
509
|
}
|
|
498
510
|
/**
|
|
@@ -505,11 +517,11 @@ class Market {
|
|
|
505
517
|
if (borrowAssets > loanTokenBalance)
|
|
506
518
|
return {
|
|
507
519
|
value: loanTokenBalance,
|
|
508
|
-
limiter: CapacityLimitReason.balance,
|
|
520
|
+
limiter: utils_js_1.CapacityLimitReason.balance,
|
|
509
521
|
};
|
|
510
522
|
return {
|
|
511
523
|
value: borrowAssets,
|
|
512
|
-
limiter: CapacityLimitReason.position,
|
|
524
|
+
limiter: utils_js_1.CapacityLimitReason.position,
|
|
513
525
|
};
|
|
514
526
|
}
|
|
515
527
|
/**
|
|
@@ -523,11 +535,11 @@ class Market {
|
|
|
523
535
|
if (supplyAssets > liquidity)
|
|
524
536
|
return {
|
|
525
537
|
value: liquidity,
|
|
526
|
-
limiter: CapacityLimitReason.liquidity,
|
|
538
|
+
limiter: utils_js_1.CapacityLimitReason.liquidity,
|
|
527
539
|
};
|
|
528
540
|
return {
|
|
529
541
|
value: supplyAssets,
|
|
530
|
-
limiter: CapacityLimitReason.position,
|
|
542
|
+
limiter: utils_js_1.CapacityLimitReason.position,
|
|
531
543
|
};
|
|
532
544
|
}
|
|
533
545
|
/**
|
|
@@ -543,11 +555,11 @@ class Market {
|
|
|
543
555
|
if (position.collateral > withdrawableCollateral)
|
|
544
556
|
return {
|
|
545
557
|
value: withdrawableCollateral,
|
|
546
|
-
limiter: CapacityLimitReason.collateral,
|
|
558
|
+
limiter: utils_js_1.CapacityLimitReason.collateral,
|
|
547
559
|
};
|
|
548
560
|
return {
|
|
549
561
|
value: position.collateral,
|
|
550
|
-
limiter: CapacityLimitReason.position,
|
|
562
|
+
limiter: utils_js_1.CapacityLimitReason.position,
|
|
551
563
|
};
|
|
552
564
|
}
|
|
553
565
|
/**
|
|
@@ -561,14 +573,14 @@ class Market {
|
|
|
561
573
|
return {
|
|
562
574
|
supply: {
|
|
563
575
|
value: loanTokenBalance,
|
|
564
|
-
limiter: CapacityLimitReason.balance,
|
|
576
|
+
limiter: utils_js_1.CapacityLimitReason.balance,
|
|
565
577
|
},
|
|
566
578
|
withdraw: this.getWithdrawCapacityLimit(position),
|
|
567
579
|
borrow: this.getBorrowCapacityLimit(position, options?.borrow),
|
|
568
580
|
repay: this.getRepayCapacityLimit(position.borrowShares, loanTokenBalance),
|
|
569
581
|
supplyCollateral: {
|
|
570
582
|
value: collateralTokenBalance,
|
|
571
|
-
limiter: CapacityLimitReason.balance,
|
|
583
|
+
limiter: utils_js_1.CapacityLimitReason.balance,
|
|
572
584
|
},
|
|
573
585
|
withdrawCollateral: this.getWithdrawCollateralCapacityLimit(position, options?.withdrawCollateral),
|
|
574
586
|
};
|
|
@@ -26,11 +26,29 @@ export declare namespace MarketUtils {
|
|
|
26
26
|
totalBorrowAssets: BigIntish;
|
|
27
27
|
}): bigint;
|
|
28
28
|
/**
|
|
29
|
-
* Returns the
|
|
29
|
+
* Returns the rate at which interest accrued for suppliers on the corresponding market,
|
|
30
|
+
* since the last time the market was updated (scaled by WAD).
|
|
31
|
+
* @param borrowRate The average borrow rate since the last market update (scaled by WAD).
|
|
32
|
+
* @param market The market state.
|
|
33
|
+
* @deprecated There's no such thing as a supply rate in Morpho. Only the supply APY is meaningful.
|
|
34
|
+
*/
|
|
35
|
+
function getSupplyRate(borrowRate: BigIntish, { utilization, fee }: {
|
|
36
|
+
utilization: BigIntish;
|
|
37
|
+
fee: BigIntish;
|
|
38
|
+
}): bigint;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the per-second rate continuously compounded over the given period, as calculated in Morpho Blue (scaled by WAD).
|
|
41
|
+
* @param rate The per-second rate to compound (scaled by WAD).
|
|
42
|
+
* @param period The period to compound the rate over (in seconds). Defaults to 1 year.
|
|
43
|
+
* @deprecated The compounded rate is inaccurate if rate * period >> 0. If interested in the APY, use `rateToApy` instead.
|
|
44
|
+
*/
|
|
45
|
+
function compoundRate(rate: BigIntish, period?: BigIntish): bigint;
|
|
46
|
+
/**
|
|
47
|
+
* Returns the per-second rate continuously compounded over a year (scaled by WAD),
|
|
30
48
|
* as calculated in Morpho Blue assuming the market is frequently accrued onchain.
|
|
31
|
-
* @param rate The per-second rate to compound annually.
|
|
49
|
+
* @param rate The per-second rate to compound annually (scaled by WAD).
|
|
32
50
|
*/
|
|
33
|
-
function rateToApy(rate: BigIntish):
|
|
51
|
+
function rateToApy(rate: BigIntish): bigint;
|
|
34
52
|
/**
|
|
35
53
|
* Returns the interest accrued on both sides of the given market
|
|
36
54
|
* as well as the supply shares minted to the fee recipient.
|
|
@@ -6,6 +6,7 @@ const utils_1 = require("@noble/hashes/utils");
|
|
|
6
6
|
const viem_1 = require("viem");
|
|
7
7
|
const constants_js_1 = require("../constants.js");
|
|
8
8
|
const index_js_1 = require("../math/index.js");
|
|
9
|
+
const utils_js_1 = require("../utils.js");
|
|
9
10
|
/**
|
|
10
11
|
* Namespace of utility functions to ease market-related calculations.
|
|
11
12
|
*/
|
|
@@ -51,12 +52,35 @@ var MarketUtils;
|
|
|
51
52
|
}
|
|
52
53
|
MarketUtils.getUtilization = getUtilization;
|
|
53
54
|
/**
|
|
54
|
-
* Returns the
|
|
55
|
+
* Returns the rate at which interest accrued for suppliers on the corresponding market,
|
|
56
|
+
* since the last time the market was updated (scaled by WAD).
|
|
57
|
+
* @param borrowRate The average borrow rate since the last market update (scaled by WAD).
|
|
58
|
+
* @param market The market state.
|
|
59
|
+
* @deprecated There's no such thing as a supply rate in Morpho. Only the supply APY is meaningful.
|
|
60
|
+
*/
|
|
61
|
+
function getSupplyRate(borrowRate, { utilization, fee }) {
|
|
62
|
+
const borrowRateWithoutFees = index_js_1.MathLib.wMulUp(borrowRate, utilization);
|
|
63
|
+
return index_js_1.MathLib.wMulUp(borrowRateWithoutFees, index_js_1.MathLib.WAD - BigInt(fee));
|
|
64
|
+
}
|
|
65
|
+
MarketUtils.getSupplyRate = getSupplyRate;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the per-second rate continuously compounded over the given period, as calculated in Morpho Blue (scaled by WAD).
|
|
68
|
+
* @param rate The per-second rate to compound (scaled by WAD).
|
|
69
|
+
* @param period The period to compound the rate over (in seconds). Defaults to 1 year.
|
|
70
|
+
* @deprecated The compounded rate is inaccurate if rate * period >> 0. If interested in the APY, use `rateToApy` instead.
|
|
71
|
+
*/
|
|
72
|
+
function compoundRate(rate, period = constants_js_1.SECONDS_PER_YEAR) {
|
|
73
|
+
return index_js_1.MathLib.wTaylorCompounded(rate, period);
|
|
74
|
+
}
|
|
75
|
+
MarketUtils.compoundRate = compoundRate;
|
|
76
|
+
/**
|
|
77
|
+
* Returns the per-second rate continuously compounded over a year (scaled by WAD),
|
|
55
78
|
* as calculated in Morpho Blue assuming the market is frequently accrued onchain.
|
|
56
|
-
* @param rate The per-second rate to compound annually.
|
|
79
|
+
* @param rate The per-second rate to compound annually (scaled by WAD).
|
|
57
80
|
*/
|
|
81
|
+
// TODO: return a Number for APYs.
|
|
58
82
|
function rateToApy(rate) {
|
|
59
|
-
return Math.expm1(+(0, viem_1.formatEther)(BigInt(rate) * constants_js_1.SECONDS_PER_YEAR));
|
|
83
|
+
return (0, utils_js_1.safeParseNumber)(Math.expm1(+(0, viem_1.formatEther)(BigInt(rate) * constants_js_1.SECONDS_PER_YEAR)));
|
|
60
84
|
}
|
|
61
85
|
MarketUtils.rateToApy = rateToApy;
|
|
62
86
|
/**
|
|
@@ -109,7 +109,7 @@ export declare class AccrualPosition extends Position implements IAccrualPositio
|
|
|
109
109
|
* Returns the maximum amount of loan assets that can be withdrawn given a certain supply position
|
|
110
110
|
* and a balance of loan assets, and the reason for the limit.
|
|
111
111
|
*/
|
|
112
|
-
get withdrawCapacityLimit(): import("../
|
|
112
|
+
get withdrawCapacityLimit(): import("../utils.js").CapacityLimit;
|
|
113
113
|
/**
|
|
114
114
|
* Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
|
|
115
115
|
* @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
|
|
@@ -137,9 +137,9 @@ export declare class AccrualPosition extends Position implements IAccrualPositio
|
|
|
137
137
|
assets: bigint;
|
|
138
138
|
shares: bigint;
|
|
139
139
|
};
|
|
140
|
-
getBorrowCapacityLimit(options?: MaxBorrowOptions): import("../
|
|
141
|
-
getWithdrawCollateralCapacityLimit(options?: MaxWithdrawCollateralOptions): import("../
|
|
142
|
-
getRepayCapacityLimit(loanTokenBalance: bigint): import("../
|
|
140
|
+
getBorrowCapacityLimit(options?: MaxBorrowOptions): import("../utils.js").CapacityLimit | undefined;
|
|
141
|
+
getWithdrawCollateralCapacityLimit(options?: MaxWithdrawCollateralOptions): import("../utils.js").CapacityLimit | undefined;
|
|
142
|
+
getRepayCapacityLimit(loanTokenBalance: bigint): import("../utils.js").CapacityLimit;
|
|
143
143
|
getMaxCapacities(loanTokenBalance: bigint, collateralTokenBalance: bigint, options?: {
|
|
144
144
|
borrow?: MaxBorrowOptions;
|
|
145
145
|
withdrawCollateral?: MaxWithdrawCollateralOptions;
|
package/lib/position/Position.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.AccrualPosition = exports.Position = void 0;
|
|
|
4
4
|
const errors_js_1 = require("../errors.js");
|
|
5
5
|
const index_js_1 = require("../market/index.js");
|
|
6
6
|
const MathLib_js_1 = require("../math/MathLib.js");
|
|
7
|
+
const utils_js_1 = require("../utils.js");
|
|
7
8
|
class Position {
|
|
8
9
|
/**
|
|
9
10
|
* The user holding this position.
|
|
@@ -225,14 +226,14 @@ class AccrualPosition extends Position {
|
|
|
225
226
|
return {
|
|
226
227
|
supply: {
|
|
227
228
|
value: loanTokenBalance,
|
|
228
|
-
limiter:
|
|
229
|
+
limiter: utils_js_1.CapacityLimitReason.balance,
|
|
229
230
|
},
|
|
230
231
|
withdraw: this.withdrawCapacityLimit,
|
|
231
232
|
borrow: this.getBorrowCapacityLimit(options?.borrow),
|
|
232
233
|
repay: this.getRepayCapacityLimit(loanTokenBalance),
|
|
233
234
|
supplyCollateral: {
|
|
234
235
|
value: collateralTokenBalance,
|
|
235
|
-
limiter:
|
|
236
|
+
limiter: utils_js_1.CapacityLimitReason.balance,
|
|
236
237
|
},
|
|
237
238
|
withdrawCollateral: this.getWithdrawCollateralCapacityLimit(options?.withdrawCollateral),
|
|
238
239
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RoundingDirection } from "../math/index.js";
|
|
2
|
-
import type { Address } from "../types.js";
|
|
2
|
+
import type { Address, BigIntish } from "../types.js";
|
|
3
3
|
import type { IVaultConfig } from "../vault/VaultConfig.js";
|
|
4
4
|
import { WrappedToken } from "./WrappedToken.js";
|
|
5
5
|
export interface IVaultToken {
|
|
@@ -18,6 +18,6 @@ export declare class VaultToken extends WrappedToken implements IVaultToken {
|
|
|
18
18
|
*/
|
|
19
19
|
totalAssets: bigint;
|
|
20
20
|
constructor(config: IVaultConfig, { totalAssets, totalSupply }: IVaultToken);
|
|
21
|
-
protected _wrap(amount:
|
|
22
|
-
protected _unwrap(amount:
|
|
21
|
+
protected _wrap(amount: BigIntish, rounding: RoundingDirection): bigint;
|
|
22
|
+
protected _unwrap(amount: BigIntish, rounding: RoundingDirection): bigint;
|
|
23
23
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -23,4 +23,5 @@ export declare enum TransactionType {
|
|
|
23
23
|
export type Loadable<T> = T | undefined;
|
|
24
24
|
export type Failable<T> = T | null;
|
|
25
25
|
export type Fetchable<T> = Failable<Loadable<T>>;
|
|
26
|
+
export declare function isFetched<T>(v: Fetchable<T>): v is T;
|
|
26
27
|
export declare const isMarketId: (value: unknown) => value is MarketId;
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isMarketId = exports.TransactionType = void 0;
|
|
4
|
+
exports.isFetched = isFetched;
|
|
4
5
|
/**
|
|
5
6
|
* The possible transaction type on the Blue contract
|
|
6
7
|
*/
|
|
@@ -13,5 +14,9 @@ var TransactionType;
|
|
|
13
14
|
TransactionType["Borrow"] = "Borrow";
|
|
14
15
|
TransactionType["Repay"] = "Repay";
|
|
15
16
|
})(TransactionType || (exports.TransactionType = TransactionType = {}));
|
|
17
|
+
// TODO: replace with isDefined
|
|
18
|
+
function isFetched(v) {
|
|
19
|
+
return v !== undefined && v !== null;
|
|
20
|
+
}
|
|
16
21
|
const isMarketId = (value) => typeof value === "string" && /^0x[0-9A-Fa-f]{64}$/.test(value);
|
|
17
22
|
exports.isMarketId = isMarketId;
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare enum CapacityLimitReason {
|
|
2
|
+
liquidity = "Liquidity",
|
|
3
|
+
balance = "Balance",
|
|
4
|
+
position = "Position",
|
|
5
|
+
collateral = "Collateral",
|
|
6
|
+
cap = "Cap",
|
|
7
|
+
vaultV2_absoluteCap = "VaultV2_AbsoluteCap",
|
|
8
|
+
vaultV2_relativeCap = "VaultV2_RelativeCap"
|
|
9
|
+
}
|
|
10
|
+
export interface CapacityLimit {
|
|
11
|
+
value: bigint;
|
|
12
|
+
limiter: CapacityLimitReason;
|
|
13
|
+
}
|
|
14
|
+
export declare const safeParseNumber: (value: number, decimals?: number) => bigint;
|
|
15
|
+
export declare const safeParseUnits: (strValue: string, decimals?: number) => bigint;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.safeParseUnits = exports.safeParseNumber = exports.CapacityLimitReason = void 0;
|
|
4
|
+
var CapacityLimitReason;
|
|
5
|
+
(function (CapacityLimitReason) {
|
|
6
|
+
CapacityLimitReason["liquidity"] = "Liquidity";
|
|
7
|
+
CapacityLimitReason["balance"] = "Balance";
|
|
8
|
+
CapacityLimitReason["position"] = "Position";
|
|
9
|
+
CapacityLimitReason["collateral"] = "Collateral";
|
|
10
|
+
CapacityLimitReason["cap"] = "Cap";
|
|
11
|
+
CapacityLimitReason["vaultV2_absoluteCap"] = "VaultV2_AbsoluteCap";
|
|
12
|
+
CapacityLimitReason["vaultV2_relativeCap"] = "VaultV2_RelativeCap";
|
|
13
|
+
})(CapacityLimitReason || (exports.CapacityLimitReason = CapacityLimitReason = {}));
|
|
14
|
+
// Alternative to Number.toFixed that doesn't use scientific notation for excessively small or large numbers.
|
|
15
|
+
const toFixed = (x, decimals) => new Intl.NumberFormat("en-US", {
|
|
16
|
+
style: "decimal",
|
|
17
|
+
useGrouping: false,
|
|
18
|
+
maximumFractionDigits: decimals,
|
|
19
|
+
minimumFractionDigits: decimals,
|
|
20
|
+
}).format(x);
|
|
21
|
+
const safeParseNumber = (value, decimals = 18) => (0, exports.safeParseUnits)(toFixed(value, decimals), decimals);
|
|
22
|
+
exports.safeParseNumber = safeParseNumber;
|
|
23
|
+
const safeParseUnits = (strValue, decimals = 18) => {
|
|
24
|
+
if (!/[-+]?[0-9]*\.?[0-9]+/.test(strValue))
|
|
25
|
+
throw Error(`invalid number: ${strValue}`);
|
|
26
|
+
let [whole, dec = ""] = strValue.split(".");
|
|
27
|
+
dec = dec.slice(0, decimals);
|
|
28
|
+
return parseUnits([whole || "0", dec].filter((v) => v.length > 0).join("."), decimals);
|
|
29
|
+
};
|
|
30
|
+
exports.safeParseUnits = safeParseUnits;
|
|
31
|
+
/**
|
|
32
|
+
* Multiplies a string representation of a number by a given exponent of base 10 (10exponent).
|
|
33
|
+
*
|
|
34
|
+
* - Docs: https://viem.sh/docs/utilities/parseUnits
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* import { parseUnits } from 'viem'
|
|
38
|
+
*
|
|
39
|
+
* parseUnits('420', 9)
|
|
40
|
+
* // 420000000000n
|
|
41
|
+
*/
|
|
42
|
+
// TODO: get rid of this copy.
|
|
43
|
+
function parseUnits(value, decimals) {
|
|
44
|
+
let [integer, fraction = "0"] = value.split(".");
|
|
45
|
+
const negative = integer.startsWith("-");
|
|
46
|
+
if (negative)
|
|
47
|
+
integer = integer.slice(1);
|
|
48
|
+
// trim trailing zeros.
|
|
49
|
+
fraction = fraction.replace(/(0+)$/, "");
|
|
50
|
+
// round off if the fraction is larger than the number of decimals.
|
|
51
|
+
if (decimals === 0) {
|
|
52
|
+
if (Math.round(Number(`.${fraction}`)) === 1)
|
|
53
|
+
integer = `${BigInt(integer) + 1n}`;
|
|
54
|
+
fraction = "";
|
|
55
|
+
}
|
|
56
|
+
else if (fraction.length > decimals) {
|
|
57
|
+
const [left, unit, right] = [
|
|
58
|
+
fraction.slice(0, decimals - 1),
|
|
59
|
+
fraction.slice(decimals - 1, decimals),
|
|
60
|
+
fraction.slice(decimals),
|
|
61
|
+
];
|
|
62
|
+
const rounded = Math.round(Number(`${unit}.${right}`));
|
|
63
|
+
if (rounded > 9)
|
|
64
|
+
fraction = `${BigInt(left) + BigInt(1)}0`.padStart(left.length + 1, "0");
|
|
65
|
+
else
|
|
66
|
+
fraction = `${left}${rounded}`;
|
|
67
|
+
if (fraction.length > decimals) {
|
|
68
|
+
fraction = fraction.slice(1);
|
|
69
|
+
integer = `${BigInt(integer) + 1n}`;
|
|
70
|
+
}
|
|
71
|
+
fraction = fraction.slice(0, decimals);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
fraction = fraction.padEnd(decimals, "0");
|
|
75
|
+
}
|
|
76
|
+
return BigInt(`${negative ? "-" : ""}${integer}${fraction}`);
|
|
77
|
+
}
|
package/lib/vault/Vault.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type CapacityLimit } from "../market/index.js";
|
|
2
1
|
import { type RoundingDirection } from "../math/index.js";
|
|
3
2
|
import { VaultToken } from "../token/index.js";
|
|
4
3
|
import type { Address, BigIntish, MarketId } from "../types.js";
|
|
4
|
+
import { type CapacityLimit } from "../utils.js";
|
|
5
5
|
import type { IVaultConfig } from "./VaultConfig.js";
|
|
6
6
|
import { type IVaultMarketAllocation, VaultMarketAllocation } from "./VaultMarketAllocation.js";
|
|
7
7
|
export interface Pending<T> {
|
|
@@ -116,8 +116,8 @@ export declare class Vault extends VaultToken implements IVault {
|
|
|
116
116
|
* The amount of interest in assets accrued since the last interaction with the vault.
|
|
117
117
|
*/
|
|
118
118
|
get totalInterest(): bigint;
|
|
119
|
-
toAssets(shares:
|
|
120
|
-
toShares(assets:
|
|
119
|
+
toAssets(shares: BigIntish, rounding?: RoundingDirection): bigint;
|
|
120
|
+
toShares(assets: BigIntish, rounding?: RoundingDirection): bigint;
|
|
121
121
|
}
|
|
122
122
|
export interface CollateralAllocation {
|
|
123
123
|
address: Address;
|
|
@@ -149,31 +149,51 @@ export declare class AccrualVault extends Vault implements IAccrualVault {
|
|
|
149
149
|
get liquidity(): bigint;
|
|
150
150
|
/**
|
|
151
151
|
* The MetaMorpho vault's current instantaneous Annual Percentage Yield (APY)
|
|
152
|
-
* weighted-averaged over its market deposits, before deducting the performance fee.
|
|
152
|
+
* weighted-averaged over its market deposits, before deducting the performance fee (scaled by WAD).
|
|
153
153
|
* If interested in the APY at a specific timestamp, use `getApy(timestamp)` instead.
|
|
154
154
|
*/
|
|
155
|
-
get apy():
|
|
155
|
+
get apy(): bigint;
|
|
156
156
|
/**
|
|
157
157
|
* The MetaMorpho vault's current instantaneous Annual Percentage Yield (APY)
|
|
158
|
-
* weighted-averaged over its market deposits, after deducting the performance fee.
|
|
158
|
+
* weighted-averaged over its market deposits, after deducting the performance fee (scaled by WAD).
|
|
159
159
|
* If interested in the APY at a specific timestamp, use `getApy(timestamp)` instead.
|
|
160
160
|
*/
|
|
161
|
-
get netApy():
|
|
161
|
+
get netApy(): bigint;
|
|
162
162
|
/**
|
|
163
163
|
* The MetaMorpho vault's experienced Annual Percentage Yield (APY)
|
|
164
164
|
* weighted-averaged over its market deposits, before deducting the performance fee,
|
|
165
|
-
* if interest was to be accrued on each market at the given timestamp.
|
|
165
|
+
* if interest was to be accrued on each market at the given timestamp (scaled by WAD).
|
|
166
166
|
*/
|
|
167
|
-
getApy(timestamp?: BigIntish):
|
|
167
|
+
getApy(timestamp?: BigIntish): bigint;
|
|
168
168
|
/**
|
|
169
169
|
* The MetaMorpho vault's experienced Annual Percentage Yield (APY)
|
|
170
170
|
* weighted-averaged over its market deposits, after deducting the performance fee,
|
|
171
|
-
* if interest was to be accrued on each market at the given timestamp.
|
|
171
|
+
* if interest was to be accrued on each market at the given timestamp (scaled by WAD).
|
|
172
172
|
*/
|
|
173
|
-
getNetApy(timestamp?: BigIntish):
|
|
173
|
+
getNetApy(timestamp?: BigIntish): bigint;
|
|
174
174
|
getAllocationProportion(marketId: MarketId): bigint;
|
|
175
|
+
/**
|
|
176
|
+
* Returns the deposit capacity limit of a given amount of assets on the vault.
|
|
177
|
+
* @param assets The maximum amount of assets to deposit.
|
|
178
|
+
* @deprecated Use `maxDeposit` instead.
|
|
179
|
+
*/
|
|
175
180
|
getDepositCapacityLimit(assets: bigint): CapacityLimit;
|
|
181
|
+
/**
|
|
182
|
+
* Returns the withdraw capacity limit corresponding to a given amount of shares of the vault.
|
|
183
|
+
* @param shares The maximum amount of shares to redeem.
|
|
184
|
+
* @deprecated Use `maxWithdraw` instead.
|
|
185
|
+
*/
|
|
176
186
|
getWithdrawCapacityLimit(shares: bigint): CapacityLimit;
|
|
187
|
+
/**
|
|
188
|
+
* Returns the maximum amount of assets that can be deposited to the vault.
|
|
189
|
+
* @param assets The maximum amount of assets to deposit.
|
|
190
|
+
*/
|
|
191
|
+
maxDeposit(assets: BigIntish): CapacityLimit;
|
|
192
|
+
/**
|
|
193
|
+
* Returns the maximum amount of assets that can be withdrawn from the vault.
|
|
194
|
+
* @param shares The maximum amount of shares to redeem.
|
|
195
|
+
*/
|
|
196
|
+
maxWithdraw(shares: BigIntish): CapacityLimit;
|
|
177
197
|
/**
|
|
178
198
|
* Returns a new vault derived from this vault, whose interest has been accrued up to the given timestamp.
|
|
179
199
|
* @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to each of the vault's market's `lastUpdate`.
|