@morpho-org/blue-sdk 2.0.0-next.9 → 2.0.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 +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 -0
- package/lib/errors.js +37 -13
- package/lib/holding/AssetBalances.d.ts +2 -2
- package/lib/holding/AssetBalances.js +5 -1
- package/lib/holding/Holding.d.ts +5 -5
- 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 +37 -25
- package/lib/market/Market.js +72 -51
- package/lib/market/MarketParams.d.ts +4 -3
- package/lib/market/MarketParams.js +16 -14
- package/lib/market/MarketUtils.d.ts +81 -30
- package/lib/market/MarketUtils.js +135 -51
- package/lib/market/index.js +19 -3
- package/lib/math/AdaptiveCurveIrmLib.js +25 -22
- package/lib/math/MathLib.d.ts +1 -16
- package/lib/math/MathLib.js +6 -29
- package/lib/math/SharesMath.js +8 -5
- package/lib/math/index.js +19 -3
- package/lib/position/Position.d.ts +28 -19
- package/lib/position/Position.js +49 -43
- package/lib/position/index.js +17 -1
- package/lib/token/ConstantWrappedToken.d.ts +2 -2
- package/lib/token/ConstantWrappedToken.js +10 -6
- package/lib/token/ExchangeRateWrappedToken.d.ts +2 -2
- package/lib/token/ExchangeRateWrappedToken.js +9 -5
- package/lib/token/Token.d.ts +15 -15
- package/lib/token/Token.js +27 -25
- package/lib/token/VaultToken.d.ts +4 -4
- package/lib/token/VaultToken.js +9 -5
- package/lib/token/WrappedToken.d.ts +2 -2
- 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 +20 -12
- package/lib/vault/Vault.js +26 -21
- package/lib/vault/VaultConfig.d.ts +3 -4
- package/lib/vault/VaultConfig.js +9 -5
- package/lib/vault/VaultMarketAllocation.d.ts +3 -3
- package/lib/vault/VaultMarketAllocation.js +8 -4
- package/lib/vault/VaultMarketConfig.d.ts +3 -3
- package/lib/vault/VaultMarketConfig.js +5 -1
- package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +3 -3
- package/lib/vault/VaultMarketPublicAllocatorConfig.js +5 -1
- package/lib/vault/VaultUser.d.ts +3 -3
- package/lib/vault/VaultUser.js +5 -1
- package/lib/vault/VaultUtils.js +9 -6
- package/lib/vault/index.js +23 -7
- package/package.json +9 -19
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.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Time } from "@morpho-org/morpho-ts";
|
|
2
1
|
import type { BigIntish } from "../types.js";
|
|
3
2
|
export type RoundingDirection = "Up" | "Down";
|
|
4
3
|
/**
|
|
@@ -88,24 +87,10 @@ export declare namespace MathLib {
|
|
|
88
87
|
function mulDiv(x: BigIntish, y: BigIntish, denominator: BigIntish, rounding: RoundingDirection): bigint;
|
|
89
88
|
/**
|
|
90
89
|
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
91
|
-
* to approximate a
|
|
90
|
+
* to approximate a continuously compounded interest rate.
|
|
92
91
|
*
|
|
93
92
|
* @param x The base of the exponent
|
|
94
93
|
* @param n The exponent
|
|
95
94
|
*/
|
|
96
95
|
function wTaylorCompounded(x: BigIntish, n: BigIntish): bigint;
|
|
97
|
-
/**
|
|
98
|
-
* Converts an rate to compounded apy
|
|
99
|
-
*
|
|
100
|
-
* @param rate The rate to convert (in WAD)
|
|
101
|
-
* @param period The compounding basis
|
|
102
|
-
*/
|
|
103
|
-
function rateToApy(rate: BigIntish, period: Time.PeriodLike): number;
|
|
104
|
-
/**
|
|
105
|
-
* Converts an apr to compounded apy
|
|
106
|
-
*
|
|
107
|
-
* @param apr The apr to convert (in WAD)
|
|
108
|
-
* @param compounding The compounding basis
|
|
109
|
-
*/
|
|
110
|
-
function aprToApy(apr: BigIntish, compounding: Time.PeriodLike): number;
|
|
111
96
|
}
|
package/lib/math/MathLib.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MathLib = void 0;
|
|
2
4
|
/**
|
|
3
5
|
* Library to manage fixed-point arithmetic.
|
|
4
6
|
* https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
|
|
5
7
|
*/
|
|
6
|
-
|
|
8
|
+
var MathLib;
|
|
7
9
|
(function (MathLib) {
|
|
8
10
|
MathLib.WAD = 1000000000000000000n;
|
|
9
11
|
MathLib.MAX_UINT_256 = maxUint(256);
|
|
@@ -145,7 +147,7 @@ export var MathLib;
|
|
|
145
147
|
MathLib.mulDiv = mulDiv;
|
|
146
148
|
/**
|
|
147
149
|
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
148
|
-
* to approximate a
|
|
150
|
+
* to approximate a continuously compounded interest rate.
|
|
149
151
|
*
|
|
150
152
|
* @param x The base of the exponent
|
|
151
153
|
* @param n The exponent
|
|
@@ -157,29 +159,4 @@ export var MathLib;
|
|
|
157
159
|
return firstTerm + secondTerm + thirdTerm;
|
|
158
160
|
}
|
|
159
161
|
MathLib.wTaylorCompounded = wTaylorCompounded;
|
|
160
|
-
|
|
161
|
-
* Converts an rate to compounded apy
|
|
162
|
-
*
|
|
163
|
-
* @param rate The rate to convert (in WAD)
|
|
164
|
-
* @param period The compounding basis
|
|
165
|
-
*/
|
|
166
|
-
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
|
-
1);
|
|
171
|
-
}
|
|
172
|
-
MathLib.rateToApy = rateToApy;
|
|
173
|
-
/**
|
|
174
|
-
* Converts an apr to compounded apy
|
|
175
|
-
*
|
|
176
|
-
* @param apr The apr to convert (in WAD)
|
|
177
|
-
* @param compounding The compounding basis
|
|
178
|
-
*/
|
|
179
|
-
function aprToApy(apr, compounding) {
|
|
180
|
-
const { unit, duration } = Time.toPeriod(compounding);
|
|
181
|
-
const rate = (BigInt(apr) * BigInt(duration)) / Time[unit].from.y(1n);
|
|
182
|
-
return rateToApy(rate, compounding);
|
|
183
|
-
}
|
|
184
|
-
MathLib.aprToApy = aprToApy;
|
|
185
|
-
})(MathLib || (MathLib = {}));
|
|
162
|
+
})(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);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Market, type MaxBorrowOptions, type MaxWithdrawCollateralOptions } from "../market/index.js";
|
|
2
2
|
import type { Address, BigIntish, MarketId } from "../types.js";
|
|
3
|
-
export interface
|
|
3
|
+
export interface IPosition {
|
|
4
4
|
user: Address;
|
|
5
5
|
marketId: MarketId;
|
|
6
6
|
supplyShares: bigint;
|
|
7
7
|
borrowShares: bigint;
|
|
8
8
|
collateral: bigint;
|
|
9
9
|
}
|
|
10
|
-
export declare class Position implements
|
|
10
|
+
export declare class Position implements IPosition {
|
|
11
11
|
/**
|
|
12
12
|
* The user holding this position.
|
|
13
13
|
*/
|
|
@@ -28,42 +28,48 @@ export declare class Position implements InputPosition {
|
|
|
28
28
|
* The amount of collateral assets held with this position.
|
|
29
29
|
*/
|
|
30
30
|
collateral: bigint;
|
|
31
|
-
constructor({ user, marketId, supplyShares, borrowShares, collateral, }:
|
|
31
|
+
constructor({ user, marketId, supplyShares, borrowShares, collateral, }: IPosition);
|
|
32
32
|
}
|
|
33
|
-
export interface
|
|
33
|
+
export interface IAccrualPosition extends Omit<IPosition, "marketId"> {
|
|
34
34
|
}
|
|
35
|
-
export declare class AccrualPosition extends Position implements
|
|
35
|
+
export declare class AccrualPosition extends Position implements IAccrualPosition {
|
|
36
36
|
/**
|
|
37
37
|
* The market on which this position is held.
|
|
38
38
|
*/
|
|
39
39
|
readonly market: Market;
|
|
40
|
-
constructor(position:
|
|
40
|
+
constructor(position: IAccrualPosition, market: Market);
|
|
41
41
|
get supplyAssets(): bigint;
|
|
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,10 +1,10 @@
|
|
|
1
1
|
import { type RoundingDirection } from "../math/index.js";
|
|
2
2
|
import type { Address, BigIntish } from "../types.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { IToken } from "./Token.js";
|
|
4
4
|
import { WrappedToken } from "./WrappedToken.js";
|
|
5
5
|
export declare class ConstantWrappedToken extends WrappedToken {
|
|
6
6
|
readonly underlyingDecimals: bigint;
|
|
7
|
-
constructor(token:
|
|
7
|
+
constructor(token: IToken, underlying: Address, underlyingDecimals?: BigIntish);
|
|
8
8
|
toWrappedExactAmountIn(unwrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
|
|
9
9
|
/** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
|
|
10
10
|
toWrappedExactAmountOut(wrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
|
|
@@ -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,11 +1,11 @@
|
|
|
1
1
|
import { type RoundingDirection } from "../math/index.js";
|
|
2
2
|
import type { Address } from "../types.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { IToken } from "./Token.js";
|
|
4
4
|
import { WrappedToken } from "./WrappedToken.js";
|
|
5
5
|
export declare class ExchangeRateWrappedToken extends WrappedToken {
|
|
6
6
|
readonly underlying: Address;
|
|
7
7
|
wrappedTokenExchangeRate: bigint;
|
|
8
|
-
constructor(token:
|
|
8
|
+
constructor(token: IToken, underlying: Address, wrappedTokenExchangeRate: bigint);
|
|
9
9
|
protected _wrap(amount: bigint, rounding: RoundingDirection): bigint;
|
|
10
10
|
protected _unwrap(amount: bigint, rounding: RoundingDirection): bigint;
|
|
11
11
|
}
|