Skip to content

Commit

Permalink
reduces precision
Browse files Browse the repository at this point in the history
  • Loading branch information
sarangparikh22 committed Jun 2, 2022
1 parent 7d3a6a2 commit c993264
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
36 changes: 17 additions & 19 deletions contracts/pool/stable/StablePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ error InsufficientLiquidityMinted();
error InvalidAmounts();
error InvalidInputToken();
error PoolUninitialized();
error Overflow();

/// @notice Trident exchange pool template with stable swap (solidly exchange) for swapping between tightly correlated assets
/// @dev The reserves are stored as bento shares.
Expand Down Expand Up @@ -213,16 +212,15 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {

function _updateReserves() internal {
(uint256 _reserve0, uint256 _reserve1) = _balance();
require(_reserve0 <= type(uint128).max && _reserve1 <= type(uint128).max, "OVERFLOW");
reserve0 = uint128(_reserve0);
reserve1 = uint128(_reserve1);
reserve0 = _reserve0;
reserve1 = _reserve1;
emit Sync(_reserve0, _reserve1);
}

function _computeLiquidity(uint256 _reserve0, uint256 _reserve1) internal view returns (uint256 liquidity) {
unchecked {
uint256 adjustedReserve0 = (_reserve0 * 1e18) / decimals0;
uint256 adjustedReserve1 = (_reserve1 * 1e18) / decimals1;
uint256 adjustedReserve0 = (_reserve0 * 1e12) / decimals0;
uint256 adjustedReserve1 = (_reserve1 * 1e12) / decimals1;
liquidity = _computeLiquidityFromAdjustedBalances(adjustedReserve0, adjustedReserve1);
}
}
Expand Down Expand Up @@ -270,17 +268,17 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
}

function _k(uint256 x, uint256 y) internal pure returns (uint256) {
uint256 _a = (x * y) / 1e18;
uint256 _b = ((x * x) / 1e18 + (y * y) / 1e18);
return ((_a * _b) / 1e18); // x3y+y3x >= k
uint256 _a = (x * y) / 1e12;
uint256 _b = ((x * x) / 1e12 + (y * y) / 1e12);
return ((_a * _b) / 1e12); // x3y+y3x >= k
}

function _f(uint256 x0, uint256 y) internal pure returns (uint256) {
return (x0 * ((((y * y) / 1e18) * y) / 1e18)) / 1e18 + (((((x0 * x0) / 1e18) * x0) / 1e18) * y) / 1e18;
return (x0 * ((((y * y) / 1e12) * y) / 1e12)) / 1e12 + (((((x0 * x0) / 1e12) * x0) / 1e12) * y) / 1e12;
}

function _d(uint256 x0, uint256 y) internal pure returns (uint256) {
return (3 * x0 * ((y * y) / 1e18)) / 1e18 + ((((x0 * x0) / 1e18) * x0) / 1e18);
return (3 * x0 * ((y * y) / 1e12)) / 1e12 + ((((x0 * x0) / 1e12) * x0) / 1e12);
}

function _get_y(
Expand All @@ -292,10 +290,10 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
uint256 y_prev = y;
uint256 k = _f(x0, y);
if (k < xy) {
uint256 dy = ((xy - k) * 1e18) / _d(x0, y);
uint256 dy = ((xy - k) * 1e12) / _d(x0, y);
y = y + dy;
} else {
uint256 dy = ((k - xy) * 1e18) / _d(x0, y);
uint256 dy = ((k - xy) * 1e12) / _d(x0, y);
y = y - dy;
}
if (y > y_prev) {
Expand All @@ -318,20 +316,20 @@ contract StablePool is IPool, ERC20, ReentrancyGuard {
bool token0In
) internal view returns (uint256 dy) {
unchecked {
uint256 adjustedReserve0 = (_reserve0 * 1e18) / decimals0;
uint256 adjustedReserve1 = (_reserve1 * 1e18) / decimals1;
uint256 adjustedReserve0 = (_reserve0 * 1e12) / decimals0;
uint256 adjustedReserve1 = (_reserve1 * 1e12) / decimals1;
uint256 feeDeductedAmountIn = amountIn - (amountIn * swapFee) / MAX_FEE;
uint256 xy = _k(adjustedReserve0, adjustedReserve1);
if (token0In) {
uint256 x0 = adjustedReserve0 + ((feeDeductedAmountIn * 1e18) / decimals0);
uint256 x0 = adjustedReserve0 + ((feeDeductedAmountIn * 1e12) / decimals0);
uint256 y = _get_y(x0, xy, adjustedReserve1);
dy = adjustedReserve1 - y;
dy = (dy * decimals1) / 1e18;
dy = (dy * decimals1) / 1e12;
} else {
uint256 x0 = adjustedReserve1 + ((feeDeductedAmountIn * 1e18) / decimals1);
uint256 x0 = adjustedReserve1 + ((feeDeductedAmountIn * 1e12) / decimals1);
uint256 y = _get_y(x0, xy, adjustedReserve0);
dy = adjustedReserve0 - y;
dy = (dy * decimals0) / 1e18;
dy = (dy * decimals0) / 1e12;
}
}
}
Expand Down
24 changes: 18 additions & 6 deletions test/stable-pool/StablePool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,26 @@ describe("Stable Pool", () => {
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());
const token1 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token1());
const bento = await ethers.getContract<BentoBoxV1>("BentoBoxV1");
await token0.transfer(bento.address, ethers.utils.parseEther("10000000000"));
await token1.transfer(bento.address, ethers.utils.parseEther("10000000000"));
await bento.deposit(token0.address, bento.address, pool.address, ethers.utils.parseEther("10000000000"), 0);
await bento.deposit(token1.address, bento.address, pool.address, ethers.utils.parseEther("10000000000"), 0);
await token0.transfer(bento.address, ethers.utils.parseUnits("100000000000", "18"));
await token1.transfer(bento.address, ethers.utils.parseUnits("100000000000", "18"));
await bento.deposit(
token0.address,
bento.address,
pool.address,
ethers.utils.parseUnits("100000000000", "18"),
0
);
await bento.deposit(
token1.address,
bento.address,
pool.address,
ethers.utils.parseUnits("100000000000", "18"),
0
);
const mintData = ethers.utils.defaultAbiCoder.encode(["address"], [deployer.address]);
await pool.mint(mintData);
// const getAmountOutData = ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [token0.address, ethers.utils.parseEther("100000")]);
// console.log(ethers.utils.formatEther(await pool.getAmountOut(getAmountOutData)));
// const getAmountOutData = ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [token0.address, ethers.utils.parseUnits("100", '18')]);
// console.log(ethers.utils.formatUnits(await pool.getAmountOut(getAmountOutData), '18'));
});

it("removes liquidity", async () => {
Expand Down

0 comments on commit c993264

Please sign in to comment.