-
Notifications
You must be signed in to change notification settings - Fork 95
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
richardliang
wants to merge
4
commits into
master
Choose a base branch
from
richard/rebate-module
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,221 changes: 1,221 additions & 0 deletions
1,221
contracts/protocol/modules/GeneralIndexModuleV2.sol
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use:
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 |
||
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) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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