@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/index.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./MarketUtils.js"), exports);
|
|
18
|
+
__exportStar(require("./MarketParams.js"), exports);
|
|
19
|
+
__exportStar(require("./Market.js"), exports);
|
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AdaptiveCurveIrmLib = void 0;
|
|
4
|
+
const constants_js_1 = require("../constants.js");
|
|
5
|
+
const MathLib_js_1 = require("./MathLib.js");
|
|
3
6
|
/**
|
|
4
7
|
* JS implementation of {@link https://github.com/morpho-org/morpho-blue-irm/blob/main/src/libraries/adaptive-curve/ExpLib.sol ExpLib} used by the Adaptive Curve IRM.
|
|
5
8
|
*/
|
|
6
|
-
|
|
9
|
+
var AdaptiveCurveIrmLib;
|
|
7
10
|
(function (AdaptiveCurveIrmLib) {
|
|
8
11
|
AdaptiveCurveIrmLib.CURVE_STEEPNESS = 4000000000000000000n;
|
|
9
12
|
AdaptiveCurveIrmLib.TARGET_UTILIZATION = 900000000000000000n;
|
|
10
|
-
AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = 40000000000000000n / SECONDS_PER_YEAR;
|
|
11
|
-
AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = 50000000000000000000n / SECONDS_PER_YEAR;
|
|
12
|
-
AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = 1000000000000000n / SECONDS_PER_YEAR;
|
|
13
|
-
AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = 2000000000000000000n / SECONDS_PER_YEAR;
|
|
13
|
+
AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = 40000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
14
|
+
AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = 50000000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
15
|
+
AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = 1000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
16
|
+
AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = 2000000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
14
17
|
/**
|
|
15
18
|
* ln(2), scaled by WAD.
|
|
16
19
|
*/
|
|
@@ -46,7 +49,7 @@ export var AdaptiveCurveIrmLib;
|
|
|
46
49
|
const q = (x + roundingAdjustment) / AdaptiveCurveIrmLib.LN_2_INT;
|
|
47
50
|
const r = x - q * AdaptiveCurveIrmLib.LN_2_INT;
|
|
48
51
|
// Compute e^r with a 2nd-order Taylor polynomial.
|
|
49
|
-
const expR = MathLib.WAD + r + (r * r) / MathLib.WAD / 2n;
|
|
52
|
+
const expR = MathLib_js_1.MathLib.WAD + r + (r * r) / MathLib_js_1.MathLib.WAD / 2n;
|
|
50
53
|
// Return e^x = 2^q * e^r.
|
|
51
54
|
if (q === 0n)
|
|
52
55
|
return expR << q;
|
|
@@ -58,9 +61,9 @@ export var AdaptiveCurveIrmLib;
|
|
|
58
61
|
startRateAtTarget = BigInt(startRateAtTarget);
|
|
59
62
|
elapsed = BigInt(elapsed);
|
|
60
63
|
const errNormFactor = startUtilization > AdaptiveCurveIrmLib.TARGET_UTILIZATION
|
|
61
|
-
? MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION
|
|
64
|
+
? MathLib_js_1.MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION
|
|
62
65
|
: AdaptiveCurveIrmLib.TARGET_UTILIZATION;
|
|
63
|
-
const err = MathLib.wDivDown(startUtilization - AdaptiveCurveIrmLib.TARGET_UTILIZATION, errNormFactor);
|
|
66
|
+
const err = MathLib_js_1.MathLib.wDivDown(startUtilization - AdaptiveCurveIrmLib.TARGET_UTILIZATION, errNormFactor);
|
|
64
67
|
let avgRateAtTarget;
|
|
65
68
|
let endRateAtTarget;
|
|
66
69
|
if (startRateAtTarget === 0n) {
|
|
@@ -71,7 +74,7 @@ export var AdaptiveCurveIrmLib;
|
|
|
71
74
|
else {
|
|
72
75
|
// The speed is assumed constant between two updates, but it is in fact not constant because of interest.
|
|
73
76
|
// So the rate is always underestimated.
|
|
74
|
-
const speed = MathLib.wMulDown(AdaptiveCurveIrmLib.ADJUSTMENT_SPEED, err);
|
|
77
|
+
const speed = MathLib_js_1.MathLib.wMulDown(AdaptiveCurveIrmLib.ADJUSTMENT_SPEED, err);
|
|
75
78
|
const linearAdaptation = speed * elapsed;
|
|
76
79
|
if (linearAdaptation === 0n) {
|
|
77
80
|
// If linearAdaptation == 0, avgRateAtTarget = endRateAtTarget = startRateAtTarget;
|
|
@@ -80,7 +83,7 @@ export var AdaptiveCurveIrmLib;
|
|
|
80
83
|
}
|
|
81
84
|
else {
|
|
82
85
|
// Non negative because MIN_RATE_AT_TARGET > 0.
|
|
83
|
-
const _newRateAtTarget = (linearAdaptation) => MathLib.min(MathLib.max(MathLib.wMulDown(startRateAtTarget, wExp(linearAdaptation)), AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET), AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET);
|
|
86
|
+
const _newRateAtTarget = (linearAdaptation) => MathLib_js_1.MathLib.min(MathLib_js_1.MathLib.max(MathLib_js_1.MathLib.wMulDown(startRateAtTarget, wExp(linearAdaptation)), AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET), AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET);
|
|
84
87
|
// Formula of the average rate that should be returned to Morpho Blue:
|
|
85
88
|
// avg = 1/T * ∫_0^T curve(startRateAtTarget*exp(speed*x), err) dx
|
|
86
89
|
// The integral is approximated with the trapezoidal rule:
|
|
@@ -104,11 +107,11 @@ export var AdaptiveCurveIrmLib;
|
|
|
104
107
|
}
|
|
105
108
|
// Non negative because 1 - 1/C >= 0, C - 1 >= 0.
|
|
106
109
|
const coeff = err < 0
|
|
107
|
-
? MathLib.WAD - MathLib.wDivDown(MathLib.WAD, AdaptiveCurveIrmLib.CURVE_STEEPNESS)
|
|
108
|
-
: AdaptiveCurveIrmLib.CURVE_STEEPNESS - MathLib.WAD;
|
|
110
|
+
? MathLib_js_1.MathLib.WAD - MathLib_js_1.MathLib.wDivDown(MathLib_js_1.MathLib.WAD, AdaptiveCurveIrmLib.CURVE_STEEPNESS)
|
|
111
|
+
: AdaptiveCurveIrmLib.CURVE_STEEPNESS - MathLib_js_1.MathLib.WAD;
|
|
109
112
|
// Non negative if avgRateAtTarget >= 0 because if err < 0, coeff <= 1.
|
|
110
113
|
return {
|
|
111
|
-
avgBorrowRate: MathLib.wMulDown(MathLib.wMulDown(coeff, err) + MathLib.WAD, avgRateAtTarget),
|
|
114
|
+
avgBorrowRate: MathLib_js_1.MathLib.wMulDown(MathLib_js_1.MathLib.wMulDown(coeff, err) + MathLib_js_1.MathLib.WAD, avgRateAtTarget),
|
|
112
115
|
endRateAtTarget,
|
|
113
116
|
};
|
|
114
117
|
}
|
|
@@ -117,15 +120,15 @@ export var AdaptiveCurveIrmLib;
|
|
|
117
120
|
borrowRate = BigInt(borrowRate);
|
|
118
121
|
rateAtTarget = BigInt(rateAtTarget);
|
|
119
122
|
if (borrowRate >= rateAtTarget) {
|
|
120
|
-
const maxBorrowRate = MathLib.wMulDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
|
|
123
|
+
const maxBorrowRate = MathLib_js_1.MathLib.wMulDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
|
|
121
124
|
const diffToMaxBorrowRate = maxBorrowRate - rateAtTarget;
|
|
122
125
|
if (diffToMaxBorrowRate === 0n)
|
|
123
|
-
return MathLib.WAD;
|
|
124
|
-
return MathLib.min(MathLib.WAD, AdaptiveCurveIrmLib.TARGET_UTILIZATION +
|
|
125
|
-
MathLib.mulDivDown(MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - rateAtTarget, diffToMaxBorrowRate));
|
|
126
|
+
return MathLib_js_1.MathLib.WAD;
|
|
127
|
+
return MathLib_js_1.MathLib.min(MathLib_js_1.MathLib.WAD, AdaptiveCurveIrmLib.TARGET_UTILIZATION +
|
|
128
|
+
MathLib_js_1.MathLib.mulDivDown(MathLib_js_1.MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - rateAtTarget, diffToMaxBorrowRate));
|
|
126
129
|
}
|
|
127
|
-
const minBorrowRate = MathLib.wDivDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
|
|
128
|
-
return MathLib.max(0n, MathLib.mulDivDown(AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - minBorrowRate, rateAtTarget - minBorrowRate));
|
|
130
|
+
const minBorrowRate = MathLib_js_1.MathLib.wDivDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
|
|
131
|
+
return MathLib_js_1.MathLib.max(0n, MathLib_js_1.MathLib.mulDivDown(AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - minBorrowRate, rateAtTarget - minBorrowRate));
|
|
129
132
|
}
|
|
130
133
|
AdaptiveCurveIrmLib.getUtilizationAtBorrowRate = getUtilizationAtBorrowRate;
|
|
131
|
-
})(AdaptiveCurveIrmLib || (AdaptiveCurveIrmLib = {}));
|
|
134
|
+
})(AdaptiveCurveIrmLib || (exports.AdaptiveCurveIrmLib = AdaptiveCurveIrmLib = {}));
|
package/lib/math/MathLib.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MathLib = void 0;
|
|
4
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
2
5
|
/**
|
|
3
6
|
* Library to manage fixed-point arithmetic.
|
|
4
7
|
* https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
|
|
5
8
|
*/
|
|
6
|
-
|
|
9
|
+
var MathLib;
|
|
7
10
|
(function (MathLib) {
|
|
8
11
|
MathLib.WAD = 1000000000000000000n;
|
|
9
12
|
MathLib.MAX_UINT_256 = maxUint(256);
|
|
@@ -164,9 +167,9 @@ export var MathLib;
|
|
|
164
167
|
* @param period The compounding basis
|
|
165
168
|
*/
|
|
166
169
|
function rateToApy(rate, period) {
|
|
167
|
-
const { unit, duration } = Time.toPeriod(period);
|
|
168
|
-
const factor = Time[unit].from.y(1) / duration;
|
|
169
|
-
return ((1 + Number(format.number.locale("en").of(BigInt(rate), 18))) ** factor -
|
|
170
|
+
const { unit, duration } = morpho_ts_1.Time.toPeriod(period);
|
|
171
|
+
const factor = morpho_ts_1.Time[unit].from.y(1) / duration;
|
|
172
|
+
return ((1 + Number(morpho_ts_1.format.number.locale("en").of(BigInt(rate), 18))) ** factor -
|
|
170
173
|
1);
|
|
171
174
|
}
|
|
172
175
|
MathLib.rateToApy = rateToApy;
|
|
@@ -177,9 +180,9 @@ export var MathLib;
|
|
|
177
180
|
* @param compounding The compounding basis
|
|
178
181
|
*/
|
|
179
182
|
function aprToApy(apr, compounding) {
|
|
180
|
-
const { unit, duration } = Time.toPeriod(compounding);
|
|
181
|
-
const rate = (BigInt(apr) * BigInt(duration)) / Time[unit].from.y(1n);
|
|
183
|
+
const { unit, duration } = morpho_ts_1.Time.toPeriod(compounding);
|
|
184
|
+
const rate = (BigInt(apr) * BigInt(duration)) / morpho_ts_1.Time[unit].from.y(1n);
|
|
182
185
|
return rateToApy(rate, compounding);
|
|
183
186
|
}
|
|
184
187
|
MathLib.aprToApy = aprToApy;
|
|
185
|
-
})(MathLib || (MathLib = {}));
|
|
188
|
+
})(MathLib || (exports.MathLib = MathLib = {}));
|
package/lib/math/SharesMath.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SharesMath = void 0;
|
|
4
|
+
const MathLib_js_1 = require("./MathLib.js");
|
|
2
5
|
/**
|
|
3
6
|
* JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
|
|
4
7
|
* & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
|
|
5
8
|
*/
|
|
6
|
-
|
|
9
|
+
var SharesMath;
|
|
7
10
|
(function (SharesMath) {
|
|
8
11
|
SharesMath.VIRTUAL_SHARES = 1000000n;
|
|
9
12
|
SharesMath.VIRTUAL_ASSETS = 1n;
|
|
10
13
|
function toAssets(shares, totalAssets, totalShares, rounding) {
|
|
11
|
-
return MathLib.mulDiv(shares, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
|
|
14
|
+
return MathLib_js_1.MathLib.mulDiv(shares, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
|
|
12
15
|
}
|
|
13
16
|
SharesMath.toAssets = toAssets;
|
|
14
17
|
function toShares(assets, totalAssets, totalShares, rounding) {
|
|
15
|
-
return MathLib.mulDiv(assets, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
|
|
18
|
+
return MathLib_js_1.MathLib.mulDiv(assets, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
|
|
16
19
|
}
|
|
17
20
|
SharesMath.toShares = toShares;
|
|
18
|
-
})(SharesMath || (SharesMath = {}));
|
|
21
|
+
})(SharesMath || (exports.SharesMath = SharesMath = {}));
|
package/lib/math/index.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./MathLib.js"), exports);
|
|
18
|
+
__exportStar(require("./SharesMath.js"), exports);
|
|
19
|
+
__exportStar(require("./AdaptiveCurveIrmLib.js"), exports);
|
|
@@ -42,28 +42,34 @@ export declare class AccrualPosition extends Position implements InputAccrualPos
|
|
|
42
42
|
get borrowAssets(): bigint;
|
|
43
43
|
/**
|
|
44
44
|
* The value of this position's collateral quoted in loan assets.
|
|
45
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
45
46
|
*/
|
|
46
|
-
get collateralValue(): bigint;
|
|
47
|
+
get collateralValue(): bigint | undefined;
|
|
47
48
|
/**
|
|
48
49
|
* The maximum amount of loan assets that can be borrowed against this position's collateral.
|
|
50
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
49
51
|
*/
|
|
50
|
-
get maxBorrowAssets(): bigint;
|
|
52
|
+
get maxBorrowAssets(): bigint | undefined;
|
|
51
53
|
/**
|
|
52
54
|
* The maximum additional amount of assets that can be borrowed against this position's collateral.
|
|
55
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
53
56
|
*/
|
|
54
|
-
get maxBorrowableAssets(): bigint;
|
|
57
|
+
get maxBorrowableAssets(): bigint | undefined;
|
|
55
58
|
/**
|
|
56
59
|
* The maximum amount of collateral that can be seized in exchange for the outstanding debt.
|
|
60
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
57
61
|
*/
|
|
58
|
-
get seizableCollateral(): bigint;
|
|
62
|
+
get seizableCollateral(): bigint | undefined;
|
|
59
63
|
/**
|
|
60
64
|
* The maximum amount of collateral that can be withdrawn.
|
|
65
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
61
66
|
*/
|
|
62
|
-
get withdrawableCollateral(): bigint;
|
|
67
|
+
get withdrawableCollateral(): bigint | undefined;
|
|
63
68
|
/**
|
|
64
69
|
* Whether this position is healthy.
|
|
70
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
65
71
|
*/
|
|
66
|
-
get isHealthy(): boolean;
|
|
72
|
+
get isHealthy(): boolean | undefined;
|
|
67
73
|
/**
|
|
68
74
|
* The price of the collateral quoted in loan assets that would allow this position to be liquidated.
|
|
69
75
|
*/
|
|
@@ -71,27 +77,30 @@ export declare class AccrualPosition extends Position implements InputAccrualPos
|
|
|
71
77
|
/**
|
|
72
78
|
* The price variation required for the position to reach its liquidation threshold (scaled by WAD).
|
|
73
79
|
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
74
|
-
*
|
|
80
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
81
|
+
* Null if the position is not a borrow.
|
|
75
82
|
*/
|
|
76
|
-
get priceVariationToLiquidationPrice(): bigint | null;
|
|
83
|
+
get priceVariationToLiquidationPrice(): bigint | null | undefined;
|
|
77
84
|
/**
|
|
78
85
|
* This position's Loan-To-Value (debt over collateral power, scaled by WAD).
|
|
79
86
|
* If the collateral price is 0, LTV is `MaxUint256`.
|
|
87
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
80
88
|
*/
|
|
81
|
-
get ltv(): bigint | null;
|
|
89
|
+
get ltv(): bigint | null | undefined;
|
|
82
90
|
/**
|
|
83
91
|
* This position's health factor (collateral power over debt, scaled by WAD).
|
|
84
92
|
* If the debt is 0, health factor is `MaxUint256`.
|
|
93
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
85
94
|
*/
|
|
86
|
-
get healthFactor(): bigint | null;
|
|
95
|
+
get healthFactor(): bigint | null | undefined;
|
|
87
96
|
/**
|
|
88
97
|
* The percentage of this position's borrow power currently used (scaled by WAD).
|
|
89
98
|
* If the collateral price is 0, usage is `MaxUint256`.
|
|
90
99
|
*/
|
|
91
|
-
get borrowCapacityUsage(): bigint |
|
|
92
|
-
get borrowCapacityLimit(): import("../market/Market.js").CapacityLimit;
|
|
100
|
+
get borrowCapacityUsage(): bigint | undefined;
|
|
101
|
+
get borrowCapacityLimit(): import("../market/Market.js").CapacityLimit | undefined;
|
|
93
102
|
get withdrawCapacityLimit(): import("../market/Market.js").CapacityLimit;
|
|
94
|
-
get withdrawCollateralCapacityLimit(): import("../market/Market.js").CapacityLimit;
|
|
103
|
+
get withdrawCollateralCapacityLimit(): import("../market/Market.js").CapacityLimit | undefined;
|
|
95
104
|
/**
|
|
96
105
|
* Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
|
|
97
106
|
* @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
|
package/lib/position/Position.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccrualPosition = exports.Position = void 0;
|
|
4
|
+
const errors_js_1 = require("../errors.js");
|
|
5
|
+
const index_js_1 = require("../market/index.js");
|
|
6
|
+
class Position {
|
|
4
7
|
/**
|
|
5
8
|
* The user holding this position.
|
|
6
9
|
*/
|
|
@@ -29,7 +32,8 @@ export class Position {
|
|
|
29
32
|
this.collateral = collateral;
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
|
-
|
|
35
|
+
exports.Position = Position;
|
|
36
|
+
class AccrualPosition extends Position {
|
|
33
37
|
/**
|
|
34
38
|
* The market on which this position is held.
|
|
35
39
|
*/
|
|
@@ -46,36 +50,42 @@ export class AccrualPosition extends Position {
|
|
|
46
50
|
}
|
|
47
51
|
/**
|
|
48
52
|
* The value of this position's collateral quoted in loan assets.
|
|
53
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
49
54
|
*/
|
|
50
55
|
get collateralValue() {
|
|
51
56
|
return this.market.getCollateralValue(this.collateral);
|
|
52
57
|
}
|
|
53
58
|
/**
|
|
54
59
|
* The maximum amount of loan assets that can be borrowed against this position's collateral.
|
|
60
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
55
61
|
*/
|
|
56
62
|
get maxBorrowAssets() {
|
|
57
63
|
return this.market.getMaxBorrowAssets(this.collateral);
|
|
58
64
|
}
|
|
59
65
|
/**
|
|
60
66
|
* The maximum additional amount of assets that can be borrowed against this position's collateral.
|
|
67
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
61
68
|
*/
|
|
62
69
|
get maxBorrowableAssets() {
|
|
63
70
|
return this.market.getMaxBorrowableAssets(this);
|
|
64
71
|
}
|
|
65
72
|
/**
|
|
66
73
|
* The maximum amount of collateral that can be seized in exchange for the outstanding debt.
|
|
74
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
67
75
|
*/
|
|
68
76
|
get seizableCollateral() {
|
|
69
77
|
return this.market.getSeizableCollateral(this);
|
|
70
78
|
}
|
|
71
79
|
/**
|
|
72
80
|
* The maximum amount of collateral that can be withdrawn.
|
|
81
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
73
82
|
*/
|
|
74
83
|
get withdrawableCollateral() {
|
|
75
84
|
return this.market.getWithdrawableCollateral(this);
|
|
76
85
|
}
|
|
77
86
|
/**
|
|
78
87
|
* Whether this position is healthy.
|
|
88
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
79
89
|
*/
|
|
80
90
|
get isHealthy() {
|
|
81
91
|
return this.market.isHealthy(this);
|
|
@@ -89,7 +99,8 @@ export class AccrualPosition extends Position {
|
|
|
89
99
|
/**
|
|
90
100
|
* The price variation required for the position to reach its liquidation threshold (scaled by WAD).
|
|
91
101
|
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
92
|
-
*
|
|
102
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
103
|
+
* Null if the position is not a borrow.
|
|
93
104
|
*/
|
|
94
105
|
get priceVariationToLiquidationPrice() {
|
|
95
106
|
return this.market.getPriceVariationToLiquidationPrice(this);
|
|
@@ -97,6 +108,7 @@ export class AccrualPosition extends Position {
|
|
|
97
108
|
/**
|
|
98
109
|
* This position's Loan-To-Value (debt over collateral power, scaled by WAD).
|
|
99
110
|
* If the collateral price is 0, LTV is `MaxUint256`.
|
|
111
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
100
112
|
*/
|
|
101
113
|
get ltv() {
|
|
102
114
|
return this.market.getLtv(this);
|
|
@@ -104,6 +116,7 @@ export class AccrualPosition extends Position {
|
|
|
104
116
|
/**
|
|
105
117
|
* This position's health factor (collateral power over debt, scaled by WAD).
|
|
106
118
|
* If the debt is 0, health factor is `MaxUint256`.
|
|
119
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
107
120
|
*/
|
|
108
121
|
get healthFactor() {
|
|
109
122
|
return this.market.getHealthFactor(this);
|
|
@@ -134,61 +147,53 @@ export class AccrualPosition extends Position {
|
|
|
134
147
|
supply(assets, shares, timestamp) {
|
|
135
148
|
let { market } = this;
|
|
136
149
|
({ market, assets, shares } = market.supply(assets, shares, timestamp));
|
|
137
|
-
this
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
assets,
|
|
141
|
-
shares,
|
|
142
|
-
};
|
|
150
|
+
const position = new AccrualPosition(this, market);
|
|
151
|
+
position.supplyShares += shares;
|
|
152
|
+
return { position, assets, shares };
|
|
143
153
|
}
|
|
144
154
|
withdraw(assets, shares, timestamp) {
|
|
145
155
|
let { market } = this;
|
|
146
156
|
({ market, assets, shares } = market.withdraw(assets, shares, timestamp));
|
|
147
|
-
this
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
assets,
|
|
153
|
-
shares,
|
|
154
|
-
};
|
|
157
|
+
const position = new AccrualPosition(this, market);
|
|
158
|
+
position.supplyShares -= shares;
|
|
159
|
+
if (position.supplyShares < 0n)
|
|
160
|
+
throw new errors_js_1.BlueErrors.InsufficientPosition(position.user, position.marketId);
|
|
161
|
+
return { position, assets, shares };
|
|
155
162
|
}
|
|
156
163
|
supplyCollateral(assets) {
|
|
157
164
|
this.collateral += assets;
|
|
158
|
-
return new AccrualPosition(this, new Market(this.market));
|
|
165
|
+
return new AccrualPosition(this, new index_js_1.Market(this.market));
|
|
159
166
|
}
|
|
160
167
|
withdrawCollateral(assets, timestamp) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
throw new BlueErrors.
|
|
167
|
-
|
|
168
|
+
if (this.market.price == null)
|
|
169
|
+
throw new errors_js_1.BlueErrors.UnknownOraclePrice(this.marketId);
|
|
170
|
+
const position = this.accrueInterest(timestamp);
|
|
171
|
+
position.collateral -= assets;
|
|
172
|
+
if (position.collateral < 0n)
|
|
173
|
+
throw new errors_js_1.BlueErrors.InsufficientPosition(position.user, position.marketId);
|
|
174
|
+
if (!position.isHealthy)
|
|
175
|
+
throw new errors_js_1.BlueErrors.InsufficientCollateral(position.user, position.marketId);
|
|
176
|
+
return position;
|
|
168
177
|
}
|
|
169
178
|
borrow(assets, shares, timestamp) {
|
|
170
179
|
let { market } = this;
|
|
180
|
+
if (market.price == null)
|
|
181
|
+
throw new errors_js_1.BlueErrors.UnknownOraclePrice(market.id);
|
|
171
182
|
({ market, assets, shares } = market.borrow(assets, shares, timestamp));
|
|
172
|
-
this
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
assets,
|
|
178
|
-
shares,
|
|
179
|
-
};
|
|
183
|
+
const position = new AccrualPosition(this, market);
|
|
184
|
+
position.borrowShares += shares;
|
|
185
|
+
if (!position.isHealthy)
|
|
186
|
+
throw new errors_js_1.BlueErrors.InsufficientCollateral(this.user, this.marketId);
|
|
187
|
+
return { position, assets, shares };
|
|
180
188
|
}
|
|
181
189
|
repay(assets, shares, timestamp) {
|
|
182
190
|
let { market } = this;
|
|
183
191
|
({ market, assets, shares } = market.repay(assets, shares, timestamp));
|
|
184
|
-
this
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
assets,
|
|
190
|
-
shares,
|
|
191
|
-
};
|
|
192
|
+
const position = new AccrualPosition(this, market);
|
|
193
|
+
position.borrowShares -= shares;
|
|
194
|
+
if (position.borrowShares < 0n)
|
|
195
|
+
throw new errors_js_1.BlueErrors.InsufficientPosition(position.user, position.marketId);
|
|
196
|
+
return { position, assets, shares };
|
|
192
197
|
}
|
|
193
198
|
getRepayCapacityLimit(loanTokenBalance) {
|
|
194
199
|
return this.market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
|
|
@@ -197,3 +202,4 @@ export class AccrualPosition extends Position {
|
|
|
197
202
|
return this.market.getMaxCapacities(this, loanTokenBalance, collateralTokenBalance, options);
|
|
198
203
|
}
|
|
199
204
|
}
|
|
205
|
+
exports.AccrualPosition = AccrualPosition;
|
package/lib/position/index.js
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Position.js"), exports);
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConstantWrappedToken = void 0;
|
|
4
|
+
const index_js_1 = require("../math/index.js");
|
|
5
|
+
const WrappedToken_js_1 = require("./WrappedToken.js");
|
|
6
|
+
class ConstantWrappedToken extends WrappedToken_js_1.WrappedToken {
|
|
4
7
|
underlyingDecimals;
|
|
5
|
-
constructor(token, underlying, underlyingDecimals =
|
|
8
|
+
constructor(token, underlying, underlyingDecimals = 0) {
|
|
6
9
|
super(token, underlying);
|
|
7
10
|
this.underlyingDecimals = BigInt(underlyingDecimals);
|
|
8
11
|
}
|
|
@@ -22,9 +25,10 @@ export class ConstantWrappedToken extends WrappedToken {
|
|
|
22
25
|
return super.toUnwrappedExactAmountOut(unwrappedAmount, 0n, rounding);
|
|
23
26
|
}
|
|
24
27
|
_wrap(amount) {
|
|
25
|
-
return MathLib.mulDivDown(amount, 10n ** BigInt(this.decimals), 10n ** this.underlyingDecimals);
|
|
28
|
+
return index_js_1.MathLib.mulDivDown(amount, 10n ** BigInt(this.decimals), 10n ** this.underlyingDecimals);
|
|
26
29
|
}
|
|
27
30
|
_unwrap(amount) {
|
|
28
|
-
return MathLib.mulDivDown(amount, 10n ** this.underlyingDecimals, 10n ** BigInt(this.decimals));
|
|
31
|
+
return index_js_1.MathLib.mulDivDown(amount, 10n ** this.underlyingDecimals, 10n ** BigInt(this.decimals));
|
|
29
32
|
}
|
|
30
33
|
}
|
|
34
|
+
exports.ConstantWrappedToken = ConstantWrappedToken;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExchangeRateWrappedToken = void 0;
|
|
4
|
+
const index_js_1 = require("../math/index.js");
|
|
5
|
+
const WrappedToken_js_1 = require("./WrappedToken.js");
|
|
6
|
+
class ExchangeRateWrappedToken extends WrappedToken_js_1.WrappedToken {
|
|
4
7
|
underlying;
|
|
5
8
|
wrappedTokenExchangeRate;
|
|
6
9
|
constructor(token, underlying, wrappedTokenExchangeRate) {
|
|
@@ -9,9 +12,10 @@ export class ExchangeRateWrappedToken extends WrappedToken {
|
|
|
9
12
|
this.wrappedTokenExchangeRate = wrappedTokenExchangeRate;
|
|
10
13
|
}
|
|
11
14
|
_wrap(amount, rounding) {
|
|
12
|
-
return MathLib.wDiv(amount, this.wrappedTokenExchangeRate, rounding);
|
|
15
|
+
return index_js_1.MathLib.wDiv(amount, this.wrappedTokenExchangeRate, rounding);
|
|
13
16
|
}
|
|
14
17
|
_unwrap(amount, rounding) {
|
|
15
|
-
return MathLib.wMul(amount, this.wrappedTokenExchangeRate, rounding);
|
|
18
|
+
return index_js_1.MathLib.wMul(amount, this.wrappedTokenExchangeRate, rounding);
|
|
16
19
|
}
|
|
17
20
|
}
|
|
21
|
+
exports.ExchangeRateWrappedToken = ExchangeRateWrappedToken;
|
package/lib/token/Token.d.ts
CHANGED
|
@@ -3,9 +3,10 @@ import { type RoundingDirection } from "../math/index.js";
|
|
|
3
3
|
import type { Address, BigIntish } from "../types.js";
|
|
4
4
|
export interface InputToken {
|
|
5
5
|
address: Address;
|
|
6
|
-
decimals: BigIntish;
|
|
7
|
-
symbol: string;
|
|
8
6
|
name?: string;
|
|
7
|
+
symbol?: string;
|
|
8
|
+
decimals?: BigIntish;
|
|
9
|
+
price?: BigIntish;
|
|
9
10
|
}
|
|
10
11
|
export declare class Token implements InputToken {
|
|
11
12
|
static native(chainId: ChainId): Token;
|
|
@@ -14,33 +15,32 @@ export declare class Token implements InputToken {
|
|
|
14
15
|
*/
|
|
15
16
|
readonly address: Address;
|
|
16
17
|
/**
|
|
17
|
-
* The token's
|
|
18
|
+
* The token's name.
|
|
18
19
|
*/
|
|
19
|
-
readonly
|
|
20
|
+
readonly name?: string;
|
|
20
21
|
/**
|
|
21
22
|
* The token's symbol.
|
|
22
23
|
*/
|
|
23
|
-
readonly symbol
|
|
24
|
+
readonly symbol?: string;
|
|
24
25
|
/**
|
|
25
|
-
* The
|
|
26
|
+
* The token's number of decimals. Defaults to 0.
|
|
26
27
|
*/
|
|
27
|
-
readonly
|
|
28
|
-
constructor({ address, decimals, symbol, name }: InputToken);
|
|
29
|
-
}
|
|
30
|
-
export declare class TokenWithPrice extends Token {
|
|
28
|
+
readonly decimals: number;
|
|
31
29
|
/**
|
|
32
30
|
* Price of the token in USD (scaled by WAD).
|
|
33
31
|
*/
|
|
34
32
|
price?: bigint;
|
|
35
|
-
constructor(
|
|
33
|
+
constructor({ address, name, symbol, decimals, price }: InputToken);
|
|
36
34
|
/**
|
|
37
35
|
* Quotes an amount in USD (scaled by WAD) in this token.
|
|
36
|
+
* Returns `undefined` iff the token's price is undefined.
|
|
38
37
|
* @param amount The amount of USD to quote.
|
|
39
38
|
*/
|
|
40
|
-
fromUsd(amount: bigint, rounding?: RoundingDirection): bigint |
|
|
39
|
+
fromUsd(amount: bigint, rounding?: RoundingDirection): bigint | undefined;
|
|
41
40
|
/**
|
|
42
41
|
* Quotes an amount of tokens in USD (scaled by WAD).
|
|
42
|
+
* Returns `undefined` iff the token's price is undefined.
|
|
43
43
|
* @param amount The amount of tokens to quote.
|
|
44
44
|
*/
|
|
45
|
-
toUsd(amount: bigint, rounding?: RoundingDirection): bigint |
|
|
45
|
+
toUsd(amount: bigint, rounding?: RoundingDirection): bigint | undefined;
|
|
46
46
|
}
|