solidity-math-utils 2.4.0 → 3.1.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 +38 -22
- package/package.json +2 -2
- package/project/contracts/AdvancedMath.sol +1 -1
- package/project/contracts/AnalyticMath.sol +3 -3
- package/project/contracts/BondingCurve.sol +102 -86
- package/project/contracts/DynamicCurve.sol +1 -1
- package/project/contracts/FractionMath.sol +1 -1
- package/project/contracts/IntegralMath.sol +14 -6
- package/project/contracts/common/Uint256.sol +1 -16
package/README.md
CHANGED
|
@@ -38,6 +38,9 @@ This module implements the following interface:
|
|
|
38
38
|
- `function floorCbrt(uint256 n)` => `(uint256)`
|
|
39
39
|
- `function ceilCbrt(uint256 n)` => `(uint256)`
|
|
40
40
|
- `function roundDiv(uint256 n, uint256 d)` => `(uint256)`
|
|
41
|
+
- `function minFactor(uint256 x, uint256 y)` => `(uint256)`
|
|
42
|
+
- `function mulShrF(uint256 x, uint256 y, uint8 s)` => `(uint256)`
|
|
43
|
+
- `function mulShrC(uint256 x, uint256 y, uint8 s)` => `(uint256)`
|
|
41
44
|
- `function mulDivF(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
42
45
|
- `function mulDivC(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
43
46
|
- `function mulDivR(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
@@ -98,7 +101,7 @@ This module implements the following interface:
|
|
|
98
101
|
- `function poweredRatioExact(uint256 n, uint256 d, uint256 exp)` => `(uint256, uint256)`
|
|
99
102
|
- `function poweredRatioQuick(uint256 n, uint256 d, uint256 exp)` => `(uint256, uint256)`
|
|
100
103
|
- `function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd)` => `(uint256, uint256)`
|
|
101
|
-
- `function reducedRatio(uint256 n, uint256 d, uint256
|
|
104
|
+
- `function reducedRatio(uint256 n, uint256 d, uint256 cap)` => `(uint256, uint256)`
|
|
102
105
|
- `function normalizedRatio(uint256 n, uint256 d, uint256 scale)` => `(uint256, uint256)`
|
|
103
106
|
|
|
104
107
|
### Powered Ratio
|
|
@@ -127,7 +130,7 @@ Note that function `reducedRatio` is not meant to replace GCD, nor does it striv
|
|
|
127
130
|
|
|
128
131
|
GCD is not being used here, because the time-complexity of this method depends on the bit-length of the input.
|
|
129
132
|
|
|
130
|
-
The worst case is when the two input
|
|
133
|
+
The worst case is when the two input values are consecutive Fibonacci numbers, in the case of `uint256` - F369 and F370, which yield 367 iterations.
|
|
131
134
|
|
|
132
135
|
Moreover, the main issue with using GCD for reducing an arbitrary ratio, is the fact that it doesn't even guarantee the desired reduction to begin with.
|
|
133
136
|
|
|
@@ -210,7 +213,7 @@ Function `solveExact` opts for accuracy and function `solveQuick` opts for perfo
|
|
|
210
213
|
|
|
211
214
|
Each one of these functions computes a value of x which satisfies the equation x * (a / b) ^ x = c / d.
|
|
212
215
|
|
|
213
|
-
A detailed description of how each one of these functions works can be found [here](readme/AdvancedMath.
|
|
216
|
+
A detailed description of how each one of these functions works can be found [here](readme/AdvancedMath.md).
|
|
214
217
|
|
|
215
218
|
### Module Customization
|
|
216
219
|
|
|
@@ -227,26 +230,39 @@ The full customization manual can be found [here](#customization).
|
|
|
227
230
|
## BondingCurve
|
|
228
231
|
|
|
229
232
|
This module implements the following interface:
|
|
230
|
-
- `function
|
|
231
|
-
- `function
|
|
232
|
-
- `function
|
|
233
|
-
- `function
|
|
234
|
-
- `function
|
|
235
|
-
- `function
|
|
233
|
+
- `function mintGain(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
234
|
+
- `function mintCost(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
235
|
+
- `function burnGain(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
236
|
+
- `function burnCost(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
237
|
+
- `function swapGain(uint256 balance1, uint256 balance2, uint256 weight1, uint256 weight2, uint256 amount)` => `(uint256)`
|
|
238
|
+
- `function swapCost(uint256 balance1, uint256 balance2, uint256 weight1, uint256 weight2, uint256 amount)` => `(uint256)`
|
|
236
239
|
|
|
237
|
-
|
|
240
|
+
Function `mintGain` calculates the amount of pool tokens returned in exchange for a specified amount of reserve tokens.
|
|
238
241
|
|
|
242
|
+
Function `mintCost` calculates the amount of reserve tokens required in exchange for a specified amount of pool tokens.
|
|
243
|
+
|
|
244
|
+
Function `burnGain` calculates the amount of reserve tokens returned in exchange for a specified amount of pool tokens.
|
|
245
|
+
|
|
246
|
+
Function `burnCost` calculates the amount of pool tokens required in exchange for a specified amount of reserve tokens.
|
|
247
|
+
|
|
248
|
+
Function `swapGain` calculates the amount of reserve2 tokens returned in exchange for a specified amount of reserve1 tokens.
|
|
249
|
+
|
|
250
|
+
Function `swapCost` calculates the amount of reserve1 tokens required in exchange for a specified amount of reserve2 tokens.
|
|
251
|
+
|
|
252
|
+
### Mathematical Model
|
|
253
|
+
|
|
254
|
+
All functions in this module are based on the constant-product formula for weighted pools:
|
|
239
255
|
```
|
|
240
|
-
|
|
241
|
-
| Function
|
|
242
|
-
|
|
243
|
-
|
|
|
244
|
-
|
|
|
245
|
-
|
|
|
246
|
-
|
|
|
247
|
-
|
|
|
248
|
-
|
|
|
249
|
-
|
|
256
|
+
+-----------------------------+----------------------------------------+-------------------------------------+
|
|
257
|
+
| Function | Formula | Note |
|
|
258
|
+
+-----------------------------+----------------------------------------+-------------------------------------+
|
|
259
|
+
| mintGain(s, b, wn, wd, x) | s * ((1 + x / b) ^ (wn / wd) - 1) | never overestimates the true result |
|
|
260
|
+
| mintCost(s, b, wn, wd, x) | b * ((1 + x / s) ^ (wd / wn) - 1) | might underestimate the true result |
|
|
261
|
+
| burnGain(s, b, wn, wd, x) | b * (1 - (1 - x / s) ^ (wd / wn)) | never overestimates the true result |
|
|
262
|
+
| burnCost(s, b, wn, wd, x) | s * (1 - (1 - x / b) ^ (wn / wd)) | might underestimate the true result |
|
|
263
|
+
| swapGain(b1, b2, w1, w2, x) | b2 * (1 - (b1 / (b1 + x)) ^ (w1 / w2)) | never overestimates the true result |
|
|
264
|
+
| swapCost(b1, b2, w1, w2, x) | b1 * ((b2 / (b2 - x)) ^ (w2 / w1) - 1) | might underestimate the true result |
|
|
265
|
+
+-----------------------------+----------------------------------------+-------------------------------------+
|
|
250
266
|
```
|
|
251
267
|
|
|
252
268
|
The bonding-curve model was conceived by [Bancor](https://github.com/bancorprotocol).
|
|
@@ -295,7 +311,7 @@ Output:
|
|
|
295
311
|
- Return x / (x + 1) as the weight of the primary reserve token
|
|
296
312
|
- Return 1 / (x + 1) as the weight of the secondary reserve token
|
|
297
313
|
|
|
298
|
-
A detailed reasoning of this method can be found [here](readme/DynamicCurve.
|
|
314
|
+
A detailed reasoning of this method can be found [here](readme/DynamicCurve.md).
|
|
299
315
|
|
|
300
316
|
If the rate-provider provides the rates for a common unit, for example:
|
|
301
317
|
- P = 2 ==> 2 primary reserve tokens = 1 ether
|
|
@@ -322,7 +338,7 @@ The dynamic-curve method was conceived by [Bancor](https://github.com/bancorprot
|
|
|
322
338
|
### Prerequisites
|
|
323
339
|
|
|
324
340
|
- `node 22.21.0`
|
|
325
|
-
- `pnpm
|
|
341
|
+
- `pnpm 12.23.0`
|
|
326
342
|
|
|
327
343
|
### Installation
|
|
328
344
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solidity-math-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Solidity Mathematical Utilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@nomiclabs/hardhat-truffle5": "2.1.0",
|
|
22
22
|
"@nomiclabs/hardhat-web3": "2.1.0",
|
|
23
23
|
"decimal.js": "10.6.0",
|
|
24
|
-
"hardhat": "2.
|
|
24
|
+
"hardhat": "2.30.0",
|
|
25
25
|
"solidity-coverage": "0.8.17"
|
|
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.39;
|
|
3
3
|
|
|
4
4
|
import "./IntegralMath.sol";
|
|
5
5
|
|
|
@@ -57,8 +57,8 @@ library AnalyticMath {
|
|
|
57
57
|
* log(x) =
|
|
58
58
|
* log(x) + log(2 ^ k) - log(2 ^ k) =
|
|
59
59
|
* log(x) - log(2 ^ k) + log(2 ^ k) =
|
|
60
|
-
* log(x /
|
|
61
|
-
* log(x /
|
|
60
|
+
* log(x / 2 ^ k) + log(2 ^ k) =
|
|
61
|
+
* log(x / 2 ^ k) + k * log(2)
|
|
62
62
|
*/
|
|
63
63
|
function fixedLog(uint256 x) internal pure returns (uint256) { unchecked {
|
|
64
64
|
if (x < FIXED_1)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.39;
|
|
3
3
|
|
|
4
4
|
import "./AnalyticMath.sol";
|
|
5
5
|
import "./IntegralMath.sol";
|
|
@@ -7,143 +7,159 @@ import "./IntegralMath.sol";
|
|
|
7
7
|
library BondingCurve {
|
|
8
8
|
error InvalidInput();
|
|
9
9
|
error WeightOutOfBound();
|
|
10
|
-
error
|
|
11
|
-
error AmountLargerThanSupply();
|
|
12
|
-
|
|
13
|
-
uint256 internal constant MAX_WEIGHT = 1000000;
|
|
10
|
+
error AmountOutOfBound();
|
|
14
11
|
|
|
15
12
|
/**
|
|
16
|
-
* @dev
|
|
13
|
+
* @dev Calculate the amount of pool tokens returned in exchange for a specified amount of reserve tokens
|
|
17
14
|
*
|
|
18
15
|
* @param supply The total amount of pool tokens
|
|
19
16
|
* @param balance The amount of reserve tokens owned by the pool
|
|
20
|
-
* @param weight The weight of
|
|
17
|
+
* @param weight The weight of this reserve in the pool
|
|
18
|
+
* @param weights The weight of all reserves in the pool
|
|
21
19
|
* @param amount The amount of reserve tokens provided
|
|
22
20
|
*
|
|
23
|
-
* @return supply * ((1 + amount / balance) ^ (weight /
|
|
21
|
+
* @return supply * ((1 + amount / balance) ^ (weight / weights) - 1)
|
|
22
|
+
*
|
|
23
|
+
* @notice This function never overestimates the true result
|
|
24
24
|
*/
|
|
25
|
-
function
|
|
26
|
-
require(supply > 0 && balance > 0 && weight > 0, InvalidInput());
|
|
27
|
-
require(weight <=
|
|
25
|
+
function mintGain(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
26
|
+
require(supply > 0 && balance > 0 && weight > 0 && weights > 0, InvalidInput());
|
|
27
|
+
require(weight <= weights, WeightOutOfBound());
|
|
28
|
+
require(amount <= ~balance, AmountOutOfBound());
|
|
28
29
|
|
|
29
|
-
if (weight ==
|
|
30
|
-
return IntegralMath.mulDivF(
|
|
30
|
+
if (weight == weights)
|
|
31
|
+
return IntegralMath.mulDivF(supply, amount, balance);
|
|
31
32
|
|
|
32
|
-
(uint256 n, uint256 d) = AnalyticMath.pow(
|
|
33
|
-
return IntegralMath.mulDivF(supply, n, d)
|
|
33
|
+
(uint256 n, uint256 d) = AnalyticMath.pow(balance + amount, balance, weight, weights);
|
|
34
|
+
return IntegralMath.mulDivF(supply, n - d, d);
|
|
34
35
|
}}
|
|
35
36
|
|
|
36
37
|
/**
|
|
37
|
-
* @dev
|
|
38
|
+
* @dev Calculate the amount of reserve tokens required in exchange for a specified amount of pool tokens
|
|
38
39
|
*
|
|
39
40
|
* @param supply The total amount of pool tokens
|
|
40
41
|
* @param balance The amount of reserve tokens owned by the pool
|
|
41
|
-
* @param weight The weight of
|
|
42
|
-
* @param
|
|
42
|
+
* @param weight The weight of this reserve in the pool
|
|
43
|
+
* @param weights The weight of all reserves in the pool
|
|
44
|
+
* @param amount The amount of pool tokens desired
|
|
45
|
+
*
|
|
46
|
+
* @return balance * ((1 + amount / supply) ^ (weights / weight) - 1)
|
|
43
47
|
*
|
|
44
|
-
* @
|
|
48
|
+
* @notice This function might underestimate the true result
|
|
45
49
|
*/
|
|
46
|
-
function
|
|
47
|
-
require(supply > 0 && balance > 0 && weight > 0, InvalidInput());
|
|
48
|
-
require(weight <=
|
|
49
|
-
require(amount <= supply,
|
|
50
|
+
function mintCost(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
51
|
+
require(supply > 0 && balance > 0 && weight > 0 && weights > 0, InvalidInput());
|
|
52
|
+
require(weight <= weights, WeightOutOfBound());
|
|
53
|
+
require(amount <= ~supply, AmountOutOfBound());
|
|
50
54
|
|
|
51
|
-
if (
|
|
52
|
-
return balance;
|
|
55
|
+
if (weight == weights)
|
|
56
|
+
return IntegralMath.mulDivC(balance, amount, supply);
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
(uint256 n, uint256 d) = AnalyticMath.pow(supply, supply - amount, MAX_WEIGHT, weight);
|
|
58
|
-
return IntegralMath.mulDivF(balance, n - d, n);
|
|
58
|
+
(uint256 n, uint256 d) = AnalyticMath.pow(supply + amount, supply, weights, weight);
|
|
59
|
+
return IntegralMath.mulDivC(balance, n - d, d);
|
|
59
60
|
}}
|
|
60
61
|
|
|
61
62
|
/**
|
|
62
|
-
* @dev
|
|
63
|
+
* @dev Calculate the amount of reserve tokens returned in exchange for a specified amount of pool tokens
|
|
64
|
+
*
|
|
65
|
+
* @param supply The total amount of pool tokens
|
|
66
|
+
* @param balance The amount of reserve tokens owned by the pool
|
|
67
|
+
* @param weight The weight of this reserve in the pool
|
|
68
|
+
* @param weights The weight of all reserves in the pool
|
|
69
|
+
* @param amount The amount of pool tokens provided
|
|
63
70
|
*
|
|
64
|
-
* @
|
|
65
|
-
* @param weight1 The weight of the source reserve (represented in ppm)
|
|
66
|
-
* @param balance2 The amount of target reserve tokens owned by the pool
|
|
67
|
-
* @param weight2 The weight of the target reserve (represented in ppm)
|
|
68
|
-
* @param amount The amount of source reserve tokens provided
|
|
71
|
+
* @return balance * (1 - (1 - amount / supply) ^ (weights / weight))
|
|
69
72
|
*
|
|
70
|
-
* @
|
|
73
|
+
* @notice This function never overestimates the true result
|
|
71
74
|
*/
|
|
72
|
-
function
|
|
73
|
-
require(
|
|
74
|
-
require(
|
|
75
|
+
function burnGain(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
76
|
+
require(supply > 0 && balance > 0 && weight > 0 && weights > 0, InvalidInput());
|
|
77
|
+
require(weight <= weights, WeightOutOfBound());
|
|
78
|
+
require(amount <= supply, AmountOutOfBound());
|
|
75
79
|
|
|
76
|
-
if (
|
|
77
|
-
return
|
|
80
|
+
if (amount == supply)
|
|
81
|
+
return balance;
|
|
82
|
+
|
|
83
|
+
if (weight == weights)
|
|
84
|
+
return IntegralMath.mulDivF(balance, amount, supply);
|
|
78
85
|
|
|
79
|
-
(uint256 n, uint256 d) = AnalyticMath.pow(
|
|
80
|
-
return IntegralMath.mulDivF(
|
|
86
|
+
(uint256 n, uint256 d) = AnalyticMath.pow(supply - amount, supply, weights, weight);
|
|
87
|
+
return IntegralMath.mulDivF(balance, d - n, d);
|
|
81
88
|
}}
|
|
82
89
|
|
|
83
90
|
/**
|
|
84
|
-
* @dev
|
|
91
|
+
* @dev Calculate the amount of pool tokens required in exchange for a specified amount of reserve tokens
|
|
85
92
|
*
|
|
86
93
|
* @param supply The total amount of pool tokens
|
|
87
|
-
* @param balance The amount of reserve tokens
|
|
88
|
-
* @param
|
|
89
|
-
* @param
|
|
94
|
+
* @param balance The amount of reserve tokens owned by the pool
|
|
95
|
+
* @param weight The weight of this reserve in the pool
|
|
96
|
+
* @param weights The weight of all reserves in the pool
|
|
97
|
+
* @param amount The amount of reserve tokens desired
|
|
98
|
+
*
|
|
99
|
+
* @return supply * (1 - (1 - amount / balance) ^ (weight / weights))
|
|
90
100
|
*
|
|
91
|
-
* @
|
|
101
|
+
* @notice This function might underestimate the true result
|
|
92
102
|
*/
|
|
93
|
-
function
|
|
94
|
-
require(supply > 0 && balance > 0 && weights > 0, InvalidInput());
|
|
95
|
-
require(
|
|
103
|
+
function burnCost(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
104
|
+
require(supply > 0 && balance > 0 && weight > 0 && weights > 0, InvalidInput());
|
|
105
|
+
require(weight <= weights, WeightOutOfBound());
|
|
106
|
+
require(amount <= balance, AmountOutOfBound());
|
|
107
|
+
|
|
108
|
+
if (amount == balance)
|
|
109
|
+
return supply;
|
|
96
110
|
|
|
97
|
-
if (
|
|
98
|
-
return IntegralMath.
|
|
111
|
+
if (weight == weights)
|
|
112
|
+
return IntegralMath.mulDivC(supply, amount, balance);
|
|
99
113
|
|
|
100
|
-
(uint256 n, uint256 d) = AnalyticMath.pow(
|
|
101
|
-
return IntegralMath.
|
|
114
|
+
(uint256 n, uint256 d) = AnalyticMath.pow(balance - amount, balance, weight, weights);
|
|
115
|
+
return IntegralMath.mulDivC(supply, d - n, d);
|
|
102
116
|
}}
|
|
103
117
|
|
|
104
118
|
/**
|
|
105
|
-
* @dev
|
|
119
|
+
* @dev Calculate the amount of reserve2 tokens returned in exchange for a specified amount of reserve1 tokens
|
|
106
120
|
*
|
|
107
|
-
* @param
|
|
108
|
-
* @param
|
|
109
|
-
* @param
|
|
110
|
-
* @param
|
|
121
|
+
* @param balance1 The amount of reserve1 tokens owned by the pool
|
|
122
|
+
* @param balance2 The amount of reserve2 tokens owned by the pool
|
|
123
|
+
* @param weight1 The weight of reserve1 in the pool
|
|
124
|
+
* @param weight2 The weight of reserve2 in the pool
|
|
125
|
+
* @param amount The amount of reserve1 tokens provided
|
|
111
126
|
*
|
|
112
|
-
* @return
|
|
127
|
+
* @return balance2 * (1 - (balance1 / (balance1 + amount)) ^ (weight1 / weight2))
|
|
128
|
+
*
|
|
129
|
+
* @notice This function never overestimates the true result
|
|
113
130
|
*/
|
|
114
|
-
function
|
|
115
|
-
require(
|
|
116
|
-
require(
|
|
117
|
-
require(amount <= supply, AmountLargerThanSupply());
|
|
118
|
-
|
|
119
|
-
if (amount == supply)
|
|
120
|
-
return balance;
|
|
131
|
+
function swapGain(uint256 balance1, uint256 balance2, uint256 weight1, uint256 weight2, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
132
|
+
require(balance1 > 0 && balance2 > 0 && weight1 > 0 && weight2 > 0, InvalidInput());
|
|
133
|
+
require(amount <= ~balance1, AmountOutOfBound());
|
|
121
134
|
|
|
122
|
-
if (
|
|
123
|
-
return IntegralMath.mulDivF(
|
|
135
|
+
if (weight1 == weight2)
|
|
136
|
+
return IntegralMath.mulDivF(balance2, amount, balance1 + amount);
|
|
124
137
|
|
|
125
|
-
(uint256 n, uint256 d) = AnalyticMath.pow(
|
|
126
|
-
return IntegralMath.mulDivF(
|
|
138
|
+
(uint256 n, uint256 d) = AnalyticMath.pow(balance1, balance1 + amount, weight1, weight2);
|
|
139
|
+
return IntegralMath.mulDivF(balance2, d - n, d);
|
|
127
140
|
}}
|
|
128
141
|
|
|
129
142
|
/**
|
|
130
|
-
* @dev
|
|
143
|
+
* @dev Calculate the amount of reserve1 tokens required in exchange for a specified amount of reserve2 tokens
|
|
131
144
|
*
|
|
132
|
-
* @param
|
|
133
|
-
* @param
|
|
134
|
-
* @param
|
|
135
|
-
* @param
|
|
145
|
+
* @param balance1 The amount of reserve1 tokens owned by the pool
|
|
146
|
+
* @param balance2 The amount of reserve2 tokens owned by the pool
|
|
147
|
+
* @param weight1 The weight of reserve1 in the pool
|
|
148
|
+
* @param weight2 The weight of reserve2 in the pool
|
|
149
|
+
* @param amount The amount of reserve2 tokens desired
|
|
150
|
+
*
|
|
151
|
+
* @return balance1 * ((balance2 / (balance2 - amount)) ^ (weight2 / weight1) - 1)
|
|
136
152
|
*
|
|
137
|
-
* @
|
|
153
|
+
* @notice This function might underestimate the true result
|
|
138
154
|
*/
|
|
139
|
-
function
|
|
140
|
-
require(
|
|
141
|
-
require(
|
|
155
|
+
function swapCost(uint256 balance1, uint256 balance2, uint256 weight1, uint256 weight2, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
156
|
+
require(balance1 > 0 && balance2 > 0 && weight1 > 0 && weight2 > 0, InvalidInput());
|
|
157
|
+
require(amount < balance2, AmountOutOfBound());
|
|
142
158
|
|
|
143
|
-
if (
|
|
144
|
-
return IntegralMath.mulDivC(
|
|
159
|
+
if (weight1 == weight2)
|
|
160
|
+
return IntegralMath.mulDivC(balance1, amount, balance2 - amount);
|
|
145
161
|
|
|
146
|
-
(uint256 n, uint256 d) = AnalyticMath.pow(
|
|
147
|
-
return IntegralMath.mulDivC(
|
|
162
|
+
(uint256 n, uint256 d) = AnalyticMath.pow(balance2, balance2 - amount, weight2, weight1);
|
|
163
|
+
return IntegralMath.mulDivC(balance1, n - d, d);
|
|
148
164
|
}}
|
|
149
165
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.39;
|
|
3
3
|
|
|
4
4
|
import "./common/Uint256.sol";
|
|
5
5
|
|
|
@@ -146,7 +146,7 @@ library IntegralMath {
|
|
|
146
146
|
function mulShrC(uint256 x, uint256 y, uint8 s) internal pure returns (uint256) { unchecked {
|
|
147
147
|
uint256 w = mulShrF(x, y, s);
|
|
148
148
|
if (mulmod(x, y, 1 << s) > 0)
|
|
149
|
-
return
|
|
149
|
+
return inc256(w);
|
|
150
150
|
return w;
|
|
151
151
|
}}
|
|
152
152
|
|
|
@@ -178,7 +178,7 @@ library IntegralMath {
|
|
|
178
178
|
function mulDivC(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {
|
|
179
179
|
uint256 w = mulDivF(x, y, z);
|
|
180
180
|
if (mulmod(x, y, z) > 0)
|
|
181
|
-
return
|
|
181
|
+
return inc256(w);
|
|
182
182
|
return w;
|
|
183
183
|
}}
|
|
184
184
|
|
|
@@ -188,7 +188,7 @@ library IntegralMath {
|
|
|
188
188
|
function mulDivR(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {
|
|
189
189
|
uint256 w = mulDivF(x, y, z);
|
|
190
190
|
if (mulmod(x, y, z) > (z - 1) / 2)
|
|
191
|
-
return
|
|
191
|
+
return inc256(w);
|
|
192
192
|
return w;
|
|
193
193
|
}}
|
|
194
194
|
|
|
@@ -230,14 +230,22 @@ library IntegralMath {
|
|
|
230
230
|
(uint256 vzwlh, uint256 vzwll) = mul512(v, zwl);
|
|
231
231
|
if (xyh == v * zwh + vzwlh && xyl == vzwll)
|
|
232
232
|
return v;
|
|
233
|
-
return
|
|
233
|
+
return inc256(v);
|
|
234
|
+
}}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @dev Compute the value of `x + 1`
|
|
238
|
+
*/
|
|
239
|
+
function inc256(uint256 x) private pure returns (uint256) { unchecked {
|
|
240
|
+
require(x < type(uint256).max, Overflow());
|
|
241
|
+
return x + 1;
|
|
234
242
|
}}
|
|
235
243
|
|
|
236
244
|
/**
|
|
237
245
|
* @dev Compute the value of `x * y`
|
|
238
246
|
*/
|
|
239
247
|
function mul512(uint256 x, uint256 y) private pure returns (uint256, uint256) { unchecked {
|
|
240
|
-
uint256 p =
|
|
248
|
+
uint256 p = mulmod(x, y, type(uint256).max);
|
|
241
249
|
uint256 q = unsafeMul(x, y);
|
|
242
250
|
if (p >= q)
|
|
243
251
|
return (p - q, q);
|
|
@@ -1,29 +1,14 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.39;
|
|
3
3
|
|
|
4
|
-
uint256 constant MAX_VAL = type(uint256).max;
|
|
5
|
-
|
|
6
|
-
// reverts on overflow
|
|
7
|
-
function safeAdd(uint256 x, uint256 y) pure returns (uint256) {
|
|
8
|
-
return x + y;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// does not revert on overflow
|
|
12
4
|
function unsafeAdd(uint256 x, uint256 y) pure returns (uint256) { unchecked {
|
|
13
5
|
return x + y;
|
|
14
6
|
}}
|
|
15
7
|
|
|
16
|
-
// does not revert on overflow
|
|
17
8
|
function unsafeSub(uint256 x, uint256 y) pure returns (uint256) { unchecked {
|
|
18
9
|
return x - y;
|
|
19
10
|
}}
|
|
20
11
|
|
|
21
|
-
// does not revert on overflow
|
|
22
12
|
function unsafeMul(uint256 x, uint256 y) pure returns (uint256) { unchecked {
|
|
23
13
|
return x * y;
|
|
24
14
|
}}
|
|
25
|
-
|
|
26
|
-
// does not overflow
|
|
27
|
-
function mulModMax(uint256 x, uint256 y) pure returns (uint256) { unchecked {
|
|
28
|
-
return mulmod(x, y, MAX_VAL);
|
|
29
|
-
}}
|