solidity-math-utils 1.11.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -89
- package/package.json +3 -3
- package/project/contracts/AdvancedMath.sol +309 -187
- package/project/contracts/AnalyticMath.sol +75 -321
- package/project/contracts/BondingCurve.sol +30 -52
- package/project/contracts/DynamicCurve.sol +73 -31
- package/project/contracts/FractionMath.sol +99 -65
- package/project/contracts/IntegralMath.sol +73 -4
- package/project/contracts/common/{Uint.sol → Uint256.sol} +1 -6
- package/readme/AdvancedMath.pdf +0 -0
- package/readme/AdvancedMath.txt +0 -44
- package/readme/DynamicCurve.pdf +0 -0
- package/readme/DynamicCurve.txt +0 -77
|
@@ -1,54 +1,96 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.28;
|
|
3
3
|
|
|
4
4
|
import "./AdvancedMath.sol";
|
|
5
|
+
import "./FractionMath.sol";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
uint256
|
|
7
|
+
library DynamicCurve {
|
|
8
|
+
uint256 internal constant MAX_WEIGHT = 1000000;
|
|
9
|
+
|
|
10
|
+
// Abstract:
|
|
11
|
+
// Consider a pool which implements the bonding-curve model over a primary reserve token and a secondary reserve token.
|
|
12
|
+
// Let 'on-chain price' denote the conversion rate between these tokens inside the pool (i.e., as determined by the pool).
|
|
13
|
+
// Let 'off-chain price' denote the conversion rate between these tokens outside the pool (i.e., as determined by the market).
|
|
14
|
+
// The arbitrage incentive is always to convert to the point where the on-chain price is equal to the off-chain price.
|
|
15
|
+
// We want this operation to also impact the primary reserve balance becoming equal to the primary reserve staked balance.
|
|
16
|
+
// In other words, we want the arbitrager to convert the difference between the reserve balance and the reserve staked balance.
|
|
17
|
+
// Hence we adjust the weights in order to create an arbitrage incentive which, when realized, will subsequently equalize the pool.
|
|
18
|
+
//
|
|
19
|
+
// Input:
|
|
20
|
+
// - Let t denote the primary reserve token staked balance
|
|
21
|
+
// - Let s denote the primary reserve token balance
|
|
22
|
+
// - Let r denote the secondary reserve token balance
|
|
23
|
+
// - Let q denote the numerator of the off-chain price
|
|
24
|
+
// - Let p denote the denominator of the off-chain price
|
|
25
|
+
// Where p primary tokens are equal to q secondary tokens
|
|
26
|
+
//
|
|
27
|
+
// Output:
|
|
28
|
+
// - Solve the equation x * (s / t) ^ x = (t / r) * (q / p)
|
|
29
|
+
// - Return x / (x + 1) as the weight of the primary reserve token
|
|
30
|
+
// - Return 1 / (x + 1) as the weight of the secondary reserve token
|
|
31
|
+
//
|
|
32
|
+
// If the rate-provider provides the rates for a common unit, for example:
|
|
33
|
+
// - P = 2 ==> 2 primary reserve tokens = 1 ether
|
|
34
|
+
// - Q = 3 ==> 3 secondary reserve tokens = 1 ether
|
|
35
|
+
// Then you can simply use p = P and q = Q
|
|
36
|
+
//
|
|
37
|
+
// If the rate-provider provides the rates for a single unit, for example:
|
|
38
|
+
// - P = 2 ==> 1 primary reserve token = 2 ethers
|
|
39
|
+
// - Q = 3 ==> 1 secondary reserve token = 3 ethers
|
|
40
|
+
// Then you can simply use p = Q and q = P
|
|
8
41
|
|
|
9
42
|
/**
|
|
10
|
-
* @dev
|
|
11
|
-
* Let 'on-chain price' denote the conversion rate between these tokens inside the pool (i.e., as determined by the pool).
|
|
12
|
-
* Let 'off-chain price' denote the conversion rate between these tokens outside the pool (i.e., as determined by the market).
|
|
13
|
-
* The arbitrage incentive is always to convert to the point where the on-chain price is equal to the off-chain price.
|
|
14
|
-
* We want this operation to also impact the primary reserve balance becoming equal to the primary reserve staked balance.
|
|
15
|
-
* In other words, we want the arbitrager to convert the difference between the reserve balance and the reserve staked balance.
|
|
16
|
-
* Hence we adjust the weights in order to create an arbitrage incentive which, when realized, will subsequently equalize the pool.
|
|
43
|
+
* @dev Equalize the weights of a given pool while opting for accuracy over performance
|
|
17
44
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* - Let p denote the denominator of the off-chain price
|
|
24
|
-
* Where p primary tokens are equal to q secondary tokens
|
|
45
|
+
* @param t The primary reserve token staked balance
|
|
46
|
+
* @param s The primary reserve token balance
|
|
47
|
+
* @param r The secondary reserve token balance
|
|
48
|
+
* @param q The numerator of the off-chain price
|
|
49
|
+
* @param p The denominator of the off-chain price
|
|
25
50
|
*
|
|
26
|
-
*
|
|
27
|
-
* - Solve the equation x * (s / t) ^ x = (t / r) * (q / p)
|
|
28
|
-
* - Return x / (x + 1) as the weight of the primary reserve token
|
|
29
|
-
* - Return 1 / (x + 1) as the weight of the secondary reserve token
|
|
51
|
+
* Note that `numerator / denominator` should represent the amount of secondary tokens equal to one primary token
|
|
30
52
|
*
|
|
31
|
-
*
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
53
|
+
* @return The weight of the primary reserve token and the weight of the secondary reserve token, both in ppm units
|
|
54
|
+
*/
|
|
55
|
+
function equalizeExact(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p) internal pure returns (uint256, uint256) { unchecked {
|
|
56
|
+
return equalize(t, s, r, q, p, AdvancedMath.solveExact);
|
|
57
|
+
}}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @dev Equalize the weights of a given pool while opting for performance over accuracy
|
|
35
61
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
62
|
+
* @param t The primary reserve token staked balance
|
|
63
|
+
* @param s The primary reserve token balance
|
|
64
|
+
* @param r The secondary reserve token balance
|
|
65
|
+
* @param q The numerator of the off-chain price
|
|
66
|
+
* @param p The denominator of the off-chain price
|
|
67
|
+
*
|
|
68
|
+
* Note that `numerator / denominator` should represent the amount of secondary tokens equal to one primary token
|
|
69
|
+
*
|
|
70
|
+
* @return The weight of the primary reserve token and the weight of the secondary reserve token, both in ppm units
|
|
71
|
+
*/
|
|
72
|
+
function equalizeQuick(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p) internal pure returns (uint256, uint256) { unchecked {
|
|
73
|
+
return equalize(t, s, r, q, p, AdvancedMath.solveQuick);
|
|
74
|
+
}}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @dev Equalize the weights of a given pool
|
|
40
78
|
*
|
|
41
79
|
* @param t The primary reserve token staked balance
|
|
42
80
|
* @param s The primary reserve token balance
|
|
43
81
|
* @param r The secondary reserve token balance
|
|
44
82
|
* @param q The numerator of the off-chain price
|
|
45
83
|
* @param p The denominator of the off-chain price
|
|
84
|
+
* @param AdvancedMath_solveFunction An equation solver
|
|
46
85
|
*
|
|
47
86
|
* Note that `numerator / denominator` should represent the amount of secondary tokens equal to one primary token
|
|
48
87
|
*
|
|
49
88
|
* @return The weight of the primary reserve token and the weight of the secondary reserve token, both in ppm units
|
|
50
89
|
*/
|
|
51
|
-
function equalize(
|
|
90
|
+
function equalize(
|
|
91
|
+
uint256 t, uint256 s, uint256 r, uint256 q, uint256 p,
|
|
92
|
+
function (uint256, uint256, uint256, uint256) pure returns (uint256, uint256) AdvancedMath_solveFunction
|
|
93
|
+
) private pure returns (uint256, uint256) { unchecked {
|
|
52
94
|
if (t == s)
|
|
53
95
|
require(t > 0 || r > 0, "invalid balance");
|
|
54
96
|
else
|
|
@@ -56,7 +98,7 @@ contract DynamicCurve is AdvancedMath {
|
|
|
56
98
|
require(q > 0 && p > 0, "invalid rate");
|
|
57
99
|
|
|
58
100
|
(uint256 tq, uint256 rp) = FractionMath.productRatio(t, q, r, p);
|
|
59
|
-
(uint256 xn, uint256 xd) =
|
|
101
|
+
(uint256 xn, uint256 xd) = AdvancedMath_solveFunction(s, t, tq, rp);
|
|
60
102
|
(uint256 w1, uint256 w2) = FractionMath.normalizedRatio(xn, xd, MAX_WEIGHT);
|
|
61
103
|
|
|
62
104
|
return (w1, w2);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.28;
|
|
3
3
|
|
|
4
4
|
import "./IntegralMath.sol";
|
|
5
5
|
|
|
@@ -9,39 +9,31 @@ library FractionMath {
|
|
|
9
9
|
uint256 internal constant MAX_UINT128 = type(uint128).max;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* @dev Compute the power of a given ratio
|
|
12
|
+
* @dev Compute the power of a given ratio while opting for accuracy over performance
|
|
13
13
|
*
|
|
14
14
|
* @param n The ratio numerator
|
|
15
15
|
* @param d The ratio denominator
|
|
16
16
|
* @param exp The exponentiation value
|
|
17
|
-
* @param fast Opt for performance over accuracy
|
|
18
17
|
*
|
|
19
18
|
* @return The powered ratio numerator
|
|
20
19
|
* @return The powered ratio denominator
|
|
21
20
|
*/
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
function (uint256, uint256, uint256, uint256) pure returns (uint256, uint256) safeRatio = fast ? mulRatio128 : productRatio;
|
|
26
|
-
|
|
27
|
-
uint256[MAX_EXP_BIT_LEN] memory ns;
|
|
28
|
-
uint256[MAX_EXP_BIT_LEN] memory ds;
|
|
29
|
-
|
|
30
|
-
(ns[0], ds[0]) = safeRatio(n, 1, d, 1);
|
|
31
|
-
for (uint256 i = 0; (exp >> i) > 1; ++i) {
|
|
32
|
-
(ns[i + 1], ds[i + 1]) = safeRatio(ns[i], ns[i], ds[i], ds[i]);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
n = 1;
|
|
36
|
-
d = 1;
|
|
37
|
-
|
|
38
|
-
for (uint256 i = 0; (exp >> i) > 0; ++i) {
|
|
39
|
-
if (((exp >> i) & 1) > 0) {
|
|
40
|
-
(n, d) = safeRatio(n, ns[i], d, ds[i]);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
21
|
+
function poweredRatioExact(uint256 n, uint256 d, uint256 exp) internal pure returns (uint256, uint256) { unchecked {
|
|
22
|
+
return poweredRatio(n, d, exp, productRatio);
|
|
23
|
+
}}
|
|
43
24
|
|
|
44
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @dev Compute the power of a given ratio while opting for performance over accuracy
|
|
27
|
+
*
|
|
28
|
+
* @param n The ratio numerator
|
|
29
|
+
* @param d The ratio denominator
|
|
30
|
+
* @param exp The exponentiation value
|
|
31
|
+
*
|
|
32
|
+
* @return The powered ratio numerator
|
|
33
|
+
* @return The powered ratio denominator
|
|
34
|
+
*/
|
|
35
|
+
function poweredRatioQuick(uint256 n, uint256 d, uint256 exp) internal pure returns (uint256, uint256) { unchecked {
|
|
36
|
+
return poweredRatio(n, d, exp, mulRatio128);
|
|
45
37
|
}}
|
|
46
38
|
|
|
47
39
|
/**
|
|
@@ -56,32 +48,32 @@ library FractionMath {
|
|
|
56
48
|
* @return The product ratio denominator
|
|
57
49
|
*/
|
|
58
50
|
function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd) internal pure returns (uint256, uint256) { unchecked {
|
|
59
|
-
uint256 n = IntegralMath.
|
|
60
|
-
uint256 d = IntegralMath.
|
|
51
|
+
uint256 n = IntegralMath.minFactor(xn, yn);
|
|
52
|
+
uint256 d = IntegralMath.minFactor(xd, yd);
|
|
61
53
|
uint256 z = n > d ? n : d;
|
|
62
|
-
|
|
63
|
-
return (IntegralMath.mulDivC(xn, yn, z), IntegralMath.mulDivC(xd, yd, z));
|
|
64
|
-
}
|
|
65
|
-
return (xn * yn, xd * yd);
|
|
54
|
+
return (IntegralMath.mulDivC(xn, yn, z), IntegralMath.mulDivC(xd, yd, z));
|
|
66
55
|
}}
|
|
67
56
|
|
|
68
57
|
/**
|
|
69
|
-
* @dev Reduce the components of a given ratio
|
|
58
|
+
* @dev Reduce the components of a given ratio to fit up to a given threshold
|
|
70
59
|
*
|
|
71
60
|
* @param n The ratio numerator
|
|
72
61
|
* @param d The ratio denominator
|
|
73
|
-
* @param
|
|
62
|
+
* @param cap The desired threshold
|
|
74
63
|
*
|
|
75
64
|
* @return The reduced ratio numerator
|
|
76
65
|
* @return The reduced ratio denominator
|
|
77
66
|
*/
|
|
78
|
-
function reducedRatio(uint256 n, uint256 d, uint256
|
|
79
|
-
|
|
80
|
-
|
|
67
|
+
function reducedRatio(uint256 n, uint256 d, uint256 cap) internal pure returns (uint256, uint256) { unchecked {
|
|
68
|
+
if (n < d)
|
|
69
|
+
(n, d) = reducedRatioCalc(n, d, cap);
|
|
70
|
+
else
|
|
71
|
+
(d, n) = reducedRatioCalc(d, n, cap);
|
|
72
|
+
return (n, d);
|
|
81
73
|
}}
|
|
82
74
|
|
|
83
75
|
/**
|
|
84
|
-
* @dev
|
|
76
|
+
* @dev Normalize the components of a given ratio to sum up to a given scale
|
|
85
77
|
*
|
|
86
78
|
* @param n The ratio numerator
|
|
87
79
|
* @param d The ratio denominator
|
|
@@ -91,47 +83,89 @@ library FractionMath {
|
|
|
91
83
|
* @return The normalized ratio denominator
|
|
92
84
|
*/
|
|
93
85
|
function normalizedRatio(uint256 n, uint256 d, uint256 scale) internal pure returns (uint256, uint256) { unchecked {
|
|
94
|
-
if (n
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
86
|
+
if (n < d)
|
|
87
|
+
(n, d) = normalizedRatioCalc(n, d, scale);
|
|
88
|
+
else
|
|
89
|
+
(d, n) = normalizedRatioCalc(d, n, scale);
|
|
98
90
|
return (n, d);
|
|
99
91
|
}}
|
|
100
92
|
|
|
101
93
|
/**
|
|
102
|
-
* @dev Compute
|
|
94
|
+
* @dev Compute the power of a given ratio
|
|
103
95
|
*
|
|
104
96
|
* @param n The ratio numerator
|
|
105
97
|
* @param d The ratio denominator
|
|
106
|
-
* @param
|
|
98
|
+
* @param exp The exponentiation value
|
|
99
|
+
* @param safeRatio The computing function
|
|
107
100
|
*
|
|
108
|
-
* @return The
|
|
109
|
-
* @return The
|
|
101
|
+
* @return The powered ratio numerator
|
|
102
|
+
* @return The powered ratio denominator
|
|
110
103
|
*/
|
|
111
|
-
function
|
|
112
|
-
uint256
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
104
|
+
function poweredRatio(
|
|
105
|
+
uint256 n, uint256 d, uint256 exp,
|
|
106
|
+
function (uint256, uint256, uint256, uint256) pure returns (uint256, uint256) safeRatio
|
|
107
|
+
) private pure returns (uint256, uint256) { unchecked {
|
|
108
|
+
require(exp <= MAX_EXP, "exp too large");
|
|
109
|
+
|
|
110
|
+
uint256[MAX_EXP_BIT_LEN] memory ns;
|
|
111
|
+
uint256[MAX_EXP_BIT_LEN] memory ds;
|
|
112
|
+
|
|
113
|
+
(ns[0], ds[0]) = safeRatio(n, 1, d, 1);
|
|
114
|
+
for (uint256 i = 0; (exp >> i) > 1; ++i) {
|
|
115
|
+
(ns[i + 1], ds[i + 1]) = safeRatio(ns[i], ns[i], ds[i], ds[i]);
|
|
119
116
|
}
|
|
120
117
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return (r, scale - r); // `r = n * scale / (n + d) < scale`
|
|
128
|
-
}
|
|
129
|
-
if (p < d - (d - n) / 2) {
|
|
130
|
-
return (0, scale); // `n * scale < (n + d) / 2 < MAX_VAL < n + d`
|
|
118
|
+
n = 1;
|
|
119
|
+
d = 1;
|
|
120
|
+
|
|
121
|
+
for (uint256 i = 0; (exp >> i) > 0; ++i) {
|
|
122
|
+
if (((exp >> i) & 1) > 0) {
|
|
123
|
+
(n, d) = safeRatio(n, ns[i], d, ds[i]);
|
|
131
124
|
}
|
|
132
|
-
return (1, scale - 1); // `(n + d) / 2 < n * scale < MAX_VAL < n + d`
|
|
133
125
|
}
|
|
134
|
-
|
|
126
|
+
|
|
127
|
+
return (n, d);
|
|
128
|
+
}}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @dev Reduce the components of a given ratio to fit up to a given threshold,
|
|
132
|
+
* under the implicit assumption that the ratio is smaller than or equal to 1
|
|
133
|
+
*
|
|
134
|
+
* @param n The ratio numerator
|
|
135
|
+
* @param d The ratio denominator
|
|
136
|
+
* @param cap The desired threshold
|
|
137
|
+
*
|
|
138
|
+
* @return The reduced ratio numerator
|
|
139
|
+
* @return The reduced ratio denominator
|
|
140
|
+
*/
|
|
141
|
+
function reducedRatioCalc(uint256 n, uint256 d, uint256 cap) private pure returns (uint256, uint256) { unchecked {
|
|
142
|
+
if (d > cap) {
|
|
143
|
+
n = IntegralMath.mulDivR(n, cap, d);
|
|
144
|
+
d = cap;
|
|
145
|
+
}
|
|
146
|
+
return (n, d);
|
|
147
|
+
}}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @dev Normalize the components of a given ratio to sum up to a given scale,
|
|
151
|
+
* under the implicit assumption that the ratio is smaller than or equal to 1
|
|
152
|
+
*
|
|
153
|
+
* @param n The ratio numerator
|
|
154
|
+
* @param d The ratio denominator
|
|
155
|
+
* @param scale The desired scale
|
|
156
|
+
*
|
|
157
|
+
* @return The normalized ratio numerator
|
|
158
|
+
* @return The normalized ratio denominator
|
|
159
|
+
*/
|
|
160
|
+
function normalizedRatioCalc(uint256 n, uint256 d, uint256 scale) private pure returns (uint256, uint256) { unchecked {
|
|
161
|
+
if (n > ~d) {
|
|
162
|
+
uint256 x = unsafeAdd(n, d) + 1;
|
|
163
|
+
uint256 y = IntegralMath.mulDivF(x, n / 2, n / 2 + d / 2);
|
|
164
|
+
n -= y;
|
|
165
|
+
d -= x - y;
|
|
166
|
+
}
|
|
167
|
+
uint256 z = IntegralMath.mulDivR(scale, n, n + d);
|
|
168
|
+
return(z, scale - z);
|
|
135
169
|
}}
|
|
136
170
|
|
|
137
171
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.28;
|
|
3
3
|
|
|
4
|
-
import "./common/
|
|
4
|
+
import "./common/Uint256.sol";
|
|
5
5
|
|
|
6
6
|
library IntegralMath {
|
|
7
7
|
/**
|
|
@@ -85,6 +85,65 @@ library IntegralMath {
|
|
|
85
85
|
return n / d + (n % d) / (d - d / 2);
|
|
86
86
|
}}
|
|
87
87
|
|
|
88
|
+
/**
|
|
89
|
+
* @dev Compute the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`
|
|
90
|
+
*/
|
|
91
|
+
function minFactor(uint256 x, uint256 y) internal pure returns (uint256) { unchecked {
|
|
92
|
+
(uint256 hi, uint256 lo) = mul512(x, y);
|
|
93
|
+
return hi > ~lo ? hi + 2 : hi + 1;
|
|
94
|
+
// General:
|
|
95
|
+
// - find the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`
|
|
96
|
+
// - the value of `x * y` is represented via `2 ^ 256 * hi + lo`
|
|
97
|
+
// - the expression `~lo` is equivalent to `2 ^ 256 - 1 - lo`
|
|
98
|
+
//
|
|
99
|
+
// Safety:
|
|
100
|
+
// - if `x < 2 ^ 256 - 1` or `y < 2 ^ 256 - 1`
|
|
101
|
+
// then `hi < 2 ^ 256 - 2`
|
|
102
|
+
// hence neither `hi + 1` nor `hi + 2` overflows
|
|
103
|
+
// - if `x = 2 ^ 256 - 1` and `y = 2 ^ 256 - 1`
|
|
104
|
+
// then `hi = 2 ^ 256 - 2 = ~lo`
|
|
105
|
+
// hence `hi + 1`, which does not overflow, is computed
|
|
106
|
+
//
|
|
107
|
+
// Symbols:
|
|
108
|
+
// - let `H` denote `hi`
|
|
109
|
+
// - let `L` denote `lo`
|
|
110
|
+
// - let `N` denote `2 ^ 256 - 1`
|
|
111
|
+
//
|
|
112
|
+
// Inference:
|
|
113
|
+
// `x * y / z <= 2 ^ 256 - 1` <-->
|
|
114
|
+
// `x * y / (2 ^ 256 - 1) <= z` <-->
|
|
115
|
+
// `((N + 1) * H + L) / N <= z` <-->
|
|
116
|
+
// `(N * H + H + L) / N <= z` <-->
|
|
117
|
+
// `H + (H + L) / N <= z`
|
|
118
|
+
//
|
|
119
|
+
// Inference:
|
|
120
|
+
// `0 <= H <= N && 0 <= L <= N` <-->
|
|
121
|
+
// `0 <= H + L <= N + N` <-->
|
|
122
|
+
// `0 <= H + L <= N * 2` <-->
|
|
123
|
+
// `0 <= (H + L) / N <= 2`
|
|
124
|
+
//
|
|
125
|
+
// Inference:
|
|
126
|
+
// - `0 = (H + L) / N` --> `H + L = 0` --> `x * y = 0` --> `z = 1 = H + 1`
|
|
127
|
+
// - `0 < (H + L) / N <= 1` --> `H + (H + L) / N <= H + 1` --> `z = H + 1`
|
|
128
|
+
// - `1 < (H + L) / N <= 2` --> `H + (H + L) / N <= H + 2` --> `z = H + 2`
|
|
129
|
+
//
|
|
130
|
+
// Implementation:
|
|
131
|
+
// - if `hi > ~lo`:
|
|
132
|
+
// `~L < H <= N` <-->
|
|
133
|
+
// `N - L < H <= N` <-->
|
|
134
|
+
// `N < H + L <= N + L` <-->
|
|
135
|
+
// `1 < (H + L) / N <= 2` <-->
|
|
136
|
+
// `H + 1 < H + (H + L) / N <= H + 2` <-->
|
|
137
|
+
// `z = H + 2`
|
|
138
|
+
// - if `hi <= ~lo`:
|
|
139
|
+
// `H <= ~L` <-->
|
|
140
|
+
// `H <= N - L` <-->
|
|
141
|
+
// `H + L <= N` <-->
|
|
142
|
+
// `(H + L) / N <= 1` <-->
|
|
143
|
+
// `H + (H + L) / N <= H + 1` <-->
|
|
144
|
+
// `z = H + 1`
|
|
145
|
+
}}
|
|
146
|
+
|
|
88
147
|
/**
|
|
89
148
|
* @dev Compute the largest integer smaller than or equal to `x * y / z`
|
|
90
149
|
*/
|
|
@@ -94,7 +153,7 @@ library IntegralMath {
|
|
|
94
153
|
return xyl / z;
|
|
95
154
|
}
|
|
96
155
|
if (xyh < z) { // `x * y / z < 2 ^ 256`
|
|
97
|
-
uint256 m =
|
|
156
|
+
uint256 m = mulmod(x, y, z); // `m = x * y % z`
|
|
98
157
|
(uint256 nh, uint256 nl) = sub512(xyh, xyl, m); // `n = x * y - m` hence `n / z = floor(x * y / z)`
|
|
99
158
|
if (nh == 0) { // `n < 2 ^ 256`
|
|
100
159
|
return nl / z;
|
|
@@ -112,7 +171,17 @@ library IntegralMath {
|
|
|
112
171
|
*/
|
|
113
172
|
function mulDivC(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {
|
|
114
173
|
uint256 w = mulDivF(x, y, z);
|
|
115
|
-
if (
|
|
174
|
+
if (mulmod(x, y, z) > 0)
|
|
175
|
+
return safeAdd(w, 1);
|
|
176
|
+
return w;
|
|
177
|
+
}}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @dev Compute the nearest integer smaller than or larger than `x * y / z`
|
|
181
|
+
*/
|
|
182
|
+
function mulDivR(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {
|
|
183
|
+
uint256 w = mulDivF(x, y, z);
|
|
184
|
+
if (mulmod(x, y, z) > (z - 1) / 2)
|
|
116
185
|
return safeAdd(w, 1);
|
|
117
186
|
return w;
|
|
118
187
|
}}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.28;
|
|
3
3
|
|
|
4
4
|
uint256 constant MAX_VAL = type(uint256).max;
|
|
5
5
|
|
|
@@ -27,8 +27,3 @@ function unsafeMul(uint256 x, uint256 y) pure returns (uint256) { unchecked {
|
|
|
27
27
|
function mulModMax(uint256 x, uint256 y) pure returns (uint256) { unchecked {
|
|
28
28
|
return mulmod(x, y, MAX_VAL);
|
|
29
29
|
}}
|
|
30
|
-
|
|
31
|
-
// does not overflow
|
|
32
|
-
function mulMod(uint256 x, uint256 y, uint256 z) pure returns (uint256) { unchecked {
|
|
33
|
-
return mulmod(x, y, z);
|
|
34
|
-
}}
|
package/readme/AdvancedMath.pdf
DELETED
|
Binary file
|
package/readme/AdvancedMath.txt
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
The solution to:
|
|
2
|
-
|
|
3
|
-
$$x\cdot\big(\frac{a}{b}\big)^x=\frac{c}{d}$$
|
|
4
|
-
|
|
5
|
-
Can be computed via:
|
|
6
|
-
|
|
7
|
-
$$x=\frac{W\Big(\log\big(\frac{a}{b}\big)\cdot\frac{c}{d}\Big)}{log\big(\frac{a}{b}\big)}$$
|
|
8
|
-
|
|
9
|
-
Where $W$ is the Lambert W Function.
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
In order to approximate this solution, we split the input domain $z=\log\big(\frac{a}{b}\big)\cdot\frac{c}{d}$ into:
|
|
14
|
-
|
|
15
|
-
$$\underbrace{-\infty\ldots-1/e}\bigg|\underbrace{-1/e\ldots0}\bigg|\underbrace{0\ldots+1/e}\bigg|\underbrace{+1/e\ldots3+1/e}\bigg|\underbrace{3+1/e\ldots+\infty}$$
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
For $z<-1/e$, the value of $W(z)$ is not real.
|
|
20
|
-
|
|
21
|
-
Respectively, the equation $x\cdot\big(\frac{a}{b}\big)^x=\frac{c}{d}$ has no real solution.
|
|
22
|
-
|
|
23
|
-
This is because $x\cdot\big(\frac{a}{b}\big)^x\leq\frac{1}{e\cdot\log\big(\frac{b}{a}\big)}<\frac{c}{d}$ for every real value of $x$.
|
|
24
|
-
|
|
25
|
-
---
|
|
26
|
-
|
|
27
|
-
For $-1/e\leq{z}\leq+1/e$, you may observe that $x=\frac{W\Big(\log\big(\frac{a}{b}\big)\cdot\frac{c}{d}\Big)}{log\big(\frac{a}{b}\big)}=\frac{c}{d}\cdot\sum\limits_{n=1}^{\infty}\frac{(-n)^{n-1}}{n!}\cdot\Big(\log\big(\frac{a}{b}\big)\cdot\frac{c}{d}\Big)^{n-1}$:
|
|
28
|
-
|
|
29
|
-
- For $-1/e\leq{z}\leq0$, which implies that ${a}\leq{b}$, we compute $x=\frac{c}{d}\cdot\sum\limits_{n=1}^{\infty}\frac{(+n)^{n-1}}{n!}\cdot\Big(\log\big(\frac{b}{a}\big)\cdot\frac{c}{d}\Big)^{n-1}$
|
|
30
|
-
- For $0\leq{z}\leq+1/e$, which implies that ${a}\geq{b}$, we compute $x=\frac{c}{d}\cdot\sum\limits_{n=1}^{\infty}\frac{(-n)^{n-1}}{n!}\cdot\Big(\log\big(\frac{a}{b}\big)\cdot\frac{c}{d}\Big)^{n-1}$
|
|
31
|
-
|
|
32
|
-
As you can see, when $a=b$, both formulas can be reduced to $x=\frac{c}{d}$.
|
|
33
|
-
|
|
34
|
-
---
|
|
35
|
-
|
|
36
|
-
For $+1/e<z<3+1/e$, we use a lookup table which maps $128$ uniformly distributed values of $z$.
|
|
37
|
-
|
|
38
|
-
Then, we calculate $W(z')$ as the weighted-average of $W(z_0)$ and $W(z_1)$, where $z_0\leq{z'}<z_1$.
|
|
39
|
-
|
|
40
|
-
---
|
|
41
|
-
|
|
42
|
-
For $z\geq3+1/e$, we rely on the fact that $W(z)\approx\log(z)-\log(\log(z))+\log(\log(z))/\log(z)$.
|
|
43
|
-
|
|
44
|
-
Of course, $z$ is ultimately restricted by the maximum input supported in our $\log$ implementation.
|
package/readme/DynamicCurve.pdf
DELETED
|
Binary file
|
package/readme/DynamicCurve.txt
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
**Conversion Formula:**
|
|
2
|
-
---
|
|
3
|
-
|
|
4
|
-
Consider a liquidity pool with two reserve tokens S and R of equal weights, where:
|
|
5
|
-
|
|
6
|
-
- $s=$ balance of token S
|
|
7
|
-
- $r=$ balance of token R
|
|
8
|
-
|
|
9
|
-
Converting an amount of S tokens into R tokens is based on $y=r\cdot\big(1−\frac{s}{s+x}\big)$, where:
|
|
10
|
-
|
|
11
|
-
- $x=$ input amount of token S
|
|
12
|
-
- $y=$ output amount of token R
|
|
13
|
-
|
|
14
|
-
The conversion formula for any type of weighted-pool is $y=r\cdot\Big(1−\big(\frac{s}{s+x}\big)^{w_1/w_2}\Big)$, where:
|
|
15
|
-
|
|
16
|
-
- $w_1=$ weight of token S
|
|
17
|
-
- $w_2=$ weight of token R
|
|
18
|
-
|
|
19
|
-
As you can see, when $w_1=w_2$, the latter formula reduces to the former formula.
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
**Calculating New Weights:**
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
Let F and G denote the primary reserve token and the secondary reserve token respectively.
|
|
27
|
-
|
|
28
|
-
Let 'on-chain price' denote the conversion rate between F and G inside the pool (i.e., as determined by the pool).
|
|
29
|
-
|
|
30
|
-
Let 'off-chain price' denote the conversion rate between F and G outside the pool (i.e., as determined by the market).
|
|
31
|
-
|
|
32
|
-
Let the following denote:
|
|
33
|
-
|
|
34
|
-
- $t=$ F staked balance of the pool
|
|
35
|
-
- $s=$ F reserve balance of the pool
|
|
36
|
-
- $r=$ G reserve balance of the pool
|
|
37
|
-
- $q=$ G/F off-chain price numerator
|
|
38
|
-
- $p=$ G/F off-chain price denominator
|
|
39
|
-
|
|
40
|
-
Where $1$ unit of F is equal to $q/p$ units of G (or $p$ units of F are equal to $q$ units of G).
|
|
41
|
-
|
|
42
|
-
First, note that the market’s arbitrage incentive is always to convert units of F to units of G or vice-versa, such that the on-chain price of F/G will become equal to the off-chain price of F/G.
|
|
43
|
-
|
|
44
|
-
Consider the case of $t>s$. Our goal is to set the weights of the pool, such that the arbitrage incentive of equalizing the on-chain price and the off-chain price will subsequently increase $s$ to become equal to $t$. In other words, we want the arbitrager to transfer $t−s$ units of F to the pool, in exchange for units of G.
|
|
45
|
-
|
|
46
|
-
Suppose that we’ve set the weights:
|
|
47
|
-
- $w_1=$ F reserve weight
|
|
48
|
-
- $w_2=$ G reserve weight
|
|
49
|
-
|
|
50
|
-
Then:
|
|
51
|
-
|
|
52
|
-
- A user converting $t−s$ units of F will get $r\cdot\Big(1−\big(\frac{s}{t}\big)^{w_1/w_2}\Big)$ units of G
|
|
53
|
-
- F reserve balance after the arbitrage conversion will be $t$ of course
|
|
54
|
-
- G reserve balance after the arbitrage conversion will be $r-r\cdot\Big(1−\big(\frac{s}{t}\big)^{w_1/w_2}\Big)$
|
|
55
|
-
- F/G on-chain price after the arbitrage conversion will be $\large\frac{t\cdot{w_2}/{w_1}}{r-r\cdot\Big(1−\big(\frac{s}{t}\big)^{w_1/w_2}\Big)}$
|
|
56
|
-
- F/G off-chain price is of course $\frac{p}{q}$ (or $\frac{q}{p}$ if the inverse rates are provided)
|
|
57
|
-
|
|
58
|
-
When either $t$ or $p/q$ change, we want to recalculate $w_1$ and $w_2$ such that the arbitrage incentive of making the on-chain price equal to the off-chain price will be equivalent to converting $t−s$ units of F to units of G, thus increasing F reserve balance ($s$) to be equal to F staked balance ($t$).
|
|
59
|
-
|
|
60
|
-
In other words, we want to recalculate $w_1$ and $w_2$ such that $\large\frac{t\cdot{w_2}/{w_1}}{r-r\cdot\Big(1−\big(\frac{s}{t}\big)^{w_1/w_2}\Big)}=\frac{p}{q}$.
|
|
61
|
-
|
|
62
|
-
Let $x$ denote $w_1/w_2$, then:
|
|
63
|
-
|
|
64
|
-
$\large\frac{t/x}{r-r\cdot\Big(1−\big(\frac{s}{t}\big)^{x}\Big)}=\frac{p}{q}\rightarrow$
|
|
65
|
-
|
|
66
|
-
$\large\frac{t/x}{r\cdot\big(\frac{s}{t}\big)^{x}}=\frac{p}{q}\rightarrow$
|
|
67
|
-
|
|
68
|
-
$\large{x}\cdot\big(\frac{s}{t}\big)^{x}=\frac{tq}{rp}\rightarrow$
|
|
69
|
-
|
|
70
|
-
$\large{x}=\frac{W\Big(\log\big(\frac{s}{t}\big)\cdot\frac{tq}{rp}\Big)}{log\big(\frac{s}{t}\big)}$, where W is the Lambert W Function.
|
|
71
|
-
|
|
72
|
-
After computing $x$, we can represent it as a quotient of integers, i.e., $x=a/b$.
|
|
73
|
-
|
|
74
|
-
Then, since $x=w_1/w_2$ and $w_2=1-w_1$, we can calculate:
|
|
75
|
-
|
|
76
|
-
- $\large{w_1}=\frac{x}{x+1}=\frac{a/b}{1+a/b}=\frac{a}{a+b}$
|
|
77
|
-
- $\large{w_2}=\frac{1}{x+1}=\frac{1}{1+a/b}=\frac{b}{a+b}$
|