@morpho-org/blue-sdk 5.16.0 → 5.17.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 (184) hide show
  1. package/lib/cjs/package.json +1 -0
  2. package/lib/{position → cjs/position}/PreLiquidationPosition.d.ts +3 -3
  3. package/lib/{position → cjs/position}/PreLiquidationPosition.js +16 -16
  4. package/lib/{preLiquidation.d.ts → cjs/preLiquidation.d.ts} +1 -1
  5. package/lib/{preLiquidation.js → cjs/preLiquidation.js} +2 -2
  6. package/lib/{vault → cjs/vault}/index.d.ts +1 -1
  7. package/lib/{vault → cjs/vault}/index.js +1 -1
  8. package/lib/{vault → cjs/vault}/v2/VaultV2.d.ts +5 -5
  9. package/lib/{vault → cjs/vault}/v2/VaultV2.js +25 -25
  10. package/lib/{vault → cjs/vault}/v2/VaultV2Adapter.d.ts +2 -2
  11. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1Adapter.d.ts +7 -7
  12. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1Adapter.js +9 -9
  13. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +6 -6
  14. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1AdapterV2.js +8 -8
  15. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoVaultV1Adapter.d.ts +6 -6
  16. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoVaultV1Adapter.js +2 -2
  17. package/lib/esm/addresses.d.ts +2231 -0
  18. package/lib/esm/addresses.js +1309 -0
  19. package/lib/esm/chain.d.ts +405 -0
  20. package/lib/esm/chain.js +276 -0
  21. package/lib/esm/constants.d.ts +20 -0
  22. package/lib/esm/constants.js +21 -0
  23. package/lib/esm/errors.d.ts +101 -0
  24. package/lib/esm/errors.js +192 -0
  25. package/lib/esm/holding/AssetBalances.d.ts +48 -0
  26. package/lib/esm/holding/AssetBalances.js +38 -0
  27. package/lib/esm/holding/Holding.d.ts +63 -0
  28. package/lib/esm/holding/Holding.js +63 -0
  29. package/lib/esm/holding/index.d.ts +2 -0
  30. package/lib/esm/holding/index.js +2 -0
  31. package/lib/esm/index.d.ts +14 -0
  32. package/lib/esm/index.js +14 -0
  33. package/lib/esm/market/Market.d.ts +424 -0
  34. package/lib/esm/market/Market.js +576 -0
  35. package/lib/esm/market/MarketParams.d.ts +74 -0
  36. package/lib/esm/market/MarketParams.js +91 -0
  37. package/lib/esm/market/MarketUtils.d.ts +273 -0
  38. package/lib/esm/market/MarketUtils.js +323 -0
  39. package/lib/esm/market/index.d.ts +3 -0
  40. package/lib/esm/market/index.js +3 -0
  41. package/lib/esm/math/AdaptiveCurveIrmLib.d.ts +40 -0
  42. package/lib/esm/math/AdaptiveCurveIrmLib.js +133 -0
  43. package/lib/esm/math/MathLib.d.ts +102 -0
  44. package/lib/esm/math/MathLib.js +168 -0
  45. package/lib/esm/math/SharesMath.d.ts +12 -0
  46. package/lib/esm/math/SharesMath.js +18 -0
  47. package/lib/esm/math/index.d.ts +3 -0
  48. package/lib/esm/math/index.js +3 -0
  49. package/lib/esm/package.json +1 -0
  50. package/lib/esm/position/Position.d.ts +147 -0
  51. package/lib/esm/position/Position.js +237 -0
  52. package/lib/esm/position/PreLiquidationPosition.d.ts +60 -0
  53. package/lib/esm/position/PreLiquidationPosition.js +97 -0
  54. package/lib/esm/position/index.d.ts +2 -0
  55. package/lib/esm/position/index.js +2 -0
  56. package/lib/esm/preLiquidation.d.ts +15 -0
  57. package/lib/esm/preLiquidation.js +91 -0
  58. package/lib/esm/token/ConstantWrappedToken.d.ts +17 -0
  59. package/lib/esm/token/ConstantWrappedToken.js +30 -0
  60. package/lib/esm/token/Eip5267Domain.d.ts +55 -0
  61. package/lib/esm/token/Eip5267Domain.js +66 -0
  62. package/lib/esm/token/ExchangeRateWrappedToken.d.ts +11 -0
  63. package/lib/esm/token/ExchangeRateWrappedToken.js +17 -0
  64. package/lib/esm/token/Token.d.ts +52 -0
  65. package/lib/esm/token/Token.js +62 -0
  66. package/lib/esm/token/VaultToken.d.ts +23 -0
  67. package/lib/esm/token/VaultToken.js +27 -0
  68. package/lib/esm/token/WrappedToken.d.ts +17 -0
  69. package/lib/esm/token/WrappedToken.js +29 -0
  70. package/lib/esm/token/index.d.ts +6 -0
  71. package/lib/esm/token/index.js +6 -0
  72. package/lib/esm/types.d.ts +26 -0
  73. package/lib/esm/types.js +13 -0
  74. package/lib/esm/user/User.d.ts +20 -0
  75. package/lib/esm/user/User.js +19 -0
  76. package/lib/esm/user/index.d.ts +1 -0
  77. package/lib/esm/user/index.js +1 -0
  78. package/lib/esm/utils.d.ts +13 -0
  79. package/lib/esm/utils.js +10 -0
  80. package/lib/esm/vault/Vault.d.ts +214 -0
  81. package/lib/esm/vault/Vault.js +289 -0
  82. package/lib/esm/vault/VaultConfig.d.ts +11 -0
  83. package/lib/esm/vault/VaultConfig.js +10 -0
  84. package/lib/esm/vault/VaultMarketAllocation.d.ts +20 -0
  85. package/lib/esm/vault/VaultMarketAllocation.js +27 -0
  86. package/lib/esm/vault/VaultMarketConfig.d.ts +43 -0
  87. package/lib/esm/vault/VaultMarketConfig.js +39 -0
  88. package/lib/esm/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
  89. package/lib/esm/vault/VaultMarketPublicAllocatorConfig.js +24 -0
  90. package/lib/esm/vault/VaultUser.d.ts +26 -0
  91. package/lib/esm/vault/VaultUser.js +24 -0
  92. package/lib/esm/vault/VaultUtils.d.ts +16 -0
  93. package/lib/esm/vault/VaultUtils.js +17 -0
  94. package/lib/esm/vault/index.d.ts +8 -0
  95. package/lib/esm/vault/index.js +8 -0
  96. package/lib/esm/vault/v2/VaultV2.d.ts +87 -0
  97. package/lib/esm/vault/v2/VaultV2.js +154 -0
  98. package/lib/esm/vault/v2/VaultV2Adapter.d.ts +29 -0
  99. package/lib/esm/vault/v2/VaultV2Adapter.js +12 -0
  100. package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.d.ts +30 -0
  101. package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.js +54 -0
  102. package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +33 -0
  103. package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.js +63 -0
  104. package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +24 -0
  105. package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.js +36 -0
  106. package/lib/esm/vault/v2/index.d.ts +5 -0
  107. package/lib/esm/vault/v2/index.js +5 -0
  108. package/package.json +16 -7
  109. package/src/index.ts +14 -0
  110. /package/lib/{addresses.d.ts → cjs/addresses.d.ts} +0 -0
  111. /package/lib/{addresses.js → cjs/addresses.js} +0 -0
  112. /package/lib/{chain.d.ts → cjs/chain.d.ts} +0 -0
  113. /package/lib/{chain.js → cjs/chain.js} +0 -0
  114. /package/lib/{constants.d.ts → cjs/constants.d.ts} +0 -0
  115. /package/lib/{constants.js → cjs/constants.js} +0 -0
  116. /package/lib/{errors.d.ts → cjs/errors.d.ts} +0 -0
  117. /package/lib/{errors.js → cjs/errors.js} +0 -0
  118. /package/lib/{holding → cjs/holding}/AssetBalances.d.ts +0 -0
  119. /package/lib/{holding → cjs/holding}/AssetBalances.js +0 -0
  120. /package/lib/{holding → cjs/holding}/Holding.d.ts +0 -0
  121. /package/lib/{holding → cjs/holding}/Holding.js +0 -0
  122. /package/lib/{holding → cjs/holding}/index.d.ts +0 -0
  123. /package/lib/{holding → cjs/holding}/index.js +0 -0
  124. /package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  125. /package/lib/{index.js → cjs/index.js} +0 -0
  126. /package/lib/{market → cjs/market}/Market.d.ts +0 -0
  127. /package/lib/{market → cjs/market}/Market.js +0 -0
  128. /package/lib/{market → cjs/market}/MarketParams.d.ts +0 -0
  129. /package/lib/{market → cjs/market}/MarketParams.js +0 -0
  130. /package/lib/{market → cjs/market}/MarketUtils.d.ts +0 -0
  131. /package/lib/{market → cjs/market}/MarketUtils.js +0 -0
  132. /package/lib/{market → cjs/market}/index.d.ts +0 -0
  133. /package/lib/{market → cjs/market}/index.js +0 -0
  134. /package/lib/{math → cjs/math}/AdaptiveCurveIrmLib.d.ts +0 -0
  135. /package/lib/{math → cjs/math}/AdaptiveCurveIrmLib.js +0 -0
  136. /package/lib/{math → cjs/math}/MathLib.d.ts +0 -0
  137. /package/lib/{math → cjs/math}/MathLib.js +0 -0
  138. /package/lib/{math → cjs/math}/SharesMath.d.ts +0 -0
  139. /package/lib/{math → cjs/math}/SharesMath.js +0 -0
  140. /package/lib/{math → cjs/math}/index.d.ts +0 -0
  141. /package/lib/{math → cjs/math}/index.js +0 -0
  142. /package/lib/{position → cjs/position}/Position.d.ts +0 -0
  143. /package/lib/{position → cjs/position}/Position.js +0 -0
  144. /package/lib/{position → cjs/position}/index.d.ts +0 -0
  145. /package/lib/{position → cjs/position}/index.js +0 -0
  146. /package/lib/{token → cjs/token}/ConstantWrappedToken.d.ts +0 -0
  147. /package/lib/{token → cjs/token}/ConstantWrappedToken.js +0 -0
  148. /package/lib/{token → cjs/token}/Eip5267Domain.d.ts +0 -0
  149. /package/lib/{token → cjs/token}/Eip5267Domain.js +0 -0
  150. /package/lib/{token → cjs/token}/ExchangeRateWrappedToken.d.ts +0 -0
  151. /package/lib/{token → cjs/token}/ExchangeRateWrappedToken.js +0 -0
  152. /package/lib/{token → cjs/token}/Token.d.ts +0 -0
  153. /package/lib/{token → cjs/token}/Token.js +0 -0
  154. /package/lib/{token → cjs/token}/VaultToken.d.ts +0 -0
  155. /package/lib/{token → cjs/token}/VaultToken.js +0 -0
  156. /package/lib/{token → cjs/token}/WrappedToken.d.ts +0 -0
  157. /package/lib/{token → cjs/token}/WrappedToken.js +0 -0
  158. /package/lib/{token → cjs/token}/index.d.ts +0 -0
  159. /package/lib/{token → cjs/token}/index.js +0 -0
  160. /package/lib/{types.d.ts → cjs/types.d.ts} +0 -0
  161. /package/lib/{types.js → cjs/types.js} +0 -0
  162. /package/lib/{user → cjs/user}/User.d.ts +0 -0
  163. /package/lib/{user → cjs/user}/User.js +0 -0
  164. /package/lib/{user → cjs/user}/index.d.ts +0 -0
  165. /package/lib/{user → cjs/user}/index.js +0 -0
  166. /package/lib/{utils.d.ts → cjs/utils.d.ts} +0 -0
  167. /package/lib/{utils.js → cjs/utils.js} +0 -0
  168. /package/lib/{vault → cjs/vault}/Vault.d.ts +0 -0
  169. /package/lib/{vault → cjs/vault}/Vault.js +0 -0
  170. /package/lib/{vault → cjs/vault}/VaultConfig.d.ts +0 -0
  171. /package/lib/{vault → cjs/vault}/VaultConfig.js +0 -0
  172. /package/lib/{vault → cjs/vault}/VaultMarketAllocation.d.ts +0 -0
  173. /package/lib/{vault → cjs/vault}/VaultMarketAllocation.js +0 -0
  174. /package/lib/{vault → cjs/vault}/VaultMarketConfig.d.ts +0 -0
  175. /package/lib/{vault → cjs/vault}/VaultMarketConfig.js +0 -0
  176. /package/lib/{vault → cjs/vault}/VaultMarketPublicAllocatorConfig.d.ts +0 -0
  177. /package/lib/{vault → cjs/vault}/VaultMarketPublicAllocatorConfig.js +0 -0
  178. /package/lib/{vault → cjs/vault}/VaultUser.d.ts +0 -0
  179. /package/lib/{vault → cjs/vault}/VaultUser.js +0 -0
  180. /package/lib/{vault → cjs/vault}/VaultUtils.d.ts +0 -0
  181. /package/lib/{vault → cjs/vault}/VaultUtils.js +0 -0
  182. /package/lib/{vault → cjs/vault}/v2/VaultV2Adapter.js +0 -0
  183. /package/lib/{vault → cjs/vault}/v2/index.d.ts +0 -0
  184. /package/lib/{vault → cjs/vault}/v2/index.js +0 -0
@@ -0,0 +1,3 @@
1
+ export * from "./MarketUtils.js";
2
+ export * from "./MarketParams.js";
3
+ export * from "./Market.js";
@@ -0,0 +1,40 @@
1
+ import type { BigIntish } from "../types.js";
2
+ /**
3
+ * 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.
4
+ */
5
+ export declare namespace AdaptiveCurveIrmLib {
6
+ const CURVE_STEEPNESS = 4000000000000000000n;
7
+ const TARGET_UTILIZATION = 900000000000000000n;
8
+ const INITIAL_RATE_AT_TARGET: bigint;
9
+ const ADJUSTMENT_SPEED: bigint;
10
+ const MIN_RATE_AT_TARGET: bigint;
11
+ const MAX_RATE_AT_TARGET: bigint;
12
+ /**
13
+ * ln(2), scaled by WAD.
14
+ */
15
+ const LN_2_INT = 693147180559945309n;
16
+ /**
17
+ * ln(1e-18), scaled by WAD.
18
+ */
19
+ const LN_WEI_INT = -41446531673892822312n;
20
+ /**
21
+ * Above this bound, `wExp` is clipped to avoid overflowing when multiplied with 1 ether.
22
+ * This upper bound corresponds to: ln(type(int256).max / 1e36) (scaled by WAD, floored).
23
+ */
24
+ const WEXP_UPPER_BOUND = 93859467695000404319n;
25
+ /**
26
+ * The value of wExp(`WEXP_UPPER_BOUND`).
27
+ */
28
+ const WEXP_UPPER_VALUE = 57716089161558943949701069502944508345128422502756744429568n;
29
+ /**
30
+ * Returns an approximation of exp(x) used by the Adaptive Curve IRM.
31
+ * @param x
32
+ */
33
+ function wExp(x: BigIntish): bigint;
34
+ function getBorrowRate(startUtilization: BigIntish, startRateAtTarget: BigIntish, elapsed: BigIntish): {
35
+ avgBorrowRate: bigint;
36
+ endBorrowRate: bigint;
37
+ endRateAtTarget: bigint;
38
+ };
39
+ function getUtilizationAtBorrowRate(borrowRate: BigIntish, rateAtTarget: BigIntish): bigint;
40
+ }
@@ -0,0 +1,133 @@
1
+ import { SECONDS_PER_YEAR } from "../constants.js";
2
+ import { MathLib } from "./MathLib.js";
3
+ /**
4
+ * JS implementation of {@link https://github.com/morpho-org/morpho-blue-irm/blob/main/src/libraries/adaptive-curve/ExpLib.sol ExpLib} used by the Adaptive Curve IRM.
5
+ */
6
+ export var AdaptiveCurveIrmLib;
7
+ (function (AdaptiveCurveIrmLib) {
8
+ AdaptiveCurveIrmLib.CURVE_STEEPNESS = 4000000000000000000n;
9
+ AdaptiveCurveIrmLib.TARGET_UTILIZATION = 900000000000000000n;
10
+ AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = 40000000000000000n / SECONDS_PER_YEAR;
11
+ AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = 50000000000000000000n / SECONDS_PER_YEAR;
12
+ AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = 1000000000000000n / SECONDS_PER_YEAR;
13
+ AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = 2000000000000000000n / SECONDS_PER_YEAR;
14
+ /**
15
+ * ln(2), scaled by WAD.
16
+ */
17
+ AdaptiveCurveIrmLib.LN_2_INT = 693147180559945309n;
18
+ /**
19
+ * ln(1e-18), scaled by WAD.
20
+ */
21
+ AdaptiveCurveIrmLib.LN_WEI_INT = -41446531673892822312n;
22
+ /**
23
+ * Above this bound, `wExp` is clipped to avoid overflowing when multiplied with 1 ether.
24
+ * This upper bound corresponds to: ln(type(int256).max / 1e36) (scaled by WAD, floored).
25
+ */
26
+ AdaptiveCurveIrmLib.WEXP_UPPER_BOUND = 93859467695000404319n;
27
+ /**
28
+ * The value of wExp(`WEXP_UPPER_BOUND`).
29
+ */
30
+ AdaptiveCurveIrmLib.WEXP_UPPER_VALUE = 57716089161558943949701069502944508345128422502756744429568n;
31
+ /**
32
+ * Returns an approximation of exp(x) used by the Adaptive Curve IRM.
33
+ * @param x
34
+ */
35
+ function wExp(x) {
36
+ x = BigInt(x);
37
+ // If x < ln(1e-18) then exp(x) < 1e-18 so it is rounded to zero.
38
+ if (x < AdaptiveCurveIrmLib.LN_WEI_INT)
39
+ return 0n;
40
+ // `wExp` is clipped to avoid overflowing when multiplied with 1 ether.
41
+ if (x >= AdaptiveCurveIrmLib.WEXP_UPPER_BOUND)
42
+ return AdaptiveCurveIrmLib.WEXP_UPPER_VALUE;
43
+ // Decompose x as x = q * ln(2) + r with q an integer and -ln(2)/2 <= r <= ln(2)/2.
44
+ // q = x / ln(2) rounded half toward zero.
45
+ const roundingAdjustment = x < 0n ? -(AdaptiveCurveIrmLib.LN_2_INT / 2n) : AdaptiveCurveIrmLib.LN_2_INT / 2n;
46
+ const q = (x + roundingAdjustment) / AdaptiveCurveIrmLib.LN_2_INT;
47
+ const r = x - q * AdaptiveCurveIrmLib.LN_2_INT;
48
+ // Compute e^r with a 2nd-order Taylor polynomial.
49
+ const expR = MathLib.WAD + r + (r * r) / MathLib.WAD / 2n;
50
+ // Return e^x = 2^q * e^r.
51
+ if (q >= 0n)
52
+ return expR << q;
53
+ return expR >> -q;
54
+ }
55
+ AdaptiveCurveIrmLib.wExp = wExp;
56
+ function getBorrowRate(startUtilization, startRateAtTarget, elapsed) {
57
+ startUtilization = BigInt(startUtilization);
58
+ startRateAtTarget = BigInt(startRateAtTarget);
59
+ elapsed = BigInt(elapsed);
60
+ const errNormFactor = startUtilization > AdaptiveCurveIrmLib.TARGET_UTILIZATION
61
+ ? MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION
62
+ : AdaptiveCurveIrmLib.TARGET_UTILIZATION;
63
+ const err = MathLib.wDivDown(startUtilization - AdaptiveCurveIrmLib.TARGET_UTILIZATION, errNormFactor);
64
+ let avgRateAtTarget;
65
+ let endRateAtTarget;
66
+ if (startRateAtTarget === 0n) {
67
+ // First interaction.
68
+ avgRateAtTarget = AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET;
69
+ endRateAtTarget = AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET;
70
+ }
71
+ else {
72
+ // The speed is assumed constant between two updates, but it is in fact not constant because of interest.
73
+ // So the rate is always underestimated.
74
+ const speed = MathLib.wMulDown(AdaptiveCurveIrmLib.ADJUSTMENT_SPEED, err);
75
+ const linearAdaptation = speed * elapsed;
76
+ if (linearAdaptation === 0n) {
77
+ // If linearAdaptation == 0, avgRateAtTarget = endRateAtTarget = startRateAtTarget;
78
+ avgRateAtTarget = startRateAtTarget;
79
+ endRateAtTarget = startRateAtTarget;
80
+ }
81
+ else {
82
+ // Non negative because MIN_RATE_AT_TARGET > 0.
83
+ const _newRateAtTarget = (linearAdaptation) => MathLib.min(MathLib.max(MathLib.wMulDown(startRateAtTarget, wExp(linearAdaptation)), AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET), AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET);
84
+ // Formula of the average rate that should be returned to Morpho Blue:
85
+ // avg = 1/T * ∫_0^T curve(startRateAtTarget*exp(speed*x), err) dx
86
+ // The integral is approximated with the trapezoidal rule:
87
+ // avg ~= 1/T * Σ_i=1^N [curve(f((i-1) * T/N), err) + curve(f(i * T/N), err)] / 2 * T/N
88
+ // Where f(x) = startRateAtTarget*exp(speed*x)
89
+ // avg ~= Σ_i=1^N [curve(f((i-1) * T/N), err) + curve(f(i * T/N), err)] / (2 * N)
90
+ // As curve is linear in its first argument:
91
+ // avg ~= curve([Σ_i=1^N [f((i-1) * T/N) + f(i * T/N)] / (2 * N), err)
92
+ // avg ~= curve([(f(0) + f(T))/2 + Σ_i=1^(N-1) f(i * T/N)] / N, err)
93
+ // avg ~= curve([(startRateAtTarget + endRateAtTarget)/2 + Σ_i=1^(N-1) f(i * T/N)] / N, err)
94
+ // With N = 2:
95
+ // avg ~= curve([(startRateAtTarget + endRateAtTarget)/2 + startRateAtTarget*exp(speed*T/2)] / 2, err)
96
+ // avg ~= curve([startRateAtTarget + endRateAtTarget + 2*startRateAtTarget*exp(speed*T/2)] / 4, err)
97
+ endRateAtTarget = _newRateAtTarget(linearAdaptation);
98
+ avgRateAtTarget =
99
+ (startRateAtTarget +
100
+ endRateAtTarget +
101
+ 2n * _newRateAtTarget(linearAdaptation / 2n)) /
102
+ 4n;
103
+ }
104
+ }
105
+ // Non negative because 1 - 1/C >= 0, C - 1 >= 0.
106
+ const coeff = err < 0
107
+ ? MathLib.WAD - MathLib.wDivDown(MathLib.WAD, AdaptiveCurveIrmLib.CURVE_STEEPNESS)
108
+ : AdaptiveCurveIrmLib.CURVE_STEEPNESS - MathLib.WAD;
109
+ const _curve = (rateAtTarget) => MathLib.wMulDown(MathLib.wMulDown(coeff, err) + MathLib.WAD, rateAtTarget);
110
+ // Non negative if avgRateAtTarget >= 0 because if err < 0, coeff <= 1.
111
+ return {
112
+ avgBorrowRate: _curve(avgRateAtTarget),
113
+ endBorrowRate: _curve(endRateAtTarget),
114
+ endRateAtTarget,
115
+ };
116
+ }
117
+ AdaptiveCurveIrmLib.getBorrowRate = getBorrowRate;
118
+ function getUtilizationAtBorrowRate(borrowRate, rateAtTarget) {
119
+ borrowRate = BigInt(borrowRate);
120
+ rateAtTarget = BigInt(rateAtTarget);
121
+ if (borrowRate >= rateAtTarget) {
122
+ const maxBorrowRate = MathLib.wMulDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
123
+ const diffToMaxBorrowRate = maxBorrowRate - rateAtTarget;
124
+ if (diffToMaxBorrowRate === 0n)
125
+ return MathLib.WAD;
126
+ return MathLib.min(MathLib.WAD, AdaptiveCurveIrmLib.TARGET_UTILIZATION +
127
+ MathLib.mulDivDown(MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - rateAtTarget, diffToMaxBorrowRate));
128
+ }
129
+ const minBorrowRate = MathLib.wDivDown(rateAtTarget, AdaptiveCurveIrmLib.CURVE_STEEPNESS);
130
+ return MathLib.max(0n, MathLib.mulDivDown(AdaptiveCurveIrmLib.TARGET_UTILIZATION, borrowRate - minBorrowRate, rateAtTarget - minBorrowRate));
131
+ }
132
+ AdaptiveCurveIrmLib.getUtilizationAtBorrowRate = getUtilizationAtBorrowRate;
133
+ })(AdaptiveCurveIrmLib || (AdaptiveCurveIrmLib = {}));
@@ -0,0 +1,102 @@
1
+ import type { BigIntish } from "../types.js";
2
+ export type RoundingDirection = "Up" | "Down";
3
+ /**
4
+ * Library to manage fixed-point arithmetic.
5
+ * https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
6
+ */
7
+ export declare namespace MathLib {
8
+ const WAD = 1000000000000000000n;
9
+ const RAY = 1000000000000000000000000000n;
10
+ const MAX_UINT_256: bigint;
11
+ const MAX_UINT_160: bigint;
12
+ const MAX_UINT_128: bigint;
13
+ const MAX_UINT_48: bigint;
14
+ function maxUint(nBits: number): bigint;
15
+ /**
16
+ * Returns the absolute value of a number
17
+ * @param a The number
18
+ */
19
+ function abs(a: BigIntish): bigint;
20
+ /**
21
+ * Returns the smallest number given as param
22
+ * @param x The first number
23
+ * @param y The second number
24
+ */
25
+ function min(...xs: BigIntish[]): bigint;
26
+ /**
27
+ * Returns the greatest number given as param
28
+ * @param x The first number
29
+ * @param y The second number
30
+ */
31
+ function max(...xs: BigIntish[]): bigint;
32
+ /**
33
+ * Returns the subtraction of b from a, floored to zero if negative
34
+ * @param x The first number
35
+ * @param y The second number
36
+ */
37
+ function zeroFloorSub(x: BigIntish, y: BigIntish): bigint;
38
+ /**
39
+ * Perform the WAD-based multiplication of 2 numbers, rounded down
40
+ * @param x The first number
41
+ * @param y The second number
42
+ */
43
+ function wMulDown(x: BigIntish, y: BigIntish): bigint;
44
+ /**
45
+ * Perform the WAD-based multiplication of 2 numbers, rounded up
46
+ * @param x The first number
47
+ * @param y The second number
48
+ */
49
+ function wMulUp(x: BigIntish, y: BigIntish): bigint;
50
+ /**
51
+ * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
52
+ * @param x The first number
53
+ * @param y The second number
54
+ */
55
+ function wMul(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
56
+ /**
57
+ * Perform the WAD-based division of 2 numbers, rounded down
58
+ * @param x The first number
59
+ * @param y The second number
60
+ */
61
+ function wDivDown(x: BigIntish, y: BigIntish): bigint;
62
+ /**
63
+ * Perform the WAD-based multiplication of 2 numbers, rounded up
64
+ * @param x The first number
65
+ * @param y The second number
66
+ */
67
+ function wDivUp(x: BigIntish, y: BigIntish): bigint;
68
+ /**
69
+ * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
70
+ * @param x The first number
71
+ * @param y The second number
72
+ */
73
+ function wDiv(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
74
+ /**
75
+ * Multiply two numbers and divide by a denominator, rounding down the result
76
+ * @param x The first number
77
+ * @param y The second number
78
+ * @param denominator The denominator
79
+ */
80
+ function mulDivDown(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
81
+ /**
82
+ * Multiply two numbers and divide by a denominator, rounding up the result
83
+ * @param x The first number
84
+ * @param y The second number
85
+ * @param denominator The denominator
86
+ */
87
+ function mulDivUp(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
88
+ function mulDiv(x: BigIntish, y: BigIntish, denominator: BigIntish, rounding: RoundingDirection): bigint;
89
+ /**
90
+ * The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
91
+ * to approximate a continuously compounded interest rate.
92
+ *
93
+ * @param x The base of the exponent
94
+ * @param n The exponent
95
+ */
96
+ function wTaylorCompounded(x: BigIntish, n: BigIntish): bigint;
97
+ /**
98
+ * Converts a WAD-based quantity to a RAY-based quantity.
99
+ * @param x The WAD-based quantity.
100
+ */
101
+ function wToRay(x: BigIntish): bigint;
102
+ }
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Library to manage fixed-point arithmetic.
3
+ * https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
4
+ */
5
+ export var MathLib;
6
+ (function (MathLib) {
7
+ MathLib.WAD = 1000000000000000000n;
8
+ MathLib.RAY = 1000000000000000000000000000n;
9
+ MathLib.MAX_UINT_256 = maxUint(256);
10
+ MathLib.MAX_UINT_160 = maxUint(160);
11
+ MathLib.MAX_UINT_128 = maxUint(128);
12
+ MathLib.MAX_UINT_48 = maxUint(48);
13
+ function maxUint(nBits) {
14
+ if (nBits % 4 !== 0)
15
+ throw new Error(`Invalid number of bits: ${nBits}`);
16
+ return BigInt(`0x${"f".repeat(nBits / 4)}`);
17
+ }
18
+ MathLib.maxUint = maxUint;
19
+ /**
20
+ * Returns the absolute value of a number
21
+ * @param a The number
22
+ */
23
+ function abs(a) {
24
+ a = BigInt(a);
25
+ return a >= 0 ? a : -a;
26
+ }
27
+ MathLib.abs = abs;
28
+ /**
29
+ * Returns the smallest number given as param
30
+ * @param x The first number
31
+ * @param y The second number
32
+ */
33
+ function min(...xs) {
34
+ return xs.map(BigInt).reduce((x, y) => (x <= y ? x : y));
35
+ }
36
+ MathLib.min = min;
37
+ /**
38
+ * Returns the greatest number given as param
39
+ * @param x The first number
40
+ * @param y The second number
41
+ */
42
+ function max(...xs) {
43
+ return xs.map(BigInt).reduce((x, y) => (x <= y ? y : x));
44
+ }
45
+ MathLib.max = max;
46
+ /**
47
+ * Returns the subtraction of b from a, floored to zero if negative
48
+ * @param x The first number
49
+ * @param y The second number
50
+ */
51
+ function zeroFloorSub(x, y) {
52
+ x = BigInt(x);
53
+ y = BigInt(y);
54
+ return x <= y ? 0n : x - y;
55
+ }
56
+ MathLib.zeroFloorSub = zeroFloorSub;
57
+ /**
58
+ * Perform the WAD-based multiplication of 2 numbers, rounded down
59
+ * @param x The first number
60
+ * @param y The second number
61
+ */
62
+ function wMulDown(x, y) {
63
+ return MathLib.wMul(x, y, "Down");
64
+ }
65
+ MathLib.wMulDown = wMulDown;
66
+ /**
67
+ * Perform the WAD-based multiplication of 2 numbers, rounded up
68
+ * @param x The first number
69
+ * @param y The second number
70
+ */
71
+ function wMulUp(x, y) {
72
+ return MathLib.wMul(x, y, "Up");
73
+ }
74
+ MathLib.wMulUp = wMulUp;
75
+ /**
76
+ * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
77
+ * @param x The first number
78
+ * @param y The second number
79
+ */
80
+ function wMul(x, y, rounding) {
81
+ return MathLib.mulDiv(x, y, MathLib.WAD, rounding);
82
+ }
83
+ MathLib.wMul = wMul;
84
+ /**
85
+ * Perform the WAD-based division of 2 numbers, rounded down
86
+ * @param x The first number
87
+ * @param y The second number
88
+ */
89
+ function wDivDown(x, y) {
90
+ return MathLib.wDiv(x, y, "Down");
91
+ }
92
+ MathLib.wDivDown = wDivDown;
93
+ /**
94
+ * Perform the WAD-based multiplication of 2 numbers, rounded up
95
+ * @param x The first number
96
+ * @param y The second number
97
+ */
98
+ function wDivUp(x, y) {
99
+ return MathLib.wDiv(x, y, "Up");
100
+ }
101
+ MathLib.wDivUp = wDivUp;
102
+ /**
103
+ * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
104
+ * @param x The first number
105
+ * @param y The second number
106
+ */
107
+ function wDiv(x, y, rounding) {
108
+ return MathLib.mulDiv(x, MathLib.WAD, y, rounding);
109
+ }
110
+ MathLib.wDiv = wDiv;
111
+ /**
112
+ * Multiply two numbers and divide by a denominator, rounding down the result
113
+ * @param x The first number
114
+ * @param y The second number
115
+ * @param denominator The denominator
116
+ */
117
+ function mulDivDown(x, y, denominator) {
118
+ x = BigInt(x);
119
+ y = BigInt(y);
120
+ denominator = BigInt(denominator);
121
+ if (denominator === 0n)
122
+ throw Error("MathLib: DIVISION_BY_ZERO");
123
+ return (x * y) / denominator;
124
+ }
125
+ MathLib.mulDivDown = mulDivDown;
126
+ /**
127
+ * Multiply two numbers and divide by a denominator, rounding up the result
128
+ * @param x The first number
129
+ * @param y The second number
130
+ * @param denominator The denominator
131
+ */
132
+ function mulDivUp(x, y, denominator) {
133
+ x = BigInt(x);
134
+ y = BigInt(y);
135
+ denominator = BigInt(denominator);
136
+ if (denominator === 0n)
137
+ throw Error("MathLib: DIVISION_BY_ZERO");
138
+ const roundup = (x * y) % denominator > 0 ? 1n : 0n;
139
+ return (x * y) / denominator + roundup;
140
+ }
141
+ MathLib.mulDivUp = mulDivUp;
142
+ function mulDiv(x, y, denominator, rounding) {
143
+ return MathLib[`mulDiv${rounding}`](x, y, denominator);
144
+ }
145
+ MathLib.mulDiv = mulDiv;
146
+ /**
147
+ * The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
148
+ * to approximate a continuously compounded interest rate.
149
+ *
150
+ * @param x The base of the exponent
151
+ * @param n The exponent
152
+ */
153
+ function wTaylorCompounded(x, n) {
154
+ const firstTerm = BigInt(x) * BigInt(n);
155
+ const secondTerm = MathLib.mulDivDown(firstTerm, firstTerm, 2n * MathLib.WAD);
156
+ const thirdTerm = MathLib.mulDivDown(secondTerm, firstTerm, 3n * MathLib.WAD);
157
+ return firstTerm + secondTerm + thirdTerm;
158
+ }
159
+ MathLib.wTaylorCompounded = wTaylorCompounded;
160
+ /**
161
+ * Converts a WAD-based quantity to a RAY-based quantity.
162
+ * @param x The WAD-based quantity.
163
+ */
164
+ function wToRay(x) {
165
+ return BigInt(x) * 1000000000n;
166
+ }
167
+ MathLib.wToRay = wToRay;
168
+ })(MathLib || (MathLib = {}));
@@ -0,0 +1,12 @@
1
+ import type { BigIntish } from "../types.js";
2
+ import { type RoundingDirection } from "./MathLib.js";
3
+ /**
4
+ * JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
5
+ * & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
6
+ */
7
+ export declare namespace SharesMath {
8
+ const VIRTUAL_SHARES = 1000000n;
9
+ const VIRTUAL_ASSETS = 1n;
10
+ function toAssets(shares: BigIntish, totalAssets: BigIntish, totalShares: BigIntish, rounding: RoundingDirection): bigint;
11
+ function toShares(assets: BigIntish, totalAssets: BigIntish, totalShares: BigIntish, rounding: RoundingDirection): bigint;
12
+ }
@@ -0,0 +1,18 @@
1
+ import { MathLib } from "./MathLib.js";
2
+ /**
3
+ * JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
4
+ * & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
5
+ */
6
+ export var SharesMath;
7
+ (function (SharesMath) {
8
+ SharesMath.VIRTUAL_SHARES = 1000000n;
9
+ SharesMath.VIRTUAL_ASSETS = 1n;
10
+ function toAssets(shares, totalAssets, totalShares, rounding) {
11
+ return MathLib.mulDiv(shares, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
12
+ }
13
+ SharesMath.toAssets = toAssets;
14
+ function toShares(assets, totalAssets, totalShares, rounding) {
15
+ return MathLib.mulDiv(assets, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
16
+ }
17
+ SharesMath.toShares = toShares;
18
+ })(SharesMath || (SharesMath = {}));
@@ -0,0 +1,3 @@
1
+ export * from "./MathLib.js";
2
+ export * from "./SharesMath.js";
3
+ export * from "./AdaptiveCurveIrmLib.js";
@@ -0,0 +1,3 @@
1
+ export * from "./MathLib.js";
2
+ export * from "./SharesMath.js";
3
+ export * from "./AdaptiveCurveIrmLib.js";
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,147 @@
1
+ import { type IMarket, Market, type MaxBorrowOptions, type MaxPositionCapacities, type MaxWithdrawCollateralOptions } from "../market/index.js";
2
+ import type { Address, BigIntish, MarketId } from "../types.js";
3
+ export interface IPosition {
4
+ user: Address;
5
+ marketId: MarketId;
6
+ supplyShares: bigint;
7
+ borrowShares: bigint;
8
+ collateral: bigint;
9
+ }
10
+ export declare class Position implements IPosition {
11
+ /**
12
+ * The user holding this position.
13
+ */
14
+ readonly user: Address;
15
+ /**
16
+ * The id of the market on which this position is held.
17
+ */
18
+ readonly marketId: MarketId;
19
+ /**
20
+ * The amount of supply shares held with this position.
21
+ */
22
+ supplyShares: bigint;
23
+ /**
24
+ * The amount of borrow shares held with this position.
25
+ */
26
+ borrowShares: bigint;
27
+ /**
28
+ * The amount of collateral assets held with this position.
29
+ */
30
+ collateral: bigint;
31
+ constructor({ user, marketId, supplyShares, borrowShares, collateral, }: IPosition);
32
+ }
33
+ export interface IAccrualPosition extends Omit<IPosition, "marketId"> {
34
+ }
35
+ export declare class AccrualPosition extends Position implements IAccrualPosition {
36
+ protected readonly _market: Market;
37
+ constructor(position: IAccrualPosition, market: IMarket);
38
+ /**
39
+ * The market on which this position is held.
40
+ */
41
+ get market(): Market;
42
+ get supplyAssets(): bigint;
43
+ get borrowAssets(): bigint;
44
+ /**
45
+ * The value of this position's collateral quoted in loan assets.
46
+ * `undefined` if the market's oracle is undefined or reverts.
47
+ */
48
+ get collateralValue(): bigint | undefined;
49
+ /**
50
+ * The maximum amount of loan assets that can be borrowed against this position's collateral.
51
+ * `undefined` if the market's oracle is undefined or reverts.
52
+ */
53
+ get maxBorrowAssets(): bigint | undefined;
54
+ /**
55
+ * The maximum additional amount of assets that can be borrowed against this position's collateral.
56
+ * `undefined` if the market's oracle is undefined or reverts.
57
+ */
58
+ get maxBorrowableAssets(): bigint | undefined;
59
+ /**
60
+ * The maximum amount of collateral that can be seized in exchange for the outstanding debt.
61
+ * `undefined` if the market's oracle is undefined or reverts.
62
+ */
63
+ get seizableCollateral(): bigint | undefined;
64
+ /**
65
+ * The maximum amount of collateral that can be withdrawn.
66
+ * `undefined` if the market's oracle is undefined or reverts.
67
+ */
68
+ get withdrawableCollateral(): bigint | undefined;
69
+ /**
70
+ * Whether this position is healthy.
71
+ * `undefined` if the market's oracle is undefined or reverts.
72
+ */
73
+ get isHealthy(): boolean | undefined;
74
+ /**
75
+ * Whether this position can be liquidated.
76
+ * `undefined` if the market's oracle is undefined or reverts.
77
+ */
78
+ get isLiquidatable(): boolean | undefined;
79
+ /**
80
+ * The price of the collateral quoted in loan assets that would allow this position to be liquidated.
81
+ * `null` if the position has no borrow.
82
+ */
83
+ get liquidationPrice(): bigint | null;
84
+ /**
85
+ * The price variation required for the position to reach its liquidation threshold (scaled by WAD).
86
+ * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
87
+ * `undefined` if the market's oracle is undefined or reverts.
88
+ * `null` if the position is not a borrow.
89
+ */
90
+ get priceVariationToLiquidationPrice(): bigint | null | undefined;
91
+ /**
92
+ * This position's Loan-To-Value (debt over collateral power, scaled by WAD).
93
+ * If the collateral price is 0, LTV is `MaxUint256`.
94
+ * `undefined` if the market's oracle is undefined or reverts.
95
+ */
96
+ get ltv(): bigint | null | undefined;
97
+ /**
98
+ * This position's health factor (collateral power over debt, scaled by WAD).
99
+ * If the debt is 0, health factor is `MaxUint256`.
100
+ * `undefined` if the market's oracle is undefined or reverts.
101
+ */
102
+ get healthFactor(): bigint | undefined;
103
+ /**
104
+ * The percentage of this position's borrow power currently used (scaled by WAD).
105
+ * If the collateral price is 0, usage is `MaxUint256`.
106
+ */
107
+ get borrowCapacityUsage(): bigint | undefined;
108
+ /**
109
+ * Returns the maximum amount of loan assets that can be withdrawn given a certain supply position
110
+ * and a balance of loan assets, and the reason for the limit.
111
+ */
112
+ get withdrawCapacityLimit(): import("../utils.js").CapacityLimit;
113
+ /**
114
+ * Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
115
+ * @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
116
+ */
117
+ accrueInterest(timestamp?: BigIntish): AccrualPosition;
118
+ supply(assets: bigint, shares: bigint, timestamp?: BigIntish): {
119
+ position: AccrualPosition;
120
+ assets: bigint;
121
+ shares: bigint;
122
+ };
123
+ withdraw(assets: bigint, shares: bigint, timestamp?: BigIntish): {
124
+ position: AccrualPosition;
125
+ assets: bigint;
126
+ shares: bigint;
127
+ };
128
+ supplyCollateral(assets: bigint): AccrualPosition;
129
+ withdrawCollateral(assets: bigint, timestamp?: BigIntish): AccrualPosition;
130
+ borrow(assets: bigint, shares: bigint, timestamp?: BigIntish): {
131
+ position: AccrualPosition;
132
+ assets: bigint;
133
+ shares: bigint;
134
+ };
135
+ repay(assets: bigint, shares: bigint, timestamp?: BigIntish): {
136
+ position: AccrualPosition;
137
+ assets: bigint;
138
+ shares: bigint;
139
+ };
140
+ getBorrowCapacityLimit(options?: MaxBorrowOptions): import("../utils.js").CapacityLimit | undefined;
141
+ getWithdrawCollateralCapacityLimit(options?: MaxWithdrawCollateralOptions): import("../utils.js").CapacityLimit | undefined;
142
+ getRepayCapacityLimit(loanTokenBalance: bigint): import("../utils.js").CapacityLimit;
143
+ getMaxCapacities(loanTokenBalance: bigint, collateralTokenBalance: bigint, options?: {
144
+ borrow?: MaxBorrowOptions;
145
+ withdrawCollateral?: MaxWithdrawCollateralOptions;
146
+ }): MaxPositionCapacities;
147
+ }