Skip to content

Commit d4346bb

Browse files
added solidly style lp calc
1 parent c993264 commit d4346bb

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
pragma solidity >=0.8.0;
4+
5+
library StablePoolMath {
6+
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
7+
z = x < y ? x : y;
8+
}
9+
}

contracts/pool/stable/StablePool.sol

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {ITridentCallee} from "../../interfaces/ITridentCallee.sol";
1212
import {IStablePoolFactory} from "../../interfaces/IStablePoolFactory.sol";
1313

1414
import {TridentMath} from "../../libraries/TridentMath.sol";
15+
import "../../libraries/stable_pool/StablePoolMath.sol";
1516
import "../../libraries/RebaseLibrary.sol";
1617

1718
/// @dev Custom Errors
@@ -94,8 +95,6 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
9495
(uint256 _reserve0, uint256 _reserve1) = _getReserves();
9596
(uint256 balance0, uint256 balance1) = _balance();
9697

97-
uint256 newLiq = _computeLiquidity(balance0, balance1);
98-
9998
uint256 amount0 = balance0 - _reserve0;
10099
uint256 amount1 = balance1 - _reserve1;
101100

@@ -104,14 +103,14 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
104103
_reserve0 += uint112(fee0);
105104
_reserve1 += uint112(fee1);
106105

107-
(uint256 _totalSupply, uint256 oldLiq) = _mintFee(_reserve0, _reserve1);
106+
(uint256 _totalSupply, ) = _mintFee(_reserve0, _reserve1);
108107

109108
if (_totalSupply == 0) {
110109
require(amount0 > 0 && amount1 > 0, "INVALID_AMOUNTS");
111-
liquidity = newLiq - MINIMUM_LIQUIDITY;
110+
liquidity = TridentMath.sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY;
112111
_mint(address(0), MINIMUM_LIQUIDITY);
113112
} else {
114-
liquidity = ((newLiq - oldLiq) * _totalSupply) / oldLiq;
113+
liquidity = StablePoolMath.min((amount0 * _totalSupply) / _reserve0, (amount1 * _totalSupply) / _reserve1);
115114
}
116115

117116
require(liquidity != 0, "INSUFFICIENT_LIQUIDITY_MINTED");
@@ -120,7 +119,7 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
120119

121120
_updateReserves();
122121

123-
kLast = newLiq;
122+
kLast = _computeLiquidity(_reserve0, _reserve1);
124123
uint256 liquidityForEvent = liquidity;
125124
emit Mint(msg.sender, amount0, amount1, recipient, liquidityForEvent);
126125
}
@@ -219,21 +218,23 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
219218

220219
function _computeLiquidity(uint256 _reserve0, uint256 _reserve1) internal view returns (uint256 liquidity) {
221220
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;
224223
liquidity = _computeLiquidityFromAdjustedBalances(adjustedReserve0, adjustedReserve1);
225224
}
226225
}
227226

228227
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);
230229
}
231230

232231
function _mintFee(uint256 _reserve0, uint256 _reserve1) internal returns (uint256 _totalSupply, uint256 computed) {
233232
_totalSupply = totalSupply;
234233
uint256 _kLast = kLast;
235234
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);
237238
if (computed > _kLast) {
238239
// `barFee` % of increase in liquidity.
239240
uint256 _barFee = barFee;
@@ -268,17 +269,17 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
268269
}
269270

270271
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
274275
}
275276

276277
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;
278279
}
279280

280281
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);
282283
}
283284

284285
function _get_y(
@@ -290,10 +291,10 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
290291
uint256 y_prev = y;
291292
uint256 k = _f(x0, y);
292293
if (k < xy) {
293-
uint256 dy = ((xy - k) * 1e12) / _d(x0, y);
294+
uint256 dy = ((xy - k) * 1e16) / _d(x0, y);
294295
y = y + dy;
295296
} else {
296-
uint256 dy = ((k - xy) * 1e12) / _d(x0, y);
297+
uint256 dy = ((k - xy) * 1e16) / _d(x0, y);
297298
y = y - dy;
298299
}
299300
if (y > y_prev) {
@@ -316,20 +317,20 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
316317
bool token0In
317318
) internal view returns (uint256 dy) {
318319
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;
321322
uint256 feeDeductedAmountIn = amountIn - (amountIn * swapFee) / MAX_FEE;
322323
uint256 xy = _k(adjustedReserve0, adjustedReserve1);
323324
if (token0In) {
324-
uint256 x0 = adjustedReserve0 + ((feeDeductedAmountIn * 1e12) / decimals0);
325+
uint256 x0 = adjustedReserve0 + ((feeDeductedAmountIn * 1e16) / decimals0);
325326
uint256 y = _get_y(x0, xy, adjustedReserve1);
326327
dy = adjustedReserve1 - y;
327-
dy = (dy * decimals1) / 1e12;
328+
dy = (dy * decimals1) / 1e16;
328329
} else {
329-
uint256 x0 = adjustedReserve1 + ((feeDeductedAmountIn * 1e12) / decimals1);
330+
uint256 x0 = adjustedReserve1 + ((feeDeductedAmountIn * 1e16) / decimals1);
330331
uint256 y = _get_y(x0, xy, adjustedReserve0);
331332
dy = adjustedReserve0 - y;
332-
dy = (dy * decimals0) / 1e12;
333+
dy = (dy * decimals0) / 1e16;
333334
}
334335
}
335336
}

0 commit comments

Comments
 (0)