/* Copyright 2020 Set Labs Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache License, Version 2.0 */ pragma solidity ^0.6.10; pragma experimental "ABIEncoderV2"; import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; import { SafeCast } from "@openzeppelin/contracts/utils/SafeCast.sol"; import { IController } from "../../../interfaces/IController.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IExchangeAdapter } from "../../../interfaces/IExchangeAdapter.sol"; import { IIntegrationRegistry } from "../../../interfaces/IIntegrationRegistry.sol"; import { Invoke } from "../../lib/Invoke.sol"; import { ISetToken } from "../../../interfaces/ISetToken.sol"; import { ModuleBase } from "../../lib/ModuleBase.sol"; import { Position } from "../../lib/Position.sol"; import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol"; /** * @title TradeModule * @author Set Protocol * * Module that enables SetTokens to perform atomic trades using Decentralized Exchanges * such as 1inch or Kyber. Integrations mappings are stored on the IntegrationRegistry contract. */ contract TradeModule is ModuleBase, ReentrancyGuard { using SafeCast for int256; using SafeMath for uint256; using Invoke for ISetToken; using Position for ISetToken; using PreciseUnitMath for uint256; /* ============ Struct ============ */ struct TradeInfo { ISetToken setToken; // Instance of SetToken IExchangeAdapter exchangeAdapter; // Instance of exchange adapter contract address sendToken; // Address of token being sold address receiveToken; // Address of token being bought uint256 setTotalSupply; // Total supply of SetToken in Precise Units (10^18) uint256 totalSendQuantity; // Total quantity of sold token (position unit x total supply) uint256 totalMinReceiveQuantity; // Total minimum quantity of token to receive back uint256 preTradeSendTokenBalance; // Total initial balance of token being sold uint256 preTradeReceiveTokenBalance; // Total initial balance of token being bought } /* ============ Events ============ */ event ComponentExchanged( ISetToken indexed _setToken, address indexed _sendToken, address indexed _receiveToken, IExchangeAdapter _exchangeAdapter, uint256 _totalSendAmount, uint256 _totalReceiveAmount, uint256 _protocolFee ); /* ============ Constants ============ */ // 0 index stores the fee % charged in the trade function uint256 constant internal TRADE_MODULE_PROTOCOL_FEE_INDEX = 0; /* ============ Constructor ============ */ constructor(IController _controller) public ModuleBase(_controller) {} /* ============ External Functions ============ */ /** * Initializes this module to the SetToken. Only callable by the SetToken's manager. * * @param _setToken Instance of the SetToken to initialize */ function initialize( ISetToken _setToken ) external onlyValidAndPendingSet(_setToken) onlySetManager(_setToken, msg.sender) { _setToken.initializeModule(); } /** * Executes a trade on a supported DEX. Only callable by the SetToken's manager. * @dev Although the SetToken units are passed in for the send and receive quantities, the total quantity * sent and received is the quantity of SetToken units multiplied by the SetToken totalSupply. * * @param _setToken Instance of the SetToken to trade * @param _exchangeName Human readable name of the exchange in the integrations registry * @param _sendToken Address of the token to be sent to the exchange * @param _sendQuantity Units of token in SetToken sent to the exchange * @param _receiveToken Address of the token that will be received from the exchange * @param _minReceiveQuantity Min units of token in SetToken to be received from the exchange * @param _data Arbitrary bytes to be used to construct trade call data */ function trade( ISetToken _setToken, string memory _exchangeName, address _sendToken, uint256 _sendQuantity, address _receiveToken, uint256 _minReceiveQuantity, bytes memory _data ) external nonReentrant onlyManagerAndValidSet(_setToken) { TradeInfo memory tradeInfo = _createTradeInfo( _setToken, _exchangeName, _sendToken, _receiveToken, _sendQuantity, _minReceiveQuantity ); _validatePreTradeData(tradeInfo, _sendQuantity); _executeTrade(tradeInfo, _data); uint256 exchangedQuantity = _validatePostTrade(tradeInfo); uint256 protocolFee = _accrueProtocolFee(tradeInfo, exchangedQuantity); ( uint256 netSendAmount, uint256 netReceiveAmount ) = _updateSetTokenPositions(tradeInfo); emit ComponentExchanged( _setToken, _sendToken, _receiveToken, tradeInfo.exchangeAdapter, netSendAmount, netReceiveAmount, protocolFee ); } /** * Removes this module from the SetToken, via call by the SetToken. Left with empty logic * here because there are no check needed to verify removal. */ function removeModule() external override {} /* ============ Internal Functions ============ */ /** * Create and return TradeInfo struct * * @param _setToken Instance of the SetToken to trade * @param _exchangeName Human readable name of the exchange in the integrations registry * @param _sendToken Address of the token to be sent to the exchange * @param _receiveToken Address of the token that will be received from the exchange * @param _sendQuantity Units of token in SetToken sent to the exchange * @param _minReceiveQuantity Min units of token in SetToken to be received from the exchange * * return TradeInfo Struct containing data for trade */ function _createTradeInfo( ISetToken _setToken, string memory _exchangeName, address _sendToken, address _receiveToken, uint256 _sendQuantity, uint256 _minReceiveQuantity ) internal view returns (TradeInfo memory) { TradeInfo memory tradeInfo; tradeInfo.setToken = _setToken; tradeInfo.exchangeAdapter = IExchangeAdapter(getAndValidateAdapter(_exchangeName)); tradeInfo.sendToken = _sendToken; tradeInfo.receiveToken = _receiveToken; tradeInfo.setTotalSupply = _setToken.totalSupply(); tradeInfo.totalSendQuantity = Position.getDefaultTotalNotional(tradeInfo.setTotalSupply, _sendQuantity); tradeInfo.totalMinReceiveQuantity = Position.getDefaultTotalNotional(tradeInfo.setTotalSupply, _minReceiveQuantity); tradeInfo.preTradeSendTokenBalance = IERC20(_sendToken).balanceOf(address(_setToken)); tradeInfo.preTradeReceiveTokenBalance = IERC20(_receiveToken).balanceOf(address(_setToken)); return tradeInfo; } /** * Validate pre trade data. Check exchange is valid, token quantity is valid. * * @param _tradeInfo Struct containing trade information used in internal functions * @param _sendQuantity Units of token in SetToken sent to the exchange */ function _validatePreTradeData(TradeInfo memory _tradeInfo, uint256 _sendQuantity) internal view { require(_tradeInfo.totalSendQuantity > 0, "Token to sell must be nonzero"); require( _tradeInfo.setToken.hasSufficientDefaultUnits(_tradeInfo.sendToken, _sendQuantity), "Unit cant be greater than existing" ); } /** * Invoke approve for send token, get method data and invoke trade in the context of the SetToken. * * @param _tradeInfo Struct containing trade information used in internal functions * @param _data Arbitrary bytes to be used to construct trade call data */ function _executeTrade( TradeInfo memory _tradeInfo, bytes memory _data ) internal { // Get spender address from exchange adapter and invoke approve for exact amount on SetToken _tradeInfo.setToken.invokeApprove( _tradeInfo.sendToken, _tradeInfo.exchangeAdapter.getSpender(), _tradeInfo.totalSendQuantity ); ( address targetExchange, uint256 callValue, bytes memory methodData ) = _tradeInfo.exchangeAdapter.getTradeCalldata( _tradeInfo.sendToken, _tradeInfo.receiveToken, address(_tradeInfo.setToken), _tradeInfo.totalSendQuantity, _tradeInfo.totalMinReceiveQuantity, _data ); _tradeInfo.setToken.invoke(targetExchange, callValue, methodData); } /** * Validate post trade data. * * @param _tradeInfo Struct containing trade information used in internal functions * @return uint256 Total quantity of receive token that was exchanged */ function _validatePostTrade(TradeInfo memory _tradeInfo) internal view returns (uint256) { uint256 exchangedQuantity = IERC20(_tradeInfo.receiveToken) .balanceOf(address(_tradeInfo.setToken)) .sub(_tradeInfo.preTradeReceiveTokenBalance); require( exchangedQuantity >= _tradeInfo.totalMinReceiveQuantity, "Slippage greater than allowed" ); return exchangedQuantity; } /** * Retrieve fee from controller and calculate total protocol fee and send from SetToken to protocol recipient * * @param _tradeInfo Struct containing trade information used in internal functions * @return uint256 Amount of receive token taken as protocol fee */ function _accrueProtocolFee(TradeInfo memory _tradeInfo, uint256 _exchangedQuantity) internal returns (uint256) { uint256 protocolFeeTotal = getModuleFee(TRADE_MODULE_PROTOCOL_FEE_INDEX, _exchangedQuantity); payProtocolFeeFromSetToken(_tradeInfo.setToken, _tradeInfo.receiveToken, protocolFeeTotal); return protocolFeeTotal; } /** * Update SetToken positions * * @param _tradeInfo Struct containing trade information used in internal functions * @return uint256 Amount of sendTokens used in the trade * @return uint256 Amount of receiveTokens received in the trade (net of fees) */ function _updateSetTokenPositions(TradeInfo memory _tradeInfo) internal returns (uint256, uint256) { (uint256 currentSendTokenBalance,,) = _tradeInfo.setToken.calculateAndEditDefaultPosition( _tradeInfo.sendToken, _tradeInfo.setTotalSupply, _tradeInfo.preTradeSendTokenBalance ); (uint256 currentReceiveTokenBalance,,) = _tradeInfo.setToken.calculateAndEditDefaultPosition( _tradeInfo.receiveToken, _tradeInfo.setTotalSupply, _tradeInfo.preTradeReceiveTokenBalance ); return ( _tradeInfo.preTradeSendTokenBalance.sub(currentSendTokenBalance), currentReceiveTokenBalance.sub(_tradeInfo.preTradeReceiveTokenBalance) ); } }