@morpho-org/blue-sdk 6.0.1 → 6.1.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/cjs/addresses.d.ts +105 -0
- package/lib/cjs/addresses.js +74 -0
- package/lib/cjs/chain.d.ts +84 -1
- package/lib/cjs/chain.js +78 -0
- package/lib/cjs/errors.d.ts +34 -0
- package/lib/cjs/errors.js +28 -0
- package/lib/cjs/holding/AssetBalances.d.ts +3 -0
- package/lib/cjs/holding/AssetBalances.js +1 -0
- package/lib/cjs/holding/Holding.d.ts +6 -0
- package/lib/cjs/holding/Holding.js +2 -0
- package/lib/cjs/market/Market.d.ts +4 -0
- package/lib/cjs/market/MarketParams.d.ts +3 -0
- package/lib/cjs/market/MarketParams.js +1 -0
- package/lib/cjs/market/MarketUtils.d.ts +397 -0
- package/lib/cjs/market/MarketUtils.js +397 -2
- package/lib/cjs/math/AdaptiveCurveIrmLib.d.ts +51 -1
- package/lib/cjs/math/AdaptiveCurveIrmLib.js +51 -1
- package/lib/cjs/math/MathLib.d.ts +152 -4
- package/lib/cjs/math/MathLib.js +151 -4
- package/lib/cjs/math/SharesMath.d.ts +34 -0
- package/lib/cjs/math/SharesMath.js +34 -0
- package/lib/cjs/position/Position.d.ts +4 -0
- package/lib/cjs/position/Position.js +2 -0
- package/lib/cjs/position/PreLiquidationPosition.d.ts +4 -0
- package/lib/cjs/position/PreLiquidationPosition.js +2 -0
- package/lib/cjs/preLiquidation.d.ts +16 -0
- package/lib/cjs/preLiquidation.js +16 -0
- package/lib/cjs/token/ConstantWrappedToken.d.ts +1 -0
- package/lib/cjs/token/ConstantWrappedToken.js +1 -0
- package/lib/cjs/token/Eip5267Domain.d.ts +4 -0
- package/lib/cjs/token/Eip5267Domain.js +2 -0
- package/lib/cjs/token/ExchangeRateWrappedToken.d.ts +1 -0
- package/lib/cjs/token/ExchangeRateWrappedToken.js +1 -0
- package/lib/cjs/token/Token.d.ts +2 -0
- package/lib/cjs/token/Token.js +1 -0
- package/lib/cjs/token/VaultToken.d.ts +2 -0
- package/lib/cjs/token/VaultToken.js +1 -0
- package/lib/cjs/token/WrappedToken.d.ts +1 -0
- package/lib/cjs/token/WrappedToken.js +1 -0
- package/lib/cjs/types.d.ts +17 -0
- package/lib/cjs/types.js +13 -0
- package/lib/cjs/user/User.d.ts +1 -0
- package/lib/cjs/user/User.js +1 -0
- package/lib/cjs/utils.d.ts +2 -0
- package/lib/cjs/utils.js +1 -0
- package/lib/cjs/vault/Vault.d.ts +7 -0
- package/lib/cjs/vault/Vault.js +2 -0
- package/lib/cjs/vault/VaultConfig.d.ts +2 -0
- package/lib/cjs/vault/VaultConfig.js +1 -0
- package/lib/cjs/vault/VaultMarketAllocation.d.ts +2 -0
- package/lib/cjs/vault/VaultMarketAllocation.js +1 -0
- package/lib/cjs/vault/VaultMarketConfig.d.ts +2 -0
- package/lib/cjs/vault/VaultMarketConfig.js +1 -0
- package/lib/cjs/vault/VaultMarketPublicAllocatorConfig.d.ts +1 -0
- package/lib/cjs/vault/VaultMarketPublicAllocatorConfig.js +1 -0
- package/lib/cjs/vault/VaultUser.d.ts +2 -0
- package/lib/cjs/vault/VaultUser.js +1 -0
- package/lib/cjs/vault/VaultUtils.d.ts +49 -0
- package/lib/cjs/vault/VaultUtils.js +49 -0
- package/lib/cjs/vault/v2/VaultV2.d.ts +5 -0
- package/lib/cjs/vault/v2/VaultV2.js +2 -0
- package/lib/cjs/vault/v2/VaultV2Adapter.d.ts +3 -0
- package/lib/cjs/vault/v2/VaultV2Adapter.js +1 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1Adapter.d.ts +4 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1Adapter.js +2 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +4 -0
- package/lib/cjs/vault/v2/VaultV2MorphoMarketV1AdapterV2.js +2 -0
- package/lib/cjs/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +4 -0
- package/lib/cjs/vault/v2/VaultV2MorphoVaultV1Adapter.js +2 -0
- package/lib/esm/addresses.d.ts +105 -0
- package/lib/esm/addresses.js +74 -0
- package/lib/esm/chain.d.ts +84 -1
- package/lib/esm/chain.js +78 -0
- package/lib/esm/errors.d.ts +34 -0
- package/lib/esm/errors.js +28 -0
- package/lib/esm/holding/AssetBalances.d.ts +3 -0
- package/lib/esm/holding/AssetBalances.js +1 -0
- package/lib/esm/holding/Holding.d.ts +6 -0
- package/lib/esm/holding/Holding.js +2 -0
- package/lib/esm/market/Market.d.ts +4 -0
- package/lib/esm/market/MarketParams.d.ts +3 -0
- package/lib/esm/market/MarketParams.js +1 -0
- package/lib/esm/market/MarketUtils.d.ts +397 -0
- package/lib/esm/market/MarketUtils.js +397 -2
- package/lib/esm/math/AdaptiveCurveIrmLib.d.ts +51 -1
- package/lib/esm/math/AdaptiveCurveIrmLib.js +51 -1
- package/lib/esm/math/MathLib.d.ts +152 -4
- package/lib/esm/math/MathLib.js +151 -4
- package/lib/esm/math/SharesMath.d.ts +34 -0
- package/lib/esm/math/SharesMath.js +34 -0
- package/lib/esm/position/Position.d.ts +4 -0
- package/lib/esm/position/Position.js +2 -0
- package/lib/esm/position/PreLiquidationPosition.d.ts +4 -0
- package/lib/esm/position/PreLiquidationPosition.js +2 -0
- package/lib/esm/preLiquidation.d.ts +16 -0
- package/lib/esm/preLiquidation.js +16 -0
- package/lib/esm/token/ConstantWrappedToken.d.ts +1 -0
- package/lib/esm/token/ConstantWrappedToken.js +1 -0
- package/lib/esm/token/Eip5267Domain.d.ts +4 -0
- package/lib/esm/token/Eip5267Domain.js +2 -0
- package/lib/esm/token/ExchangeRateWrappedToken.d.ts +1 -0
- package/lib/esm/token/ExchangeRateWrappedToken.js +1 -0
- package/lib/esm/token/Token.d.ts +2 -0
- package/lib/esm/token/Token.js +1 -0
- package/lib/esm/token/VaultToken.d.ts +2 -0
- package/lib/esm/token/VaultToken.js +1 -0
- package/lib/esm/token/WrappedToken.d.ts +1 -0
- package/lib/esm/token/WrappedToken.js +1 -0
- package/lib/esm/types.d.ts +17 -0
- package/lib/esm/types.js +13 -0
- package/lib/esm/user/User.d.ts +1 -0
- package/lib/esm/user/User.js +1 -0
- package/lib/esm/utils.d.ts +2 -0
- package/lib/esm/utils.js +1 -0
- package/lib/esm/vault/Vault.d.ts +7 -0
- package/lib/esm/vault/Vault.js +2 -0
- package/lib/esm/vault/VaultConfig.d.ts +2 -0
- package/lib/esm/vault/VaultConfig.js +1 -0
- package/lib/esm/vault/VaultMarketAllocation.d.ts +2 -0
- package/lib/esm/vault/VaultMarketAllocation.js +1 -0
- package/lib/esm/vault/VaultMarketConfig.d.ts +2 -0
- package/lib/esm/vault/VaultMarketConfig.js +1 -0
- package/lib/esm/vault/VaultMarketPublicAllocatorConfig.d.ts +1 -0
- package/lib/esm/vault/VaultMarketPublicAllocatorConfig.js +1 -0
- package/lib/esm/vault/VaultUser.d.ts +2 -0
- package/lib/esm/vault/VaultUser.js +1 -0
- package/lib/esm/vault/VaultUtils.d.ts +49 -0
- package/lib/esm/vault/VaultUtils.js +49 -0
- package/lib/esm/vault/v2/VaultV2.d.ts +5 -0
- package/lib/esm/vault/v2/VaultV2.js +2 -0
- package/lib/esm/vault/v2/VaultV2Adapter.d.ts +3 -0
- package/lib/esm/vault/v2/VaultV2Adapter.js +1 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.d.ts +4 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.js +2 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +4 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.js +2 -0
- package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +4 -0
- package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.js +2 -0
- package/package.json +2 -2
|
@@ -8,11 +8,17 @@ const MathLib_js_1 = require("./MathLib.js");
|
|
|
8
8
|
*/
|
|
9
9
|
var AdaptiveCurveIrmLib;
|
|
10
10
|
(function (AdaptiveCurveIrmLib) {
|
|
11
|
+
/** Curve steepness constant, scaled by WAD. */
|
|
11
12
|
AdaptiveCurveIrmLib.CURVE_STEEPNESS = 4000000000000000000n;
|
|
13
|
+
/** Target utilization for the Adaptive Curve IRM, scaled by WAD. */
|
|
12
14
|
AdaptiveCurveIrmLib.TARGET_UTILIZATION = 900000000000000000n;
|
|
15
|
+
/** Initial per-second rate at target utilization, scaled by WAD. */
|
|
13
16
|
AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = 40000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
17
|
+
/** Per-second speed used to adjust the rate at target utilization. */
|
|
14
18
|
AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = 50000000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
19
|
+
/** Minimum per-second rate at target utilization, scaled by WAD. */
|
|
15
20
|
AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = 1000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
21
|
+
/** Maximum per-second rate at target utilization, scaled by WAD. */
|
|
16
22
|
AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = 2000000000000000000n / constants_js_1.SECONDS_PER_YEAR;
|
|
17
23
|
/**
|
|
18
24
|
* ln(2), scaled by WAD.
|
|
@@ -33,7 +39,15 @@ var AdaptiveCurveIrmLib;
|
|
|
33
39
|
AdaptiveCurveIrmLib.WEXP_UPPER_VALUE = 57716089161558943949701069502944508345128422502756744429568n;
|
|
34
40
|
/**
|
|
35
41
|
* Returns an approximation of exp(x) used by the Adaptive Curve IRM.
|
|
36
|
-
* @param x
|
|
42
|
+
* @param x - The WAD-scaled exponent.
|
|
43
|
+
* @returns The WAD-scaled exponential approximation.
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { AdaptiveCurveIrmLib } from "@morpho-org/blue-sdk";
|
|
47
|
+
*
|
|
48
|
+
* const value = AdaptiveCurveIrmLib.wExp(0n);
|
|
49
|
+
* // value === 1000000000000000000n
|
|
50
|
+
* ```
|
|
37
51
|
*/
|
|
38
52
|
function wExp(x) {
|
|
39
53
|
// biome-ignore lint/style/noParameterAssign: TODO refactor to avoid mutating parameter
|
|
@@ -57,6 +71,25 @@ var AdaptiveCurveIrmLib;
|
|
|
57
71
|
return expR >> -q;
|
|
58
72
|
}
|
|
59
73
|
AdaptiveCurveIrmLib.wExp = wExp;
|
|
74
|
+
/**
|
|
75
|
+
* Computes Adaptive Curve IRM borrow rates from utilization and elapsed time.
|
|
76
|
+
*
|
|
77
|
+
* @param startUtilization - The market utilization at the start of the period, scaled by WAD.
|
|
78
|
+
* @param startRateAtTarget - The rate at target utilization at the start of the period, scaled by WAD.
|
|
79
|
+
* @param elapsed - The elapsed time in seconds.
|
|
80
|
+
* @returns The average borrow rate, end borrow rate, and end rate at target, all scaled by WAD.
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { AdaptiveCurveIrmLib } from "@morpho-org/blue-sdk";
|
|
84
|
+
*
|
|
85
|
+
* const rates = AdaptiveCurveIrmLib.getBorrowRate(
|
|
86
|
+
* AdaptiveCurveIrmLib.TARGET_UTILIZATION,
|
|
87
|
+
* AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET,
|
|
88
|
+
* 12n,
|
|
89
|
+
* );
|
|
90
|
+
* // rates satisfies { avgBorrowRate: bigint; endBorrowRate: bigint; endRateAtTarget: bigint }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
60
93
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
61
94
|
function getBorrowRate(startUtilization, startRateAtTarget, elapsed) {
|
|
62
95
|
// biome-ignore lint/style/noParameterAssign: TODO refactor to avoid mutating parameter
|
|
@@ -124,6 +157,23 @@ var AdaptiveCurveIrmLib;
|
|
|
124
157
|
};
|
|
125
158
|
}
|
|
126
159
|
AdaptiveCurveIrmLib.getBorrowRate = getBorrowRate;
|
|
160
|
+
/**
|
|
161
|
+
* Finds the utilization corresponding to a borrow rate and rate at target.
|
|
162
|
+
*
|
|
163
|
+
* @param borrowRate - The borrow rate to invert, scaled by WAD.
|
|
164
|
+
* @param rateAtTarget - The rate at target utilization, scaled by WAD.
|
|
165
|
+
* @returns The utilization corresponding to `borrowRate`, scaled by WAD.
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* import { AdaptiveCurveIrmLib } from "@morpho-org/blue-sdk";
|
|
169
|
+
*
|
|
170
|
+
* const utilization = AdaptiveCurveIrmLib.getUtilizationAtBorrowRate(
|
|
171
|
+
* AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET,
|
|
172
|
+
* AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET,
|
|
173
|
+
* );
|
|
174
|
+
* // utilization === AdaptiveCurveIrmLib.TARGET_UTILIZATION
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
127
177
|
function getUtilizationAtBorrowRate(borrowRate, rateAtTarget) {
|
|
128
178
|
// biome-ignore lint/style/noParameterAssign: TODO refactor to avoid mutating parameter
|
|
129
179
|
borrowRate = BigInt(borrowRate);
|
|
@@ -1,74 +1,174 @@
|
|
|
1
1
|
import type { BigIntish } from "../types.js";
|
|
2
|
+
/** Rounding direction used by fixed-point math helpers. */
|
|
2
3
|
export type RoundingDirection = "Up" | "Down";
|
|
3
4
|
/**
|
|
4
5
|
* Library to manage fixed-point arithmetic.
|
|
5
6
|
* https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
|
|
6
7
|
*/
|
|
7
8
|
export declare namespace MathLib {
|
|
9
|
+
/** WAD scale used for 18-decimal fixed-point values. */
|
|
8
10
|
const WAD = 1000000000000000000n;
|
|
11
|
+
/** RAY scale used for 27-decimal fixed-point values. */
|
|
9
12
|
const RAY = 1000000000000000000000000000n;
|
|
13
|
+
/** Maximum unsigned integer representable with 256 bits. */
|
|
10
14
|
const MAX_UINT_256: bigint;
|
|
15
|
+
/** Maximum unsigned integer representable with 160 bits. */
|
|
11
16
|
const MAX_UINT_160: bigint;
|
|
17
|
+
/** Maximum unsigned integer representable with 128 bits. */
|
|
12
18
|
const MAX_UINT_128: bigint;
|
|
19
|
+
/** Maximum unsigned integer representable with 48 bits. */
|
|
13
20
|
const MAX_UINT_48: bigint;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the maximum unsigned integer representable with a bit width.
|
|
23
|
+
*
|
|
24
|
+
* @param nBits - The bit width, which must be divisible by 4.
|
|
25
|
+
* @returns The maximum unsigned integer representable by `nBits`.
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
29
|
+
*
|
|
30
|
+
* const max = MathLib.maxUint(8);
|
|
31
|
+
* // max === 255n
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
14
34
|
function maxUint(nBits: number): bigint;
|
|
15
35
|
/**
|
|
16
36
|
* Returns the absolute value of a number
|
|
17
37
|
* @param a The number
|
|
38
|
+
* @returns The absolute value as a bigint.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
42
|
+
*
|
|
43
|
+
* const value = MathLib.abs(-2n);
|
|
44
|
+
* // value === 2n
|
|
45
|
+
* ```
|
|
18
46
|
*/
|
|
19
47
|
function abs(a: BigIntish): bigint;
|
|
20
48
|
/**
|
|
21
49
|
* Returns the smallest number given as param
|
|
22
|
-
* @param
|
|
23
|
-
* @
|
|
50
|
+
* @param xs The numbers to compare.
|
|
51
|
+
* @returns The smallest value as a bigint.
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
55
|
+
*
|
|
56
|
+
* const value = MathLib.min(2n, 1n, 3n);
|
|
57
|
+
* // value === 1n
|
|
58
|
+
* ```
|
|
24
59
|
*/
|
|
25
60
|
function min(...xs: BigIntish[]): bigint;
|
|
26
61
|
/**
|
|
27
62
|
* Returns the greatest number given as param
|
|
28
|
-
* @param
|
|
29
|
-
* @
|
|
63
|
+
* @param xs The numbers to compare.
|
|
64
|
+
* @returns The greatest value as a bigint.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
68
|
+
*
|
|
69
|
+
* const value = MathLib.max(2n, 1n, 3n);
|
|
70
|
+
* // value === 3n
|
|
71
|
+
* ```
|
|
30
72
|
*/
|
|
31
73
|
function max(...xs: BigIntish[]): bigint;
|
|
32
74
|
/**
|
|
33
75
|
* Returns the subtraction of b from a, floored to zero if negative
|
|
34
76
|
* @param x The first number
|
|
35
77
|
* @param y The second number
|
|
78
|
+
* @returns `x - y`, floored to `0n`.
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
82
|
+
*
|
|
83
|
+
* const value = MathLib.zeroFloorSub(1n, 2n);
|
|
84
|
+
* // value === 0n
|
|
85
|
+
* ```
|
|
36
86
|
*/
|
|
37
87
|
function zeroFloorSub(x: BigIntish, y: BigIntish): bigint;
|
|
38
88
|
/**
|
|
39
89
|
* Perform the WAD-based multiplication of 2 numbers, rounded down
|
|
40
90
|
* @param x The first number
|
|
41
91
|
* @param y The second number
|
|
92
|
+
* @returns The WAD-scaled product rounded down.
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
96
|
+
*
|
|
97
|
+
* const value = MathLib.wMulDown(MathLib.WAD, 2n * MathLib.WAD);
|
|
98
|
+
* // value === 2n * MathLib.WAD
|
|
99
|
+
* ```
|
|
42
100
|
*/
|
|
43
101
|
function wMulDown(x: BigIntish, y: BigIntish): bigint;
|
|
44
102
|
/**
|
|
45
103
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
46
104
|
* @param x The first number
|
|
47
105
|
* @param y The second number
|
|
106
|
+
* @returns The WAD-scaled product rounded up.
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
110
|
+
*
|
|
111
|
+
* const value = MathLib.wMulUp(1n, MathLib.WAD);
|
|
112
|
+
* // value === 1n
|
|
113
|
+
* ```
|
|
48
114
|
*/
|
|
49
115
|
function wMulUp(x: BigIntish, y: BigIntish): bigint;
|
|
50
116
|
/**
|
|
51
117
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
52
118
|
* @param x The first number
|
|
53
119
|
* @param y The second number
|
|
120
|
+
* @param rounding The rounding direction.
|
|
121
|
+
* @returns The WAD-scaled product rounded as requested.
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
125
|
+
*
|
|
126
|
+
* const value = MathLib.wMul(MathLib.WAD, MathLib.WAD, "Down");
|
|
127
|
+
* // value === MathLib.WAD
|
|
128
|
+
* ```
|
|
54
129
|
*/
|
|
55
130
|
function wMul(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
|
|
56
131
|
/**
|
|
57
132
|
* Perform the WAD-based division of 2 numbers, rounded down
|
|
58
133
|
* @param x The first number
|
|
59
134
|
* @param y The second number
|
|
135
|
+
* @returns The WAD-scaled quotient rounded down.
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
139
|
+
*
|
|
140
|
+
* const value = MathLib.wDivDown(MathLib.WAD, 2n * MathLib.WAD);
|
|
141
|
+
* // value === 500000000000000000n
|
|
142
|
+
* ```
|
|
60
143
|
*/
|
|
61
144
|
function wDivDown(x: BigIntish, y: BigIntish): bigint;
|
|
62
145
|
/**
|
|
63
146
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
64
147
|
* @param x The first number
|
|
65
148
|
* @param y The second number
|
|
149
|
+
* @returns The WAD-scaled quotient rounded up.
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
153
|
+
*
|
|
154
|
+
* const value = MathLib.wDivUp(MathLib.WAD, 2n * MathLib.WAD);
|
|
155
|
+
* // value === 500000000000000000n
|
|
156
|
+
* ```
|
|
66
157
|
*/
|
|
67
158
|
function wDivUp(x: BigIntish, y: BigIntish): bigint;
|
|
68
159
|
/**
|
|
69
160
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
70
161
|
* @param x The first number
|
|
71
162
|
* @param y The second number
|
|
163
|
+
* @param rounding The rounding direction.
|
|
164
|
+
* @returns The WAD-scaled quotient rounded as requested.
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
168
|
+
*
|
|
169
|
+
* const value = MathLib.wDiv(MathLib.WAD, MathLib.WAD, "Down");
|
|
170
|
+
* // value === MathLib.WAD
|
|
171
|
+
* ```
|
|
72
172
|
*/
|
|
73
173
|
function wDiv(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
|
|
74
174
|
/**
|
|
@@ -76,6 +176,14 @@ export declare namespace MathLib {
|
|
|
76
176
|
* @param x The first number
|
|
77
177
|
* @param y The second number
|
|
78
178
|
* @param denominator The denominator
|
|
179
|
+
* @returns `x * y / denominator`, rounded down.
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
183
|
+
*
|
|
184
|
+
* const value = MathLib.mulDivDown(5n, 2n, 3n);
|
|
185
|
+
* // value === 3n
|
|
186
|
+
* ```
|
|
79
187
|
*/
|
|
80
188
|
function mulDivDown(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
|
|
81
189
|
/**
|
|
@@ -83,8 +191,32 @@ export declare namespace MathLib {
|
|
|
83
191
|
* @param x The first number
|
|
84
192
|
* @param y The second number
|
|
85
193
|
* @param denominator The denominator
|
|
194
|
+
* @returns `x * y / denominator`, rounded up.
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
198
|
+
*
|
|
199
|
+
* const value = MathLib.mulDivUp(5n, 2n, 3n);
|
|
200
|
+
* // value === 4n
|
|
201
|
+
* ```
|
|
86
202
|
*/
|
|
87
203
|
function mulDivUp(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
|
|
204
|
+
/**
|
|
205
|
+
* Multiplies two numbers and divides by a denominator.
|
|
206
|
+
*
|
|
207
|
+
* @param x - The first number.
|
|
208
|
+
* @param y - The second number.
|
|
209
|
+
* @param denominator - The denominator.
|
|
210
|
+
* @param rounding - The rounding direction.
|
|
211
|
+
* @returns `x * y / denominator`, rounded as requested.
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
215
|
+
*
|
|
216
|
+
* const value = MathLib.mulDiv(5n, 2n, 3n, "Down");
|
|
217
|
+
* // value === 3n
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
88
220
|
function mulDiv(x: BigIntish, y: BigIntish, denominator: BigIntish, rounding: RoundingDirection): bigint;
|
|
89
221
|
/**
|
|
90
222
|
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
@@ -92,11 +224,27 @@ export declare namespace MathLib {
|
|
|
92
224
|
*
|
|
93
225
|
* @param x The base of the exponent
|
|
94
226
|
* @param n The exponent
|
|
227
|
+
* @returns The WAD-scaled compounded rate approximation.
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
231
|
+
*
|
|
232
|
+
* const compounded = MathLib.wTaylorCompounded(1n, 1n);
|
|
233
|
+
* // compounded satisfies bigint
|
|
234
|
+
* ```
|
|
95
235
|
*/
|
|
96
236
|
function wTaylorCompounded(x: BigIntish, n: BigIntish): bigint;
|
|
97
237
|
/**
|
|
98
238
|
* Converts a WAD-based quantity to a RAY-based quantity.
|
|
99
239
|
* @param x The WAD-based quantity.
|
|
240
|
+
* @returns The same quantity scaled to RAY precision.
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
244
|
+
*
|
|
245
|
+
* const ray = MathLib.wToRay(MathLib.WAD);
|
|
246
|
+
* // ray === MathLib.RAY
|
|
247
|
+
* ```
|
|
100
248
|
*/
|
|
101
249
|
function wToRay(x: BigIntish): bigint;
|
|
102
250
|
}
|
package/lib/cjs/math/MathLib.js
CHANGED
|
@@ -7,12 +7,31 @@ exports.MathLib = void 0;
|
|
|
7
7
|
*/
|
|
8
8
|
var MathLib;
|
|
9
9
|
(function (MathLib) {
|
|
10
|
+
/** WAD scale used for 18-decimal fixed-point values. */
|
|
10
11
|
MathLib.WAD = 1000000000000000000n;
|
|
12
|
+
/** RAY scale used for 27-decimal fixed-point values. */
|
|
11
13
|
MathLib.RAY = 1000000000000000000000000000n;
|
|
14
|
+
/** Maximum unsigned integer representable with 256 bits. */
|
|
12
15
|
MathLib.MAX_UINT_256 = maxUint(256);
|
|
16
|
+
/** Maximum unsigned integer representable with 160 bits. */
|
|
13
17
|
MathLib.MAX_UINT_160 = maxUint(160);
|
|
18
|
+
/** Maximum unsigned integer representable with 128 bits. */
|
|
14
19
|
MathLib.MAX_UINT_128 = maxUint(128);
|
|
20
|
+
/** Maximum unsigned integer representable with 48 bits. */
|
|
15
21
|
MathLib.MAX_UINT_48 = maxUint(48);
|
|
22
|
+
/**
|
|
23
|
+
* Returns the maximum unsigned integer representable with a bit width.
|
|
24
|
+
*
|
|
25
|
+
* @param nBits - The bit width, which must be divisible by 4.
|
|
26
|
+
* @returns The maximum unsigned integer representable by `nBits`.
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
30
|
+
*
|
|
31
|
+
* const max = MathLib.maxUint(8);
|
|
32
|
+
* // max === 255n
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
16
35
|
function maxUint(nBits) {
|
|
17
36
|
if (nBits % 4 !== 0)
|
|
18
37
|
throw new Error(`Invalid number of bits: ${nBits}`);
|
|
@@ -22,6 +41,14 @@ var MathLib;
|
|
|
22
41
|
/**
|
|
23
42
|
* Returns the absolute value of a number
|
|
24
43
|
* @param a The number
|
|
44
|
+
* @returns The absolute value as a bigint.
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
48
|
+
*
|
|
49
|
+
* const value = MathLib.abs(-2n);
|
|
50
|
+
* // value === 2n
|
|
51
|
+
* ```
|
|
25
52
|
*/
|
|
26
53
|
function abs(a) {
|
|
27
54
|
// biome-ignore lint/style/noParameterAssign: TODO refactor to avoid mutating parameter
|
|
@@ -31,8 +58,15 @@ var MathLib;
|
|
|
31
58
|
MathLib.abs = abs;
|
|
32
59
|
/**
|
|
33
60
|
* Returns the smallest number given as param
|
|
34
|
-
* @param
|
|
35
|
-
* @
|
|
61
|
+
* @param xs The numbers to compare.
|
|
62
|
+
* @returns The smallest value as a bigint.
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
66
|
+
*
|
|
67
|
+
* const value = MathLib.min(2n, 1n, 3n);
|
|
68
|
+
* // value === 1n
|
|
69
|
+
* ```
|
|
36
70
|
*/
|
|
37
71
|
function min(...xs) {
|
|
38
72
|
return xs.map(BigInt).reduce((x, y) => (x <= y ? x : y));
|
|
@@ -40,8 +74,15 @@ var MathLib;
|
|
|
40
74
|
MathLib.min = min;
|
|
41
75
|
/**
|
|
42
76
|
* Returns the greatest number given as param
|
|
43
|
-
* @param
|
|
44
|
-
* @
|
|
77
|
+
* @param xs The numbers to compare.
|
|
78
|
+
* @returns The greatest value as a bigint.
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
82
|
+
*
|
|
83
|
+
* const value = MathLib.max(2n, 1n, 3n);
|
|
84
|
+
* // value === 3n
|
|
85
|
+
* ```
|
|
45
86
|
*/
|
|
46
87
|
function max(...xs) {
|
|
47
88
|
return xs.map(BigInt).reduce((x, y) => (x <= y ? y : x));
|
|
@@ -51,6 +92,14 @@ var MathLib;
|
|
|
51
92
|
* Returns the subtraction of b from a, floored to zero if negative
|
|
52
93
|
* @param x The first number
|
|
53
94
|
* @param y The second number
|
|
95
|
+
* @returns `x - y`, floored to `0n`.
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
99
|
+
*
|
|
100
|
+
* const value = MathLib.zeroFloorSub(1n, 2n);
|
|
101
|
+
* // value === 0n
|
|
102
|
+
* ```
|
|
54
103
|
*/
|
|
55
104
|
function zeroFloorSub(x, y) {
|
|
56
105
|
// biome-ignore lint/style/noParameterAssign: TODO refactor to avoid mutating parameter
|
|
@@ -64,6 +113,14 @@ var MathLib;
|
|
|
64
113
|
* Perform the WAD-based multiplication of 2 numbers, rounded down
|
|
65
114
|
* @param x The first number
|
|
66
115
|
* @param y The second number
|
|
116
|
+
* @returns The WAD-scaled product rounded down.
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
120
|
+
*
|
|
121
|
+
* const value = MathLib.wMulDown(MathLib.WAD, 2n * MathLib.WAD);
|
|
122
|
+
* // value === 2n * MathLib.WAD
|
|
123
|
+
* ```
|
|
67
124
|
*/
|
|
68
125
|
function wMulDown(x, y) {
|
|
69
126
|
return MathLib.wMul(x, y, "Down");
|
|
@@ -73,6 +130,14 @@ var MathLib;
|
|
|
73
130
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
74
131
|
* @param x The first number
|
|
75
132
|
* @param y The second number
|
|
133
|
+
* @returns The WAD-scaled product rounded up.
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
137
|
+
*
|
|
138
|
+
* const value = MathLib.wMulUp(1n, MathLib.WAD);
|
|
139
|
+
* // value === 1n
|
|
140
|
+
* ```
|
|
76
141
|
*/
|
|
77
142
|
function wMulUp(x, y) {
|
|
78
143
|
return MathLib.wMul(x, y, "Up");
|
|
@@ -82,6 +147,15 @@ var MathLib;
|
|
|
82
147
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
83
148
|
* @param x The first number
|
|
84
149
|
* @param y The second number
|
|
150
|
+
* @param rounding The rounding direction.
|
|
151
|
+
* @returns The WAD-scaled product rounded as requested.
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
155
|
+
*
|
|
156
|
+
* const value = MathLib.wMul(MathLib.WAD, MathLib.WAD, "Down");
|
|
157
|
+
* // value === MathLib.WAD
|
|
158
|
+
* ```
|
|
85
159
|
*/
|
|
86
160
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
87
161
|
function wMul(x, y, rounding) {
|
|
@@ -92,6 +166,14 @@ var MathLib;
|
|
|
92
166
|
* Perform the WAD-based division of 2 numbers, rounded down
|
|
93
167
|
* @param x The first number
|
|
94
168
|
* @param y The second number
|
|
169
|
+
* @returns The WAD-scaled quotient rounded down.
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
173
|
+
*
|
|
174
|
+
* const value = MathLib.wDivDown(MathLib.WAD, 2n * MathLib.WAD);
|
|
175
|
+
* // value === 500000000000000000n
|
|
176
|
+
* ```
|
|
95
177
|
*/
|
|
96
178
|
function wDivDown(x, y) {
|
|
97
179
|
return MathLib.wDiv(x, y, "Down");
|
|
@@ -101,6 +183,14 @@ var MathLib;
|
|
|
101
183
|
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
102
184
|
* @param x The first number
|
|
103
185
|
* @param y The second number
|
|
186
|
+
* @returns The WAD-scaled quotient rounded up.
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
190
|
+
*
|
|
191
|
+
* const value = MathLib.wDivUp(MathLib.WAD, 2n * MathLib.WAD);
|
|
192
|
+
* // value === 500000000000000000n
|
|
193
|
+
* ```
|
|
104
194
|
*/
|
|
105
195
|
function wDivUp(x, y) {
|
|
106
196
|
return MathLib.wDiv(x, y, "Up");
|
|
@@ -110,6 +200,15 @@ var MathLib;
|
|
|
110
200
|
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
111
201
|
* @param x The first number
|
|
112
202
|
* @param y The second number
|
|
203
|
+
* @param rounding The rounding direction.
|
|
204
|
+
* @returns The WAD-scaled quotient rounded as requested.
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
208
|
+
*
|
|
209
|
+
* const value = MathLib.wDiv(MathLib.WAD, MathLib.WAD, "Down");
|
|
210
|
+
* // value === MathLib.WAD
|
|
211
|
+
* ```
|
|
113
212
|
*/
|
|
114
213
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
115
214
|
function wDiv(x, y, rounding) {
|
|
@@ -121,6 +220,14 @@ var MathLib;
|
|
|
121
220
|
* @param x The first number
|
|
122
221
|
* @param y The second number
|
|
123
222
|
* @param denominator The denominator
|
|
223
|
+
* @returns `x * y / denominator`, rounded down.
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
227
|
+
*
|
|
228
|
+
* const value = MathLib.mulDivDown(5n, 2n, 3n);
|
|
229
|
+
* // value === 3n
|
|
230
|
+
* ```
|
|
124
231
|
*/
|
|
125
232
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
126
233
|
function mulDivDown(x, y, denominator) {
|
|
@@ -140,6 +247,14 @@ var MathLib;
|
|
|
140
247
|
* @param x The first number
|
|
141
248
|
* @param y The second number
|
|
142
249
|
* @param denominator The denominator
|
|
250
|
+
* @returns `x * y / denominator`, rounded up.
|
|
251
|
+
* @example
|
|
252
|
+
* ```ts
|
|
253
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
254
|
+
*
|
|
255
|
+
* const value = MathLib.mulDivUp(5n, 2n, 3n);
|
|
256
|
+
* // value === 4n
|
|
257
|
+
* ```
|
|
143
258
|
*/
|
|
144
259
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
145
260
|
function mulDivUp(x, y, denominator) {
|
|
@@ -155,6 +270,22 @@ var MathLib;
|
|
|
155
270
|
return (x * y) / denominator + roundup;
|
|
156
271
|
}
|
|
157
272
|
MathLib.mulDivUp = mulDivUp;
|
|
273
|
+
/**
|
|
274
|
+
* Multiplies two numbers and divides by a denominator.
|
|
275
|
+
*
|
|
276
|
+
* @param x - The first number.
|
|
277
|
+
* @param y - The second number.
|
|
278
|
+
* @param denominator - The denominator.
|
|
279
|
+
* @param rounding - The rounding direction.
|
|
280
|
+
* @returns `x * y / denominator`, rounded as requested.
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
284
|
+
*
|
|
285
|
+
* const value = MathLib.mulDiv(5n, 2n, 3n, "Down");
|
|
286
|
+
* // value === 3n
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
158
289
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
159
290
|
function mulDiv(x, y, denominator, rounding) {
|
|
160
291
|
return MathLib[`mulDiv${rounding}`](x, y, denominator);
|
|
@@ -166,6 +297,14 @@ var MathLib;
|
|
|
166
297
|
*
|
|
167
298
|
* @param x The base of the exponent
|
|
168
299
|
* @param n The exponent
|
|
300
|
+
* @returns The WAD-scaled compounded rate approximation.
|
|
301
|
+
* @example
|
|
302
|
+
* ```ts
|
|
303
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
304
|
+
*
|
|
305
|
+
* const compounded = MathLib.wTaylorCompounded(1n, 1n);
|
|
306
|
+
* // compounded satisfies bigint
|
|
307
|
+
* ```
|
|
169
308
|
*/
|
|
170
309
|
function wTaylorCompounded(x, n) {
|
|
171
310
|
const firstTerm = BigInt(x) * BigInt(n);
|
|
@@ -177,6 +316,14 @@ var MathLib;
|
|
|
177
316
|
/**
|
|
178
317
|
* Converts a WAD-based quantity to a RAY-based quantity.
|
|
179
318
|
* @param x The WAD-based quantity.
|
|
319
|
+
* @returns The same quantity scaled to RAY precision.
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
323
|
+
*
|
|
324
|
+
* const ray = MathLib.wToRay(MathLib.WAD);
|
|
325
|
+
* // ray === MathLib.RAY
|
|
326
|
+
* ```
|
|
180
327
|
*/
|
|
181
328
|
function wToRay(x) {
|
|
182
329
|
return BigInt(x) * 1000000000n;
|
|
@@ -5,8 +5,42 @@ import { type RoundingDirection } from "./MathLib.js";
|
|
|
5
5
|
* & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
|
|
6
6
|
*/
|
|
7
7
|
export declare namespace SharesMath {
|
|
8
|
+
/** Virtual shares added to Morpho Blue and ERC-4626 share conversions. */
|
|
8
9
|
const VIRTUAL_SHARES = 1000000n;
|
|
10
|
+
/** Virtual assets added to Morpho Blue and ERC-4626 asset conversions. */
|
|
9
11
|
const VIRTUAL_ASSETS = 1n;
|
|
12
|
+
/**
|
|
13
|
+
* Converts shares to assets using Morpho virtual shares and assets.
|
|
14
|
+
*
|
|
15
|
+
* @param shares - The amount of shares.
|
|
16
|
+
* @param totalAssets - The total assets before conversion.
|
|
17
|
+
* @param totalShares - The total shares before conversion.
|
|
18
|
+
* @param rounding - The rounding direction.
|
|
19
|
+
* @returns The equivalent amount of assets.
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { SharesMath } from "@morpho-org/blue-sdk";
|
|
23
|
+
*
|
|
24
|
+
* const assets = SharesMath.toAssets(100n, 1_000n, 100n, "Down");
|
|
25
|
+
* // assets satisfies bigint
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
10
28
|
function toAssets(shares: BigIntish, totalAssets: BigIntish, totalShares: BigIntish, rounding: RoundingDirection): bigint;
|
|
29
|
+
/**
|
|
30
|
+
* Converts assets to shares using Morpho virtual shares and assets.
|
|
31
|
+
*
|
|
32
|
+
* @param assets - The amount of assets.
|
|
33
|
+
* @param totalAssets - The total assets before conversion.
|
|
34
|
+
* @param totalShares - The total shares before conversion.
|
|
35
|
+
* @param rounding - The rounding direction.
|
|
36
|
+
* @returns The equivalent amount of shares.
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { SharesMath } from "@morpho-org/blue-sdk";
|
|
40
|
+
*
|
|
41
|
+
* const shares = SharesMath.toShares(100n, 1_000n, 100n, "Up");
|
|
42
|
+
* // shares satisfies bigint
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
11
45
|
function toShares(assets: BigIntish, totalAssets: BigIntish, totalShares: BigIntish, rounding: RoundingDirection): bigint;
|
|
12
46
|
}
|
|
@@ -8,13 +8,47 @@ const MathLib_js_1 = require("./MathLib.js");
|
|
|
8
8
|
*/
|
|
9
9
|
var SharesMath;
|
|
10
10
|
(function (SharesMath) {
|
|
11
|
+
/** Virtual shares added to Morpho Blue and ERC-4626 share conversions. */
|
|
11
12
|
SharesMath.VIRTUAL_SHARES = 1000000n;
|
|
13
|
+
/** Virtual assets added to Morpho Blue and ERC-4626 asset conversions. */
|
|
12
14
|
SharesMath.VIRTUAL_ASSETS = 1n;
|
|
15
|
+
/**
|
|
16
|
+
* Converts shares to assets using Morpho virtual shares and assets.
|
|
17
|
+
*
|
|
18
|
+
* @param shares - The amount of shares.
|
|
19
|
+
* @param totalAssets - The total assets before conversion.
|
|
20
|
+
* @param totalShares - The total shares before conversion.
|
|
21
|
+
* @param rounding - The rounding direction.
|
|
22
|
+
* @returns The equivalent amount of assets.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { SharesMath } from "@morpho-org/blue-sdk";
|
|
26
|
+
*
|
|
27
|
+
* const assets = SharesMath.toAssets(100n, 1_000n, 100n, "Down");
|
|
28
|
+
* // assets satisfies bigint
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
13
31
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
14
32
|
function toAssets(shares, totalAssets, totalShares, rounding) {
|
|
15
33
|
return MathLib_js_1.MathLib.mulDiv(shares, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
|
|
16
34
|
}
|
|
17
35
|
SharesMath.toAssets = toAssets;
|
|
36
|
+
/**
|
|
37
|
+
* Converts assets to shares using Morpho virtual shares and assets.
|
|
38
|
+
*
|
|
39
|
+
* @param assets - The amount of assets.
|
|
40
|
+
* @param totalAssets - The total assets before conversion.
|
|
41
|
+
* @param totalShares - The total shares before conversion.
|
|
42
|
+
* @param rounding - The rounding direction.
|
|
43
|
+
* @returns The equivalent amount of shares.
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { SharesMath } from "@morpho-org/blue-sdk";
|
|
47
|
+
*
|
|
48
|
+
* const shares = SharesMath.toShares(100n, 1_000n, 100n, "Up");
|
|
49
|
+
* // shares satisfies bigint
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
18
52
|
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
19
53
|
function toShares(assets, totalAssets, totalShares, rounding) {
|
|
20
54
|
return MathLib_js_1.MathLib.mulDiv(assets, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
|