-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZeroExOperator.sol
47 lines (40 loc) · 1.85 KB
/
ZeroExOperator.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.14;
import "./IZeroExOperator.sol";
import "./ZeroExStorage.sol";
import "../../libraries/ExchangeHelpers.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/// @title The 0x protocol operator to execute swap with the aggregator
contract ZeroExOperator is IZeroExOperator {
ZeroExStorage public immutable operatorStorage;
/// @dev Deploy with the storage contract
constructor(address swapTarget) {
operatorStorage = new ZeroExStorage();
ZeroExStorage(operatorStorage).updatesSwapTarget(swapTarget);
ZeroExStorage(operatorStorage).transferOwnership(msg.sender);
}
/// @inheritdoc IZeroExOperator
function performSwap(
IERC20 sellToken,
IERC20 buyToken,
bytes calldata swapCallData
) external payable override returns (uint256[] memory amounts, address[] memory tokens) {
require(sellToken != buyToken, "ZEO: SAME_INPUT_OUTPUT");
amounts = new uint256[](2);
tokens = new address[](2);
uint256 buyBalanceBeforePurchase = buyToken.balanceOf(address(this));
uint256 sellBalanceBeforePurchase = sellToken.balanceOf(address(this));
bool success = ExchangeHelpers.fillQuote(sellToken, operatorStorage.swapTarget(), swapCallData);
require(success, "ZEO: SWAP_FAILED");
uint256 amountBought = buyToken.balanceOf(address(this)) - buyBalanceBeforePurchase;
uint256 amountSold = sellBalanceBeforePurchase - sellToken.balanceOf(address(this));
require(amountBought != 0, "ZEO: INVALID_AMOUNT_BOUGHT");
require(amountSold != 0, "ZEO: INVALID_AMOUNT_SOLD");
// Output amounts
amounts[0] = amountBought;
amounts[1] = amountSold;
// Output token
tokens[0] = address(buyToken);
tokens[1] = address(sellToken);
}
}