-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathTradeModuleV2.sol
383 lines (322 loc) · 14.7 KB
/
TradeModuleV2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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);
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)
);
}
}