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