|
| 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.17; |
| 5 | + |
| 6 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | +import {RAY} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol"; |
| 8 | +import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; |
| 9 | + |
| 10 | +import {AbstractAdapter} from "../AbstractAdapter.sol"; |
| 11 | +import {AdapterType} from "@gearbox-protocol/sdk-gov/contracts/AdapterType.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 | + AdapterType public constant override _gearboxAdapterType = AdapterType.BALANCER_V3_ROUTER; |
| 23 | + uint16 public constant override _gearboxAdapterVersion = 3_00; |
| 24 | + |
| 25 | + /// @dev Set of all pools that are currently allowed |
| 26 | + EnumerableSet.AddressSet internal _allowedPools; |
| 27 | + |
| 28 | + /// @notice Constructor |
| 29 | + /// @param _creditManager Credit manager address |
| 30 | + /// @param _router Balancer V3 Router address |
| 31 | + constructor(address _creditManager, address _router) AbstractAdapter(_creditManager, _router) {} |
| 32 | + |
| 33 | + /// @notice Swaps given amount of input token for output token through a single pool |
| 34 | + /// @param pool Pool address |
| 35 | + /// @param tokenIn Input token |
| 36 | + /// @param tokenOut Output token |
| 37 | + /// @param exactAmountIn Exact amount of input token to swap |
| 38 | + /// @param minAmountOut Minimum amount of output token to receive |
| 39 | + /// @param deadline Deadline for the swap |
| 40 | + /// @dev wethIsEth and userData are ignored, since they are always set to false and empty |
| 41 | + function swapSingleTokenExactIn( |
| 42 | + address pool, |
| 43 | + IERC20 tokenIn, |
| 44 | + IERC20 tokenOut, |
| 45 | + uint256 exactAmountIn, |
| 46 | + uint256 minAmountOut, |
| 47 | + uint256 deadline, |
| 48 | + bool, |
| 49 | + bytes calldata |
| 50 | + ) external override creditFacadeOnly returns (uint256 tokensToEnable, uint256 tokensToDisable) { |
| 51 | + if (!isPoolAllowed(pool)) revert InvalidPoolException(); |
| 52 | + |
| 53 | + (tokensToEnable, tokensToDisable,) = _executeSwapSafeApprove( |
| 54 | + address(tokenIn), |
| 55 | + address(tokenOut), |
| 56 | + abi.encodeCall( |
| 57 | + IBalancerV3Router.swapSingleTokenExactIn, |
| 58 | + (pool, tokenIn, tokenOut, exactAmountIn, minAmountOut, deadline, false, "") |
| 59 | + ), |
| 60 | + false |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /// @notice Swaps all balance of input token for output token through a single pool, except the specified amount |
| 65 | + /// @param pool Pool address |
| 66 | + /// @param tokenIn Input token |
| 67 | + /// @param tokenOut Output token |
| 68 | + /// @param leftoverAmount Amount of input token to keep |
| 69 | + /// @param rateMinRAY Minimum exchange rate [RAY] |
| 70 | + /// @param deadline Deadline for the swap |
| 71 | + function swapSingleTokenDiffIn( |
| 72 | + address pool, |
| 73 | + IERC20 tokenIn, |
| 74 | + IERC20 tokenOut, |
| 75 | + uint256 leftoverAmount, |
| 76 | + uint256 rateMinRAY, |
| 77 | + uint256 deadline |
| 78 | + ) external override creditFacadeOnly returns (uint256 tokensToEnable, uint256 tokensToDisable) { |
| 79 | + if (!isPoolAllowed(pool)) revert InvalidPoolException(); |
| 80 | + |
| 81 | + address creditAccount = _creditAccount(); |
| 82 | + |
| 83 | + uint256 amount = tokenIn.balanceOf(creditAccount); |
| 84 | + if (amount <= leftoverAmount) return (0, 0); |
| 85 | + unchecked { |
| 86 | + amount -= leftoverAmount; |
| 87 | + } |
| 88 | + |
| 89 | + (tokensToEnable, tokensToDisable,) = _executeSwapSafeApprove( |
| 90 | + address(tokenIn), |
| 91 | + address(tokenOut), |
| 92 | + abi.encodeCall( |
| 93 | + IBalancerV3Router.swapSingleTokenExactIn, |
| 94 | + (pool, tokenIn, tokenOut, amount, (amount * rateMinRAY) / RAY, deadline, false, "") |
| 95 | + ), |
| 96 | + leftoverAmount <= 1 |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // ------------- // |
| 101 | + // CONFIGURATION // |
| 102 | + // ------------- // |
| 103 | + |
| 104 | + /// @notice Returns whether the pool is allowed to be traded through the adapter |
| 105 | + function isPoolAllowed(address pool) public view override returns (bool) { |
| 106 | + return _allowedPools.contains(pool); |
| 107 | + } |
| 108 | + |
| 109 | + /// @notice Returns the list of all pools that were ever allowed in this adapter |
| 110 | + function getAllowedPools() external view override returns (address[] memory pools) { |
| 111 | + return _allowedPools.values(); |
| 112 | + } |
| 113 | + |
| 114 | + /// @notice Sets status for a batch of pools |
| 115 | + /// @param pools Array of pool addresses |
| 116 | + /// @param statuses Array of pool statuses |
| 117 | + function setPoolStatusBatch(address[] calldata pools, bool[] calldata statuses) |
| 118 | + external |
| 119 | + override |
| 120 | + configuratorOnly |
| 121 | + { |
| 122 | + uint256 len = pools.length; |
| 123 | + if (len != statuses.length) revert InvalidLengthException(); |
| 124 | + unchecked { |
| 125 | + for (uint256 i; i < len; ++i) { |
| 126 | + address pool = pools[i]; |
| 127 | + bool status = statuses[i]; |
| 128 | + |
| 129 | + if (status) { |
| 130 | + _allowedPools.add(pool); |
| 131 | + } else { |
| 132 | + _allowedPools.remove(pool); |
| 133 | + } |
| 134 | + emit SetPoolStatus(pool, status); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments