-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exchange.sol
285 lines (239 loc) · 8.99 KB
/
Exchange.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
pragma solidity >=0.4.22 <0.6.0;
pragma experimental ABIEncoderV2;
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "../utils/LibMath.sol";
import "../utils/LibOrder.sol";
import "../utils/LibSignatureValidator.sol";
import "./ExchangeStorage.sol";
contract Exchange is LibMath, LibOrder, LibSignatureValidator, ExchangeStorage {
using SafeMath for uint256;
/**
* @dev emitted when a trade is executed
*/
event Trade(
address indexed makerAddress, // Address that created the order
address indexed takerAddress, // Address that filled the order
bytes32 indexed orderHash, // Hash of the order
address makerFilledAsset, // Address of assets filled for maker
address takerFilledAsset, // Address of assets filled for taker
uint256 makerFilledAmount, // Amount of assets filled for maker
uint256 takerFilledAmount, // Amount of assets filled for taker
uint256 takerFeePaid, // Amount of fee paid by the taker
uint256 makerFeeReceived, // Amount of fee received by the maker
uint256 referralFeeReceived // Amount of fee received by the referrer
);
/**
* @dev emitted when a cancel order is executed
*/
event Cancel(
address indexed makerBuyToken, // Address of asset being bought.
address makerSellToken, // Address of asset being sold.
address indexed maker, // Address that created the order
bytes32 indexed orderHash // Hash of the order
);
/**
* @dev Compute the status of an order.
* Should be called before a contract execution is performet in order to not waste gas.
* @return OrderStatus.FILLABLE if the order is valid for taking.
* Note: See LibOrder.sol to see all statuses
*/
function getOrderInfo(
uint256 partialAmount,
Order memory order
)
public
view
returns (OrderInfo memory orderInfo)
{
// Compute the order hash
orderInfo.hash = getPrefixedHash(order);
// Fetch filled amount
orderInfo.filledAmount = filled[orderInfo.hash];
// Check taker balance
if(balances[order.makerBuyToken][order.taker] < order.takerSellAmount) {
orderInfo.status = uint8(OrderStatus.INVALID_TAKER_AMOUNT);
return orderInfo;
}
// Check maker balance
if(balances[order.makerSellToken][order.maker] < partialAmount) {
orderInfo.status = uint8(OrderStatus.INVALID_MAKER_AMOUNT);
return orderInfo;
}
// Check if order is filled
if (orderInfo.filledAmount.add(order.takerSellAmount) > order.makerBuyAmount) {
orderInfo.status = uint8(OrderStatus.FULLY_FILLED);
return orderInfo;
}
// Check for expiration
if (block.number >= order.expiration) {
orderInfo.status = uint8(OrderStatus.EXPIRED);
return orderInfo;
}
// Check if order has been cancelled
if (cancelled[orderInfo.hash]) {
orderInfo.status = uint8(OrderStatus.CANCELLED);
return orderInfo;
}
orderInfo.status = uint8(OrderStatus.FILLABLE);
return orderInfo;
}
/**
* @dev Execute a trade based on the input order and signature.
* Reverts if order is not valid
*/
function trade(
Order memory order,
bytes memory signature
)
public
{
bool result = _trade(order, signature);
require(result, "INVALID_TRADE");
}
/**
* @dev Execute a trade based on the input order and signature.
* If the order is valid returns true.
*/
function _trade(
Order memory order,
bytes memory signature
)
internal
returns(bool)
{
order.taker = msg.sender;
uint256 takerReceivedAmount = getPartialAmount(
order.makerSellAmount,
order.makerBuyAmount,
order.takerSellAmount
);
OrderInfo memory orderInfo = getOrderInfo(takerReceivedAmount, order);
uint8 status = assertTakeOrder(orderInfo.hash, orderInfo.status, order.maker, signature);
if(status != uint8(OrderStatus.FILLABLE)) {
return false;
}
OrderFill memory orderFill = getOrderFillResult(takerReceivedAmount, order);
executeTrade(order, orderFill);
filled[orderInfo.hash] = filled[orderInfo.hash].add(order.takerSellAmount);
emit Trade(
order.maker,
order.taker,
orderInfo.hash,
order.makerBuyToken,
order.makerSellToken,
orderFill.makerFillAmount,
orderFill.takerFillAmount,
orderFill.takerFeePaid,
orderFill.makerFeeReceived,
orderFill.referralFeeReceived
);
return true;
}
/**
* @dev Cancel an order if msg.sender is the order signer.
*/
function cancelSingleOrder(
Order memory order,
bytes memory signature
)
public
{
bytes32 orderHash = getPrefixedHash(order);
require(
recover(orderHash, signature) == msg.sender,
"INVALID_SIGNER"
);
require(
cancelled[orderHash] == false,
"ALREADY_CANCELLED"
);
cancelled[orderHash] = true;
emit Cancel(
order.makerBuyToken,
order.makerSellToken,
msg.sender,
orderHash
);
}
/**
* @dev Computation of the following properties based on the order input:
* takerFillAmount -> amount of assets received by the taker
* makerFillAmount -> amount of assets received by the maker
* takerFeePaid -> amount of fee paid by the taker (0.2% of takerFillAmount)
* makerFeeReceived -> amount of fee received by the maker (50% of takerFeePaid)
* referralFeeReceived -> amount of fee received by the taker referrer (10% of takerFeePaid)
* exchangeFeeReceived -> amount of fee received by the exchange (40% of takerFeePaid)
*/
function getOrderFillResult(
uint256 takerReceivedAmount,
Order memory order
)
internal
view
returns (OrderFill memory orderFill)
{
orderFill.takerFillAmount = takerReceivedAmount;
orderFill.makerFillAmount = order.takerSellAmount;
// 0.2% == 0.2*10^16
orderFill.takerFeePaid = getFeeAmount(
takerReceivedAmount,
takerFeeRate
);
// 50% of taker fee == 50*10^16
orderFill.makerFeeReceived = getFeeAmount(
orderFill.takerFeePaid,
makerFeeRate
);
// 10% of taker fee == 10*10^16
orderFill.referralFeeReceived = getFeeAmount(
orderFill.takerFeePaid,
referralFeeRate
);
// exchangeFee = (takerFeePaid - makerFeeReceived - referralFeeReceived)
orderFill.exchangeFeeReceived = orderFill.takerFeePaid.sub(
orderFill.makerFeeReceived).sub(
orderFill.referralFeeReceived);
}
/**
* @dev Throws when the order status is invalid or the signer is not valid.
*/
function assertTakeOrder(
bytes32 orderHash,
uint8 status,
address signer,
bytes memory signature
)
internal
pure
returns(uint8)
{
uint8 result = uint8(OrderStatus.FILLABLE);
if(recover(orderHash, signature) != signer) {
result = uint8(OrderStatus.INVALID_SIGNER);
}
if(status != uint8(OrderStatus.FILLABLE)) {
result = status;
}
return status;
}
/**
* @dev Updates the contract state i.e. user balances
*/
function executeTrade(
Order memory order,
OrderFill memory orderFill
)
private
{
uint256 makerGiveAmount = orderFill.takerFillAmount.sub(orderFill.makerFeeReceived);
uint256 takerFillAmount = orderFill.takerFillAmount.sub(orderFill.takerFeePaid);
address referrer = referrals[order.taker];
address feeAddress = feeAccount;
balances[order.makerSellToken][referrer] = balances[order.makerSellToken][referrer].add(orderFill.referralFeeReceived);
balances[order.makerSellToken][feeAddress] = balances[order.makerSellToken][feeAddress].add(orderFill.exchangeFeeReceived);
balances[order.makerBuyToken][order.taker] = balances[order.makerBuyToken][order.taker].sub(orderFill.makerFillAmount);
balances[order.makerBuyToken][order.maker] = balances[order.makerBuyToken][order.maker].add(orderFill.makerFillAmount);
balances[order.makerSellToken][order.taker] = balances[order.makerSellToken][order.taker].add(takerFillAmount);
balances[order.makerSellToken][order.maker] = balances[order.makerSellToken][order.maker].sub(makerGiveAmount);
}
}