forked from CoinAlpha/basket-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasketEscrow.sol
396 lines (343 loc) · 18.4 KB
/
BasketEscrow.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
/*
Copyright 2018 CoinAlpha, 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.21;
import "./zeppelin/SafeMath.sol";
import "./zeppelin/ERC20.sol";
import "./BasketRegistry.sol";
/// @title BasketEscrow -- Escrow contract to facilitate trading
/// @author CoinAlpha, Inc. <[email protected]>
contract BasketEscrow {
using SafeMath for uint;
// Constants set at contract inception
address public admin;
address public transactionFeeRecipient;
uint public transactionFee;
uint public FEE_DECIMALS;
uint public orderIndex;
address public basketRegistryAddress;
address public ETH_ADDRESS;
// mapping of token addresses to mapping of account balances (token=0 means Ether)
// ADDRESS USER || ADDRESS TOKEN || UINT BALANCE
mapping(address => mapping(address => uint)) public balances;
// mapping of user accounts to mapping of order hashes to orderIndex (equivalent to offchain signature)
// ADDRESS USER || ORDER HASH || uint
mapping(address => mapping(bytes32 => uint)) public orders;
// mapping of user accounts to mapping of order hashes to booleans (true = order has been filled)
// ADDRESS USER || ORDER HASH || BOOL
mapping(address => mapping(bytes32 => bool)) public filledOrders;
mapping(uint => Order) public orderMap; // Used to lookup existing orders
// Modules
IBasketRegistry public basketRegistry;
// Structs
struct Order {
address orderCreator;
address tokenGet;
uint amountGet;
address tokenGive;
uint amountGive;
uint expiration;
uint nonce;
}
// Modifiers
modifier onlyAdmin {
require(msg.sender == admin); // Check: "Only the admin can call this function"
_;
}
// Events
event LogBuyOrderCreated(uint newOrderIndex, address indexed buyer, address basket, uint amountEth, uint amountBasket, uint expiration, uint nonce);
event LogSellOrderCreated(uint newOrderIndex, address indexed seller, address basket, uint amountEth, uint amountBasket, uint expiration, uint nonce);
event LogBuyOrderCancelled(uint cancelledOrderIndex, address indexed buyer, address basket, uint amountEth, uint amountBasket);
event LogSellOrderCancelled(uint cancelledOrderIndex, address indexed seller, address basket, uint amountEth, uint amountBasket);
event LogBuyOrderFilled(uint filledOrderIndex, address indexed buyOrderFiller, address indexed orderCreator, address basket, uint amountEth, uint amountBasket);
event LogSellOrderFilled(uint filledOrderIndex, address indexed sellOrderFiller, address indexed orderCreator, address basket, uint amountEth, uint amountBasket);
event LogTransactionFeeRecipientChange(address oldRecipient, address newRecipient);
event LogTransactionFeeChange(uint oldFee, uint newFee);
/// @dev BasketEscrow constructor
/// @param _basketRegistryAddress Address of basket registry
/// @param _transactionFeeRecipient Address to send transactionFee
/// @param _transactionFee Transaction fee in ETH percentage
function BasketEscrow(
address _basketRegistryAddress,
address _transactionFeeRecipient,
uint _transactionFee
) public {
basketRegistryAddress = _basketRegistryAddress;
basketRegistry = IBasketRegistry(_basketRegistryAddress);
ETH_ADDRESS = 0; // Use address 0 to indicate Eth
orderIndex = 1; // Initialize order index at 1
admin = msg.sender; // record admin
transactionFeeRecipient = _transactionFeeRecipient;
transactionFee = _transactionFee;
FEE_DECIMALS = 18;
}
/// @dev Create an order to buy baskets with ETH
/// @param _basketAddress Address of basket to purchase
/// @param _amountBasket Amount of baskets to purchase
/// @param _expiration Unix timestamp
/// @param _nonce Random number to generate unique order hash
/// @return success Operation successful
function createBuyOrder(
address _basketAddress,
uint _amountBasket,
uint _expiration,
uint _nonce
) public payable returns (bool success) {
uint index = _createOrder(msg.sender, _basketAddress, _amountBasket, ETH_ADDRESS, msg.value, _expiration, _nonce);
emit LogBuyOrderCreated(index, msg.sender, _basketAddress, msg.value, _amountBasket, _expiration, _nonce);
return true;
}
/// @dev Create an order to sell baskets for ETH NOTE: REQUIRES TOKEN APPROVAL
/// @param _basketAddress Address of basket to sell
/// @param _amountBasket Amount of baskets to sell
/// @param _amountEth Amount of ETH to receive in exchange
/// @param _expiration Unix timestamp
/// @param _nonce Random number to generate unique order hash
/// @return success Operation successful
function createSellOrder(
address _basketAddress,
uint _amountBasket,
uint _amountEth,
uint _expiration,
uint _nonce
)
public
returns (bool success)
{
ERC20(_basketAddress).transferFrom(msg.sender, this, _amountBasket);
uint index = _createOrder(msg.sender, ETH_ADDRESS, _amountEth, _basketAddress, _amountBasket, _expiration, _nonce);
emit LogSellOrderCreated(index, msg.sender, _basketAddress, _amountEth, _amountBasket, _expiration, _nonce);
return true;
}
/// @dev Contract internal function to record submitted orders
/// @param _orderCreator Address of the order's creator
/// @param _tokenGet Address of token/ETH to receive
/// @param _amountGet Amount of token/ETH to receive
/// @param _tokenGive Address of token/ETH to give
/// @param _amountGive Amount of token/ETH to give
/// @param _expiration Unix timestamp
/// @param _nonce Random number to generate unique order hash
/// @return newOrderIndex
function _createOrder(
address _orderCreator,
address _tokenGet,
uint _amountGet,
address _tokenGive,
uint _amountGive,
uint _expiration,
uint _nonce
)
internal
returns (uint newOrderIndex)
{
require(_expiration > now);
require(_tokenGet == ETH_ADDRESS || basketRegistry.checkBasketExists(_tokenGet)); // Check: "Order not for ETH or invalid basket"
require(_tokenGive == ETH_ADDRESS || basketRegistry.checkBasketExists(_tokenGive)); // Check: "Order not for ETH or invalid basket"
bytes32 hash = sha256(this, _tokenGet, _amountGet, _tokenGive, _amountGive, _expiration, _nonce);
require(orders[_orderCreator][hash] == 0); // Check: "Duplicate order"
orders[_orderCreator][hash] = orderIndex;
balances[_orderCreator][_tokenGive] = balances[_orderCreator][_tokenGive].add(_amountGive);
orderMap[orderIndex] = Order(_orderCreator, _tokenGet, _amountGet, _tokenGive, _amountGive, _expiration, _nonce);
orderIndex = orderIndex.add(1);
return orderIndex.sub(1);
}
/// @dev Cancel an existing buy order
/// @param _basketAddress Address of basket to purchase in original order
/// @param _amountBasket Amount of baskets to purchase in original order
/// @param _amountEth Amount of ETH sent in original order
/// @param _expiration Unix timestamp in original order
/// @param _nonce Random number in original order
/// @return success Operation successful
function cancelBuyOrder(
address _basketAddress,
uint _amountBasket,
uint _amountEth,
uint _expiration,
uint _nonce
) public returns (bool success) {
uint cancelledOrderIndex = _cancelOrder(msg.sender, _basketAddress, _amountBasket, ETH_ADDRESS, _amountEth, _expiration, _nonce);
if (now >= _expiration) {
msg.sender.transfer(_amountEth); // if order has expired, no transaction fee is charged
} else {
uint fee = _amountEth.mul(transactionFee).div(10 ** FEE_DECIMALS);
msg.sender.transfer(_amountEth.sub(fee));
transactionFeeRecipient.transfer(fee);
}
emit LogBuyOrderCancelled(cancelledOrderIndex, msg.sender, _basketAddress, _amountEth, _amountBasket);
return true;
}
/// @dev Cancel an existing sell order
/// @param _basketAddress Address of basket to sell in original order
/// @param _amountBasket Amount of baskets to sell in original order
/// @param _amountEth Amount of ETH to receive in original order
/// @param _expiration Unix timestamp in original order
/// @param _nonce Random number in original order
/// @return success Operation successful
function cancelSellOrder(
address _basketAddress,
uint _amountBasket,
uint _amountEth,
uint _expiration,
uint _nonce
) public returns (bool success) {
uint cancelledOrderIndex = _cancelOrder(msg.sender, ETH_ADDRESS, _amountEth, _basketAddress, _amountBasket, _expiration, _nonce);
ERC20(_basketAddress).transfer(msg.sender, _amountBasket);
emit LogSellOrderCancelled(cancelledOrderIndex, msg.sender, _basketAddress, _amountEth, _amountBasket);
return true;
}
/// @dev Contract internal function to cancel an existing order
/// @param _orderCreator Address of the original order's creator
/// @param _tokenGet Address of token/ETH to receive in original order
/// @param _amountGet Amount of token/ETH to receive in original order
/// @param _tokenGive Address of token/ETH to give in original order
/// @param _amountGive Amount of token/ETH to give in original order
/// @param _expiration Unix timestamp in original order
/// @param _nonce Random number in original order
/// @return cancelledOrderIndex Index of cancelled order
function _cancelOrder(
address _orderCreator,
address _tokenGet,
uint _amountGet,
address _tokenGive,
uint _amountGive,
uint _expiration,
uint _nonce
)
internal
returns (uint index)
{
bytes32 hash = sha256(this, _tokenGet, _amountGet, _tokenGive, _amountGive, _expiration, _nonce);
uint cancelledOrderIndex = orders[_orderCreator][hash];
require(cancelledOrderIndex > 0); // Check: "Order does not exist"
require(filledOrders[_orderCreator][hash] != true); // Check: "Order has been filled"
orders[_orderCreator][hash] = 0;
balances[_orderCreator][_tokenGive] = balances[_orderCreator][_tokenGive].sub(_amountGive);
return cancelledOrderIndex;
}
/// @dev Fill an existing buy order NOTE: REQUIRES TOKEN APPROVAL
/// @param _orderCreator Address of order's creator
/// @param _basketAddress Address of basket to purchase in original order
/// @param _amountBasket Amount of baskets to purchase in original order
/// @param _amountEth Amount of ETH to sent in original order
/// @param _expiration Unix timestamp in original order
/// @param _nonce Random number in original order
/// @return success Operation successful
function fillBuyOrder(
address _orderCreator,
address _basketAddress,
uint _amountBasket,
uint _amountEth,
uint _expiration,
uint _nonce
) public returns (bool success) {
uint filledOrderIndex = _fillOrder(_orderCreator, _basketAddress, _amountBasket, ETH_ADDRESS, _amountEth, _expiration, _nonce);
ERC20(_basketAddress).transferFrom(msg.sender, _orderCreator, _amountBasket);
uint fee = _amountEth.mul(transactionFee).div(10 ** FEE_DECIMALS);
msg.sender.transfer(_amountEth.sub(fee));
transactionFeeRecipient.transfer(fee);
emit LogBuyOrderFilled(filledOrderIndex, msg.sender, _orderCreator, _basketAddress, _amountEth, _amountBasket);
return true;
}
/// @dev Fill an existing sell order
/// @param _orderCreator Address of order's creator
/// @param _basketAddress Address of basket to sell in original order
/// @param _amountBasket Amount of baskets to sell in original order
/// @param _expiration Unix timestamp in original order
/// @param _nonce Random number in original order
/// @return success Operation successful
function fillSellOrder(
address _orderCreator,
address _basketAddress,
uint _amountBasket,
uint _expiration,
uint _nonce
) public payable returns (bool success) {
uint filledOrderIndex = _fillOrder(_orderCreator, ETH_ADDRESS, msg.value, _basketAddress, _amountBasket, _expiration, _nonce);
ERC20(_basketAddress).transfer(msg.sender, _amountBasket);
uint fee = msg.value.mul(transactionFee).div(10 ** FEE_DECIMALS);
_orderCreator.transfer(msg.value.sub(fee));
transactionFeeRecipient.transfer(fee);
emit LogSellOrderFilled(filledOrderIndex, msg.sender, _orderCreator, _basketAddress, msg.value, _amountBasket);
return true;
}
/// @dev Contract internal function to fill an existing order
/// @param _orderCreator Address of the original order's creator
/// @param _tokenGet Address of token/ETH to receive in original order
/// @param _amountGet Amount of token/ETH to receive in original order
/// @param _tokenGive Address of token/ETH to give in original order
/// @param _amountGive Amount of token/ETH to give in original order
/// @param _expiration Unix timestamp in original order
/// @param _nonce Random number in original order
/// @return filledOrderIndex Index of filled order
function _fillOrder(
address _orderCreator,
address _tokenGet,
uint _amountGet,
address _tokenGive,
uint _amountGive,
uint _expiration,
uint _nonce
)
internal
returns (uint index)
{
bytes32 hash = sha256(this, _tokenGet, _amountGet, _tokenGive, _amountGive, _expiration, _nonce);
uint filledOrderIndex = orders[_orderCreator][hash];
require(filledOrderIndex > 0); // Check: "Order does not exist"
require(filledOrders[_orderCreator][hash] != true); // Check: "Order has been filled"
require(now <= _expiration); // Check: "Order has expired"
filledOrders[_orderCreator][hash] = true;
balances[_orderCreator][_tokenGive] = balances[_orderCreator][_tokenGive].sub(_amountGive);
return filledOrderIndex;
}
/// @dev Get details of an order with its index;
/// @param _orderIndex Order index assigned at order creation
/// @return Order struct
function getOrderDetails(uint _orderIndex) public view returns (
address orderCreator,
address tokenGet,
uint amountGet,
address tokenGive,
uint amountGive,
uint expiration,
uint nonce,
bool _orderExists,
bool _isFilled
) {
Order memory o = orderMap[_orderIndex];
bytes32 hash = sha256(this, o.tokenGet, o.amountGet, o.tokenGive, o.amountGive, o.expiration, o.nonce);
bool orderExists = orders[o.orderCreator][hash] > 0;
bool isFilled = filledOrders[o.orderCreator][hash];
return (o.orderCreator, o.tokenGet, o.amountGet, o.tokenGive, o.amountGive, o.expiration, o.nonce, orderExists, isFilled);
}
/// @dev Change recipient of transaction fees
/// @param _newRecipient New fee recipient
/// @return success Operation successful
function changeTransactionFeeRecipient(address _newRecipient) public onlyAdmin returns (bool success) {
address oldRecipient = transactionFeeRecipient;
transactionFeeRecipient = _newRecipient;
emit LogTransactionFeeRecipientChange(oldRecipient, transactionFeeRecipient);
return true;
}
/// @dev Change percentage of fee charged for ETH transactions
/// @param _newFee New fee amount
/// @return success Operation successful
function changeTransactionFee(uint _newFee) public onlyAdmin returns (bool success) {
uint oldFee = transactionFee;
transactionFee = _newFee;
emit LogTransactionFeeChange(oldFee, transactionFee);
return true;
}
/// @dev Fallback to reject any ether sent directly to contract
// Check: "BasketEscrow does not accept ETH transfers"
function () public payable { revert(); }
}