-
Notifications
You must be signed in to change notification settings - Fork 25
/
BlurExchange.sol
559 lines (487 loc) · 16.7 KB
/
BlurExchange.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
pragma abicoder v2;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./lib/ReentrancyGuarded.sol";
import "./lib/EIP712.sol";
import "./lib/MerkleVerifier.sol";
import "./interfaces/IBlurExchange.sol";
import "./interfaces/IExecutionDelegate.sol";
import "./interfaces/IPolicyManager.sol";
import "./interfaces/IMatchingPolicy.sol";
import {
Side,
SignatureVersion,
AssetType,
Fee,
Order,
Input
} from "./lib/OrderStructs.sol";
/**
* @title BlurExchange
* @dev Core Blur exchange contract
*/
contract BlurExchange is IBlurExchange, ReentrancyGuarded, EIP712, OwnableUpgradeable, UUPSUpgradeable {
/* Auth */
uint256 public isOpen;
modifier whenOpen() {
require(isOpen == 1, "Closed");
_;
}
event Opened();
event Closed();
function open() external onlyOwner {
isOpen = 1;
emit Opened();
}
function close() external onlyOwner {
isOpen = 0;
emit Closed();
}
// required by the OZ UUPS module
function _authorizeUpgrade(address) internal override onlyOwner {}
/* Constants */
string public constant name = "Blur Exchange";
string public constant version = "1.0";
uint256 public constant INVERSE_BASIS_POINT = 10000;
/* Variables */
address public weth;
IExecutionDelegate public executionDelegate;
IPolicyManager public policyManager;
address public oracle;
uint256 public blockRange;
/* Storage */
mapping(bytes32 => bool) public cancelledOrFilled;
mapping(address => uint256) public nonces;
/* Events */
event OrdersMatched(
address indexed maker,
address indexed taker,
Order sell,
bytes32 sellHash,
Order buy,
bytes32 buyHash
);
event OrderCancelled(bytes32 hash);
event NonceIncremented(address trader, uint256 newNonce);
event NewExecutionDelegate(IExecutionDelegate executionDelegate);
event NewPolicyManager(IPolicyManager policyManager);
event NewOracle(address oracle);
event NewBlockRange(uint256 blockRange);
/* Constructor (for ERC1967) */
function initialize(
uint chainId,
address _weth,
IExecutionDelegate _executionDelegate,
IPolicyManager _policyManager,
address _oracle,
uint _blockRange
) public initializer {
__Ownable_init();
isOpen = 1;
DOMAIN_SEPARATOR = _hashDomain(EIP712Domain({
name : name,
version : version,
chainId : chainId,
verifyingContract : address(this)
}));
weth = _weth;
executionDelegate = _executionDelegate;
policyManager = _policyManager;
oracle = _oracle;
blockRange = _blockRange;
}
/* External Functions */
/**
* @dev Match two orders, ensuring validity of the match, and execute all associated state transitions. Protected against reentrancy by a contract-global lock.
* @param sell Sell input
* @param buy Buy input
*/
function execute(Input calldata sell, Input calldata buy)
external
payable
reentrancyGuard
whenOpen
{
require(sell.order.side == Side.Sell);
bytes32 sellHash = _hashOrder(sell.order, nonces[sell.order.trader]);
bytes32 buyHash = _hashOrder(buy.order, nonces[buy.order.trader]);
require(_validateOrderParameters(sell.order, sellHash), "Sell has invalid parameters");
require(_validateOrderParameters(buy.order, buyHash), "Buy has invalid parameters");
require(_validateSignatures(sell, sellHash), "Sell failed authorization");
require(_validateSignatures(buy, buyHash), "Buy failed authorization");
(uint256 price, uint256 tokenId, uint256 amount, AssetType assetType) = _canMatchOrders(sell.order, buy.order);
_executeFundsTransfer(
sell.order.trader,
buy.order.trader,
sell.order.paymentToken,
sell.order.fees,
price
);
_executeTokenTransfer(
sell.order.collection,
sell.order.trader,
buy.order.trader,
tokenId,
amount,
assetType
);
/* Mark orders as filled. */
cancelledOrFilled[sellHash] = true;
cancelledOrFilled[buyHash] = true;
emit OrdersMatched(
sell.order.listingTime <= buy.order.listingTime ? sell.order.trader : buy.order.trader,
sell.order.listingTime > buy.order.listingTime ? sell.order.trader : buy.order.trader,
sell.order,
sellHash,
buy.order,
buyHash
);
}
/**
* @dev Cancel an order, preventing it from being matched. Must be called by the trader of the order
* @param order Order to cancel
*/
function cancelOrder(Order calldata order) public {
/* Assert sender is authorized to cancel order. */
require(msg.sender == order.trader);
bytes32 hash = _hashOrder(order, nonces[order.trader]);
if (!cancelledOrFilled[hash]) {
/* Mark order as cancelled, preventing it from being matched. */
cancelledOrFilled[hash] = true;
emit OrderCancelled(hash);
}
}
/**
* @dev Cancel multiple orders
* @param orders Orders to cancel
*/
function cancelOrders(Order[] calldata orders) external {
for (uint8 i = 0; i < orders.length; i++) {
cancelOrder(orders[i]);
}
}
/**
* @dev Cancel all current orders for a user, preventing them from being matched. Must be called by the trader of the order
*/
function incrementNonce() external {
nonces[msg.sender] += 1;
emit NonceIncremented(msg.sender, nonces[msg.sender]);
}
/* Setters */
function setExecutionDelegate(IExecutionDelegate _executionDelegate)
external
onlyOwner
{
require(address(_executionDelegate) != address(0), "Address cannot be zero");
executionDelegate = _executionDelegate;
emit NewExecutionDelegate(executionDelegate);
}
function setPolicyManager(IPolicyManager _policyManager)
external
onlyOwner
{
require(address(_policyManager) != address(0), "Address cannot be zero");
policyManager = _policyManager;
emit NewPolicyManager(policyManager);
}
function setOracle(address _oracle)
external
onlyOwner
{
require(_oracle != address(0), "Address cannot be zero");
oracle = _oracle;
emit NewOracle(oracle);
}
function setBlockRange(uint256 _blockRange)
external
onlyOwner
{
blockRange = _blockRange;
emit NewBlockRange(blockRange);
}
/* Internal Functions */
/**
* @dev Verify the validity of the order parameters
* @param order order
* @param orderHash hash of order
*/
function _validateOrderParameters(Order calldata order, bytes32 orderHash)
internal
view
returns (bool)
{
return (
/* Order must have a trader. */
(order.trader != address(0)) &&
/* Order must not be cancelled or filled. */
(cancelledOrFilled[orderHash] == false) &&
/* Order must be settleable. */
_canSettleOrder(order.listingTime, order.expirationTime)
);
}
/**
* @dev Check if the order can be settled at the current timestamp
* @param listingTime order listing time
* @param expirationTime order expiration time
*/
function _canSettleOrder(uint256 listingTime, uint256 expirationTime)
view
internal
returns (bool)
{
return (listingTime < block.timestamp) && (expirationTime == 0 || block.timestamp < expirationTime);
}
/**
* @dev Verify the validity of the signatures
* @param order order
* @param orderHash hash of order
*/
function _validateSignatures(Input calldata order, bytes32 orderHash)
internal
view
returns (bool)
{
if (order.order.trader == msg.sender) {
return true;
}
/* Check user authorization. */
if (
!_validateUserAuthorization(
orderHash,
order.order.trader,
order.v,
order.r,
order.s,
order.signatureVersion,
order.extraSignature
)
) {
return false;
}
if (order.order.expirationTime == 0) {
/* Check oracle authorization. */
require(block.number - order.blockNumber < blockRange, "Signed block number out of range");
if (
!_validateOracleAuthorization(
orderHash,
order.signatureVersion,
order.extraSignature,
order.blockNumber
)
) {
return false;
}
}
return true;
}
/**
* @dev Verify the validity of the user signature
* @param orderHash hash of the order
* @param trader order trader who should be the signer
* @param v v
* @param r r
* @param s s
* @param signatureVersion signature version
* @param extraSignature packed merkle path
*/
function _validateUserAuthorization(
bytes32 orderHash,
address trader,
uint8 v,
bytes32 r,
bytes32 s,
SignatureVersion signatureVersion,
bytes calldata extraSignature
) internal view returns (bool) {
bytes32 hashToSign;
if (signatureVersion == SignatureVersion.Single) {
/* Single-listing authentication: Order signed by trader */
hashToSign = _hashToSign(orderHash);
} else if (signatureVersion == SignatureVersion.Bulk) {
/* Bulk-listing authentication: Merkle root of orders signed by trader */
(bytes32[] memory merklePath) = abi.decode(extraSignature, (bytes32[]));
bytes32 computedRoot = MerkleVerifier._computeRoot(orderHash, merklePath);
hashToSign = _hashToSignRoot(computedRoot);
}
return _recover(hashToSign, v, r, s) == trader;
}
/**
* @dev Verify the validity of oracle signature
* @param orderHash hash of the order
* @param signatureVersion signature version
* @param extraSignature packed oracle signature
* @param blockNumber block number used in oracle signature
*/
function _validateOracleAuthorization(
bytes32 orderHash,
SignatureVersion signatureVersion,
bytes calldata extraSignature,
uint256 blockNumber
) internal view returns (bool) {
bytes32 oracleHash = _hashToSignOracle(orderHash, blockNumber);
uint8 v; bytes32 r; bytes32 s;
if (signatureVersion == SignatureVersion.Single) {
(v, r, s) = abi.decode(extraSignature, (uint8, bytes32, bytes32));
} else if (signatureVersion == SignatureVersion.Bulk) {
/* If the signature was a bulk listing the merkle path musted be unpacked before the oracle signature. */
(bytes32[] memory merklePath, uint8 _v, bytes32 _r, bytes32 _s) = abi.decode(extraSignature, (bytes32[], uint8, bytes32, bytes32));
v = _v; r = _r; s = _s;
}
return _recover(oracleHash, v, r, s) == oracle;
}
/**
* @dev Wrapped ecrecover with safety check for v parameter
* @param v v
* @param r r
* @param s s
*/
function _recover(
bytes32 digest,
uint8 v,
bytes32 r,
bytes32 s
) internal pure returns (address) {
require(v == 27 || v == 28, "Invalid v parameter");
return ecrecover(digest, v, r, s);
}
/**
* @dev Call the matching policy to check orders can be matched and get execution parameters
* @param sell sell order
* @param buy buy order
*/
function _canMatchOrders(Order calldata sell, Order calldata buy)
internal
view
returns (uint256 price, uint256 tokenId, uint256 amount, AssetType assetType)
{
bool canMatch;
if (sell.listingTime <= buy.listingTime) {
/* Seller is maker. */
require(policyManager.isPolicyWhitelisted(sell.matchingPolicy), "Policy is not whitelisted");
(canMatch, price, tokenId, amount, assetType) = IMatchingPolicy(sell.matchingPolicy).canMatchMakerAsk(sell, buy);
} else {
/* Buyer is maker. */
require(policyManager.isPolicyWhitelisted(buy.matchingPolicy), "Policy is not whitelisted");
(canMatch, price, tokenId, amount, assetType) = IMatchingPolicy(buy.matchingPolicy).canMatchMakerBid(buy, sell);
}
require(canMatch, "Orders cannot be matched");
return (price, tokenId, amount, assetType);
}
/**
* @dev Execute all ERC20 token / ETH transfers associated with an order match (fees and buyer => seller transfer)
* @param seller seller
* @param buyer buyer
* @param paymentToken payment token
* @param fees fees
* @param price price
*/
function _executeFundsTransfer(
address seller,
address buyer,
address paymentToken,
Fee[] calldata fees,
uint256 price
) internal {
if (paymentToken == address(0)) {
require(msg.value == price);
}
/* Take fee. */
uint256 receiveAmount = _transferFees(fees, paymentToken, buyer, price);
/* Transfer remainder to seller. */
_transferTo(paymentToken, buyer, seller, receiveAmount);
}
/**
* @dev Charge a fee in ETH or WETH
* @param fees fees to distribute
* @param paymentToken address of token to pay in
* @param from address to charge fees
* @param price price of token
*/
function _transferFees(
Fee[] calldata fees,
address paymentToken,
address from,
uint256 price
) internal returns (uint256) {
uint256 totalFee = 0;
for (uint8 i = 0; i < fees.length; i++) {
uint256 fee = (price * fees[i].rate) / INVERSE_BASIS_POINT;
_transferTo(paymentToken, from, fees[i].recipient, fee);
totalFee += fee;
}
require(totalFee <= price, "Total amount of fees are more than the price");
/* Amount that will be received by seller. */
uint256 receiveAmount = price - totalFee;
return (receiveAmount);
}
/**
* @dev Transfer amount in ETH or WETH
* @param paymentToken address of token to pay in
* @param from token sender
* @param to token recipient
* @param amount amount to transfer
*/
function _transferTo(
address paymentToken,
address from,
address to,
uint256 amount
) internal {
if (amount == 0) {
return;
}
if (paymentToken == address(0)) {
/* Transfer funds in ETH. */
payable(to).transfer(amount);
} else if (paymentToken == weth) {
/* Transfer funds in WETH. */
executionDelegate.transferERC20(weth, from, to, amount);
} else {
revert("Invalid payment token");
}
}
/**
* @dev Execute call through delegate proxy
* @param collection collection contract address
* @param from seller address
* @param to buyer address
* @param tokenId tokenId
* @param assetType asset type of the token
*/
function _executeTokenTransfer(
address collection,
address from,
address to,
uint256 tokenId,
uint256 amount,
AssetType assetType
) internal {
/* Assert collection exists. */
require(_exists(collection), "Collection does not exist");
/* Call execution delegate. */
if (assetType == AssetType.ERC721) {
executionDelegate.transferERC721(collection, from, to, tokenId);
} else if (assetType == AssetType.ERC1155) {
executionDelegate.transferERC1155(collection, from, to, tokenId, amount);
}
}
/**
* @dev Determine if the given address exists
* @param what address to check
*/
function _exists(address what)
internal
view
returns (bool)
{
uint size;
assembly {
size := extcodesize(what)
}
return size > 0;
}
}