- This needed to be in BalancerV2 so that the executor can also take care of transfers from the user into the tycho router, to avoid having transfer actions mixed between the router and executors
136 lines
4.6 KiB
Solidity
136 lines
4.6 KiB
Solidity
// SPDX-License-Identifier: BUSL-1.1
|
|
pragma solidity ^0.8.26;
|
|
|
|
import "@src/executors/BalancerV2Executor.sol";
|
|
import {Test} from "../../lib/forge-std/src/Test.sol";
|
|
import {Constants} from "../Constants.sol";
|
|
|
|
contract BalancerV2ExecutorExposed is BalancerV2Executor {
|
|
constructor(address _permit2) BalancerV2Executor(_permit2) {}
|
|
|
|
function decodeParams(bytes calldata data)
|
|
external
|
|
pure
|
|
returns (
|
|
IERC20 tokenIn,
|
|
IERC20 tokenOut,
|
|
bytes32 poolId,
|
|
address receiver,
|
|
bool needsApproval,
|
|
TransferType transferType
|
|
)
|
|
{
|
|
return _decodeData(data);
|
|
}
|
|
}
|
|
|
|
contract BalancerV2ExecutorTest is Test, Constants {
|
|
using SafeERC20 for IERC20;
|
|
|
|
BalancerV2ExecutorExposed balancerV2Exposed;
|
|
IERC20 WETH = IERC20(WETH_ADDR);
|
|
IERC20 BAL = IERC20(BAL_ADDR);
|
|
bytes32 constant WETH_BAL_POOL_ID =
|
|
0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014;
|
|
|
|
function setUp() public {
|
|
uint256 forkBlock = 17323404;
|
|
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
|
balancerV2Exposed = new BalancerV2ExecutorExposed(PERMIT2_ADDRESS);
|
|
}
|
|
|
|
function testDecodeParams() public view {
|
|
bytes memory params = abi.encodePacked(
|
|
WETH_ADDR,
|
|
BAL_ADDR,
|
|
WETH_BAL_POOL_ID,
|
|
address(2),
|
|
true,
|
|
TokenTransfer.TransferType.NONE
|
|
);
|
|
|
|
(
|
|
IERC20 tokenIn,
|
|
IERC20 tokenOut,
|
|
bytes32 poolId,
|
|
address receiver,
|
|
bool needsApproval,
|
|
TokenTransfer.TransferType transferType
|
|
) = balancerV2Exposed.decodeParams(params);
|
|
|
|
assertEq(address(tokenIn), WETH_ADDR);
|
|
assertEq(address(tokenOut), BAL_ADDR);
|
|
assertEq(poolId, WETH_BAL_POOL_ID);
|
|
assertEq(receiver, address(2));
|
|
assertEq(needsApproval, true);
|
|
assertEq(uint8(transferType), uint8(TokenTransfer.TransferType.NONE));
|
|
}
|
|
|
|
function testDecodeParamsInvalidDataLength() public {
|
|
bytes memory invalidParams =
|
|
abi.encodePacked(WETH_ADDR, BAL_ADDR, WETH_BAL_POOL_ID, address(2));
|
|
|
|
vm.expectRevert(BalancerV2Executor__InvalidDataLength.selector);
|
|
balancerV2Exposed.decodeParams(invalidParams);
|
|
}
|
|
|
|
function testSwap() public {
|
|
uint256 amountIn = 10 ** 18;
|
|
bytes memory protocolData = abi.encodePacked(
|
|
WETH_ADDR,
|
|
BAL_ADDR,
|
|
WETH_BAL_POOL_ID,
|
|
BOB,
|
|
true,
|
|
TokenTransfer.TransferType.NONE
|
|
);
|
|
|
|
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);
|
|
}
|
|
|
|
function testDecodeIntegration() public view {
|
|
// Generated by the SwapEncoder - test_encode_balancer_v2
|
|
bytes memory protocolData =
|
|
hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e0105";
|
|
|
|
(
|
|
IERC20 tokenIn,
|
|
IERC20 tokenOut,
|
|
bytes32 poolId,
|
|
address receiver,
|
|
bool needsApproval,
|
|
TokenTransfer.TransferType transferType
|
|
) = 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);
|
|
assertEq(uint8(transferType), uint8(TokenTransfer.TransferType.NONE));
|
|
}
|
|
|
|
function testSwapIntegration() public {
|
|
// Generated by the SwapEncoder - test_encode_balancer_v2
|
|
bytes memory protocolData =
|
|
hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e0105";
|
|
|
|
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);
|
|
}
|
|
}
|