@morpho-org/blue-sdk 1.0.1 → 1.0.2

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 (151) hide show
  1. package/package.json +10 -6
  2. package/src/addresses.ts +261 -0
  3. package/src/chain/chain.constants.ts +235 -0
  4. package/src/chain/chain.test.ts +51 -0
  5. package/src/chain/chain.types.ts +42 -0
  6. package/src/chain/chain.utils.ts +44 -0
  7. package/src/constants.ts +18 -0
  8. package/src/errors.ts +75 -0
  9. package/src/ethers/ethers.test.ts +17 -0
  10. package/src/ethers/safeGetAddress.ts +4 -0
  11. package/src/ethers/safeParseUnits.ts +29 -0
  12. package/src/evm.ts +172 -0
  13. package/src/helpers/format/format.test.ts +340 -0
  14. package/src/helpers/format/format.ts +416 -0
  15. package/src/helpers/getChecksumedAddress.ts +15 -0
  16. package/{lib/helpers/isZeroAddressOrUnset.d.ts → src/helpers/isZeroAddressOrUnset.ts} +7 -1
  17. package/src/helpers/locale.ts +108 -0
  18. package/src/holding/Holding.ts +109 -0
  19. package/src/market/Market.ts +479 -0
  20. package/src/market/MarketConfig.ts +108 -0
  21. package/src/market/MarketUtils.test.ts +25 -0
  22. package/src/market/MarketUtils.ts +467 -0
  23. package/src/maths/AdaptiveCurveIrmLib.ts +143 -0
  24. package/src/maths/MathLib.ts +208 -0
  25. package/src/maths/MathUtils.ts +31 -0
  26. package/src/maths/SharesMath.ts +40 -0
  27. package/src/notifications.ts +167 -0
  28. package/src/position/Position.ts +251 -0
  29. package/src/signatures/index.ts +18 -0
  30. package/src/signatures/manager.ts +50 -0
  31. package/src/signatures/permit.ts +126 -0
  32. package/src/signatures/permit2.ts +120 -0
  33. package/src/signatures/types.ts +18 -0
  34. package/src/signatures/utils.ts +83 -0
  35. package/src/tests/mocks/markets.ts +110 -0
  36. package/src/token/ERC20Metadata.ts +124 -0
  37. package/src/token/Token.ts +83 -0
  38. package/src/token/TokenNamespace.ts +76 -0
  39. package/src/token/WrappedToken.ts +142 -0
  40. package/src/types.ts +37 -0
  41. package/src/user/User.ts +32 -0
  42. package/src/user/user.types.ts +23 -0
  43. package/src/vault/Vault.ts +370 -0
  44. package/src/vault/VaultAllocation.ts +58 -0
  45. package/src/vault/VaultConfig.ts +55 -0
  46. package/src/vault/VaultUtils.ts +47 -0
  47. package/lib/addresses.d.ts +0 -168
  48. package/lib/addresses.js +0 -169
  49. package/lib/chain/chain.constants.d.ts +0 -3
  50. package/lib/chain/chain.constants.js +0 -232
  51. package/lib/chain/chain.types.d.ts +0 -20
  52. package/lib/chain/chain.types.js +0 -30
  53. package/lib/chain/chain.utils.d.ts +0 -14
  54. package/lib/chain/chain.utils.js +0 -30
  55. package/lib/chain/index.js +0 -18
  56. package/lib/constants.d.ts +0 -8
  57. package/lib/constants.js +0 -13
  58. package/lib/errors.d.ts +0 -37
  59. package/lib/errors.js +0 -71
  60. package/lib/ethers/index.js +0 -18
  61. package/lib/ethers/safeGetAddress.d.ts +0 -1
  62. package/lib/ethers/safeGetAddress.js +0 -6
  63. package/lib/ethers/safeParseUnits.d.ts +0 -2
  64. package/lib/ethers/safeParseUnits.js +0 -25
  65. package/lib/evm.d.ts +0 -36
  66. package/lib/evm.js +0 -113
  67. package/lib/helpers/format/format.d.ts +0 -98
  68. package/lib/helpers/format/format.js +0 -301
  69. package/lib/helpers/format/index.js +0 -17
  70. package/lib/helpers/getChecksumedAddress.d.ts +0 -7
  71. package/lib/helpers/getChecksumedAddress.js +0 -17
  72. package/lib/helpers/index.js +0 -20
  73. package/lib/helpers/isZeroAddressOrUnset.js +0 -14
  74. package/lib/helpers/locale.d.ts +0 -36
  75. package/lib/helpers/locale.js +0 -86
  76. package/lib/holding/Holding.d.ts +0 -60
  77. package/lib/holding/Holding.js +0 -31
  78. package/lib/holding/index.js +0 -17
  79. package/lib/index.d.ts +0 -33
  80. package/lib/index.js +0 -62
  81. package/lib/market/Market.d.ts +0 -159
  82. package/lib/market/Market.js +0 -240
  83. package/lib/market/MarketConfig.d.ts +0 -44
  84. package/lib/market/MarketConfig.js +0 -56
  85. package/lib/market/MarketUtils.d.ts +0 -165
  86. package/lib/market/MarketUtils.js +0 -182
  87. package/lib/market/index.js +0 -19
  88. package/lib/maths/AdaptiveCurveIrmLib.d.ts +0 -37
  89. package/lib/maths/AdaptiveCurveIrmLib.js +0 -116
  90. package/lib/maths/MathLib.d.ts +0 -94
  91. package/lib/maths/MathLib.js +0 -153
  92. package/lib/maths/MathUtils.d.ts +0 -15
  93. package/lib/maths/MathUtils.js +0 -33
  94. package/lib/maths/SharesMath.d.ts +0 -12
  95. package/lib/maths/SharesMath.js +0 -22
  96. package/lib/maths/index.js +0 -20
  97. package/lib/notifications.d.ts +0 -98
  98. package/lib/notifications.js +0 -52
  99. package/lib/position/Position.d.ts +0 -118
  100. package/lib/position/Position.js +0 -145
  101. package/lib/position/index.js +0 -17
  102. package/lib/signatures/index.d.ts +0 -12
  103. package/lib/signatures/index.js +0 -39
  104. package/lib/signatures/manager.d.ts +0 -10
  105. package/lib/signatures/manager.js +0 -37
  106. package/lib/signatures/permit.d.ts +0 -21
  107. package/lib/signatures/permit.js +0 -101
  108. package/lib/signatures/permit2.d.ts +0 -20
  109. package/lib/signatures/permit2.js +0 -91
  110. package/lib/signatures/types.d.ts +0 -13
  111. package/lib/signatures/types.js +0 -2
  112. package/lib/signatures/utils.d.ts +0 -6
  113. package/lib/signatures/utils.js +0 -44
  114. package/lib/tests/mocks/markets.d.ts +0 -17
  115. package/lib/tests/mocks/markets.js +0 -108
  116. package/lib/token/ERC20Metadata.d.ts +0 -249
  117. package/lib/token/ERC20Metadata.js +0 -81
  118. package/lib/token/Token.d.ts +0 -45
  119. package/lib/token/Token.js +0 -39
  120. package/lib/token/TokenNamespace.d.ts +0 -18
  121. package/lib/token/TokenNamespace.js +0 -55
  122. package/lib/token/WrappedToken.d.ts +0 -42
  123. package/lib/token/WrappedToken.js +0 -87
  124. package/lib/token/index.js +0 -18
  125. package/lib/types.d.ts +0 -29
  126. package/lib/types.js +0 -23
  127. package/lib/user/User.d.ts +0 -20
  128. package/lib/user/User.js +0 -11
  129. package/lib/user/index.js +0 -18
  130. package/lib/user/user.types.d.ts +0 -18
  131. package/lib/user/user.types.js +0 -2
  132. package/lib/vault/Vault.d.ts +0 -167
  133. package/lib/vault/Vault.js +0 -156
  134. package/lib/vault/VaultAllocation.d.ts +0 -38
  135. package/lib/vault/VaultAllocation.js +0 -18
  136. package/lib/vault/VaultConfig.d.ts +0 -23
  137. package/lib/vault/VaultConfig.js +0 -26
  138. package/lib/vault/VaultUtils.d.ts +0 -17
  139. package/lib/vault/VaultUtils.js +0 -17
  140. package/lib/vault/index.js +0 -20
  141. /package/{lib/chain/index.d.ts → src/chain/index.ts} +0 -0
  142. /package/{lib/ethers/index.d.ts → src/ethers/index.ts} +0 -0
  143. /package/{lib/helpers/format/index.d.ts → src/helpers/format/index.ts} +0 -0
  144. /package/{lib/helpers/index.d.ts → src/helpers/index.ts} +0 -0
  145. /package/{lib/holding/index.d.ts → src/holding/index.ts} +0 -0
  146. /package/{lib/market/index.d.ts → src/market/index.ts} +0 -0
  147. /package/{lib/maths/index.d.ts → src/maths/index.ts} +0 -0
  148. /package/{lib/position/index.d.ts → src/position/index.ts} +0 -0
  149. /package/{lib/token/index.d.ts → src/token/index.ts} +0 -0
  150. /package/{lib/user/index.d.ts → src/user/index.ts} +0 -0
  151. /package/{lib/vault/index.d.ts → src/vault/index.ts} +0 -0
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./MarketUtils"), exports);
18
- __exportStar(require("./MarketConfig"), exports);
19
- __exportStar(require("./Market"), exports);
@@ -1,37 +0,0 @@
1
- /**
2
- * 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.
3
- */
4
- export declare namespace AdaptiveCurveIrmLib {
5
- const CURVE_STEEPNESS: bigint;
6
- const TARGET_UTILIZATION: bigint;
7
- const INITIAL_RATE_AT_TARGET: bigint;
8
- const ADJUSTMENT_SPEED: bigint;
9
- const MIN_RATE_AT_TARGET: bigint;
10
- const MAX_RATE_AT_TARGET: bigint;
11
- /**
12
- * ln(2), scaled by WAD.
13
- */
14
- const LN_2_INT = 693147180559945309n;
15
- /**
16
- * ln(1e-18), scaled by WAD.
17
- */
18
- const LN_WEI_INT = -41446531673892822312n;
19
- /**
20
- * Above this bound, `wExp` is clipped to avoid overflowing when multiplied with 1 ether.
21
- * This upper bound corresponds to: ln(type(int256).max / 1e36) (scaled by WAD, floored).
22
- */
23
- const WEXP_UPPER_BOUND = 93859467695000404319n;
24
- /**
25
- * The value of wExp(`WEXP_UPPER_BOUND`).
26
- */
27
- const WEXP_UPPER_VALUE = 57716089161558943949701069502944508345128422502756744429568n;
28
- /**
29
- * Returns an approximation of exp(x) used by the Adaptive Curve IRM.
30
- * @param x
31
- */
32
- function wExp(x: bigint): bigint;
33
- function getBorrowRate(startUtilization: bigint, startRateAtTarget: bigint, elapsed: bigint): {
34
- avgBorrowRate: bigint;
35
- endRateAtTarget: bigint;
36
- };
37
- }
@@ -1,116 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdaptiveCurveIrmLib = void 0;
4
- const ethers_1 = require("ethers");
5
- const constants_1 = require("../constants");
6
- const MathLib_1 = require("./MathLib");
7
- /**
8
- * 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.
9
- */
10
- var AdaptiveCurveIrmLib;
11
- (function (AdaptiveCurveIrmLib) {
12
- AdaptiveCurveIrmLib.CURVE_STEEPNESS = (0, ethers_1.parseUnits)("4");
13
- AdaptiveCurveIrmLib.TARGET_UTILIZATION = (0, ethers_1.parseUnits)("0.9");
14
- AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = (0, ethers_1.parseUnits)("0.04") / constants_1.SECONDS_PER_YEAR;
15
- AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = (0, ethers_1.parseUnits)("50") / constants_1.SECONDS_PER_YEAR;
16
- AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = (0, ethers_1.parseUnits)("0.001") / constants_1.SECONDS_PER_YEAR;
17
- AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = (0, ethers_1.parseUnits)("2") / constants_1.SECONDS_PER_YEAR;
18
- /**
19
- * ln(2), scaled by WAD.
20
- */
21
- AdaptiveCurveIrmLib.LN_2_INT = 693147180559945309n;
22
- /**
23
- * ln(1e-18), scaled by WAD.
24
- */
25
- AdaptiveCurveIrmLib.LN_WEI_INT = -41446531673892822312n;
26
- /**
27
- * Above this bound, `wExp` is clipped to avoid overflowing when multiplied with 1 ether.
28
- * This upper bound corresponds to: ln(type(int256).max / 1e36) (scaled by WAD, floored).
29
- */
30
- AdaptiveCurveIrmLib.WEXP_UPPER_BOUND = 93859467695000404319n;
31
- /**
32
- * The value of wExp(`WEXP_UPPER_BOUND`).
33
- */
34
- AdaptiveCurveIrmLib.WEXP_UPPER_VALUE = 57716089161558943949701069502944508345128422502756744429568n;
35
- /**
36
- * Returns an approximation of exp(x) used by the Adaptive Curve IRM.
37
- * @param x
38
- */
39
- function wExp(x) {
40
- // If x < ln(1e-18) then exp(x) < 1e-18 so it is rounded to zero.
41
- if (x < AdaptiveCurveIrmLib.LN_WEI_INT)
42
- return 0n;
43
- // `wExp` is clipped to avoid overflowing when multiplied with 1 ether.
44
- if (x >= AdaptiveCurveIrmLib.WEXP_UPPER_BOUND)
45
- return AdaptiveCurveIrmLib.WEXP_UPPER_VALUE;
46
- // Decompose x as x = q * ln(2) + r with q an integer and -ln(2)/2 <= r <= ln(2)/2.
47
- // q = x / ln(2) rounded half toward zero.
48
- const roundingAdjustment = x < 0n ? -(AdaptiveCurveIrmLib.LN_2_INT / 2n) : AdaptiveCurveIrmLib.LN_2_INT / 2n;
49
- const q = (x + roundingAdjustment) / AdaptiveCurveIrmLib.LN_2_INT;
50
- const r = x - q * AdaptiveCurveIrmLib.LN_2_INT;
51
- // Compute e^r with a 2nd-order Taylor polynomial.
52
- const expR = MathLib_1.MathLib.WAD + r + (r * r) / MathLib_1.MathLib.WAD / 2n;
53
- // Return e^x = 2^q * e^r.
54
- if (q === 0n)
55
- return expR << q;
56
- return expR >> -q;
57
- }
58
- AdaptiveCurveIrmLib.wExp = wExp;
59
- function getBorrowRate(startUtilization, startRateAtTarget, elapsed) {
60
- const errNormFactor = startUtilization > AdaptiveCurveIrmLib.TARGET_UTILIZATION
61
- ? MathLib_1.MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION
62
- : AdaptiveCurveIrmLib.TARGET_UTILIZATION;
63
- const err = MathLib_1.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_1.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_1.MathLib.min(MathLib_1.MathLib.max(MathLib_1.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_1.MathLib.WAD - MathLib_1.MathLib.wDivDown(MathLib_1.MathLib.WAD, AdaptiveCurveIrmLib.CURVE_STEEPNESS)
108
- : AdaptiveCurveIrmLib.CURVE_STEEPNESS - MathLib_1.MathLib.WAD;
109
- // Non negative if avgRateAtTarget >= 0 because if err < 0, coeff <= 1.
110
- return {
111
- avgBorrowRate: MathLib_1.MathLib.wMulDown(MathLib_1.MathLib.wMulDown(coeff, err) + MathLib_1.MathLib.WAD, avgRateAtTarget),
112
- endRateAtTarget,
113
- };
114
- }
115
- AdaptiveCurveIrmLib.getBorrowRate = getBorrowRate;
116
- })(AdaptiveCurveIrmLib || (exports.AdaptiveCurveIrmLib = AdaptiveCurveIrmLib = {}));
@@ -1,94 +0,0 @@
1
- import { BigNumberish } from "ethers";
2
- import { Time } from "@morpho-org/morpho-ts";
3
- export type RoundingDirection = "Up" | "Down";
4
- /**
5
- * Library to manage fixed-point arithmetic.
6
- * This library reproduces the behaviour of the solidity library MathLib
7
- * TODO: add library link
8
- * @category Maths
9
- *
10
- */
11
- export declare class MathLib {
12
- static WAD: bigint;
13
- static MAX_UINT_160: bigint;
14
- static MAX_UINT_128: bigint;
15
- static MAX_UINT_48: bigint;
16
- /**
17
- * Returns the smallest number given as param
18
- * @param x The first number
19
- * @param y The second number
20
- */
21
- static min(x: BigNumberish, y: BigNumberish): bigint;
22
- /**
23
- * Returns the greatest number given as param
24
- * @param x The first number
25
- * @param y The second number
26
- */
27
- static max(x: BigNumberish, y: BigNumberish): bigint;
28
- /**
29
- * Perform the WAD-based multiplication of 2 numbers, rounded down
30
- * @param x The first number
31
- * @param y The second number
32
- */
33
- static wMulDown(x: BigNumberish, y: BigNumberish): bigint;
34
- /**
35
- * Perform the WAD-based multiplication of 2 numbers, rounded up
36
- * @param x The first number
37
- * @param y The second number
38
- */
39
- static wMulUp(x: BigNumberish, y: BigNumberish): bigint;
40
- /**
41
- * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
42
- * @param x The first number
43
- * @param y The second number
44
- */
45
- static wMul(x: bigint, y: bigint, rounding: RoundingDirection): bigint;
46
- /**
47
- * Perform the WAD-based division of 2 numbers, rounded down
48
- * @param x The first number
49
- * @param y The second number
50
- */
51
- static wDivDown(x: BigNumberish, y: BigNumberish): bigint;
52
- /**
53
- * Perform the WAD-based multiplication of 2 numbers, rounded up
54
- * @param x The first number
55
- * @param y The second number
56
- */
57
- static wDivUp(x: BigNumberish, y: BigNumberish): bigint;
58
- /**
59
- * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
60
- * @param x The first number
61
- * @param y The second number
62
- */
63
- static wDiv(x: BigNumberish, y: BigNumberish, rounding: RoundingDirection): bigint;
64
- /**
65
- * Multiply two numbers and divide by a denominator, rounding down the result
66
- * @param x The first number
67
- * @param y The second number
68
- * @param denominator The denominator
69
- */
70
- static mulDivDown(x: BigNumberish, y: BigNumberish, denominator: BigNumberish): bigint;
71
- /**
72
- * Multiply two numbers and divide by a denominator, rounding up the result
73
- * @param x The first number
74
- * @param y The second number
75
- * @param denominator The denominator
76
- */
77
- static mulDivUp(x: BigNumberish, y: BigNumberish, denominator: BigNumberish): bigint;
78
- static mulDiv(x: BigNumberish, y: BigNumberish, denominator: BigNumberish, rounding: RoundingDirection): bigint;
79
- /**
80
- * The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
81
- * to approximate a continuous compound interest rate.
82
- *
83
- * @param x The base of the exponent
84
- * @param n The exponent
85
- */
86
- static wTaylorCompounded(x: BigNumberish, n: BigNumberish): bigint;
87
- /**
88
- * Converts an apr to compounded apy
89
- *
90
- * @param apr The apr to convert (in WAD)
91
- * @param compounding The compounding basis
92
- */
93
- static aprToApy(apr: bigint, compounding: Time.Unit): bigint;
94
- }
@@ -1,153 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MathLib = void 0;
4
- const ethers_1 = require("ethers");
5
- const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
- /**
7
- * Library to manage fixed-point arithmetic.
8
- * This library reproduces the behaviour of the solidity library MathLib
9
- * TODO: add library link
10
- * @category Maths
11
- *
12
- */
13
- class MathLib {
14
- /**
15
- * Returns the smallest number given as param
16
- * @param x The first number
17
- * @param y The second number
18
- */
19
- static min(x, y) {
20
- x = (0, ethers_1.toBigInt)(x);
21
- y = (0, ethers_1.toBigInt)(y);
22
- return x <= y ? x : y;
23
- }
24
- /**
25
- * Returns the greatest number given as param
26
- * @param x The first number
27
- * @param y The second number
28
- */
29
- static max(x, y) {
30
- x = (0, ethers_1.toBigInt)(x);
31
- y = (0, ethers_1.toBigInt)(y);
32
- return x <= y ? y : x;
33
- }
34
- /**
35
- * Perform the WAD-based multiplication of 2 numbers, rounded down
36
- * @param x The first number
37
- * @param y The second number
38
- */
39
- static wMulDown(x, y) {
40
- x = (0, ethers_1.toBigInt)(x);
41
- y = (0, ethers_1.toBigInt)(y);
42
- return MathLib.wMul(x, y, "Down");
43
- }
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
- static wMulUp(x, y) {
50
- x = (0, ethers_1.toBigInt)(x);
51
- y = (0, ethers_1.toBigInt)(y);
52
- return MathLib.wMul(x, y, "Up");
53
- }
54
- /**
55
- * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
56
- * @param x The first number
57
- * @param y The second number
58
- */
59
- static wMul(x, y, rounding) {
60
- return MathLib.mulDiv(x, y, MathLib.WAD, rounding);
61
- }
62
- /**
63
- * Perform the WAD-based division of 2 numbers, rounded down
64
- * @param x The first number
65
- * @param y The second number
66
- */
67
- static wDivDown(x, y) {
68
- x = (0, ethers_1.toBigInt)(x);
69
- y = (0, ethers_1.toBigInt)(y);
70
- return MathLib.wDiv(x, y, "Down");
71
- }
72
- /**
73
- * Perform the WAD-based multiplication of 2 numbers, rounded up
74
- * @param x The first number
75
- * @param y The second number
76
- */
77
- static wDivUp(x, y) {
78
- x = (0, ethers_1.toBigInt)(x);
79
- y = (0, ethers_1.toBigInt)(y);
80
- return MathLib.wDiv(x, y, "Up");
81
- }
82
- /**
83
- * Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
84
- * @param x The first number
85
- * @param y The second number
86
- */
87
- static wDiv(x, y, rounding) {
88
- return MathLib.mulDiv(x, MathLib.WAD, y, rounding);
89
- }
90
- /**
91
- * Multiply two numbers and divide by a denominator, rounding down the result
92
- * @param x The first number
93
- * @param y The second number
94
- * @param denominator The denominator
95
- */
96
- static mulDivDown(x, y, denominator) {
97
- x = (0, ethers_1.toBigInt)(x);
98
- y = (0, ethers_1.toBigInt)(y);
99
- denominator = (0, ethers_1.toBigInt)(denominator);
100
- if (denominator === 0n)
101
- throw Error("MathLib: DIVISION_BY_ZERO");
102
- return (x * y) / denominator;
103
- }
104
- /**
105
- * Multiply two numbers and divide by a denominator, rounding up the result
106
- * @param x The first number
107
- * @param y The second number
108
- * @param denominator The denominator
109
- */
110
- static mulDivUp(x, y, denominator) {
111
- x = (0, ethers_1.toBigInt)(x);
112
- y = (0, ethers_1.toBigInt)(y);
113
- denominator = (0, ethers_1.toBigInt)(denominator);
114
- if (denominator === 0n)
115
- throw Error("MathLib: DIVISION_BY_ZERO");
116
- const roundup = (x * y) % denominator > 0 ? 1n : 0n;
117
- return (x * y) / denominator + roundup;
118
- }
119
- static mulDiv(x, y, denominator, rounding) {
120
- return MathLib[`mulDiv${rounding}`](x, y, denominator);
121
- }
122
- /**
123
- * The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
124
- * to approximate a continuous compound interest rate.
125
- *
126
- * @param x The base of the exponent
127
- * @param n The exponent
128
- */
129
- static wTaylorCompounded(x, n) {
130
- x = (0, ethers_1.toBigInt)(x);
131
- n = (0, ethers_1.toBigInt)(n);
132
- const firstTerm = x * n;
133
- const secondTerm = MathLib.mulDivDown(firstTerm, firstTerm, 2n * MathLib.WAD);
134
- const thirdTerm = MathLib.mulDivDown(secondTerm, firstTerm, 3n * MathLib.WAD);
135
- return firstTerm + secondTerm + thirdTerm;
136
- }
137
- /**
138
- * Converts an apr to compounded apy
139
- *
140
- * @param apr The apr to convert (in WAD)
141
- * @param compounding The compounding basis
142
- */
143
- static aprToApy(apr, compounding) {
144
- const factor = morpho_ts_1.Time[compounding].from.y(1n);
145
- const rate = apr / factor;
146
- return ((0, ethers_1.parseUnits)(((+(0, ethers_1.formatUnits)(this.WAD + rate)) ** Number(factor)).toFixed(18)) - this.WAD);
147
- }
148
- }
149
- exports.MathLib = MathLib;
150
- MathLib.WAD = (0, ethers_1.parseUnits)("1");
151
- MathLib.MAX_UINT_160 = BigInt("0x" + "f".repeat(40));
152
- MathLib.MAX_UINT_128 = BigInt("0x" + "f".repeat(32));
153
- MathLib.MAX_UINT_48 = BigInt("0x" + "f".repeat(12));
@@ -1,15 +0,0 @@
1
- /**
2
- * Math utilities
3
- * This library reproduces the behaviour of the solidity library MathUtils
4
- * TODO: add library link
5
- * @category Maths
6
- */
7
- export declare class MathUtils {
8
- static zeroFloorSub(a: bigint, b: bigint): bigint;
9
- static min(a: bigint, b: bigint): bigint;
10
- static mins(...args: bigint[]): bigint;
11
- static max(a: bigint, b: bigint): bigint;
12
- static pos(a: bigint): bigint;
13
- static neg(a: bigint): bigint;
14
- static abs(a: bigint): bigint;
15
- }
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MathUtils = void 0;
4
- /**
5
- * Math utilities
6
- * This library reproduces the behaviour of the solidity library MathUtils
7
- * TODO: add library link
8
- * @category Maths
9
- */
10
- class MathUtils {
11
- static zeroFloorSub(a, b) {
12
- return a - b < 0n ? 0n : a - b;
13
- }
14
- static min(a, b) {
15
- return a < b ? a : b;
16
- }
17
- static mins(...args) {
18
- return args.reduce((a, b) => this.min(a, b));
19
- }
20
- static max(a, b) {
21
- return a > b ? a : b;
22
- }
23
- static pos(a) {
24
- return a >= 0n ? a : 0n;
25
- }
26
- static neg(a) {
27
- return a < 0n ? -a : 0n;
28
- }
29
- static abs(a) {
30
- return a >= 0 ? a : -a;
31
- }
32
- }
33
- exports.MathUtils = MathUtils;
@@ -1,12 +0,0 @@
1
- import { BigNumberish } from "ethers";
2
- import { RoundingDirection } from "./MathLib";
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: BigNumberish, totalAssets: BigNumberish, totalShares: BigNumberish, rounding: RoundingDirection): bigint;
11
- function toShares(assets: BigNumberish, totalAssets: BigNumberish, totalShares: BigNumberish, rounding: RoundingDirection): bigint;
12
- }
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SharesMath = void 0;
4
- const ethers_1 = require("ethers");
5
- const MathLib_1 = require("./MathLib");
6
- /**
7
- * JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
8
- * & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
9
- */
10
- var SharesMath;
11
- (function (SharesMath) {
12
- SharesMath.VIRTUAL_SHARES = 1000000n;
13
- SharesMath.VIRTUAL_ASSETS = 1n;
14
- function toAssets(shares, totalAssets, totalShares, rounding) {
15
- return MathLib_1.MathLib.mulDiv(shares, (0, ethers_1.toBigInt)(totalAssets) + SharesMath.VIRTUAL_ASSETS, (0, ethers_1.toBigInt)(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
16
- }
17
- SharesMath.toAssets = toAssets;
18
- function toShares(assets, totalAssets, totalShares, rounding) {
19
- return MathLib_1.MathLib.mulDiv(assets, (0, ethers_1.toBigInt)(totalShares) + SharesMath.VIRTUAL_SHARES, (0, ethers_1.toBigInt)(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
20
- }
21
- SharesMath.toShares = toShares;
22
- })(SharesMath || (exports.SharesMath = SharesMath = {}));
@@ -1,20 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./MathLib"), exports);
18
- __exportStar(require("./SharesMath"), exports);
19
- __exportStar(require("./MathUtils"), exports);
20
- __exportStar(require("./AdaptiveCurveIrmLib"), exports);
@@ -1,98 +0,0 @@
1
- import { Signature, TransactionReceipt, TransactionRequest, TransactionResponse } from "ethers";
2
- import { Observable, Observer, ReplaySubject } from "rxjs";
3
- import { SignatureMessage } from "./signatures/types";
4
- export declare enum NotificationStatus {
5
- signing = "signing",
6
- success = "success",
7
- error = "error",
8
- pending = "pending"
9
- }
10
- export declare enum NotificationTopic {
11
- tx = "tx",
12
- signature = "signature"
13
- }
14
- export interface NotificationContext {
15
- [NotificationTopic.tx]: {
16
- [NotificationStatus.signing]: {
17
- tx: TransactionRequest;
18
- };
19
- [NotificationStatus.pending]: {
20
- args: Record<PropertyKey, any>;
21
- tx: TransactionRequest;
22
- response?: TransactionResponse;
23
- };
24
- [NotificationStatus.success]: {
25
- args: Record<PropertyKey, any>;
26
- tx: TransactionRequest;
27
- response?: TransactionResponse;
28
- receipt: TransactionReceipt | null;
29
- };
30
- [NotificationStatus.error]: {
31
- args: Record<PropertyKey, any>;
32
- tx: TransactionRequest;
33
- response?: TransactionResponse;
34
- error: Error;
35
- };
36
- };
37
- [NotificationTopic.signature]: {
38
- [NotificationStatus.signing]: never;
39
- [NotificationStatus.pending]: {
40
- message: SignatureMessage;
41
- args: Record<PropertyKey, any>;
42
- };
43
- [NotificationStatus.success]: {
44
- message: SignatureMessage;
45
- args: Record<PropertyKey, any>;
46
- signature: Signature;
47
- };
48
- [NotificationStatus.error]: {
49
- message: SignatureMessage;
50
- args: Record<PropertyKey, any>;
51
- error: Error;
52
- };
53
- };
54
- }
55
- export interface NotificationBody<Topic extends NotificationTopic = NotificationTopic, Status extends NotificationStatus = NotificationStatus> {
56
- id: string;
57
- topic: Topic;
58
- status: Status;
59
- }
60
- export type Notifications = {
61
- [Topic in NotificationTopic]: {
62
- [Status in NotificationStatus]: NotificationBody<Topic, Status> & (NotificationContext[Topic][Status] extends never ? unknown : {
63
- context: NotificationContext[Topic][Status];
64
- });
65
- };
66
- };
67
- export type TxNotification = Notifications[NotificationTopic.tx][NotificationStatus];
68
- export type SignatureNotification = Notifications[NotificationTopic.signature][NotificationStatus];
69
- export type Notification = Notifications[NotificationTopic][NotificationStatus];
70
- export type NotificationsWithoutTopic = {
71
- [Topic in NotificationTopic]: {
72
- [Status in NotificationStatus]: Omit<NotificationBody<Topic, Status>, "topic"> & (NotificationContext[Topic][Status] extends never ? unknown : {
73
- context: NotificationContext[Topic][Status];
74
- });
75
- };
76
- };
77
- export interface NotificationConsumer<Topic extends NotificationTopic> {
78
- notifications$: Observable<Notifications[Topic][NotificationStatus]>;
79
- wait: (observerOrNext?: Partial<Observer<Notifications[Topic][NotificationStatus]>> | ((value: Notifications[Topic][NotificationStatus]) => void)) => Promise<Notifications[Topic][NotificationStatus.success | NotificationStatus.error]>;
80
- }
81
- /**
82
- * MUST receive a success or error notification at some point to ensure the result promise resolves.
83
- */
84
- export declare class NotificationProducer<Topic extends NotificationTopic> {
85
- readonly topic: Topic;
86
- protected readonly _notifications$: ReplaySubject<Notifications[Topic][NotificationStatus]>;
87
- readonly notifications$: Observable<Notifications[Topic][NotificationStatus]>;
88
- protected readonly _result: Promise<Notifications[Topic][NotificationStatus]>;
89
- readonly consumer: NotificationConsumer<Topic>;
90
- /**
91
- * Creates a notification producer.
92
- * MUST receive a success or error notification at some point to ensure the result promise resolves.
93
- * @param topic The notification topic to populate each notification with.
94
- */
95
- constructor(topic: Topic);
96
- wait(observerOrNext?: Partial<Observer<Notifications[Topic][NotificationStatus]>> | ((value: Notifications[Topic][NotificationStatus]) => void)): Promise<Notifications[Topic][NotificationStatus.success | NotificationStatus.error]>;
97
- next(notification: NotificationsWithoutTopic[Topic][NotificationStatus]): void;
98
- }