|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +// Gearbox Protocol. Generalized leverage for DeFi protocols |
| 3 | +// (c) Gearbox Foundation, 2024. |
| 4 | +pragma solidity ^0.8.23; |
| 5 | + |
| 6 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | +import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; |
| 8 | + |
| 9 | +import {RAY} from "@gearbox-protocol/core-v3/contracts/libraries/Constants.sol"; |
| 10 | + |
| 11 | +import {AbstractAdapter} from "../AbstractAdapter.sol"; |
| 12 | + |
| 13 | +import {IBalancerV3Router} from "../../integrations/balancer/IBalancerV3Router.sol"; |
| 14 | +import {IBalancerV3Pool} from "../../integrations/balancer/IBalancerV3Pool.sol"; |
| 15 | +import {IBalancerV3RouterAdapter} from "../../interfaces/balancer/IBalancerV3RouterAdapter.sol"; |
| 16 | + |
| 17 | +/// @title Balancer V3 Router adapter |
| 18 | +/// @notice Implements logic allowing CAs to perform swaps via Balancer V3 |
| 19 | +contract BalancerV3RouterAdapter is AbstractAdapter, IBalancerV3RouterAdapter { |
| 20 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 21 | + |
| 22 | + bytes32 public constant override contractType = "ADAPTER::BALANCER_V3_ROUTER"; |
| 23 | + uint256 public constant override version = 3_10; |
| 24 | + |
| 25 | + /// @dev Mapping from pool address to whether it can be traded through the adapter |
| 26 | + mapping(address => bool) internal _poolStatus; |
| 27 | + |
| 28 | + /// @dev Set of all pools that are currently allowed |
| 29 | + EnumerableSet.AddressSet internal _allowedPools; |
| 30 | + |
| 31 | + /// @notice Constructor |
| 32 | + /// @param _creditManager Credit manager address |
| 33 | + /// @param _router Balancer V3 Router address |
| 34 | + constructor(address _creditManager, address _router) AbstractAdapter(_creditManager, _router) {} |
| 35 | + |
| 36 | + /// @notice Swaps given amount of input token for output token through a single pool |
| 37 | + /// @param pool Pool address |
| 38 | + /// @param tokenIn Input token |
| 39 | + /// @param tokenOut Output token |
| 40 | + /// @param exactAmountIn Exact amount of input token to swap |
| 41 | + /// @param minAmountOut Minimum amount of output token to receive |
| 42 | + /// @param deadline Deadline for the swap |
| 43 | + /// @dev wethIsEth and userData are ignored, since they are always set to false and empty |
| 44 | + function swapSingleTokenExactIn( |
| 45 | + address pool, |
| 46 | + IERC20 tokenIn, |
| 47 | + IERC20 tokenOut, |
| 48 | + uint256 exactAmountIn, |
| 49 | + uint256 minAmountOut, |
| 50 | + uint256 deadline, |
| 51 | + bool, |
| 52 | + bytes calldata |
| 53 | + ) external override creditFacadeOnly returns (bool) { |
| 54 | + if (!isPoolAllowed(pool)) revert InvalidPoolException(); |
| 55 | + |
| 56 | + _executeSwapSafeApprove( |
| 57 | + address(tokenIn), |
| 58 | + abi.encodeCall( |
| 59 | + IBalancerV3Router.swapSingleTokenExactIn, |
| 60 | + (pool, tokenIn, tokenOut, exactAmountIn, minAmountOut, deadline, false, "") |
| 61 | + ) |
| 62 | + ); |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + /// @notice Swaps all balance of input token for output token through a single pool, except the specified amount |
| 67 | + /// @param pool Pool address |
| 68 | + /// @param tokenIn Input token |
| 69 | + /// @param tokenOut Output token |
| 70 | + /// @param leftoverAmount Amount of input token to keep |
| 71 | + /// @param rateMinRAY Minimum exchange rate [RAY] |
| 72 | + /// @param deadline Deadline for the swap |
| 73 | + function swapSingleTokenDiffIn( |
| 74 | + address pool, |
| 75 | + IERC20 tokenIn, |
| 76 | + IERC20 tokenOut, |
| 77 | + uint256 leftoverAmount, |
| 78 | + uint256 rateMinRAY, |
| 79 | + uint256 deadline |
| 80 | + ) external override creditFacadeOnly returns (bool) { |
| 81 | + if (!isPoolAllowed(pool)) revert InvalidPoolException(); |
| 82 | + |
| 83 | + address creditAccount = _creditAccount(); |
| 84 | + |
| 85 | + uint256 amount = IERC20(address(tokenIn)).balanceOf(creditAccount); |
| 86 | + if (amount <= leftoverAmount) return false; |
| 87 | + unchecked { |
| 88 | + amount -= leftoverAmount; |
| 89 | + } |
| 90 | + |
| 91 | + _executeSwapSafeApprove( |
| 92 | + address(tokenIn), |
| 93 | + abi.encodeCall( |
| 94 | + IBalancerV3Router.swapSingleTokenExactIn, |
| 95 | + (pool, tokenIn, tokenOut, amount, (amount * rateMinRAY) / RAY, deadline, false, "") |
| 96 | + ) |
| 97 | + ); |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + // ------------- // |
| 102 | + // CONFIGURATION // |
| 103 | + // ------------- // |
| 104 | + |
| 105 | + /// @notice Returns whether the pool is allowed to be traded through the adapter |
| 106 | + function isPoolAllowed(address pool) public view override returns (bool) { |
| 107 | + return _poolStatus[pool]; |
| 108 | + } |
| 109 | + |
| 110 | + /// @notice Returns the list of all pools that were ever allowed in this adapter |
| 111 | + function getAllowedPools() public view override returns (address[] memory pools) { |
| 112 | + return _allowedPools.values(); |
| 113 | + } |
| 114 | + |
| 115 | + /// @notice Sets status for a batch of pools |
| 116 | + /// @param pools Array of pool addresses |
| 117 | + /// @param statuses Array of pool statuses |
| 118 | + function setPoolStatusBatch(address[] calldata pools, bool[] calldata statuses) |
| 119 | + external |
| 120 | + override |
| 121 | + configuratorOnly |
| 122 | + { |
| 123 | + uint256 len = pools.length; |
| 124 | + if (len != statuses.length) revert InvalidLengthException(); |
| 125 | + unchecked { |
| 126 | + for (uint256 i; i < len; ++i) { |
| 127 | + address pool = pools[i]; |
| 128 | + bool status = statuses[i]; |
| 129 | + |
| 130 | + if (status) { |
| 131 | + // Verify that all tokens in the pool are valid collaterals |
| 132 | + IERC20[] memory tokens = IBalancerV3Pool(pool).getTokens(); |
| 133 | + for (uint256 j; j < tokens.length; ++j) { |
| 134 | + _getMaskOrRevert(address(tokens[j])); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + _poolStatus[pool] = status; |
| 139 | + if (status) { |
| 140 | + _allowedPools.add(pool); |
| 141 | + } else { |
| 142 | + _allowedPools.remove(pool); |
| 143 | + } |
| 144 | + emit SetPoolStatus(pool, status); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // ---- // |
| 150 | + // DATA // |
| 151 | + // ---- // |
| 152 | + |
| 153 | + /// @notice Serialized adapter parameters |
| 154 | + function serialize() external view returns (bytes memory serializedData) { |
| 155 | + serializedData = abi.encode(creditManager, targetContract, getAllowedPools()); |
| 156 | + } |
| 157 | +} |
0 commit comments