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 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 max)` => `(uint256, 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 valus are consecutive Fibonacci numbers, in the case of `uint256` - F369 and F370, which yield 367 iterations.
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.pdf).
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 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)`
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
- ### Functionality
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 | 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
- |----------------------------|--------------------------------------------------|---------------------------------------------|
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.pdf).
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 8.9.2`
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": "2.4.0",
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.28.6",
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.34;
2
+ pragma solidity ^0.8.39;
3
3
 
4
4
  import "./AnalyticMath.sol";
5
5
  import "./FractionMath.sol";
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity ^0.8.34;
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 / log(2 ^ k)) + log(2 ^ k) =
61
- * log(x / log(2 ^ k)) + k * log(2)
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.34;
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 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
  }
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity ^0.8.34;
2
+ pragma solidity ^0.8.39;
3
3
 
4
4
  import "./AdvancedMath.sol";
5
5
  import "./FractionMath.sol";
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity ^0.8.34;
2
+ pragma solidity ^0.8.39;
3
3
 
4
4
  import "./IntegralMath.sol";
5
5
 
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity ^0.8.34;
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 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
- pragma solidity ^0.8.34;
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
- }}