Skip to content

Commit

Permalink
Move computation of expected out amount to dedicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
fedgiac committed Jan 30, 2024
1 parent e3b82a6 commit f4e9518
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions test/ConstantProduct/verify/ValidateAmmMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ 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;
uint256 poolIn = 10 ether;
(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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit f4e9518

Please sign in to comment.