This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BalancerV2Swap.sol
66 lines (50 loc) · 2.8 KB
/
BalancerV2Swap.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: UNLICENSED
// Copyright (c) 2023 Tokemak Foundation. All rights reserved.
pragma solidity 0.8.17;
import { IERC20, SafeERC20 } from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
import { Errors } from "src/utils/Errors.sol";
import { IVault } from "src/interfaces/external/balancer/IVault.sol";
import { IAsset } from "src/interfaces/external/balancer/IAsset.sol";
import { ISwapRouter } from "src/interfaces/swapper/ISwapRouter.sol";
import { IBasePool } from "src/interfaces/external/balancer/IBasePool.sol";
import { BaseAdapter, ISyncSwapper } from "src/swapper/adapters/BaseAdapter.sol";
contract BalancerV2Swap is BaseAdapter {
using SafeERC20 for IERC20;
IVault public immutable vault;
constructor(address _router, address _balancerVault) BaseAdapter(_router) {
Errors.verifyNotZero(_balancerVault, "_balancerVault");
vault = IVault(_balancerVault);
}
/// @inheritdoc ISyncSwapper
function validate(address fromAddress, ISwapRouter.SwapData memory swapData) external view override {
bytes32 poolId = abi.decode(swapData.data, (bytes32));
bytes32 id = IBasePool(swapData.pool).getPoolId();
// verify that the swapData.pool has the same id as the encoded poolId
if (id != poolId) revert DataMismatch("poolId");
// verify that the fromAddress and toAddress are in the pool. getPoolTokenInfo will revert if not
// slither-disable-start low-level-calls,missing-zero-check,unchecked-lowlevel
string memory funcSelector = "getPoolTokenInfo(bytes32,address)";
// solhint-disable-next-line avoid-low-level-calls
(bool success,) = address(vault).staticcall(abi.encodeWithSignature(funcSelector, poolId, fromAddress));
if (!success) revert DataMismatch("fromAddress");
(success,) = address(vault).staticcall(abi.encodeWithSignature(funcSelector, poolId, swapData.token));
if (!success) revert DataMismatch("toAddress");
// slither-disable-end low-level-calls,missing-zero-check,unchecked-lowlevel
}
/// @inheritdoc ISyncSwapper
function swap(
address,
address sellTokenAddress,
uint256 sellAmount,
address buyTokenAddress,
uint256 minBuyAmount,
bytes memory data
) external override onlyRouter returns (uint256) {
bytes32 poolId = abi.decode(data, (bytes32));
IVault.SingleSwap memory singleSwap =
IVault.SingleSwap(poolId, IVault.SwapKind.GIVEN_IN, sellTokenAddress, buyTokenAddress, sellAmount, "");
IVault.FundManagement memory funds = IVault.FundManagement(address(this), false, payable(address(this)), false);
IERC20(sellTokenAddress).safeApprove(address(vault), sellAmount);
return vault.swap(singleSwap, funds, minBuyAmount, block.timestamp);
}
}