@@ -12,6 +12,7 @@ import {ITridentCallee} from "../../interfaces/ITridentCallee.sol";
12
12
import {IStablePoolFactory} from "../../interfaces/IStablePoolFactory.sol " ;
13
13
14
14
import {TridentMath} from "../../libraries/TridentMath.sol " ;
15
+ import "../../libraries/stable_pool/StablePoolMath.sol " ;
15
16
import "../../libraries/RebaseLibrary.sol " ;
16
17
17
18
/// @dev Custom Errors
@@ -94,8 +95,6 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
94
95
(uint256 _reserve0 , uint256 _reserve1 ) = _getReserves ();
95
96
(uint256 balance0 , uint256 balance1 ) = _balance ();
96
97
97
- uint256 newLiq = _computeLiquidity (balance0, balance1);
98
-
99
98
uint256 amount0 = balance0 - _reserve0;
100
99
uint256 amount1 = balance1 - _reserve1;
101
100
@@ -104,14 +103,14 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
104
103
_reserve0 += uint112 (fee0);
105
104
_reserve1 += uint112 (fee1);
106
105
107
- (uint256 _totalSupply , uint256 oldLiq ) = _mintFee (_reserve0, _reserve1);
106
+ (uint256 _totalSupply , ) = _mintFee (_reserve0, _reserve1);
108
107
109
108
if (_totalSupply == 0 ) {
110
109
require (amount0 > 0 && amount1 > 0 , "INVALID_AMOUNTS " );
111
- liquidity = newLiq - MINIMUM_LIQUIDITY;
110
+ liquidity = TridentMath. sqrt (amount0 * amount1) - MINIMUM_LIQUIDITY;
112
111
_mint (address (0 ), MINIMUM_LIQUIDITY);
113
112
} else {
114
- liquidity = ((newLiq - oldLiq) * _totalSupply) / oldLiq ;
113
+ liquidity = StablePoolMath. min ((amount0 * _totalSupply) / _reserve0, (amount1 * _totalSupply) / _reserve1) ;
115
114
}
116
115
117
116
require (liquidity != 0 , "INSUFFICIENT_LIQUIDITY_MINTED " );
@@ -120,7 +119,7 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
120
119
121
120
_updateReserves ();
122
121
123
- kLast = newLiq ;
122
+ kLast = _computeLiquidity (_reserve0, _reserve1) ;
124
123
uint256 liquidityForEvent = liquidity;
125
124
emit Mint (msg .sender , amount0, amount1, recipient, liquidityForEvent);
126
125
}
@@ -219,21 +218,23 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
219
218
220
219
function _computeLiquidity (uint256 _reserve0 , uint256 _reserve1 ) internal view returns (uint256 liquidity ) {
221
220
unchecked {
222
- uint256 adjustedReserve0 = (_reserve0 * 1e12 ) / decimals0;
223
- uint256 adjustedReserve1 = (_reserve1 * 1e12 ) / decimals1;
221
+ uint256 adjustedReserve0 = (_reserve0 * 1e16 ) / decimals0;
222
+ uint256 adjustedReserve1 = (_reserve1 * 1e16 ) / decimals1;
224
223
liquidity = _computeLiquidityFromAdjustedBalances (adjustedReserve0, adjustedReserve1);
225
224
}
226
225
}
227
226
228
227
function _computeLiquidityFromAdjustedBalances (uint256 x , uint256 y ) internal pure returns (uint256 computed ) {
229
- return TridentMath. sqrt (TridentMath. sqrt ( _k (x, y)) );
228
+ return _k (x, y);
230
229
}
231
230
232
231
function _mintFee (uint256 _reserve0 , uint256 _reserve1 ) internal returns (uint256 _totalSupply , uint256 computed ) {
233
232
_totalSupply = totalSupply;
234
233
uint256 _kLast = kLast;
235
234
if (_kLast != 0 ) {
236
- computed = _computeLiquidity (_reserve0, _reserve1);
235
+ uint256 adjustedReserve0 = (_reserve0 * 1e16 ) / decimals0;
236
+ uint256 adjustedReserve1 = (_reserve1 * 1e16 ) / decimals1;
237
+ computed = _computeLiquidity (adjustedReserve0, adjustedReserve1);
237
238
if (computed > _kLast) {
238
239
// `barFee` % of increase in liquidity.
239
240
uint256 _barFee = barFee;
@@ -268,17 +269,17 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
268
269
}
269
270
270
271
function _k (uint256 x , uint256 y ) internal pure returns (uint256 ) {
271
- uint256 _a = (x * y) / 1e12 ;
272
- uint256 _b = ((x * x) / 1e12 + (y * y) / 1e12 );
273
- return ((_a * _b) / 1e12 ); // x3y+y3x >= k
272
+ uint256 _a = (x * y) / 1e16 ;
273
+ uint256 _b = ((x * x) / 1e16 + (y * y) / 1e16 );
274
+ return ((_a * _b) / 1e16 ); // x3y+y3x >= k
274
275
}
275
276
276
277
function _f (uint256 x0 , uint256 y ) internal pure returns (uint256 ) {
277
- return (x0 * ((((y * y) / 1e12 ) * y) / 1e12 )) / 1e12 + (((((x0 * x0) / 1e12 ) * x0) / 1e12 ) * y) / 1e12 ;
278
+ return (x0 * ((((y * y) / 1e16 ) * y) / 1e16 )) / 1e16 + (((((x0 * x0) / 1e16 ) * x0) / 1e16 ) * y) / 1e16 ;
278
279
}
279
280
280
281
function _d (uint256 x0 , uint256 y ) internal pure returns (uint256 ) {
281
- return (3 * x0 * ((y * y) / 1e12 )) / 1e12 + ((((x0 * x0) / 1e12 ) * x0) / 1e12 );
282
+ return (3 * x0 * ((y * y) / 1e16 )) / 1e16 + ((((x0 * x0) / 1e16 ) * x0) / 1e16 );
282
283
}
283
284
284
285
function _get_y (
@@ -290,10 +291,10 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
290
291
uint256 y_prev = y;
291
292
uint256 k = _f (x0, y);
292
293
if (k < xy) {
293
- uint256 dy = ((xy - k) * 1e12 ) / _d (x0, y);
294
+ uint256 dy = ((xy - k) * 1e16 ) / _d (x0, y);
294
295
y = y + dy;
295
296
} else {
296
- uint256 dy = ((k - xy) * 1e12 ) / _d (x0, y);
297
+ uint256 dy = ((k - xy) * 1e16 ) / _d (x0, y);
297
298
y = y - dy;
298
299
}
299
300
if (y > y_prev) {
@@ -316,20 +317,20 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
316
317
bool token0In
317
318
) internal view returns (uint256 dy ) {
318
319
unchecked {
319
- uint256 adjustedReserve0 = (_reserve0 * 1e12 ) / decimals0;
320
- uint256 adjustedReserve1 = (_reserve1 * 1e12 ) / decimals1;
320
+ uint256 adjustedReserve0 = (_reserve0 * 1e16 ) / decimals0;
321
+ uint256 adjustedReserve1 = (_reserve1 * 1e16 ) / decimals1;
321
322
uint256 feeDeductedAmountIn = amountIn - (amountIn * swapFee) / MAX_FEE;
322
323
uint256 xy = _k (adjustedReserve0, adjustedReserve1);
323
324
if (token0In) {
324
- uint256 x0 = adjustedReserve0 + ((feeDeductedAmountIn * 1e12 ) / decimals0);
325
+ uint256 x0 = adjustedReserve0 + ((feeDeductedAmountIn * 1e16 ) / decimals0);
325
326
uint256 y = _get_y (x0, xy, adjustedReserve1);
326
327
dy = adjustedReserve1 - y;
327
- dy = (dy * decimals1) / 1e12 ;
328
+ dy = (dy * decimals1) / 1e16 ;
328
329
} else {
329
- uint256 x0 = adjustedReserve1 + ((feeDeductedAmountIn * 1e12 ) / decimals1);
330
+ uint256 x0 = adjustedReserve1 + ((feeDeductedAmountIn * 1e16 ) / decimals1);
330
331
uint256 y = _get_y (x0, xy, adjustedReserve0);
331
332
dy = adjustedReserve0 - y;
332
- dy = (dy * decimals0) / 1e12 ;
333
+ dy = (dy * decimals0) / 1e16 ;
333
334
}
334
335
}
335
336
}
0 commit comments