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
package/README.md
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
This package consists of the following modules:
|
|
4
4
|
- [IntegralMath](#integralmath) - a set of functions, each of which returning an integer result
|
|
5
5
|
- [FractionMath](#fractionmath) - a set of functions, each of which returning a rational result
|
|
6
|
-
- [AnalyticMath](#analyticmath) - a set of exponential and logarithmic
|
|
7
|
-
- [AdvancedMath](#advancedmath) - a
|
|
6
|
+
- [AnalyticMath](#analyticmath) - a set of functions for exponential and logarithmic operations
|
|
7
|
+
- [AdvancedMath](#advancedmath) - a set of functions for solving equations of the form xA^x = B
|
|
8
8
|
- [BondingCurve](#bondingcurve) - a set of functions implementing the bonding-curve mechanism
|
|
9
|
-
- [DynamicCurve](#dynamiccurve) - a
|
|
9
|
+
- [DynamicCurve](#dynamiccurve) - a set of functions for equalizing the weights in a bonding-curve model
|
|
10
10
|
|
|
11
11
|
### Class Hierarchy
|
|
12
12
|
```
|
|
@@ -23,12 +23,6 @@ AnalyticMath < - - - - AdvancedMath
|
|
|
23
23
|
BondingCurve DynamicCurve
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
Note that some of these modules are implemented as `library`, while others are implemented as `contract`.
|
|
27
|
-
|
|
28
|
-
Nevertheless, they can all be regarded as *libraries*, since the reason for this is purely technical.
|
|
29
|
-
|
|
30
|
-
A library cannot utilize non-constant state variables, nor can it extend (inherit) another library.
|
|
31
|
-
|
|
32
26
|
<br/><br/>
|
|
33
27
|
|
|
34
28
|
---
|
|
@@ -46,6 +40,7 @@ This module implements the following interface:
|
|
|
46
40
|
- `function roundDiv(uint256 n, uint256 d)` => `(uint256)`
|
|
47
41
|
- `function mulDivF(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
48
42
|
- `function mulDivC(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
43
|
+
- `function mulDivR(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
49
44
|
- `function mulDivExF(uint256 x, uint256 y, uint256 z, uint256 w)` => `(uint256)`
|
|
50
45
|
- `function mulDivExC(uint256 x, uint256 y, uint256 z, uint256 w)` => `(uint256)`
|
|
51
46
|
|
|
@@ -61,17 +56,19 @@ Function `ceilCbrt(n)` computes the smallest integer larger than or equal to the
|
|
|
61
56
|
|
|
62
57
|
Function `roundDiv(n, d)` computes the nearest integer to the quotient of `n` and `d` (or `n / d`).
|
|
63
58
|
|
|
59
|
+
Function `minFactor(x, y)` computes the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`.
|
|
60
|
+
|
|
64
61
|
Function `mulDivF(x, y, z)` computes the largest integer smaller than or equal to `x * y / z`.
|
|
65
62
|
|
|
66
63
|
Function `mulDivC(x, y, z)` computes the smallest integer larger than or equal to `x * y / z`.
|
|
67
64
|
|
|
65
|
+
Function `mulDivR(x, y, z)` computes the nearest integer smaller than or larger than `x * y / z`.
|
|
66
|
+
|
|
68
67
|
Function `mulDivExF(x, y, z, w)` computes the largest integer smaller than or equal to `(x * y) / (z * w)`.
|
|
69
68
|
|
|
70
69
|
Function `mulDivExC(x, y, z, w)` computes the smallest integer larger than or equal to `(x * y) / (z * w)`.
|
|
71
70
|
|
|
72
|
-
Note that
|
|
73
|
-
|
|
74
|
-
Note that function `mulDivExF` and function `mulDivExC` revert when the **actual** result is larger than 256 bits.
|
|
71
|
+
Note that each one of the 'mulDiv' functions reverts when the **actual** result is larger than 256 bits.
|
|
75
72
|
|
|
76
73
|
Note that function `floorSqrt` and function `ceilSqrt` are guaranteed to return the correct output for every input.
|
|
77
74
|
|
|
@@ -94,22 +91,23 @@ For example, `floorCbrt(7)` returns 1, but the actual cubic root of 7 is ~1.91,
|
|
|
94
91
|
## FractionMath
|
|
95
92
|
|
|
96
93
|
This module implements the following interface:
|
|
97
|
-
- `function
|
|
94
|
+
- `function poweredRatioExact(uint256 n, uint256 d, uint256 exp)` => `(uint256, uint256)`
|
|
95
|
+
- `function poweredRatioQuick(uint256 n, uint256 d, uint256 exp)` => `(uint256, uint256)`
|
|
98
96
|
- `function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd)` => `(uint256, uint256)`
|
|
99
97
|
- `function reducedRatio(uint256 n, uint256 d, uint256 max)` => `(uint256, uint256)`
|
|
100
98
|
- `function normalizedRatio(uint256 n, uint256 d, uint256 scale)` => `(uint256, uint256)`
|
|
101
99
|
|
|
102
100
|
### Powered Ratio
|
|
103
101
|
|
|
104
|
-
Function `
|
|
102
|
+
Function `poweredRatioExact` opts for accuracy and function `poweredRatioQuick` opts for performance.
|
|
103
|
+
|
|
104
|
+
Each one of the two 'poweredRatio' functions computes the power of a given ratio by a given exponent.
|
|
105
105
|
|
|
106
106
|
In order to avoid multiplication overflow, it may truncate the intermediate result on each iteration.
|
|
107
107
|
|
|
108
108
|
Subsequently, the larger the input exponent is, the lower the accuracy of the output is likely to be.
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
This library defines a maximum exponent of 4 bits (i.e., 15), which can be customized to fit the system requirements.
|
|
110
|
+
This module defines a maximum exponent of 4 bits (i.e., 15), which can be customized to fit the system requirements.
|
|
113
111
|
|
|
114
112
|
### Product Ratio
|
|
115
113
|
|
|
@@ -119,7 +117,7 @@ If either one of the intermediate components is larger than 256 bits, then both
|
|
|
119
117
|
|
|
120
118
|
### Reduced Ratio
|
|
121
119
|
|
|
122
|
-
Function `reducedRatio` computes the nearest ratio whose components (numerator and denominator)
|
|
120
|
+
Function `reducedRatio` computes the nearest ratio whose components (numerator and denominator) fit up to a given threshold.
|
|
123
121
|
|
|
124
122
|
Note that function `reducedRatio` is not meant to replace GCD, nor does it strive to achieve better accuracy.
|
|
125
123
|
|
|
@@ -135,13 +133,13 @@ However, without knowing specific characteristics of that ratio (e.g., each one
|
|
|
135
133
|
|
|
136
134
|
### Normalized Ratio
|
|
137
135
|
|
|
138
|
-
Function `normalizedRatio` computes the nearest ratio whose
|
|
136
|
+
Function `normalizedRatio` computes the nearest ratio whose components (numerator and denominator) sum up to a given scale.
|
|
139
137
|
|
|
140
138
|
Note that the output ratio can be larger than the input ratio in some cases, and smaller than the input ratio in other cases.
|
|
141
139
|
|
|
142
140
|
For example:
|
|
143
|
-
- `normalizedRatio(12, 34, 100)` returns `(26 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
|
|
144
|
-
- `normalizedRatio(1234, 5678, 100)` returns `(18 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
|
|
141
|
+
- `normalizedRatio(12, 34, 100)` returns `(26, 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
|
|
142
|
+
- `normalizedRatio(1234, 5678, 100)` returns `(18, 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
|
|
145
143
|
|
|
146
144
|
Keep in mind that it is an important consideration to take when choosing to use this function.
|
|
147
145
|
|
|
@@ -157,21 +155,11 @@ The same consideration applies for all the other functions in this module.
|
|
|
157
155
|
|
|
158
156
|
## AnalyticMath
|
|
159
157
|
|
|
160
|
-
This module
|
|
161
|
-
|
|
162
|
-
It implements the following interface:
|
|
158
|
+
This module implements the following interface:
|
|
163
159
|
- `function pow(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
|
|
164
160
|
- `function log(uint256 a, uint256 b)` => `(uint256, uint256)`
|
|
165
161
|
- `function exp(uint256 a, uint256 b)` => `(uint256, uint256)`
|
|
166
162
|
|
|
167
|
-
### Initialization
|
|
168
|
-
|
|
169
|
-
Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
|
|
170
|
-
|
|
171
|
-
Function `init` should be executed either during construction or after construction (if too large for the constructor).
|
|
172
|
-
|
|
173
|
-
Thus, any contract which inherits this contract can choose whether or not to call function `init` in its constructor.
|
|
174
|
-
|
|
175
163
|
### Exponentiation
|
|
176
164
|
|
|
177
165
|
Function `pow(a, b, c, d)` approximates the power of `a / b` by `c / d`.
|
|
@@ -194,9 +182,9 @@ Function `exp(a, b)` approximates the natural exponentiation of `a / b`.
|
|
|
194
182
|
|
|
195
183
|
The output of this function is guaranteed to be smaller than or equal to the actual value of exp(a / b).
|
|
196
184
|
|
|
197
|
-
###
|
|
185
|
+
### Module Customization
|
|
198
186
|
|
|
199
|
-
This
|
|
187
|
+
This module can be customized to support different input ranges (as a tradeoff with accuracy and/or performance).
|
|
200
188
|
|
|
201
189
|
The full customization manual can be found [here](#customization).
|
|
202
190
|
|
|
@@ -208,28 +196,21 @@ The full customization manual can be found [here](#customization).
|
|
|
208
196
|
|
|
209
197
|
## AdvancedMath
|
|
210
198
|
|
|
211
|
-
This module
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
- `function solve(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
|
|
215
|
-
|
|
216
|
-
### Initialization
|
|
217
|
-
|
|
218
|
-
Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
|
|
219
|
-
|
|
220
|
-
Function `init` should be executed either during construction or after construction (if too large for the constructor).
|
|
221
|
-
|
|
222
|
-
Thus, any contract which inherits this contract can choose whether or not to call function `init` in its constructor.
|
|
199
|
+
This module implements the following interface:
|
|
200
|
+
- `function solveExact(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
|
|
201
|
+
- `function solveQuick(uint256 a, uint256 b, uint256 c, uint256 d)` => `(uint256, uint256)`
|
|
223
202
|
|
|
224
203
|
### Equation Solving
|
|
225
204
|
|
|
226
|
-
Function `
|
|
205
|
+
Function `solveExact` opts for accuracy and function `solveQuick` opts for performance.
|
|
227
206
|
|
|
228
|
-
|
|
207
|
+
Each one of these functions computes a value of x which satisfies the equation x * (a / b) ^ x = c / d.
|
|
229
208
|
|
|
230
|
-
|
|
209
|
+
A detailed description of how each one of these functions works can be found [here](readme/AdvancedMath.pdf).
|
|
231
210
|
|
|
232
|
-
|
|
211
|
+
### Module Customization
|
|
212
|
+
|
|
213
|
+
This module can be customized to support different input ranges (as a tradeoff with accuracy and/or performance).
|
|
233
214
|
|
|
234
215
|
The full customization manual can be found [here](#customization).
|
|
235
216
|
|
|
@@ -241,9 +222,7 @@ The full customization manual can be found [here](#customization).
|
|
|
241
222
|
|
|
242
223
|
## BondingCurve
|
|
243
224
|
|
|
244
|
-
This module
|
|
245
|
-
|
|
246
|
-
It implements the following interface:
|
|
225
|
+
This module implements the following interface:
|
|
247
226
|
- `function buy(uint256 supply, uint256 balance, uint256 weight, uint256 amount)` => `(uint256)`
|
|
248
227
|
- `function sell(uint256 supply, uint256 balance, uint256 weight, uint256 amount)` => `(uint256)`
|
|
249
228
|
- `function convert(uint256 balance1, uint256 weight1, uint256 balance2, uint256 weight2, uint256 amount)` => `(uint256)`
|
|
@@ -251,14 +230,6 @@ It implements the following interface:
|
|
|
251
230
|
- `function withdraw(uint256 supply, uint256 balance, uint256 weights, uint256 amount)` => `(uint256)`
|
|
252
231
|
- `function invest(uint256 supply, uint256 balance, uint256 weights, uint256 amount)` => `(uint256)`
|
|
253
232
|
|
|
254
|
-
### Initialization
|
|
255
|
-
|
|
256
|
-
Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
|
|
257
|
-
|
|
258
|
-
Function `init` should be executed either during construction or after construction (if too large for the constructor).
|
|
259
|
-
|
|
260
|
-
Thus, any contract which inherits this contract can choose whether or not to call function `init` in its constructor.
|
|
261
|
-
|
|
262
233
|
### Functionality
|
|
263
234
|
|
|
264
235
|
```
|
|
@@ -284,18 +255,11 @@ The bonding-curve model was conceived by [Bancor](https://github.com/bancorproto
|
|
|
284
255
|
|
|
285
256
|
## DynamicCurve
|
|
286
257
|
|
|
287
|
-
This module
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
- `function equalize(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p)` => `(uint256, uint256)`
|
|
291
|
-
|
|
292
|
-
### Initialization
|
|
293
|
-
|
|
294
|
-
Prior to using any of these functions, internal data structure must be initialized by executing function `init`.
|
|
295
|
-
|
|
296
|
-
Function `init` should be executed either during construction or after construction (if too large for the constructor).
|
|
258
|
+
This module implements the following interface:
|
|
259
|
+
- `function equalizeExact(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p)` => `(uint256, uint256)`
|
|
260
|
+
- `function equalizeQuick(uint256 t, uint256 s, uint256 r, uint256 q, uint256 p)` => `(uint256, uint256)`
|
|
297
261
|
|
|
298
|
-
|
|
262
|
+
Function `equalizeExact` opts for accuracy and function `equalizeQuick` opts for performance.
|
|
299
263
|
|
|
300
264
|
### Equalization
|
|
301
265
|
|
|
@@ -353,8 +317,8 @@ The dynamic-curve method was conceived by [Bancor](https://github.com/bancorprot
|
|
|
353
317
|
|
|
354
318
|
### Prerequisites
|
|
355
319
|
|
|
356
|
-
- `node
|
|
357
|
-
- `yarn 1.22.
|
|
320
|
+
- `node 20.17.0`
|
|
321
|
+
- `yarn 1.22.22` or `npm 10.8.2`
|
|
358
322
|
|
|
359
323
|
### Installation
|
|
360
324
|
|
|
@@ -382,11 +346,11 @@ The dynamic-curve method was conceived by [Bancor](https://github.com/bancorprot
|
|
|
382
346
|
|
|
383
347
|
### Prerequisites
|
|
384
348
|
|
|
385
|
-
- `python 3.
|
|
349
|
+
- `python 3.12.3`
|
|
386
350
|
|
|
387
351
|
### Execution
|
|
388
352
|
|
|
389
|
-
In order to allow rapid testing and verification, all modules
|
|
353
|
+
In order to allow rapid testing and verification, all modules have been ported from Solidity to Python:
|
|
390
354
|
- The emulation modules themselves are located under [FixedPoint](project/emulation/FixedPoint)
|
|
391
355
|
- The corresponding floating-point functionality is located under [FloatPoint](project/emulation/FloatPoint)
|
|
392
356
|
- A set of unit-tests for various functions (one per function) is located under [emulation](project/emulation)
|
|
@@ -399,27 +363,23 @@ In order to allow rapid testing and verification, all modules (contracts and lib
|
|
|
399
363
|
|
|
400
364
|
## Customization
|
|
401
365
|
|
|
402
|
-
All customization parameters are located in [constants.py](project/
|
|
366
|
+
All customization parameters are located in [constants.py](project/customization/constants.py).
|
|
403
367
|
|
|
404
368
|
When modifying **any** of them, one should regenerate **all** the code.
|
|
405
369
|
|
|
406
370
|
The following scripts generate the code for [AnalyticMath.sol](project/contracts/AnalyticMath.sol):
|
|
407
|
-
- [
|
|
408
|
-
- [
|
|
409
|
-
- [
|
|
410
|
-
- [PrintFunctionOptimalLog.py](project/emulation/AutoGenerate/PrintFunctionOptimalLog.py)
|
|
411
|
-
- [PrintFunctionOptimalExp.py](project/emulation/AutoGenerate/PrintFunctionOptimalExp.py)
|
|
412
|
-
- [PrintMaxExpArray.py ](project/emulation/AutoGenerate/PrintMaxExpArray.py )
|
|
371
|
+
- [PrintAnalyticMathConstants.py ](project/customization/PrintAnalyticMathConstants.py )
|
|
372
|
+
- [PrintAnalyticMathOptimalLog.py](project/customization/PrintAnalyticMathOptimalLog.py)
|
|
373
|
+
- [PrintAnalyticMathOptimalExp.py](project/customization/PrintAnalyticMathOptimalExp.py)
|
|
413
374
|
|
|
414
375
|
The following scripts generate the code for [AdvancedMath.sol](project/contracts/AdvancedMath.sol):
|
|
415
|
-
- [
|
|
416
|
-
- [
|
|
417
|
-
- [
|
|
418
|
-
- [PrintLambertArray.py ](project/emulation/AutoGenerate/PrintLambertArray.py )
|
|
376
|
+
- [PrintAdvancedMathConstants.py ](project/customization/PrintAdvancedMathConstants.py )
|
|
377
|
+
- [PrintAdvancedMathLambertNeg1.py](project/customization/PrintAdvancedMathLambertNeg1.py)
|
|
378
|
+
- [PrintAdvancedMathLambertPos1.py](project/customization/PrintAdvancedMathLambertPos1.py)
|
|
419
379
|
|
|
420
380
|
In order to retain the [testing infrastructure](#testing), one should proceed by:
|
|
421
|
-
- Running the script [PrintTestConstants.py](project/
|
|
422
|
-
- Pasting the printout into [
|
|
381
|
+
- Running the script [PrintTestConstants.py](project/customization/PrintTestConstants.py)
|
|
382
|
+
- Pasting the printout into [Constants.js](project/tests/helpers/Constants.js)
|
|
423
383
|
|
|
424
384
|
In order to retain the [emulation infrastructure](#emulation), one should proceed by:
|
|
425
385
|
- Porting all changes from [AnalyticMath.sol](project/contracts/AnalyticMath.sol) to [AnalyticMath.py](project/emulation/FixedPoint/AnalyticMath.py)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solidity-math-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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.17",
|
|
25
|
+
"solidity-coverage": "0.8.14"
|
|
26
26
|
}
|
|
27
27
|
}
|