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 buy(uint256 supply, uint256 balance, uint256 weight, uint256 amount)` => `(uint256)`
231
- - `function sell(uint256 supply, uint256 balance, uint256 weight, uint256 amount)` => `(uint256)`
232
- - `function convert(uint256 balance1, uint256 weight1, uint256 balance2, uint256 weight2, uint256 amount)` => `(uint256)`
233
- - `function deposit(uint256 supply, uint256 balance, uint256 weights, uint256 amount)` => `(uint256)`
234
- - `function withdraw(uint256 supply, uint256 balance, uint256 weights, uint256 amount)` => `(uint256)`
235
- - `function invest(uint256 supply, uint256 balance, uint256 weights, uint256 amount)` => `(uint256)`
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
- ### Functionality
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 | Compute the return of | Formula |
242
- |----------------------------|--------------------------------------------------|---------------------------------------------|
243
- | buy(s, b, w, x) | buying pool tokens with reserve tokens | s * ((1 + x / b) ^ (w / MAX_WEIGHT) - 1) |
244
- | sell(s, b, w, x) | selling pool tokens for reserve tokens | b * (1 - (1 - x / s) ^ (MAX_WEIGHT / w)) |
245
- | convert(b1, w1, b2, w2, x) | converting reserve tokens of one type to another | b2 * (1 - (b1 / (b1 + x)) ^ (w1 / w2)) |
246
- | deposit(s, b, ws, x) | depositing reserve tokens for pool tokens | s * ((x / b + 1) ^ (ws / MAX_WEIGHT) - 1) |
247
- | withdraw(s, b, ws, x) | withdrawing reserve tokens with pool tokens | b * (1 - ((s - x) / s) ^ (MAX_WEIGHT / ws)) |
248
- | invest(s, b, ws, x) | investing reserve tokens for pool tokens | b * (((s + x) / s) ^ (MAX_WEIGHT / ws) - 1) |
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solidity-math-utils",
3
- "version": "2.4.0",
3
+ "version": "3.0.0",
4
4
  "description": "Solidity Mathematical Utilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -7,143 +7,159 @@ import "./IntegralMath.sol";
7
7
  library BondingCurve {
8
8
  error InvalidInput();
9
9
  error WeightOutOfBound();
10
- error WeightsOutOfBound();
11
- error AmountLargerThanSupply();
12
-
13
- uint256 internal constant MAX_WEIGHT = 1000000;
10
+ error AmountOutOfBound();
14
11
 
15
12
  /**
16
- * @dev Buy pool tokens with reserve tokens
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 the reserve (represented in ppm)
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 / MAX_WEIGHT) - 1)
21
+ * @return supply * ((1 + amount / balance) ^ (weight / weights) - 1)
22
+ *
23
+ * @notice This function never overestimates the true result
24
24
  */
25
- function buy(uint256 supply, uint256 balance, uint256 weight, uint256 amount) internal pure returns (uint256) { unchecked {
26
- require(supply > 0 && balance > 0 && weight > 0, InvalidInput());
27
- require(weight <= MAX_WEIGHT, WeightOutOfBound());
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 == MAX_WEIGHT)
30
- return IntegralMath.mulDivF(amount, supply, balance);
30
+ if (weight == weights)
31
+ return IntegralMath.mulDivF(supply, amount, balance);
31
32
 
32
- (uint256 n, uint256 d) = AnalyticMath.pow(safeAdd(balance, amount), balance, weight, MAX_WEIGHT);
33
- return IntegralMath.mulDivF(supply, n, d) - supply;
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 Sell pool tokens for reserve tokens
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 the reserve (represented in ppm)
42
- * @param amount The amount of pool tokens provided
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
- * @return balance * (1 - (1 - amount / supply) ^ (MAX_WEIGHT / weight))
48
+ * @notice This function might underestimate the true result
45
49
  */
46
- function sell(uint256 supply, uint256 balance, uint256 weight, uint256 amount) internal pure returns (uint256) { unchecked {
47
- require(supply > 0 && balance > 0 && weight > 0, InvalidInput());
48
- require(weight <= MAX_WEIGHT, WeightOutOfBound());
49
- require(amount <= supply, AmountLargerThanSupply());
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 (amount == supply)
52
- return balance;
55
+ if (weight == weights)
56
+ return IntegralMath.mulDivC(balance, amount, supply);
53
57
 
54
- if (weight == MAX_WEIGHT)
55
- return IntegralMath.mulDivF(amount, balance, supply);
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 Convert reserve tokens of one type to another
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
- * @param balance1 The amount of source reserve tokens owned by the pool
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
- * @return balance2 * (1 - (balance1 / (balance1 + amount)) ^ (weight1 / weight2))
73
+ * @notice This function never overestimates the true result
71
74
  */
72
- function convert(uint256 balance1, uint256 weight1, uint256 balance2, uint256 weight2, uint256 amount) internal pure returns (uint256) { unchecked {
73
- require(balance1 > 0 && balance2 > 0 && weight1 > 0 && weight2 > 0, InvalidInput());
74
- require(weight1 <= MAX_WEIGHT && weight2 <= MAX_WEIGHT, WeightsOutOfBound());
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 (weight1 == weight2)
77
- return IntegralMath.mulDivF(balance2, amount, safeAdd(balance1, amount));
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(safeAdd(balance1, amount), balance1, weight1, weight2);
80
- return IntegralMath.mulDivF(balance2, n - d, n);
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 Deposit reserve tokens for pool tokens
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 of the desired type owned by the pool
88
- * @param weights The combined weights of the reserves (represented in ppm)
89
- * @param amount The amount of reserve tokens of the desired type provided
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
- * @return supply * ((amount / balance + 1) ^ (weights / MAX_WEIGHT) - 1)
101
+ * @notice This function might underestimate the true result
92
102
  */
93
- function deposit(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
94
- require(supply > 0 && balance > 0 && weights > 0, InvalidInput());
95
- require(weights <= MAX_WEIGHT * 2, WeightsOutOfBound());
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 (weights == MAX_WEIGHT)
98
- return IntegralMath.mulDivF(amount, supply, balance);
111
+ if (weight == weights)
112
+ return IntegralMath.mulDivC(supply, amount, balance);
99
113
 
100
- (uint256 n, uint256 d) = AnalyticMath.pow(safeAdd(balance, amount), balance, weights, MAX_WEIGHT);
101
- return IntegralMath.mulDivF(supply, n, d) - supply;
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 Withdraw reserve tokens with pool tokens
119
+ * @dev Calculate the amount of reserve2 tokens returned in exchange for a specified amount of reserve1 tokens
106
120
  *
107
- * @param supply The total amount of pool tokens
108
- * @param balance The amount of reserve tokens of the desired type owned by the pool
109
- * @param weights The combined weights of the reserves (represented in ppm)
110
- * @param amount The amount of pool tokens provided
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 balance * (1 - ((supply - amount) / supply) ^ (MAX_WEIGHT / weights))
127
+ * @return balance2 * (1 - (balance1 / (balance1 + amount)) ^ (weight1 / weight2))
128
+ *
129
+ * @notice This function never overestimates the true result
113
130
  */
114
- function withdraw(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
115
- require(supply > 0 && balance > 0 && weights > 0, InvalidInput());
116
- require(weights <= MAX_WEIGHT * 2, WeightsOutOfBound());
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 (weights == MAX_WEIGHT)
123
- return IntegralMath.mulDivF(amount, balance, supply);
135
+ if (weight1 == weight2)
136
+ return IntegralMath.mulDivF(balance2, amount, balance1 + amount);
124
137
 
125
- (uint256 n, uint256 d) = AnalyticMath.pow(supply, supply - amount, MAX_WEIGHT, weights);
126
- return IntegralMath.mulDivF(balance, n - d, n);
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 Invest reserve tokens for pool tokens
143
+ * @dev Calculate the amount of reserve1 tokens required in exchange for a specified amount of reserve2 tokens
131
144
  *
132
- * @param supply The total amount of pool tokens
133
- * @param balance The amount of reserve tokens of the desired type owned by the pool
134
- * @param weights The combined weights of the reserves (represented in ppm)
135
- * @param amount The amount of pool tokens desired
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
- * @return balance * (((supply + amount) / supply) ^ (MAX_WEIGHT / weights) - 1)
153
+ * @notice This function might underestimate the true result
138
154
  */
139
- function invest(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
140
- require(supply > 0 && balance > 0 && weights > 0, InvalidInput());
141
- require(weights <= MAX_WEIGHT * 2, WeightsOutOfBound());
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 (weights == MAX_WEIGHT)
144
- return IntegralMath.mulDivC(amount, balance, supply);
159
+ if (weight1 == weight2)
160
+ return IntegralMath.mulDivC(balance1, amount, balance2 - amount);
145
161
 
146
- (uint256 n, uint256 d) = AnalyticMath.pow(safeAdd(supply, amount), supply, MAX_WEIGHT, weights);
147
- return IntegralMath.mulDivC(balance, n, d) - balance;
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 safeAdd(w, 1);
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 safeAdd(w, 1);
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 safeAdd(w, 1);
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 safeAdd(v, 1);
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 = mulModMax(x, y);
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
- }}