solidity-math-utils 1.11.0 → 1.12.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 +2 -0
- package/package.json +3 -3
- package/project/contracts/AdvancedMath.sol +1 -1
- package/project/contracts/AnalyticMath.sol +1 -1
- package/project/contracts/BondingCurve.sol +1 -1
- package/project/contracts/DynamicCurve.sol +1 -1
- package/project/contracts/FractionMath.sol +4 -7
- package/project/contracts/IntegralMath.sol +60 -1
- package/project/contracts/common/Uint.sol +1 -1
- package/readme/AdvancedMath.pdf +0 -0
- package/readme/AdvancedMath.txt +0 -44
- package/readme/DynamicCurve.pdf +0 -0
- package/readme/DynamicCurve.txt +0 -77
package/README.md
CHANGED
|
@@ -61,6 +61,8 @@ Function `ceilCbrt(n)` computes the smallest integer larger than or equal to the
|
|
|
61
61
|
|
|
62
62
|
Function `roundDiv(n, d)` computes the nearest integer to the quotient of `n` and `d` (or `n / d`).
|
|
63
63
|
|
|
64
|
+
Function `minFactor(x, y)` computes the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`.
|
|
65
|
+
|
|
64
66
|
Function `mulDivF(x, y, z)` computes the largest integer smaller than or equal to `x * y / z`.
|
|
65
67
|
|
|
66
68
|
Function `mulDivC(x, y, z)` computes the smallest integer larger than or equal to `x * y / z`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solidity-math-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "Solidity Mathematical Utilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@nomiclabs/hardhat-truffle5": "2.0.7",
|
|
22
22
|
"@nomiclabs/hardhat-web3": "2.0.0",
|
|
23
23
|
"decimal.js": "10.4.3",
|
|
24
|
-
"hardhat": "2.
|
|
25
|
-
"solidity-coverage": "0.8.
|
|
24
|
+
"hardhat": "2.22.8",
|
|
25
|
+
"solidity-coverage": "0.8.12"
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.26;
|
|
3
3
|
|
|
4
4
|
import "./IntegralMath.sol";
|
|
5
5
|
|
|
@@ -56,13 +56,10 @@ library FractionMath {
|
|
|
56
56
|
* @return The product ratio denominator
|
|
57
57
|
*/
|
|
58
58
|
function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd) internal pure returns (uint256, uint256) { unchecked {
|
|
59
|
-
uint256 n = IntegralMath.
|
|
60
|
-
uint256 d = IntegralMath.
|
|
59
|
+
uint256 n = IntegralMath.minFactor(xn, yn);
|
|
60
|
+
uint256 d = IntegralMath.minFactor(xd, yd);
|
|
61
61
|
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);
|
|
62
|
+
return (IntegralMath.mulDivC(xn, yn, z), IntegralMath.mulDivC(xd, yd, z));
|
|
66
63
|
}}
|
|
67
64
|
|
|
68
65
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.26;
|
|
3
3
|
|
|
4
4
|
import "./common/Uint.sol";
|
|
5
5
|
|
|
@@ -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
|
*/
|
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}$
|