Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks: Implement balancer v2 swapper task #148

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/tasks/contracts/interfaces/swap/IBalancerV2Swapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity >=0.8.0;

import './IBaseSwapTask.sol';

/**
* @dev Balancer v2 swapper task interface
*/
interface IBalancerV2Swapper is IBaseSwapTask {
/**
* @dev Execution function
*/
function call(address tokenIn, uint256 amountIn, uint256 slippage) external;
}
117 changes: 117 additions & 0 deletions packages/tasks/contracts/swap/BalancerV2Swapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity ^0.8.0;

import '@mimic-fi/v3-helpers/contracts/math/FixedPoint.sol';
import '@mimic-fi/v3-helpers/contracts/utils/BytesHelpers.sol';
import '@mimic-fi/v3-connectors/contracts/interfaces/balancer/IBalancerV2SwapConnector.sol';

import './BaseSwapTask.sol';
import '../interfaces/swap/IBalancerV2Swapper.sol';
import '../interfaces/liquidity/balancer/IBalancerPool.sol';

/**
* @title Balancer v2 swapper task
* @dev Task that extends the base swap task to use Balancer
*/
contract BalancerV2Swapper is IBalancerV2Swapper, BaseSwapTask {
using FixedPoint for uint256;
using BytesHelpers for bytes;

// Execution type for relayers
bytes32 public constant override EXECUTION_TYPE = keccak256('BALANCER_V2_SWAPPER');

/**
* @dev Balancer v2 swap config. Only used in the initalizer
*/
struct BalancerV2SwapConfig {
BaseSwapConfig baseSwapConfig;
}

/**
* @dev Initializes the Balancer v2 swapper
* @param config Balancer v2 swap config
*/
function initialize(BalancerV2SwapConfig memory config) external initializer {
__BalancerV2Swapper_init(config);
}

/**
* @dev Initializes the Balancer v2 swapper. It does call upper contracts.
* @param config Balancer v2 swap config
*/
function __BalancerV2Swapper_init(BalancerV2SwapConfig memory config) internal onlyInitializing {
__BaseSwapTask_init(config.baseSwapConfig);
__BalancerV2Swapper_init_unchained(config);
}

/**
* @dev Initializes the balancer swapper. It does not call upper contracts
PatricioHenderson marked this conversation as resolved.
Show resolved Hide resolved
* @param config Balancer V2 swap config
*/
function __BalancerV2Swapper_init_unchained(BalancerV2SwapConfig memory config) internal onlyInitializing {
// solhint-disable-previous-line no-empty-blocks
}

/**
* @dev Executes the Balancer v2 swapper task
*/
function call(address tokenIn, uint256 amountIn, uint256 slippage)
external
override
authP(authParams(tokenIn, amountIn, slippage))
{
if (amountIn == 0) amountIn = getTaskAmount(tokenIn);
_beforeBalancerV2Swapper(tokenIn, amountIn, slippage);

address tokenOut = getTokenOut(tokenIn);
uint256 price = _getPrice(tokenIn, tokenOut);
uint256 minAmountOut = amountIn.mulUp(price).mulUp(FixedPoint.ONE - slippage);

bytes memory connectorData = abi.encodeWithSelector(
IBalancerV2SwapConnector.execute.selector,
tokenIn,
tokenOut,
amountIn,
minAmountOut,
IBalancerPool(tokenIn).getPoolId(),
PatricioHenderson marked this conversation as resolved.
Show resolved Hide resolved
new bytes32[](0),
new address[](0)
);

bytes memory result = ISmartVault(smartVault).execute(connector, connectorData);
_afterBalancerV2Swapper(tokenIn, amountIn, slippage, tokenOut, result.toUint256());
}

/**
* @dev Before Balancer V2 swapper hook
*/
function _beforeBalancerV2Swapper(address token, uint256 amount, uint256 slippage) internal virtual {
_beforeBaseSwapTask(token, amount, slippage);
}

/**
* @dev After Balancer v2 swapper hook
*/
function _afterBalancerV2Swapper(
address tokenIn,
uint256 amountIn,
uint256 slippage,
address tokenOut,
uint256 amountOut
) internal virtual {
_afterBaseSwapTask(tokenIn, amountIn, slippage, tokenOut, amountOut);
}
}
53 changes: 53 additions & 0 deletions packages/tasks/test/swap/BalancerV2Swapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { deploy, deployProxy, getSigners, ZERO_ADDRESS } from '@mimic-fi/v3-helpers'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/dist/src/signer-with-address'
import { Contract } from 'ethers'

import { buildEmptyTaskConfig, deployEnvironment } from '../../src/setup'
import { itBehavesLikeBaseSwapTask } from './BaseSwapTask.behavior'

describe('BalancerV2Swapper', () => {
let task: Contract
let smartVault: Contract, authorizer: Contract, connector: Contract, owner: SignerWithAddress

before('setup', async () => {
// eslint-disable-next-line prettier/prettier
[, owner] = await getSigners()
;({ authorizer, smartVault } = await deployEnvironment(owner))
})

before('deploy connector', async () => {
connector = await deploy('BalancerV2SwapConnectorMock', [ZERO_ADDRESS])
const overrideConnectorCheckRole = smartVault.interface.getSighash('overrideConnectorCheck')
await authorizer.connect(owner).authorize(owner.address, smartVault.address, overrideConnectorCheckRole, [])
await smartVault.connect(owner).overrideConnectorCheck(connector.address, true)
})

beforeEach('deploy task', async () => {
task = await deployProxy(
'BalancerV2Swapper',
[],
[
{
baseSwapConfig: {
connector: connector.address,
tokenOut: ZERO_ADDRESS,
maxSlippage: 0,
customTokensOut: [],
customMaxSlippages: [],
taskConfig: buildEmptyTaskConfig(owner, smartVault),
},
},
]
)
})

describe('swapper', () => {
beforeEach('set params', async function () {
this.owner = owner
this.task = task
this.authorizer = authorizer
})

itBehavesLikeBaseSwapTask('BALANCER_V2_SWAPPER')
})
})
Loading