solidity-math-utils 2.4.0 → 3.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
CHANGED
|
@@ -227,26 +227,39 @@ The full customization manual can be found [here](#customization).
|
|
|
227
227
|
## BondingCurve
|
|
228
228
|
|
|
229
229
|
This module implements the following interface:
|
|
230
|
-
- `function
|
|
231
|
-
- `function
|
|
232
|
-
- `function
|
|
233
|
-
- `function
|
|
234
|
-
- `function
|
|
235
|
-
- `function
|
|
230
|
+
- `function mintGain(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
231
|
+
- `function mintCost(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
232
|
+
- `function burnGain(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
233
|
+
- `function burnCost(uint256 supply, uint256 balance, uint256 weight, uint256 weights, uint256 amount)` => `(uint256)`
|
|
234
|
+
- `function swapGain(uint256 balance1, uint256 balance2, uint256 weight1, uint256 weight2, uint256 amount)` => `(uint256)`
|
|
235
|
+
- `function swapCost(uint256 balance1, uint256 balance2, uint256 weight1, uint256 weight2, uint256 amount)` => `(uint256)`
|
|
236
236
|
|
|
237
|
-
|
|
237
|
+
Function `mintGain` calculates the amount of pool tokens returned in exchange for a specified amount of reserve tokens.
|
|
238
238
|
|
|
239
|
+
Function `mintCost` calculates the amount of reserve tokens required in exchange for a specified amount of pool tokens.
|
|
240
|
+
|
|
241
|
+
Function `burnGain` calculates the amount of reserve tokens returned in exchange for a specified amount of pool tokens.
|
|
242
|
+
|
|
243
|
+
Function `burnCost` calculates the amount of pool tokens required in exchange for a specified amount of reserve tokens.
|
|
244
|
+
|
|
245
|
+
Function `swapGain` calculates the amount of reserve2 tokens returned in exchange for a specified amount of reserve1 tokens.
|
|
246
|
+
|
|
247
|
+
Function `swapCost` calculates the amount of reserve1 tokens required in exchange for a specified amount of reserve2 tokens.
|
|
248
|
+
|
|
249
|
+
### Mathematical Model
|
|
250
|
+
|
|
251
|
+
All functions in this module are based on the constant-product formula for weighted pools:
|
|
239
252
|
```
|
|
240
|
-
|
|
241
|
-
| Function
|
|
242
|
-
|
|
243
|
-
|
|
|
244
|
-
|
|
|
245
|
-
|
|
|
246
|
-
|
|
|
247
|
-
|
|
|
248
|
-
|
|
|
249
|
-
|
|
253
|
+
+-----------------------------+----------------------------------------+-------------------------------------+
|
|
254
|
+
| Function | Formula | Note |
|
|
255
|
+
+-----------------------------+----------------------------------------+-------------------------------------+
|
|
256
|
+
| mintGain(s, b, wn, wd, x) | s * ((1 + x / b) ^ (wn / wd) - 1) | never overestimates the true result |
|
|
257
|
+
| mintCost(s, b, wn, wd, x) | b * ((1 + x / s) ^ (wd / wn) - 1) | might underestimate the true result |
|
|
258
|
+
| burnGain(s, b, wn, wd, x) | b * (1 - (1 - x / s) ^ (wd / wn)) | never overestimates the true result |
|
|
259
|
+
| burnCost(s, b, wn, wd, x) | s * (1 - (1 - x / b) ^ (wn / wd)) | might underestimate the true result |
|
|
260
|
+
| swapGain(b1, b2, w1, w2, x) | b2 * (1 - (b1 / (b1 + x)) ^ (w1 / w2)) | never overestimates the true result |
|
|
261
|
+
| swapCost(b1, b2, w1, w2, x) | b1 * ((b2 / (b2 - x)) ^ (w2 / w1) - 1) | might underestimate the true result |
|
|
262
|
+
+-----------------------------+----------------------------------------+-------------------------------------+
|
|
250
263
|
```
|
|
251
264
|
|
|
252
265
|
The bonding-curve model was conceived by [Bancor](https://github.com/bancorprotocol).
|
package/package.json
CHANGED
|
@@ -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
|
}
|
|
@@ -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
2
|
pragma solidity ^0.8.34;
|
|
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
|
-
}}
|