@morpho-org/blue-sdk 2.0.0-test.1 → 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/LICENSE +21 -0
- package/README.md +46 -26
- package/lib/addresses.d.ts +59 -55
- package/lib/addresses.js +42 -43
- package/lib/chain.d.ts +1 -1
- package/lib/chain.js +3 -3
- package/lib/errors.d.ts +7 -8
- package/lib/errors.js +21 -19
- package/lib/holding/AssetBalances.d.ts +3 -3
- package/lib/holding/Holding.d.ts +10 -5
- package/lib/holding/Holding.js +14 -4
- package/lib/holding/index.d.ts +2 -2
- package/lib/holding/index.js +2 -2
- package/lib/index.d.ts +12 -25
- package/lib/index.js +12 -38
- package/lib/market/Market.d.ts +48 -34
- package/lib/market/Market.js +76 -57
- package/lib/market/{MarketConfig.d.ts → MarketParams.d.ts} +8 -7
- package/lib/market/{MarketConfig.js → MarketParams.js} +20 -21
- package/lib/market/MarketUtils.d.ts +94 -42
- package/lib/market/MarketUtils.js +145 -67
- package/lib/market/index.d.ts +3 -3
- package/lib/market/index.js +3 -3
- package/lib/{maths → math}/AdaptiveCurveIrmLib.d.ts +1 -1
- package/lib/{maths → math}/AdaptiveCurveIrmLib.js +20 -20
- package/lib/{maths → math}/MathLib.d.ts +24 -42
- package/lib/{maths → math}/MathLib.js +41 -54
- package/lib/{maths → math}/SharesMath.d.ts +2 -2
- package/lib/{maths → math}/SharesMath.js +3 -3
- package/lib/math/index.d.ts +3 -0
- package/lib/{helpers/format → math}/index.js +3 -1
- package/lib/position/Position.d.ts +35 -25
- package/lib/position/Position.js +46 -44
- package/lib/position/index.d.ts +1 -1
- package/lib/position/index.js +1 -1
- package/lib/token/ConstantWrappedToken.d.ts +6 -7
- package/lib/token/ConstantWrappedToken.js +8 -10
- package/lib/token/ExchangeRateWrappedToken.d.ts +5 -5
- package/lib/token/ExchangeRateWrappedToken.js +5 -5
- package/lib/token/Token.d.ts +18 -18
- package/lib/token/Token.js +24 -27
- package/lib/token/VaultToken.d.ts +20 -11
- package/lib/token/VaultToken.js +15 -7
- package/lib/token/WrappedToken.d.ts +4 -4
- package/lib/token/WrappedToken.js +7 -7
- package/lib/token/index.d.ts +5 -5
- package/lib/token/index.js +5 -5
- package/lib/types.d.ts +3 -6
- package/lib/user/User.d.ts +1 -1
- package/lib/user/index.d.ts +1 -1
- package/lib/user/index.js +1 -1
- package/lib/vault/Vault.d.ts +22 -28
- package/lib/vault/Vault.js +43 -51
- package/lib/vault/VaultConfig.d.ts +6 -7
- package/lib/vault/VaultConfig.js +5 -5
- package/lib/vault/VaultMarketAllocation.d.ts +8 -8
- package/lib/vault/VaultMarketAllocation.js +3 -3
- package/lib/vault/VaultMarketConfig.d.ts +10 -10
- package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +6 -6
- package/lib/vault/VaultUser.d.ts +26 -0
- package/lib/vault/VaultUser.js +28 -0
- package/lib/vault/VaultUtils.d.ts +4 -6
- package/lib/vault/VaultUtils.js +6 -6
- package/lib/vault/index.d.ts +7 -6
- package/lib/vault/index.js +7 -6
- package/package.json +25 -56
- package/lib/chain.test.d.ts +0 -1
- package/lib/chain.test.js +0 -21
- package/lib/helpers/format/format.d.ts +0 -122
- package/lib/helpers/format/format.js +0 -286
- package/lib/helpers/format/format.test.d.ts +0 -1
- package/lib/helpers/format/format.test.js +0 -255
- package/lib/helpers/format/index.d.ts +0 -1
- package/lib/helpers/index.d.ts +0 -2
- package/lib/helpers/index.js +0 -18
- package/lib/helpers/locale.d.ts +0 -46
- package/lib/helpers/locale.js +0 -96
- package/lib/market/MarketUtils.test.d.ts +0 -1
- package/lib/market/MarketUtils.test.js +0 -38
- package/lib/maths/index.d.ts +0 -3
- package/lib/maths/index.js +0 -19
- package/lib/tests/mocks/markets.d.ts +0 -19
- package/lib/tests/mocks/markets.js +0 -121
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AdaptiveCurveIrmLib = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const constants_js_1 = require("../constants.js");
|
|
5
|
+
const MathLib_js_1 = require("./MathLib.js");
|
|
6
6
|
/**
|
|
7
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.
|
|
8
8
|
*/
|
|
@@ -10,10 +10,10 @@ var AdaptiveCurveIrmLib;
|
|
|
10
10
|
(function (AdaptiveCurveIrmLib) {
|
|
11
11
|
AdaptiveCurveIrmLib.CURVE_STEEPNESS = 4000000000000000000n;
|
|
12
12
|
AdaptiveCurveIrmLib.TARGET_UTILIZATION = 900000000000000000n;
|
|
13
|
-
AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = 40000000000000000n /
|
|
14
|
-
AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = 50000000000000000000n /
|
|
15
|
-
AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = 1000000000000000n /
|
|
16
|
-
AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = 2000000000000000000n /
|
|
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;
|
|
17
17
|
/**
|
|
18
18
|
* ln(2), scaled by WAD.
|
|
19
19
|
*/
|
|
@@ -49,7 +49,7 @@ var AdaptiveCurveIrmLib;
|
|
|
49
49
|
const q = (x + roundingAdjustment) / AdaptiveCurveIrmLib.LN_2_INT;
|
|
50
50
|
const r = x - q * AdaptiveCurveIrmLib.LN_2_INT;
|
|
51
51
|
// Compute e^r with a 2nd-order Taylor polynomial.
|
|
52
|
-
const expR =
|
|
52
|
+
const expR = MathLib_js_1.MathLib.WAD + r + (r * r) / MathLib_js_1.MathLib.WAD / 2n;
|
|
53
53
|
// Return e^x = 2^q * e^r.
|
|
54
54
|
if (q === 0n)
|
|
55
55
|
return expR << q;
|
|
@@ -61,9 +61,9 @@ var AdaptiveCurveIrmLib;
|
|
|
61
61
|
startRateAtTarget = BigInt(startRateAtTarget);
|
|
62
62
|
elapsed = BigInt(elapsed);
|
|
63
63
|
const errNormFactor = startUtilization > AdaptiveCurveIrmLib.TARGET_UTILIZATION
|
|
64
|
-
?
|
|
64
|
+
? MathLib_js_1.MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION
|
|
65
65
|
: AdaptiveCurveIrmLib.TARGET_UTILIZATION;
|
|
66
|
-
const err =
|
|
66
|
+
const err = MathLib_js_1.MathLib.wDivDown(startUtilization - AdaptiveCurveIrmLib.TARGET_UTILIZATION, errNormFactor);
|
|
67
67
|
let avgRateAtTarget;
|
|
68
68
|
let endRateAtTarget;
|
|
69
69
|
if (startRateAtTarget === 0n) {
|
|
@@ -74,7 +74,7 @@ var AdaptiveCurveIrmLib;
|
|
|
74
74
|
else {
|
|
75
75
|
// The speed is assumed constant between two updates, but it is in fact not constant because of interest.
|
|
76
76
|
// So the rate is always underestimated.
|
|
77
|
-
const speed =
|
|
77
|
+
const speed = MathLib_js_1.MathLib.wMulDown(AdaptiveCurveIrmLib.ADJUSTMENT_SPEED, err);
|
|
78
78
|
const linearAdaptation = speed * elapsed;
|
|
79
79
|
if (linearAdaptation === 0n) {
|
|
80
80
|
// If linearAdaptation == 0, avgRateAtTarget = endRateAtTarget = startRateAtTarget;
|
|
@@ -83,7 +83,7 @@ var AdaptiveCurveIrmLib;
|
|
|
83
83
|
}
|
|
84
84
|
else {
|
|
85
85
|
// Non negative because MIN_RATE_AT_TARGET > 0.
|
|
86
|
-
const _newRateAtTarget = (linearAdaptation) =>
|
|
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);
|
|
87
87
|
// Formula of the average rate that should be returned to Morpho Blue:
|
|
88
88
|
// avg = 1/T * ∫_0^T curve(startRateAtTarget*exp(speed*x), err) dx
|
|
89
89
|
// The integral is approximated with the trapezoidal rule:
|
|
@@ -107,11 +107,11 @@ var AdaptiveCurveIrmLib;
|
|
|
107
107
|
}
|
|
108
108
|
// Non negative because 1 - 1/C >= 0, C - 1 >= 0.
|
|
109
109
|
const coeff = err < 0
|
|
110
|
-
?
|
|
111
|
-
: AdaptiveCurveIrmLib.CURVE_STEEPNESS -
|
|
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;
|
|
112
112
|
// Non negative if avgRateAtTarget >= 0 because if err < 0, coeff <= 1.
|
|
113
113
|
return {
|
|
114
|
-
avgBorrowRate:
|
|
114
|
+
avgBorrowRate: MathLib_js_1.MathLib.wMulDown(MathLib_js_1.MathLib.wMulDown(coeff, err) + MathLib_js_1.MathLib.WAD, avgRateAtTarget),
|
|
115
115
|
endRateAtTarget,
|
|
116
116
|
};
|
|
117
117
|
}
|
|
@@ -120,15 +120,15 @@ var AdaptiveCurveIrmLib;
|
|
|
120
120
|
borrowRate = BigInt(borrowRate);
|
|
121
121
|
rateAtTarget = BigInt(rateAtTarget);
|
|
122
122
|
if (borrowRate >= rateAtTarget) {
|
|
123
|
-
const maxBorrowRate =
|
|
123
|
+
const maxBorrowRate = MathLib_js_1.MathLib.wMulDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
|
|
124
124
|
const diffToMaxBorrowRate = maxBorrowRate - rateAtTarget;
|
|
125
125
|
if (diffToMaxBorrowRate === 0n)
|
|
126
|
-
return
|
|
127
|
-
return
|
|
128
|
-
|
|
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));
|
|
129
129
|
}
|
|
130
|
-
const minBorrowRate =
|
|
131
|
-
return
|
|
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));
|
|
132
132
|
}
|
|
133
133
|
AdaptiveCurveIrmLib.getUtilizationAtBorrowRate = getUtilizationAtBorrowRate;
|
|
134
134
|
})(AdaptiveCurveIrmLib || (exports.AdaptiveCurveIrmLib = AdaptiveCurveIrmLib = {}));
|
|
@@ -1,114 +1,96 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BigIntish } from "../types";
|
|
1
|
+
import type { BigIntish } from "../types.js";
|
|
3
2
|
export type RoundingDirection = "Up" | "Down";
|
|
4
3
|
/**
|
|
5
4
|
* Library to manage fixed-point arithmetic.
|
|
6
|
-
*
|
|
7
|
-
* TODO: add library link
|
|
8
|
-
* @category Maths
|
|
9
|
-
*
|
|
5
|
+
* https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
|
|
10
6
|
*/
|
|
11
|
-
export declare
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
export declare namespace MathLib {
|
|
8
|
+
const WAD = 1000000000000000000n;
|
|
9
|
+
const MAX_UINT_256: bigint;
|
|
10
|
+
const MAX_UINT_160: bigint;
|
|
11
|
+
const MAX_UINT_128: bigint;
|
|
12
|
+
const MAX_UINT_48: bigint;
|
|
13
|
+
function maxUint(nBits: number): bigint;
|
|
18
14
|
/**
|
|
19
15
|
* Returns the absolute value of a number
|
|
20
16
|
* @param a The number
|
|
21
17
|
*/
|
|
22
|
-
|
|
18
|
+
function abs(a: BigIntish): bigint;
|
|
23
19
|
/**
|
|
24
20
|
* Returns the smallest number given as param
|
|
25
21
|
* @param x The first number
|
|
26
22
|
* @param y The second number
|
|
27
23
|
*/
|
|
28
|
-
|
|
24
|
+
function min(...xs: BigIntish[]): bigint;
|
|
29
25
|
/**
|
|
30
26
|
* Returns the greatest number given as param
|
|
31
27
|
* @param x The first number
|
|
32
28
|
* @param y The second number
|
|
33
29
|
*/
|
|
34
|
-
|
|
30
|
+
function max(...xs: BigIntish[]): bigint;
|
|
35
31
|
/**
|
|
36
32
|
* Returns the subtraction of b from a, floored to zero if negative
|
|
37
33
|
* @param x The first number
|
|
38
34
|
* @param y The second number
|
|
39
35
|
*/
|
|
40
|
-
|
|
36
|
+
function zeroFloorSub(x: BigIntish, y: BigIntish): bigint;
|
|
41
37
|
/**
|
|
42
38
|
* Perform the WAD-based multiplication of 2 numbers, rounded down
|
|
43
39
|
* @param x The first number
|
|
44
40
|
* @param y The second number
|
|
45
41
|
*/
|
|
46
|
-
|
|
42
|
+
function wMulDown(x: BigIntish, y: BigIntish): bigint;
|
|
47
43
|
/**
|
|
48
44
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
49
45
|
* @param x The first number
|
|
50
46
|
* @param y The second number
|
|
51
47
|
*/
|
|
52
|
-
|
|
48
|
+
function wMulUp(x: BigIntish, y: BigIntish): bigint;
|
|
53
49
|
/**
|
|
54
50
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
55
51
|
* @param x The first number
|
|
56
52
|
* @param y The second number
|
|
57
53
|
*/
|
|
58
|
-
|
|
54
|
+
function wMul(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
|
|
59
55
|
/**
|
|
60
56
|
* Perform the WAD-based division of 2 numbers, rounded down
|
|
61
57
|
* @param x The first number
|
|
62
58
|
* @param y The second number
|
|
63
59
|
*/
|
|
64
|
-
|
|
60
|
+
function wDivDown(x: BigIntish, y: BigIntish): bigint;
|
|
65
61
|
/**
|
|
66
62
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
67
63
|
* @param x The first number
|
|
68
64
|
* @param y The second number
|
|
69
65
|
*/
|
|
70
|
-
|
|
66
|
+
function wDivUp(x: BigIntish, y: BigIntish): bigint;
|
|
71
67
|
/**
|
|
72
68
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
73
69
|
* @param x The first number
|
|
74
70
|
* @param y The second number
|
|
75
71
|
*/
|
|
76
|
-
|
|
72
|
+
function wDiv(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
|
|
77
73
|
/**
|
|
78
74
|
* Multiply two numbers and divide by a denominator, rounding down the result
|
|
79
75
|
* @param x The first number
|
|
80
76
|
* @param y The second number
|
|
81
77
|
* @param denominator The denominator
|
|
82
78
|
*/
|
|
83
|
-
|
|
79
|
+
function mulDivDown(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
|
|
84
80
|
/**
|
|
85
81
|
* Multiply two numbers and divide by a denominator, rounding up the result
|
|
86
82
|
* @param x The first number
|
|
87
83
|
* @param y The second number
|
|
88
84
|
* @param denominator The denominator
|
|
89
85
|
*/
|
|
90
|
-
|
|
91
|
-
|
|
86
|
+
function mulDivUp(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
|
|
87
|
+
function mulDiv(x: BigIntish, y: BigIntish, denominator: BigIntish, rounding: RoundingDirection): bigint;
|
|
92
88
|
/**
|
|
93
89
|
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
94
|
-
* to approximate a
|
|
90
|
+
* to approximate a continuously compounded interest rate.
|
|
95
91
|
*
|
|
96
92
|
* @param x The base of the exponent
|
|
97
93
|
* @param n The exponent
|
|
98
94
|
*/
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Converts an rate to compounded apy
|
|
102
|
-
*
|
|
103
|
-
* @param rate The rate to convert (in WAD)
|
|
104
|
-
* @param period The compounding basis
|
|
105
|
-
*/
|
|
106
|
-
static rateToApy(rate: BigIntish, period: Time.PeriodLike): number;
|
|
107
|
-
/**
|
|
108
|
-
* Converts an apr to compounded apy
|
|
109
|
-
*
|
|
110
|
-
* @param apr The apr to convert (in WAD)
|
|
111
|
-
* @param compounding The compounding basis
|
|
112
|
-
*/
|
|
113
|
-
static aprToApy(apr: BigIntish, compounding: Time.PeriodLike): number;
|
|
95
|
+
function wTaylorCompounded(x: BigIntish, n: BigIntish): bigint;
|
|
114
96
|
}
|
|
@@ -1,115 +1,122 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MathLib = void 0;
|
|
4
|
-
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
|
-
const helpers_1 = require("../helpers");
|
|
6
4
|
/**
|
|
7
5
|
* Library to manage fixed-point arithmetic.
|
|
8
|
-
*
|
|
9
|
-
* TODO: add library link
|
|
10
|
-
* @category Maths
|
|
11
|
-
*
|
|
6
|
+
* https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
|
|
12
7
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
8
|
+
var MathLib;
|
|
9
|
+
(function (MathLib) {
|
|
10
|
+
MathLib.WAD = 1000000000000000000n;
|
|
11
|
+
MathLib.MAX_UINT_256 = maxUint(256);
|
|
12
|
+
MathLib.MAX_UINT_160 = maxUint(160);
|
|
13
|
+
MathLib.MAX_UINT_128 = maxUint(128);
|
|
14
|
+
MathLib.MAX_UINT_48 = maxUint(48);
|
|
15
|
+
function maxUint(nBits) {
|
|
20
16
|
if (nBits % 4 !== 0)
|
|
21
17
|
throw new Error(`Invalid number of bits: ${nBits}`);
|
|
22
|
-
return BigInt(
|
|
18
|
+
return BigInt(`0x${"f".repeat(nBits / 4)}`);
|
|
23
19
|
}
|
|
20
|
+
MathLib.maxUint = maxUint;
|
|
24
21
|
/**
|
|
25
22
|
* Returns the absolute value of a number
|
|
26
23
|
* @param a The number
|
|
27
24
|
*/
|
|
28
|
-
|
|
25
|
+
function abs(a) {
|
|
29
26
|
a = BigInt(a);
|
|
30
27
|
return a >= 0 ? a : -a;
|
|
31
28
|
}
|
|
29
|
+
MathLib.abs = abs;
|
|
32
30
|
/**
|
|
33
31
|
* Returns the smallest number given as param
|
|
34
32
|
* @param x The first number
|
|
35
33
|
* @param y The second number
|
|
36
34
|
*/
|
|
37
|
-
|
|
35
|
+
function min(...xs) {
|
|
38
36
|
return xs.map(BigInt).reduce((x, y) => (x <= y ? x : y));
|
|
39
37
|
}
|
|
38
|
+
MathLib.min = min;
|
|
40
39
|
/**
|
|
41
40
|
* Returns the greatest number given as param
|
|
42
41
|
* @param x The first number
|
|
43
42
|
* @param y The second number
|
|
44
43
|
*/
|
|
45
|
-
|
|
44
|
+
function max(...xs) {
|
|
46
45
|
return xs.map(BigInt).reduce((x, y) => (x <= y ? y : x));
|
|
47
46
|
}
|
|
47
|
+
MathLib.max = max;
|
|
48
48
|
/**
|
|
49
49
|
* Returns the subtraction of b from a, floored to zero if negative
|
|
50
50
|
* @param x The first number
|
|
51
51
|
* @param y The second number
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
function zeroFloorSub(x, y) {
|
|
54
54
|
x = BigInt(x);
|
|
55
55
|
y = BigInt(y);
|
|
56
56
|
return x <= y ? 0n : x - y;
|
|
57
57
|
}
|
|
58
|
+
MathLib.zeroFloorSub = zeroFloorSub;
|
|
58
59
|
/**
|
|
59
60
|
* Perform the WAD-based multiplication of 2 numbers, rounded down
|
|
60
61
|
* @param x The first number
|
|
61
62
|
* @param y The second number
|
|
62
63
|
*/
|
|
63
|
-
|
|
64
|
+
function wMulDown(x, y) {
|
|
64
65
|
return MathLib.wMul(x, y, "Down");
|
|
65
66
|
}
|
|
67
|
+
MathLib.wMulDown = wMulDown;
|
|
66
68
|
/**
|
|
67
69
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
68
70
|
* @param x The first number
|
|
69
71
|
* @param y The second number
|
|
70
72
|
*/
|
|
71
|
-
|
|
73
|
+
function wMulUp(x, y) {
|
|
72
74
|
return MathLib.wMul(x, y, "Up");
|
|
73
75
|
}
|
|
76
|
+
MathLib.wMulUp = wMulUp;
|
|
74
77
|
/**
|
|
75
78
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
76
79
|
* @param x The first number
|
|
77
80
|
* @param y The second number
|
|
78
81
|
*/
|
|
79
|
-
|
|
82
|
+
function wMul(x, y, rounding) {
|
|
80
83
|
return MathLib.mulDiv(x, y, MathLib.WAD, rounding);
|
|
81
84
|
}
|
|
85
|
+
MathLib.wMul = wMul;
|
|
82
86
|
/**
|
|
83
87
|
* Perform the WAD-based division of 2 numbers, rounded down
|
|
84
88
|
* @param x The first number
|
|
85
89
|
* @param y The second number
|
|
86
90
|
*/
|
|
87
|
-
|
|
91
|
+
function wDivDown(x, y) {
|
|
88
92
|
return MathLib.wDiv(x, y, "Down");
|
|
89
93
|
}
|
|
94
|
+
MathLib.wDivDown = wDivDown;
|
|
90
95
|
/**
|
|
91
96
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
92
97
|
* @param x The first number
|
|
93
98
|
* @param y The second number
|
|
94
99
|
*/
|
|
95
|
-
|
|
100
|
+
function wDivUp(x, y) {
|
|
96
101
|
return MathLib.wDiv(x, y, "Up");
|
|
97
102
|
}
|
|
103
|
+
MathLib.wDivUp = wDivUp;
|
|
98
104
|
/**
|
|
99
105
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
100
106
|
* @param x The first number
|
|
101
107
|
* @param y The second number
|
|
102
108
|
*/
|
|
103
|
-
|
|
109
|
+
function wDiv(x, y, rounding) {
|
|
104
110
|
return MathLib.mulDiv(x, MathLib.WAD, y, rounding);
|
|
105
111
|
}
|
|
112
|
+
MathLib.wDiv = wDiv;
|
|
106
113
|
/**
|
|
107
114
|
* Multiply two numbers and divide by a denominator, rounding down the result
|
|
108
115
|
* @param x The first number
|
|
109
116
|
* @param y The second number
|
|
110
117
|
* @param denominator The denominator
|
|
111
118
|
*/
|
|
112
|
-
|
|
119
|
+
function mulDivDown(x, y, denominator) {
|
|
113
120
|
x = BigInt(x);
|
|
114
121
|
y = BigInt(y);
|
|
115
122
|
denominator = BigInt(denominator);
|
|
@@ -117,13 +124,14 @@ class MathLib {
|
|
|
117
124
|
throw Error("MathLib: DIVISION_BY_ZERO");
|
|
118
125
|
return (x * y) / denominator;
|
|
119
126
|
}
|
|
127
|
+
MathLib.mulDivDown = mulDivDown;
|
|
120
128
|
/**
|
|
121
129
|
* Multiply two numbers and divide by a denominator, rounding up the result
|
|
122
130
|
* @param x The first number
|
|
123
131
|
* @param y The second number
|
|
124
132
|
* @param denominator The denominator
|
|
125
133
|
*/
|
|
126
|
-
|
|
134
|
+
function mulDivUp(x, y, denominator) {
|
|
127
135
|
x = BigInt(x);
|
|
128
136
|
y = BigInt(y);
|
|
129
137
|
denominator = BigInt(denominator);
|
|
@@ -132,44 +140,23 @@ class MathLib {
|
|
|
132
140
|
const roundup = (x * y) % denominator > 0 ? 1n : 0n;
|
|
133
141
|
return (x * y) / denominator + roundup;
|
|
134
142
|
}
|
|
135
|
-
|
|
143
|
+
MathLib.mulDivUp = mulDivUp;
|
|
144
|
+
function mulDiv(x, y, denominator, rounding) {
|
|
136
145
|
return MathLib[`mulDiv${rounding}`](x, y, denominator);
|
|
137
146
|
}
|
|
147
|
+
MathLib.mulDiv = mulDiv;
|
|
138
148
|
/**
|
|
139
149
|
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
140
|
-
* to approximate a
|
|
150
|
+
* to approximate a continuously compounded interest rate.
|
|
141
151
|
*
|
|
142
152
|
* @param x The base of the exponent
|
|
143
153
|
* @param n The exponent
|
|
144
154
|
*/
|
|
145
|
-
|
|
155
|
+
function wTaylorCompounded(x, n) {
|
|
146
156
|
const firstTerm = BigInt(x) * BigInt(n);
|
|
147
157
|
const secondTerm = MathLib.mulDivDown(firstTerm, firstTerm, 2n * MathLib.WAD);
|
|
148
158
|
const thirdTerm = MathLib.mulDivDown(secondTerm, firstTerm, 3n * MathLib.WAD);
|
|
149
159
|
return firstTerm + secondTerm + thirdTerm;
|
|
150
160
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
*
|
|
154
|
-
* @param rate The rate to convert (in WAD)
|
|
155
|
-
* @param period The compounding basis
|
|
156
|
-
*/
|
|
157
|
-
static rateToApy(rate, period) {
|
|
158
|
-
const { unit, duration } = morpho_ts_1.Time.toPeriod(period);
|
|
159
|
-
const factor = morpho_ts_1.Time[unit].from.y(1) / duration;
|
|
160
|
-
return ((1 + Number(helpers_1.format.number.locale("en").of(BigInt(rate), 18))) ** factor -
|
|
161
|
-
1);
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Converts an apr to compounded apy
|
|
165
|
-
*
|
|
166
|
-
* @param apr The apr to convert (in WAD)
|
|
167
|
-
* @param compounding The compounding basis
|
|
168
|
-
*/
|
|
169
|
-
static aprToApy(apr, compounding) {
|
|
170
|
-
const { unit, duration } = morpho_ts_1.Time.toPeriod(compounding);
|
|
171
|
-
const rate = (BigInt(apr) * BigInt(duration)) / morpho_ts_1.Time[unit].from.y(1n);
|
|
172
|
-
return this.rateToApy(rate, compounding);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
exports.MathLib = MathLib;
|
|
161
|
+
MathLib.wTaylorCompounded = wTaylorCompounded;
|
|
162
|
+
})(MathLib || (exports.MathLib = MathLib = {}));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BigIntish } from "../types";
|
|
2
|
-
import { RoundingDirection } from "./MathLib";
|
|
1
|
+
import type { BigIntish } from "../types.js";
|
|
2
|
+
import { type RoundingDirection } from "./MathLib.js";
|
|
3
3
|
/**
|
|
4
4
|
* JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
|
|
5
5
|
* & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SharesMath = void 0;
|
|
4
|
-
const
|
|
4
|
+
const MathLib_js_1 = require("./MathLib.js");
|
|
5
5
|
/**
|
|
6
6
|
* JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
|
|
7
7
|
* & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
|
|
@@ -11,11 +11,11 @@ var SharesMath;
|
|
|
11
11
|
SharesMath.VIRTUAL_SHARES = 1000000n;
|
|
12
12
|
SharesMath.VIRTUAL_ASSETS = 1n;
|
|
13
13
|
function toAssets(shares, totalAssets, totalShares, rounding) {
|
|
14
|
-
return
|
|
14
|
+
return MathLib_js_1.MathLib.mulDiv(shares, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
|
|
15
15
|
}
|
|
16
16
|
SharesMath.toAssets = toAssets;
|
|
17
17
|
function toShares(assets, totalAssets, totalShares, rounding) {
|
|
18
|
-
return
|
|
18
|
+
return MathLib_js_1.MathLib.mulDiv(assets, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
|
|
19
19
|
}
|
|
20
20
|
SharesMath.toShares = toShares;
|
|
21
21
|
})(SharesMath || (exports.SharesMath = SharesMath = {}));
|
|
@@ -14,4 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./MathLib.js"), exports);
|
|
18
|
+
__exportStar(require("./SharesMath.js"), exports);
|
|
19
|
+
__exportStar(require("./AdaptiveCurveIrmLib.js"), exports);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Market, MaxBorrowOptions, MaxWithdrawCollateralOptions } from "../market";
|
|
2
|
-
import { Address, BigIntish, MarketId } from "../types";
|
|
3
|
-
export interface
|
|
1
|
+
import { Market, type MaxBorrowOptions, type MaxWithdrawCollateralOptions } from "../market/index.js";
|
|
2
|
+
import type { Address, BigIntish, MarketId } from "../types.js";
|
|
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,69 +28,79 @@ 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
|
*/
|
|
70
76
|
get liquidationPrice(): bigint | null;
|
|
71
77
|
/**
|
|
72
|
-
* The variation
|
|
73
|
-
*
|
|
78
|
+
* The price variation required for the position to reach its liquidation threshold (scaled by WAD).
|
|
79
|
+
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
80
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
81
|
+
* Null if the position is not a borrow.
|
|
74
82
|
*/
|
|
75
|
-
get
|
|
83
|
+
get priceVariationToLiquidationPrice(): bigint | null | undefined;
|
|
76
84
|
/**
|
|
77
85
|
* This position's Loan-To-Value (debt over collateral power, scaled by WAD).
|
|
78
86
|
* If the collateral price is 0, LTV is `MaxUint256`.
|
|
87
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
79
88
|
*/
|
|
80
|
-
get ltv(): bigint | null;
|
|
89
|
+
get ltv(): bigint | null | undefined;
|
|
81
90
|
/**
|
|
82
91
|
* This position's health factor (collateral power over debt, scaled by WAD).
|
|
83
92
|
* If the debt is 0, health factor is `MaxUint256`.
|
|
93
|
+
* `undefined` iff the market's oracle is undefined or reverts.
|
|
84
94
|
*/
|
|
85
|
-
get healthFactor(): bigint | null;
|
|
95
|
+
get healthFactor(): bigint | null | undefined;
|
|
86
96
|
/**
|
|
87
97
|
* The percentage of this position's borrow power currently used (scaled by WAD).
|
|
88
98
|
* If the collateral price is 0, usage is `MaxUint256`.
|
|
89
99
|
*/
|
|
90
|
-
get borrowCapacityUsage(): bigint |
|
|
91
|
-
get borrowCapacityLimit(): import("../market").CapacityLimit;
|
|
92
|
-
get withdrawCapacityLimit(): import("../market").CapacityLimit;
|
|
93
|
-
get withdrawCollateralCapacityLimit(): import("../market").CapacityLimit;
|
|
100
|
+
get borrowCapacityUsage(): bigint | undefined;
|
|
101
|
+
get borrowCapacityLimit(): import("../market/Market.js").CapacityLimit | undefined;
|
|
102
|
+
get withdrawCapacityLimit(): import("../market/Market.js").CapacityLimit;
|
|
103
|
+
get withdrawCollateralCapacityLimit(): import("../market/Market.js").CapacityLimit | undefined;
|
|
94
104
|
/**
|
|
95
105
|
* Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
|
|
96
106
|
* @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
|
|
@@ -118,9 +128,9 @@ export declare class AccrualPosition extends Position implements InputAccrualPos
|
|
|
118
128
|
assets: bigint;
|
|
119
129
|
shares: bigint;
|
|
120
130
|
};
|
|
121
|
-
getRepayCapacityLimit(loanTokenBalance: bigint): import("../market").CapacityLimit;
|
|
131
|
+
getRepayCapacityLimit(loanTokenBalance: bigint): import("../market/Market.js").CapacityLimit;
|
|
122
132
|
getMaxCapacities(loanTokenBalance: bigint, collateralTokenBalance: bigint, options?: {
|
|
123
133
|
borrow?: MaxBorrowOptions;
|
|
124
134
|
withdrawCollateral?: MaxWithdrawCollateralOptions;
|
|
125
|
-
}): import("../market").MaxPositionCapacities;
|
|
135
|
+
}): import("../market/Market.js").MaxPositionCapacities;
|
|
126
136
|
}
|