-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathBancorSwapper.sol
67 lines (55 loc) · 2.06 KB
/
BancorSwapper.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
67
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4 <0.9.0;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import './AsyncSwapper.sol';
import 'hardhat/console.sol';
interface IContractRegistry {
function addressOf(bytes32 contractName) external returns (address);
}
interface IBancorNetwork {
function convertByPath(
address[] memory _path,
uint256 _amount,
uint256 _minReturn,
address _beneficiary,
address _affiliateAccount,
uint256 _affiliateFee
) external payable returns (uint256);
function rateByPath(address[] memory _path, uint256 _amount) external view returns (uint256);
function conversionPath(address _sourceToken, address _targetToken) external view returns (address[] memory);
function convert(
address[] memory path,
uint256 amount,
uint256 minReturn
) external payable returns (uint256 returnAmount);
}
interface IBancorSwapper is IAsyncSwapper {}
contract BancorSwapper is IBancorSwapper, AsyncSwapper {
using SafeERC20 for IERC20;
IContractRegistry public contractRegistry;
bytes32 public bancorNetworkName;
constructor(
address _governor,
address _tradeFactory,
IContractRegistry _contractRegistry,
bytes32 _bancorNetworkName
) AsyncSwapper(_governor, _tradeFactory) {
contractRegistry = _contractRegistry;
bancorNetworkName = _bancorNetworkName;
}
function _executeSwap(
address _receiver,
address _tokenIn,
address _tokenOut,
uint256 _amountIn,
bytes calldata _data
) internal override {
address[] memory _path = abi.decode(_data, (address[]));
if (_tokenIn != _path[0] || _tokenOut != _path[_path.length - 1]) revert CommonErrors.IncorrectSwapInformation();
IBancorNetwork _bancorNetwork = IBancorNetwork(contractRegistry.addressOf(bancorNetworkName));
IERC20(_tokenIn).approve(address(_bancorNetwork), 0);
IERC20(_tokenIn).approve(address(_bancorNetwork), _amountIn);
IERC20(_tokenOut).safeTransfer(_receiver, _bancorNetwork.convert(_path, _amountIn, 1));
}
}