chore: merge main
This commit is contained in:
1
foundry/lib/balancer-v2-monorepo
Submodule
1
foundry/lib/balancer-v2-monorepo
Submodule
Submodule foundry/lib/balancer-v2-monorepo added at 36d282374b
@@ -2,4 +2,5 @@
|
||||
@interfaces/=interfaces/
|
||||
@permit2/=lib/permit2/
|
||||
@src/=src/
|
||||
@uniswap-v2/=lib/v2-core/
|
||||
@uniswap-v2/=lib/v2-core/
|
||||
@balancer-labs/v2-interfaces=lib/balancer-v2-monorepo/pkg/interfaces
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"filter_paths": "lib"
|
||||
}
|
||||
}
|
||||
|
||||
83
foundry/src/executors/BalancerV2Executor.sol
Normal file
83
foundry/src/executors/BalancerV2Executor.sol
Normal file
@@ -0,0 +1,83 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.28;
|
||||
|
||||
import "@interfaces/IExecutor.sol";
|
||||
import {
|
||||
IERC20,
|
||||
SafeERC20
|
||||
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
// slither-disable-next-line solc-version
|
||||
import {IAsset} from "@balancer-labs/v2-interfaces/contracts/vault/IAsset.sol";
|
||||
// slither-disable-next-line solc-version
|
||||
import {IVault} from "@balancer-labs/v2-interfaces/contracts/vault/IVault.sol";
|
||||
|
||||
error BalancerV2Executor__InvalidDataLength();
|
||||
|
||||
contract BalancerV2Executor is IExecutor {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
address private constant VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
|
||||
|
||||
// slither-disable-next-line locked-ether
|
||||
function swap(uint256 givenAmount, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
(
|
||||
IERC20 tokenIn,
|
||||
IERC20 tokenOut,
|
||||
bytes32 poolId,
|
||||
address receiver,
|
||||
bool needsApproval
|
||||
) = _decodeData(data);
|
||||
|
||||
if (needsApproval) {
|
||||
// slither-disable-next-line unused-return
|
||||
tokenIn.approve(VAULT, type(uint256).max);
|
||||
}
|
||||
|
||||
IVault.SingleSwap memory singleSwap = IVault.SingleSwap({
|
||||
poolId: poolId,
|
||||
kind: IVault.SwapKind.GIVEN_IN,
|
||||
assetIn: IAsset(address(tokenIn)),
|
||||
assetOut: IAsset(address(tokenOut)),
|
||||
amount: givenAmount,
|
||||
userData: ""
|
||||
});
|
||||
|
||||
IVault.FundManagement memory funds = IVault.FundManagement({
|
||||
sender: address(this),
|
||||
fromInternalBalance: false,
|
||||
recipient: payable(receiver),
|
||||
toInternalBalance: false
|
||||
});
|
||||
|
||||
uint256 limit = 0;
|
||||
|
||||
calculatedAmount =
|
||||
IVault(VAULT).swap(singleSwap, funds, limit, block.timestamp);
|
||||
}
|
||||
|
||||
function _decodeData(bytes calldata data)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
IERC20 tokenIn,
|
||||
IERC20 tokenOut,
|
||||
bytes32 poolId,
|
||||
address receiver,
|
||||
bool needsApproval
|
||||
)
|
||||
{
|
||||
if (data.length != 93) {
|
||||
revert BalancerV2Executor__InvalidDataLength();
|
||||
}
|
||||
|
||||
tokenIn = IERC20(address(bytes20(data[0:20])));
|
||||
tokenOut = IERC20(address(bytes20(data[20:40])));
|
||||
poolId = bytes32(data[40:72]);
|
||||
receiver = address(bytes20(data[72:92]));
|
||||
needsApproval = uint8(data[92]) > 0;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ contract Constants is Test {
|
||||
// Assets
|
||||
address WETH_ADDR = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
|
||||
address DAI_ADDR = address(0x6B175474E89094C44Da98b954EedeAC495271d0F);
|
||||
address BAL_ADDR = address(0xba100000625a3754423978a60c9317c58a424e3D);
|
||||
address USDC_ADDR = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
|
||||
address WBTC_ADDR = address(0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599);
|
||||
|
||||
|
||||
85
foundry/test/executors/BalancerV2Executor.t.sol
Normal file
85
foundry/test/executors/BalancerV2Executor.t.sol
Normal file
@@ -0,0 +1,85 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.28;
|
||||
|
||||
import "@src/executors/BalancerV2Executor.sol";
|
||||
import {Test} from "../../lib/forge-std/src/Test.sol";
|
||||
import {Constants} from "../Constants.sol";
|
||||
|
||||
contract BalancerV2ExecutorExposed is BalancerV2Executor {
|
||||
function decodeParams(bytes calldata data)
|
||||
external
|
||||
pure
|
||||
returns (
|
||||
IERC20 tokenIn,
|
||||
IERC20 tokenOut,
|
||||
bytes32 poolId,
|
||||
address receiver,
|
||||
bool needsApproval
|
||||
)
|
||||
{
|
||||
return _decodeData(data);
|
||||
}
|
||||
}
|
||||
|
||||
contract BalancerV2ExecutorTest is
|
||||
BalancerV2ExecutorExposed,
|
||||
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();
|
||||
}
|
||||
|
||||
function testDecodeParams() public view {
|
||||
bytes memory params = abi.encodePacked(
|
||||
WETH_ADDR, BAL_ADDR, WETH_BAL_POOL_ID, address(2), true
|
||||
);
|
||||
|
||||
(
|
||||
IERC20 tokenIn,
|
||||
IERC20 tokenOut,
|
||||
bytes32 poolId,
|
||||
address receiver,
|
||||
bool needsApproval
|
||||
) = 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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user