From f4e9518b9817521516627196707165b7d3552c51 Mon Sep 17 00:00:00 2001 From: Federico Giacon <58218759+fedgiac@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:28:29 +0100 Subject: [PATCH] Move computation of expected out amount to dedicated function --- test/ConstantProduct/verify/ValidateAmmMath.sol | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/test/ConstantProduct/verify/ValidateAmmMath.sol b/test/ConstantProduct/verify/ValidateAmmMath.sol index 3b0e5f6..0de046e 100644 --- a/test/ConstantProduct/verify/ValidateAmmMath.sol +++ b/test/ConstantProduct/verify/ValidateAmmMath.sol @@ -44,6 +44,11 @@ abstract contract ValidateAmmMath is ConstantProductTestHarness { // Y * x // y = --------- // X - 2x + function getExpectedAmountIn(uint256[2] memory reserves, uint256 amountOut) internal pure returns (uint256) { + uint256 poolIn = reserves[0]; + uint256 poolOut = reserves[1]; + return poolIn * amountOut / (poolOut - 2 * amountOut); + } function testExactAmountsInOut() public { uint256 poolOut = 1000 ether; @@ -51,7 +56,7 @@ abstract contract ValidateAmmMath is ConstantProductTestHarness { (ConstantProduct.Data memory data, GPv2Order.Data memory order) = setUpOrderWithReserves(poolOut, poolIn); uint256 amountOut = 100 ether; - uint256 amountIn = poolIn * amountOut / (poolOut - 2 * amountOut); + uint256 amountIn = getExpectedAmountIn([poolIn, poolOut], amountOut); order.sellAmount = amountOut; order.buyAmount = amountIn; @@ -69,7 +74,7 @@ abstract contract ValidateAmmMath is ConstantProductTestHarness { (ConstantProduct.Data memory data, GPv2Order.Data memory order) = setUpOrderWithReserves(poolOut, poolIn); uint256 amountOut = 100 ether; - uint256 amountIn = poolIn * amountOut / (poolOut - 2 * amountOut); + uint256 amountIn = getExpectedAmountIn([poolIn, poolOut], amountOut); order.sellAmount = amountOut + 1; order.buyAmount = amountIn; @@ -83,7 +88,7 @@ abstract contract ValidateAmmMath is ConstantProductTestHarness { (ConstantProduct.Data memory data, GPv2Order.Data memory order) = setUpOrderWithReserves(poolOut, poolIn); uint256 amountOut = 100 ether; - uint256 amountIn = poolIn * amountOut / (poolOut - 2 * amountOut); + uint256 amountIn = getExpectedAmountIn([poolIn, poolOut], amountOut); order.sellAmount = amountOut; order.buyAmount = amountIn - 1; @@ -97,7 +102,7 @@ abstract contract ValidateAmmMath is ConstantProductTestHarness { (ConstantProduct.Data memory data, GPv2Order.Data memory order) = setUpOrderWithReserves(poolIn, poolOut); uint256 amountOut = 100 ether; - uint256 amountIn = poolIn * amountOut / (poolOut - 2 * amountOut); + uint256 amountIn = getExpectedAmountIn([poolIn, poolOut], amountOut); (order.sellToken, order.buyToken) = (order.buyToken, order.sellToken); order.sellAmount = amountOut; order.buyAmount = amountIn; @@ -111,7 +116,7 @@ abstract contract ValidateAmmMath is ConstantProductTestHarness { (ConstantProduct.Data memory data, GPv2Order.Data memory order) = setUpOrderWithReserves(poolIn, poolOut); uint256 amountOut = 100 ether; - uint256 amountIn = poolIn * amountOut / (poolOut - 2 * amountOut); + uint256 amountIn = getExpectedAmountIn([poolIn, poolOut], amountOut); (order.sellToken, order.buyToken) = (order.buyToken, order.sellToken); order.sellAmount = amountOut + 1; order.buyAmount = amountIn; @@ -126,7 +131,7 @@ abstract contract ValidateAmmMath is ConstantProductTestHarness { (ConstantProduct.Data memory data, GPv2Order.Data memory order) = setUpOrderWithReserves(poolIn, poolOut); uint256 amountOut = 100 ether; - uint256 amountIn = poolIn * amountOut / (poolOut - 2 * amountOut); + uint256 amountIn = getExpectedAmountIn([poolIn, poolOut], amountOut); (order.sellToken, order.buyToken) = (order.buyToken, order.sellToken); order.sellAmount = amountOut; order.buyAmount = amountIn - 1;