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

TradeModuleV2 and GeneralIndexModuleV2 #154

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1,221 changes: 1,221 additions & 0 deletions contracts/protocol/modules/GeneralIndexModuleV2.sol

Large diffs are not rendered by default.

383 changes: 383 additions & 0 deletions contracts/protocol/modules/TradeModuleV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,383 @@
/*
Copyright 2021 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 TradeModuleV2
* @author Set Protocol
*
* Module that enables SetTokens to perform atomic trades using Decentralized Exchanges
* such as Uniswap or 0x. Integrations mappings are stored on the IntegrationRegistry contract.
*
* Note: TradeModuleV2 will allow governance to specify a protocol fee and rebate split percentage
* which is sent to the manager's address. The fee rebates will be automatic per trade.
*/
contract TradeModuleV2 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,
uint256 _managerRebate
);

event FeeRecipientUpdated(ISetToken indexed _setToken, address _newFeeRecipient);

/* ============ Constants ============ */

// 0 index stores the total fee % charged in the trade function
uint256 constant internal TRADE_MODULE_V2_TOTAL_FEE_INDEX = 0;

// 1 index stores the % of total fees that the manager receives back as rebates
uint256 constant internal TRADE_MODULE_V2_MANAGER_REBATE_SPLIT_INDEX = 1;

/* ============ State Variables ============ */

// Mapping to efficiently identify a manager rebate recipient address for a given SetToken
mapping(ISetToken => address) public managerRebateRecipient;

/* ============ 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,
address _managerRebateRecipient
)
external
onlyValidAndPendingSet(_setToken)
onlySetManager(_setToken, msg.sender)
{
require(_managerRebateRecipient != address(0), "Recipient must be non-zero address.");

managerRebateRecipient[_setToken] = _managerRebateRecipient;

_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, uint256 managerRebate) = _accrueFees(tradeInfo, exchangedQuantity);

(
uint256 netSendAmount,
uint256 netReceiveAmount
) = _updateSetTokenPositions(tradeInfo);

emit ComponentExchanged(
_setToken,
_sendToken,
_receiveToken,
tradeInfo.exchangeAdapter,
netSendAmount,
netReceiveAmount,
protocolFee,
managerRebate
);
}

/**
* MANAGER ONLY: Updates address receiving manager rebate fees for a given SetToken.
*
* @param _setToken Instance of the SetToken to update fee recipient
* @param _newRebateRecipient New rebate fee recipient address
*/
function updateFeeRecipient(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this clearer and call it updateRebateRecipient

ISetToken _setToken,
address _newRebateRecipient
)
external
onlyManagerAndValidSet(_setToken)
{
require(_newRebateRecipient != address(0), "Recipient must be non-zero address.");
require(_newRebateRecipient != managerRebateRecipient[_setToken], "Same fee recipient passed");

managerRebateRecipient[_setToken] = _newRebateRecipient;

emit FeeRecipientUpdated(_setToken, _newRebateRecipient);
}

/**
* Removes this module from the SetToken, via call by the SetToken. Remove the manager rebate recipient address.
*/
function removeModule() external override {
delete managerRebateRecipient[ISetToken(msg.sender)];
}

/* ============ 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 fees from controller and calculate protocol fee and manager rebate. Send from SetToken to protocol recipient and
* to manager recipient.
*
* @param _tradeInfo Struct containing trade information used in internal functions
* @return protocolFee Amount of receive token taken as protocol fee
* @return managerRebate Amount of receive token taken as manager rebate fee
*/
function _accrueFees(
TradeInfo memory _tradeInfo,
uint256 _exchangedQuantity
)
internal
returns (uint256 protocolFee, uint256 managerRebate)
{
uint256 totalFeePercentage = controller.getModuleFee(address(this), TRADE_MODULE_V2_TOTAL_FEE_INDEX);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use:

uint256 totalFees = getModuleFee(TRADE_MODULE_PROTOCOL_FEE_INDEX, _exchangedQuantity);
managerRebate = controller.getModuleFee(address(this), TRADE_MODULE_V2_MANAGER_REBATE_SPLIT_INDEX).preciseMul(totalFees);
protocolFee = totalFees.sub(managerRebate);

I think that way you can be more confident about not having some extra wei sent out due to rounding and you aren't recalculating totalFeePercentage.preciseMul(_exchangedQuantity)

uint256 managerRebateSplitPercentage = controller.getModuleFee(address(this), TRADE_MODULE_V2_MANAGER_REBATE_SPLIT_INDEX);

managerRebate = totalFeePercentage.preciseMul(_exchangedQuantity).preciseMul(managerRebateSplitPercentage);
protocolFee = totalFeePercentage.preciseMul(_exchangedQuantity).sub(managerRebate);

payProtocolFeeFromSetToken(_tradeInfo.setToken, _tradeInfo.receiveToken, protocolFee);

if (managerRebate > 0) {
_tradeInfo.setToken.strictInvokeTransfer(
_tradeInfo.receiveToken,
managerRebateRecipient[_tradeInfo.setToken],
managerRebate
);
}
}

/**
* 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)
);
}
}
Loading