From 6333072178b77bdc9a7950ed0ab84f30695d1b72 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Tue, 4 Feb 2025 19:56:52 +0530 Subject: [PATCH 1/2] feat: add swap encoder test in balancer v2 executor --- .../test/executors/BalancerV2Executor.t.sol | 36 +++++++++++++++++++ .../evm/swap_encoder/swap_encoders.rs | 13 ++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/foundry/test/executors/BalancerV2Executor.t.sol b/foundry/test/executors/BalancerV2Executor.t.sol index 6d34c9a..8999fa1 100644 --- a/foundry/test/executors/BalancerV2Executor.t.sol +++ b/foundry/test/executors/BalancerV2Executor.t.sol @@ -82,4 +82,40 @@ contract BalancerV2ExecutorTest is assertGt(balanceAfter, balanceBefore); assertEq(balanceAfter - balanceBefore, amountOut); } + + function testSwapExecutorEncoderData() public { + // Generated by the SwapEncoder - test_encode_balancer_v2 + bytes memory protocolData = + hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e01"; + + ( + IERC20 tokenIn, + IERC20 tokenOut, + bytes32 poolId, + address receiver, + bool needsApproval + ) = balancerV2Exposed.decodeParams(protocolData); + + assertEq(address(tokenIn), WETH_ADDR); + assertEq(address(tokenOut), BAL_ADDR); + assertEq(poolId, WETH_BAL_POOL_ID); + assertEq(receiver, BOB); + assertEq(needsApproval, true); + } + + function testSwapExecutorSwap() public { + // Generated by the SwapEncoder - test_encode_balancer_v2 + bytes memory protocolData = + hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e01"; + + uint256 amountIn = 10 ** 18; + deal(WETH_ADDR, address(balancerV2Exposed), amountIn); + uint256 balanceBefore = BAL.balanceOf(BOB); + + uint256 amountOut = balancerV2Exposed.swap(amountIn, protocolData); + + uint256 balanceAfter = BAL.balanceOf(BOB); + assertGt(balanceAfter, balanceBefore); + assertEq(balanceAfter - balanceBefore, amountOut); + } } diff --git a/src/encoding/evm/swap_encoder/swap_encoders.rs b/src/encoding/evm/swap_encoder/swap_encoders.rs index 6c0a894..432b2b0 100644 --- a/src/encoding/evm/swap_encoder/swap_encoders.rs +++ b/src/encoding/evm/swap_encoder/swap_encoders.rs @@ -168,7 +168,6 @@ impl SwapEncoder for BalancerV2SwapEncoder { bytes_to_address(&swap.token_out)?, component_id, bytes_to_address(&encoding_context.receiver)?, - encoding_context.exact_out, approval_needed, ); Ok(args.abi_encode_packed()) @@ -285,11 +284,12 @@ mod tests { let swap = Swap { component: balancer_pool, token_in: Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"), // WETH - token_out: Bytes::from("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"), // BAL + token_out: Bytes::from("0xba100000625a3754423978a60c9317c58a424e3D"), // BAL split: 0f64, }; let encoding_context = EncodingContext { - receiver: Bytes::from("0x0000000000000000000000000000000000000001"), + // The receiver was generated with `makeAddr("bob") using forge` + receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"), exact_out: false, router_address: Bytes::zero(20), }; @@ -299,6 +299,7 @@ mod tests { .encode_swap(swap, encoding_context) .unwrap(); let hex_swap = encode(&encoded_swap); + println!("{}", hex_swap); assert_eq!( hex_swap, @@ -306,13 +307,11 @@ mod tests { // token in "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out - "2791bca1f2de4661ed88a30c99a7a9449aa84174", + "ba100000625a3754423978a60c9317c58a424e3d", // pool id "5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014", // receiver - "0000000000000000000000000000000000000001", - // exact out - "00", + "1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e", // approval needed "01" )) From e6310d65d1cb1c37ef0cef55090bf4abbe1fb275 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Tue, 4 Feb 2025 20:56:21 +0530 Subject: [PATCH 2/2] fix: executor test naming --- foundry/test/executors/BalancerV2Executor.t.sol | 4 ++-- foundry/test/executors/UniswapV2Executor.t.sol | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/foundry/test/executors/BalancerV2Executor.t.sol b/foundry/test/executors/BalancerV2Executor.t.sol index 8999fa1..f537cbf 100644 --- a/foundry/test/executors/BalancerV2Executor.t.sol +++ b/foundry/test/executors/BalancerV2Executor.t.sol @@ -83,7 +83,7 @@ contract BalancerV2ExecutorTest is assertEq(balanceAfter - balanceBefore, amountOut); } - function testSwapExecutorEncoderData() public { + function testDecodeIntegration() public { // Generated by the SwapEncoder - test_encode_balancer_v2 bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e01"; @@ -103,7 +103,7 @@ contract BalancerV2ExecutorTest is assertEq(needsApproval, true); } - function testSwapExecutorSwap() public { + function testSwapIntegration() public { // Generated by the SwapEncoder - test_encode_balancer_v2 bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e01"; diff --git a/foundry/test/executors/UniswapV2Executor.t.sol b/foundry/test/executors/UniswapV2Executor.t.sol index 9c7066f..765d79c 100644 --- a/foundry/test/executors/UniswapV2Executor.t.sol +++ b/foundry/test/executors/UniswapV2Executor.t.sol @@ -94,7 +94,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { assertGe(finalBalance, amountOut); } - function testSwapExecutorEncoderData() public { + function testDecodeIntegration() public { // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f5640000000000000000000000000000000000000000100"; @@ -108,7 +108,7 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants { assertEq(zeroForOne, false); } - function testSwapExecutorSwap() public { + function testSwapIntegration() public { // Generated by the ExecutorStrategyEncoder - test_executor_strategy_encode bytes memory protocolData = hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb111d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e00";