This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
IssuanceOrderModule.sol
681 lines (604 loc) Β· 23.6 KB
/
IssuanceOrderModule.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
Copyright 2018 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.
*/
pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";
import { ReentrancyGuard } from "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol";
import { Math } from "openzeppelin-solidity/contracts/math/Math.sol";
import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { ExchangeHeaderLibrary } from "../lib/ExchangeHeaderLibrary.sol";
import { ExchangeWrapperLibrary } from "../lib/ExchangeWrapperLibrary.sol";
import { ICore } from "../interfaces/ICore.sol";
import { IExchangeWrapper } from "../interfaces/IExchangeWrapper.sol";
import { ISetToken } from "../interfaces/ISetToken.sol";
import { ISignatureValidator } from "../interfaces/ISignatureValidator.sol";
import { ITransferProxy } from "../interfaces/ITransferProxy.sol";
import { IVault } from "../interfaces/IVault.sol";
import { LibBytes } from "../../external/0x/LibBytes.sol";
import { OrderLibrary } from "../lib/OrderLibrary.sol";
/**
* @title Core Issuance Order
* @author Set Protocol
*
* The Core Issuance Order extension houses all functions related to the filling and
* canceling of issuance orders.
*/
contract IssuanceOrderModule is
ReentrancyGuard
{
using SafeMath for uint256;
using Math for uint256;
/* ============ State Variables ============ */
// Address of core contract
address public core;
// Address of transferProxy contract
address public transferProxy;
// Address of vault contract
address public vault;
// Mapping of filled Issuance Orders
mapping(bytes32 => uint256) public orderFills;
// Mapping of canceled Issuance Orders
mapping(bytes32 => uint256) public orderCancels;
/* ============ Events ============ */
event LogFill(
address setAddress,
address indexed makerAddress,
address indexed takerAddress,
address makerToken,
address indexed relayerAddress,
address relayerToken,
uint256 quantityFilled,
uint256 makerTokenToTaker,
uint256 relayerTokenAmountPaid,
bytes32 orderHash
);
event LogCancel(
address setAddress,
address indexed makerAddress,
address makerToken,
address indexed relayerAddress,
uint256 quantityCanceled,
bytes32 orderHash
);
/* ============ Constructor ============ */
/**
* Constructor function for IssuanceOrderModule
*
* @param _core The address of Core
* @param _transferProxy The address of transferProxy
* @param _vault The address of Vault
*/
constructor(
address _core,
address _transferProxy,
address _vault
)
public
{
// Commit passed address to core state variable
core = _core;
// Commit passed address to transferProxy state variable
transferProxy = _transferProxy;
// Commit passed address to vault state variable
vault = _vault;
}
/* ============ External Functions ============ */
/**
* Fill an issuance order
*
* @param _addresses [setAddress, makerAddress, makerToken, relayerAddress, relayerToken]
* @param _values [quantity, makerTokenAmount, expiration, makerRelayerFee, takerRelayerFee, salt]
* @param _requiredComponents Components required for the issuance order
* @param _requiredComponentAmounts Component amounts required for the issuance order
* @param _fillQuantity Quantity of set to be filled
* @param _signature Bytes with v, r and s segments of ECDSA signature
* @param _orderData Bytes array containing the exchange orders to execute
*/
function fillOrder(
address[5] _addresses,
uint256[6] _values,
address[] _requiredComponents,
uint256[] _requiredComponentAmounts,
uint256 _fillQuantity,
bytes _signature,
bytes _orderData
)
external
nonReentrant
{
// Create IssuanceOrder struct
OrderLibrary.IssuanceOrder memory order = OrderLibrary.constructOrder(
_addresses,
_values,
_requiredComponents,
_requiredComponentAmounts
);
uint256 executeQuantity = calculateExecuteQuantity(
order,
_fillQuantity
);
// Checks the signature, order, and execution quantity validity
validateFillOrder(
order,
executeQuantity,
_signature
);
// Creates data structure that represents the fraction of issuance order filled
OrderLibrary.FractionFilled memory fractionFilled = OrderLibrary.FractionFilled({
filled: executeQuantity,
attempted: _fillQuantity
});
// Settle Order
settleOrder(
order,
fractionFilled,
_orderData
);
// Issue Set
ICore(core).issueModule(
order.makerAddress,
order.setAddress,
executeQuantity
);
}
/**
* Cancel an issuance order
*
* @param _addresses [setAddress, makerAddress, makerToken, relayerAddress, relayerToken]
* @param _values [quantity, makerTokenAmount, expiration, makerRelayerFee, takerRelayerFee, salt]
* @param _requiredComponents Components required for the issuance order
* @param _requiredComponentAmounts Component amounts required for the issuance order
* @param _cancelQuantity Quantity of set to be canceled
*/
function cancelOrder(
address[5] _addresses,
uint256[6] _values,
address[] _requiredComponents,
uint256[] _requiredComponentAmounts,
uint256 _cancelQuantity
)
external
nonReentrant
{
// Create IssuanceOrder struct
OrderLibrary.IssuanceOrder memory order = OrderLibrary.constructOrder(
_addresses,
_values,
_requiredComponents,
_requiredComponentAmounts
);
uint256 cancelledAmount = calculateExecuteQuantity(
order,
_cancelQuantity
);
// Make sure cancel order comes from maker
require(
order.makerAddress == msg.sender,
"IssuanceOrderModule.cancelOrder: Unauthorized sender"
);
require(
cancelledAmount % ISetToken(order.setAddress).naturalUnit() == 0,
"IssuanceOrderModule.cancelOrder: Cancel amount must be multiple of natural unit"
);
require(
cancelledAmount > 0,
"IssuanceOrderModule.cancelOrder: Cancel amount must be greater than 0"
);
// Verify order is valid
OrderLibrary.validateOrder(
order,
core
);
// Tally cancel in orderCancels mapping
orderCancels[order.orderHash] = orderCancels[order.orderHash].add(cancelledAmount);
// Emit cancel order event
emit LogCancel(
order.setAddress,
order.makerAddress,
order.makerToken,
order.relayerAddress,
cancelledAmount,
order.orderHash
);
}
/* ============ Private Functions ============ */
/**
* Makes assertions regarding the executability of the order
*
* @param _order Bytes array containing the exchange orders to execute
* @param _executeQuantity Quantity of the issuance order to execute
* @param _signature Bytes array containing the order signature to validate
*/
function validateFillOrder(
OrderLibrary.IssuanceOrder memory _order,
uint256 _executeQuantity,
bytes _signature
)
public
{
require(
_executeQuantity > 0,
"IssuanceOrderModule.fillOrder: Execute amount must be greater than 0"
);
require(
_executeQuantity % ISetToken(_order.setAddress).naturalUnit() == 0,
"IssuanceOrderModule.fillOrder: Execute amount must be multiple of natural unit"
);
// Verify signature is authentic, if already been filled before skip to save gas
if (orderFills[_order.orderHash] == 0) {
ISignatureValidator(ICore(core).signatureValidator()).validateSignature(
_order.orderHash,
_order.makerAddress,
_signature
);
}
// Verify order is valid
OrderLibrary.validateOrder(
_order,
core
);
}
/**
* Execute the exchange orders by parsing the order data and facilitating the transfers. Each
* header represents a batch of orders for a particular exchange (0x, Kyber, taker)
*
* @param _orderData Bytes array containing the exchange orders to execute
* @param _makerAddress Issuance order maker address
* @param _makerTokenAddress Address of maker token to use to execute exchange orders
* @param _fractionFilled Fraction of original fill quantity
* @return makerTokenUsed Amount of maker token used to execute orders
*/
function executeExchangeOrders(
bytes _orderData,
address _makerAddress,
address _makerTokenAddress,
OrderLibrary.FractionFilled _fractionFilled
)
private
returns (uint256)
{
uint256 scannedBytes;
uint256 makerTokenUsed;
while (scannedBytes < _orderData.length) {
// Parse next exchange header based on scannedBytes
ExchangeHeaderLibrary.ExchangeHeader memory header = ExchangeHeaderLibrary.parseExchangeHeader(
_orderData,
scannedBytes
);
// Get exchange address from state mapping based on header exchange info
address exchangeWrapper = ICore(core).exchanges(header.exchange);
// Verify exchange address is registered
require(
exchangeWrapper != address(0),
"IssuanceOrderModule.executeExchangeOrders: Invalid or disabled Exchange address"
);
// Read the order body based on order data length info in header plus the length of the header (128)
uint256 exchangeDataLength = header.orderDataBytesLength.add(128);
bytes memory bodyData = LibBytes.slice(
_orderData,
scannedBytes.add(128),
scannedBytes.add(exchangeDataLength)
);
// Calculate amount of makerToken actually needed
uint256 makerTokenAmount = OrderLibrary.getPartialAmount(
header.makerTokenAmount,
_fractionFilled.filled,
_fractionFilled.attempted
);
// Transfer maker token to Exchange Wrapper to execute exchange orders
// Using maker token from signed issuance order to prevent malicious encoding of another maker token
ITransferProxy(transferProxy).transfer(
_makerTokenAddress,
makerTokenAmount,
_makerAddress,
exchangeWrapper
);
ExchangeWrapperLibrary.ExchangeData memory exchangeData = ExchangeWrapperLibrary.ExchangeData({
maker: _makerAddress,
taker: msg.sender,
makerToken: _makerTokenAddress,
makerAssetAmount: makerTokenAmount,
orderCount: header.orderCount,
fillQuantity: _fractionFilled.filled,
attemptedFillQuantity: _fractionFilled.attempted
});
// Call Exchange
callExchange(
exchangeData,
exchangeWrapper,
bodyData
);
// Update scanned bytes with header and body lengths
scannedBytes = scannedBytes.add(exchangeDataLength);
makerTokenUsed = makerTokenUsed.add(makerTokenAmount);
}
return makerTokenUsed;
}
/**
* Calls exchange to execute trades and deposits fills into Vault for issuanceOrder maker.
*
* maker Issuance order maker
* taker Issuance order taker
* makerToken Address of maker token used in exchange orders
* makerAssetAmount Amount of issuance order maker token to use on this exchange
* orderCount Expected number of orders to execute
* fillQuantity Quantity of Set to be filled
* attemptedfillQuantity Quantity of Set taker attempted to fill
*
* @param _exchangeData Standard exchange wrapper interface object containing exchange metadata
* @param _exchange Address of exchange wrapper being called
* @param _bodyData Arbitrary bytes data for orders to be executed on exchange
*/
function callExchange(
ExchangeWrapperLibrary.ExchangeData memory _exchangeData,
address _exchange,
bytes _bodyData
)
private
{
// Call Exchange
address[] memory componentFillTokens = new address[](_exchangeData.orderCount);
uint256[] memory componentFillAmounts = new uint256[](_exchangeData.orderCount);
(componentFillTokens, componentFillAmounts) = IExchangeWrapper(_exchange).exchange(
_exchangeData,
_bodyData
);
// Transfer component tokens from wrapper to vault
ICore(core).batchDepositModule(
_exchange,
_exchangeData.maker,
componentFillTokens,
componentFillAmounts
);
}
/**
* Check exchange orders acquire correct amount of tokens. Settle accounts for taker
* and relayer.
*
* @param _order IssuanceOrder object containing order params
* @param _fractionFilled Fraction of original quantity filled
* @param _orderData Bytestring encoding all exchange order data
*/
function settleOrder(
OrderLibrary.IssuanceOrder _order,
OrderLibrary.FractionFilled _fractionFilled,
bytes _orderData
)
private
{
// Calculate amount of maker token required
uint256 requiredMakerTokenAmount = OrderLibrary.getPartialAmount(
_order.makerTokenAmount,
_fractionFilled.filled,
_order.quantity
);
// Calculate require balances to issue after exchange orders executed
uint256[] memory requiredBalances = calculateRequiredTokenBalances(
_order,
_fractionFilled.filled
);
// Execute exchange orders
uint256 makerTokenAmountUsed = executeExchangeOrders(
_orderData,
_order.makerAddress,
_order.makerToken,
_fractionFilled
);
// Check that the correct amount of tokens were sourced using allotment of maker token
assertPostExchangeTokenBalances(
_order,
requiredBalances,
requiredMakerTokenAmount,
makerTokenAmountUsed
);
// Settle relayer and taker accounts
settleAccounts(
_order,
_fractionFilled.filled,
requiredMakerTokenAmount,
makerTokenAmountUsed
);
// Tally fill in orderFills mapping
orderFills[_order.orderHash] = orderFills[_order.orderHash].add(_fractionFilled.filled);
}
/**
* Calculate and send tokens to taker and relayer
*
* @param _order IssuanceOrder object containing order params
* @param _fillQuantity Quantity of Set to be filled
* @param _requiredMakerTokenAmount Max amount of maker token available to fill orders
* @param _makerTokenUsed Amount of maker token used to fill order
*/
function settleAccounts(
OrderLibrary.IssuanceOrder _order,
uint256 _fillQuantity,
uint256 _requiredMakerTokenAmount,
uint256 _makerTokenUsed
)
private
{
// Send left over maker token balance to taker, if greater than 0
uint256 leftoverMakerToken = _requiredMakerTokenAmount.sub(_makerTokenUsed);
if (leftoverMakerToken > 0) {
ITransferProxy(transferProxy).transfer(
_order.makerToken,
leftoverMakerToken, // Required less used is amount sent to taker
_order.makerAddress,
msg.sender
);
}
uint256 relayerFees = 0;
// Settle Relayer fees
if (_order.relayerAddress != address(0)) {
relayerFees = settleRelayerFees(
_order,
_fillQuantity
);
}
// Emit fill order event
emit LogFill(
_order.setAddress,
_order.makerAddress,
msg.sender,
_order.makerToken,
_order.relayerAddress,
_order.relayerToken,
_fillQuantity,
_requiredMakerTokenAmount.sub(_makerTokenUsed), // Required less used amount is sent to taker
relayerFees,
_order.orderHash
);
}
/**
* Calculate and send tokens to relayer (if necessary)
*
* @param _order IssuanceOrder object containing order params
* @param _fillQuantity Quantity of Set to be filled
* @return uint256 Amount of fees being sent to relayerAddress
*/
function settleRelayerFees(
OrderLibrary.IssuanceOrder _order,
uint256 _fillQuantity
)
private
returns (uint256)
{
//Declare transferProxy interface variable
ITransferProxy transferProxyInstance = ITransferProxy(transferProxy);
uint256 makerFee;
uint256 takerFee;
if (_order.makerRelayerFee > 0) {
// Calculate maker fees required
makerFee = OrderLibrary.getPartialAmount(
_order.makerRelayerFee,
_fillQuantity,
_order.quantity
);
// Send maker fees to relayer
transferProxyInstance.transfer(
_order.relayerToken,
makerFee,
_order.makerAddress,
_order.relayerAddress
);
}
if (_order.takerRelayerFee > 0) {
// Calculate taker fees required
takerFee = OrderLibrary.getPartialAmount(
_order.takerRelayerFee,
_fillQuantity,
_order.quantity
);
// Send taker fees to relayer
transferProxyInstance.transfer(
_order.relayerToken,
takerFee,
msg.sender,
_order.relayerAddress
);
}
return makerFee.add(takerFee);
}
/**
* Check exchange orders acquire correct amount of tokens and taker doesn't over use
* the issuance order maker's tokens
*
* @param _order IssuanceOrder object containing order params
* @param _requiredBalances Array of required balances for each component
after exchange orders are executed
* @param _requiredMakerTokenAmount Max amount of maker token used to source tokens
* @param _makerTokenAmountUsed Amount of maker token used to source tokens
*/
function assertPostExchangeTokenBalances(
OrderLibrary.IssuanceOrder _order,
uint256[] _requiredBalances,
uint256 _requiredMakerTokenAmount,
uint256 _makerTokenAmountUsed
)
private
view
{
// Verify maker token used is less than amount allocated that user signed
require(
_makerTokenAmountUsed <= _requiredMakerTokenAmount,
"IssuanceOrderModule.settleOrder: Maker token used exceeds allotted limit"
);
// Check that maker's component tokens in Vault have been incremented correctly
for (uint256 i = 0; i < _order.requiredComponents.length; i++) {
uint256 currentBal = IVault(vault).getOwnerBalance(
_order.requiredComponents[i],
_order.makerAddress
);
require(
currentBal >= _requiredBalances[i],
"IssuanceOrderModule.settleOrder: Insufficient component tokens acquired"
);
}
}
/**
* Check exchange orders acquire correct amount of tokens and taker doesn't over use
* the issuance order maker's tokens
*
* @param _order IssuanceOrder object containing order params
* @param _fillQuantity Amount of order taker is filling
* @return uint256[] Array of required token balances after order execution
*/
function calculateRequiredTokenBalances(
OrderLibrary.IssuanceOrder _order,
uint256 _fillQuantity
)
private
view
returns (uint256[])
{
// Calculate amount of component tokens required to issue
uint256[] memory requiredBalances = new uint256[](_order.requiredComponents.length);
for (uint256 i = 0; i < _order.requiredComponents.length; i++) {
// Get current vault balances
uint256 tokenBalance = IVault(vault).getOwnerBalance(
_order.requiredComponents[i],
_order.makerAddress
);
// Amount of component tokens to be added to Vault
uint256 requiredAddition = OrderLibrary.getPartialAmount(
_order.requiredComponentAmounts[i],
_fillQuantity,
_order.quantity
);
// Required vault balances after exchange order executed
requiredBalances[i] = tokenBalance.add(requiredAddition);
}
return requiredBalances;
}
/**
* Calculates the execute quantity for a fill or cancel order. This is calculated
* by taking the minimum of the open order amount and the user's input quantity.
* We do this allow orders to be filled even if a small quantity has been cancelled
* or filled ahead of this order.
*
* @param _order IssuanceOrder object containing order params
* @param _executeQuantity Amount of order taker is executing
* @return uint256 Quantity that can be filled or cancelled
*/
function calculateExecuteQuantity(
OrderLibrary.IssuanceOrder _order,
uint256 _executeQuantity
)
internal
view
returns (uint256)
{
uint256 closedOrderAmount = orderFills[_order.orderHash].add(orderCancels[_order.orderHash]);
uint256 openOrderAmount = _order.quantity.sub(closedOrderAmount);
return openOrderAmount.min(_executeQuantity);
}
}