chore: Add BalancerSwapExecutor

This commit is contained in:
Diana Carvalho
2024-08-23 17:58:00 +01:00
parent d1e21a8d63
commit e49af99b1f
6 changed files with 259 additions and 9 deletions

View File

@@ -0,0 +1,81 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "./SwapExecutorTest.sol";
import "../src/balancer-v2/BalancerSwapExecutor.sol";
contract TestBalancerSwapExecutor is SwapExecutorTest {
BalancerSwapExecutor balancer;
IERC20 USDC = IERC20(USDC_ADDR);
IERC20 USDT = IERC20(USDT_ADDR);
constructor() {}
function setUp() public {
//Fork
uint256 forkBlock = 16000000;
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
//Setup
balancer = new BalancerSwapExecutor();
}
function testBalancerSwap() public {
//Set up
uint256 sellAmount = 1000_000000;
uint256 expectedAmount = 998_919380; //Swap 1k USDT for 998 USDC
bool exactOut = false;
// This is required because balancer does a transferFrom sender.
// That also means we need to do this approval with our swapRouter.
bool tokenApprovalNeeded = true;
bytes memory protocolData = abi.encodePacked(
USDT_ADDR,
USDC_ADDR,
DAI_USDC_USDT_balancer,
bob,
exactOut,
tokenApprovalNeeded
);
// Logic
vm.prank(address(balancer));
deal(USDT_ADDR, address(balancer), sellAmount);
vm.prank(executor);
uint256 responseAmount = balancer.swap(sellAmount, protocolData);
//Assertions
assertEq(responseAmount, expectedAmount);
assertEq(USDC.balanceOf(bob), expectedAmount);
assertEq(USDT.balanceOf(address(balancer)), 0);
}
function testBalancerExactOutSwap() public {
//Set up
uint256 buyAmount = 1000_979168;
uint256 expectedSellAmount = 1000 * 10 ** 6;
bool exactOut = true;
bool tokenApprovalNeeded = true;
bytes memory protocolData = abi.encodePacked(
USDC_ADDR,
USDT_ADDR,
DAI_USDC_USDT_balancer,
bob,
exactOut,
tokenApprovalNeeded
);
//Logic
// This is required because balancer does a transferFrom sender.
// That also means we need to do this approval with our swapRouter.
vm.prank(address(balancer));
deal(USDC_ADDR, address(balancer), expectedSellAmount);
vm.prank(executor);
uint256 responseAmount = balancer.swap(buyAmount, protocolData);
// //Assertions
assertEq(responseAmount, expectedSellAmount);
assertEq(USDT.balanceOf(bob), buyAmount);
assertEq(USDC.balanceOf(address(balancer)), 0);
}
}

23
evm/test/Constants.sol Normal file
View File

@@ -0,0 +1,23 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Constants {
address executor = address(1239501235123412541234); //executor=us || cowswap
address admin = address(12395012351212343412541234); //admin=us
address bob = address(123); //bob=someone!=us
// tokens
address WETH_ADDR = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address USDT_ADDR = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address USDC_ADDR = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address DAI_ADDR = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
address WBTC_ADDR = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;
address LDO_ADDR = 0x5A98FcBEA516Cf06857215779Fd812CA3beF1B32;
address CRV_ADDR = 0xD533a949740bb3306d119CC777fa900bA034cd52;
// balancer
address balancerVault = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
bytes32 DAI_USDC_USDT_balancer = bytes32(
0x06df3b2bbb68adc8b0e302443692037ed9f91b42000000000000000000000063
);
}

View File

@@ -0,0 +1,18 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "openzeppelin-contracts/contracts/interfaces/IERC20.sol";
import "./Constants.sol";
contract SwapExecutorTest is Test, Constants {
function twoTokens(address token0, address token1)
internal
pure
returns (IERC20[] memory tokens)
{
tokens = new IERC20[](2);
tokens[0] = IERC20(token0);
tokens[1] = IERC20(token1);
}
}