-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathReferenceBasicOrderFulfiller.sol
935 lines (847 loc) · 35.4 KB
/
ReferenceBasicOrderFulfiller.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// prettier-ignore
import {
OrderType,
BasicOrderType,
ItemType,
BasicOrderRouteType
} from "contracts/lib/ConsiderationEnums.sol";
// prettier-ignore
import {
AdditionalRecipient,
BasicOrderParameters,
OfferItem,
ConsiderationItem,
SpentItem,
ReceivedItem
} from "contracts/lib/ConsiderationStructs.sol";
// prettier-ignore
import {
AccumulatorStruct,
BasicFulfillmentHashes,
FulfillmentItemTypes
} from "./ReferenceConsiderationStructs.sol";
import { ReferenceOrderValidator } from "./ReferenceOrderValidator.sol";
import "contracts/lib/ConsiderationConstants.sol";
/**
* @title BasicOrderFulfiller
* @author 0age
* @notice BasicOrderFulfiller contains functionality for fulfilling "basic"
* orders.
*/
contract ReferenceBasicOrderFulfiller is ReferenceOrderValidator {
// Map BasicOrderType to BasicOrderRouteType
mapping(BasicOrderType => BasicOrderRouteType) internal _OrderToRouteType;
// Map BasicOrderType to OrderType
mapping(BasicOrderType => OrderType) internal _BasicOrderToOrderType;
/**
* @dev Derive and set hashes, reference chainId, and associated domain
* separator during deployment.
*
* @param conduitController A contract that deploys conduits, or proxies
* that may optionally be used to transfer approved
* ERC20/721/1155 tokens.
*/
constructor(address conduitController)
ReferenceOrderValidator(conduitController)
{
createMappings();
}
/**
* @dev Creates a mapping of BasicOrderType Enums to BasicOrderRouteType Enums
* and BasicOrderType Enums to OrderType Enums
*/
function createMappings() internal {
// BasicOrderType to BasicOrderRouteType
// ETH TO ERC 721
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC721_FULL_OPEN
] = BasicOrderRouteType.ETH_TO_ERC721;
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC721_PARTIAL_OPEN
] = BasicOrderRouteType.ETH_TO_ERC721;
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC721_FULL_RESTRICTED
] = BasicOrderRouteType.ETH_TO_ERC721;
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC721_PARTIAL_RESTRICTED
] = BasicOrderRouteType.ETH_TO_ERC721;
// ETH TO ERC 1155
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC1155_FULL_OPEN
] = BasicOrderRouteType.ETH_TO_ERC1155;
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC1155_PARTIAL_OPEN
] = BasicOrderRouteType.ETH_TO_ERC1155;
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC1155_FULL_RESTRICTED
] = BasicOrderRouteType.ETH_TO_ERC1155;
_OrderToRouteType[
BasicOrderType.ETH_TO_ERC1155_PARTIAL_RESTRICTED
] = BasicOrderRouteType.ETH_TO_ERC1155;
// ERC 20 TO ERC 721
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC721_FULL_OPEN
] = BasicOrderRouteType.ERC20_TO_ERC721;
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC721_PARTIAL_OPEN
] = BasicOrderRouteType.ERC20_TO_ERC721;
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC721_FULL_RESTRICTED
] = BasicOrderRouteType.ERC20_TO_ERC721;
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC721_PARTIAL_RESTRICTED
] = BasicOrderRouteType.ERC20_TO_ERC721;
// ERC 20 TO ERC 1155
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC1155_FULL_OPEN
] = BasicOrderRouteType.ERC20_TO_ERC1155;
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC1155_PARTIAL_OPEN
] = BasicOrderRouteType.ERC20_TO_ERC1155;
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC1155_FULL_RESTRICTED
] = BasicOrderRouteType.ERC20_TO_ERC1155;
_OrderToRouteType[
BasicOrderType.ERC20_TO_ERC1155_PARTIAL_RESTRICTED
] = BasicOrderRouteType.ERC20_TO_ERC1155;
// ERC 721 TO ERC 20
_OrderToRouteType[
BasicOrderType.ERC721_TO_ERC20_FULL_OPEN
] = BasicOrderRouteType.ERC721_TO_ERC20;
_OrderToRouteType[
BasicOrderType.ERC721_TO_ERC20_PARTIAL_OPEN
] = BasicOrderRouteType.ERC721_TO_ERC20;
_OrderToRouteType[
BasicOrderType.ERC721_TO_ERC20_FULL_RESTRICTED
] = BasicOrderRouteType.ERC721_TO_ERC20;
_OrderToRouteType[
BasicOrderType.ERC721_TO_ERC20_PARTIAL_RESTRICTED
] = BasicOrderRouteType.ERC721_TO_ERC20;
// ERC 1155 TO ERC 20
_OrderToRouteType[
BasicOrderType.ERC1155_TO_ERC20_FULL_OPEN
] = BasicOrderRouteType.ERC1155_TO_ERC20;
_OrderToRouteType[
BasicOrderType.ERC1155_TO_ERC20_PARTIAL_OPEN
] = BasicOrderRouteType.ERC1155_TO_ERC20;
_OrderToRouteType[
BasicOrderType.ERC1155_TO_ERC20_FULL_RESTRICTED
] = BasicOrderRouteType.ERC1155_TO_ERC20;
_OrderToRouteType[
BasicOrderType.ERC1155_TO_ERC20_PARTIAL_RESTRICTED
] = BasicOrderRouteType.ERC1155_TO_ERC20;
// Basic OrderType to OrderType
// FULL OPEN
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC721_FULL_OPEN
] = OrderType.FULL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC1155_FULL_OPEN
] = OrderType.FULL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC721_FULL_OPEN
] = OrderType.FULL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC1155_FULL_OPEN
] = OrderType.FULL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC721_TO_ERC20_FULL_OPEN
] = OrderType.FULL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC1155_TO_ERC20_FULL_OPEN
] = OrderType.FULL_OPEN;
// PARTIAL OPEN
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC721_PARTIAL_OPEN
] = OrderType.PARTIAL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC1155_PARTIAL_OPEN
] = OrderType.PARTIAL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC721_PARTIAL_OPEN
] = OrderType.PARTIAL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC1155_PARTIAL_OPEN
] = OrderType.PARTIAL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC721_TO_ERC20_PARTIAL_OPEN
] = OrderType.PARTIAL_OPEN;
_BasicOrderToOrderType[
BasicOrderType.ERC1155_TO_ERC20_PARTIAL_OPEN
] = OrderType.PARTIAL_OPEN;
// FULL RESTRICTED
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC721_FULL_RESTRICTED
] = OrderType.FULL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC1155_FULL_RESTRICTED
] = OrderType.FULL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC721_FULL_RESTRICTED
] = OrderType.FULL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC1155_FULL_RESTRICTED
] = OrderType.FULL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC721_TO_ERC20_FULL_RESTRICTED
] = OrderType.FULL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC1155_TO_ERC20_FULL_RESTRICTED
] = OrderType.FULL_RESTRICTED;
// PARTIAL RESTRICTED
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC721_PARTIAL_RESTRICTED
] = OrderType.PARTIAL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ETH_TO_ERC1155_PARTIAL_RESTRICTED
] = OrderType.PARTIAL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC721_PARTIAL_RESTRICTED
] = OrderType.PARTIAL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC20_TO_ERC1155_PARTIAL_RESTRICTED
] = OrderType.PARTIAL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC721_TO_ERC20_PARTIAL_RESTRICTED
] = OrderType.PARTIAL_RESTRICTED;
_BasicOrderToOrderType[
BasicOrderType.ERC1155_TO_ERC20_PARTIAL_RESTRICTED
] = OrderType.PARTIAL_RESTRICTED;
}
/**
* @dev Internal function to fulfill an order offering an ERC20, ERC721, or
* ERC1155 item by supplying Ether (or other native tokens), ERC20
* tokens, an ERC721 item, or an ERC1155 item as consideration. Six
* permutations are supported: Native token to ERC721, Native token to
* ERC1155, ERC20 to ERC721, ERC20 to ERC1155, ERC721 to ERC20, and
* ERC1155 to ERC20 (with native tokens supplied as msg.value). For an
* order to be eligible for fulfillment via this method, it must
* contain a single offer item (though that item may have a greater
* amount if the item is not an ERC721). An arbitrary number of
* "additional recipients" may also be supplied which will each receive
* native tokens or ERC20 items from the fulfiller as consideration.
* Refer to the documentation for a more comprehensive summary of how
* to utilize with this method and what orders are compatible with it.
*
* @param parameters Additional information on the fulfilled order. Note
* that the offerer and the fulfiller must first approve
* this contract (or their chosen conduit if indicated)
* before any tokens can be transferred. Also note that
* contract recipients of ERC1155 consideration items must
* implement `onERC1155Received` in order to receive those
* items.
*
* @return A boolean indicating whether the order has been fulfilled.
*/
function _validateAndFulfillBasicOrder(
BasicOrderParameters calldata parameters
) internal returns (bool) {
// Determine the basic order route type from the basic order type.
BasicOrderRouteType route;
{
BasicOrderType basicType = parameters.basicOrderType;
route = _OrderToRouteType[basicType];
}
// Determine the order type from the basic order type.
OrderType orderType;
{
BasicOrderType basicType = parameters.basicOrderType;
orderType = _BasicOrderToOrderType[basicType];
}
// Declare additional recipient item type to derive from the route type.
ItemType additionalRecipientsItemType;
if (
route == BasicOrderRouteType.ETH_TO_ERC721 ||
route == BasicOrderRouteType.ETH_TO_ERC1155
) {
additionalRecipientsItemType = ItemType.NATIVE;
} else {
additionalRecipientsItemType = ItemType.ERC20;
}
// Revert if msg.value was not supplied as part of a payable route.
if (msg.value == 0 && additionalRecipientsItemType == ItemType.NATIVE) {
revert InvalidMsgValue(msg.value);
}
// Revert if msg.value was supplied as part of a non-payable route.
if (msg.value != 0 && additionalRecipientsItemType == ItemType.ERC20) {
revert InvalidMsgValue(msg.value);
}
// Determine the token that additional recipients should have set.
address additionalRecipientsToken;
if (
route == BasicOrderRouteType.ERC721_TO_ERC20 ||
route == BasicOrderRouteType.ERC1155_TO_ERC20
) {
additionalRecipientsToken = parameters.offerToken;
} else {
additionalRecipientsToken = parameters.considerationToken;
}
// Determine the item type for received items.
ItemType receivedItemType;
if (
route == BasicOrderRouteType.ETH_TO_ERC721 ||
route == BasicOrderRouteType.ETH_TO_ERC1155
) {
receivedItemType = ItemType.NATIVE;
} else if (
route == BasicOrderRouteType.ERC20_TO_ERC721 ||
route == BasicOrderRouteType.ERC20_TO_ERC1155
) {
receivedItemType = ItemType.ERC20;
} else if (route == BasicOrderRouteType.ERC721_TO_ERC20) {
receivedItemType = ItemType.ERC721;
} else {
receivedItemType = ItemType.ERC1155;
}
// Determine the item type for the offered item.
ItemType offeredItemType;
if (
route == BasicOrderRouteType.ERC721_TO_ERC20 ||
route == BasicOrderRouteType.ERC1155_TO_ERC20
) {
offeredItemType = ItemType.ERC20;
} else if (
route == BasicOrderRouteType.ETH_TO_ERC721 ||
route == BasicOrderRouteType.ERC20_TO_ERC721
) {
offeredItemType = ItemType.ERC721;
} else {
offeredItemType = ItemType.ERC1155;
}
// Derive & validate order using parameters and update order status.
_prepareBasicFulfillment(
parameters,
orderType,
receivedItemType,
additionalRecipientsItemType,
additionalRecipientsToken,
offeredItemType
);
// Read offerer from calldata and place on the stack.
address payable offerer = parameters.offerer;
// Determine conduitKey argument used by transfer functions.
bytes32 conduitKey;
if (
route == BasicOrderRouteType.ERC721_TO_ERC20 ||
route == BasicOrderRouteType.ERC1155_TO_ERC20
) {
conduitKey = parameters.fulfillerConduitKey;
} else {
conduitKey = parameters.offererConduitKey;
}
// Declare transfer accumulator that will collect transfers that can be
// bundled into a single call to their associated conduit.
AccumulatorStruct memory accumulatorStruct;
// Transfer tokens based on the route.
if (route == BasicOrderRouteType.ETH_TO_ERC721) {
// Transfer ERC721 to caller using offerer's conduit if applicable.
_transferERC721(
parameters.offerToken,
offerer,
msg.sender,
parameters.offerIdentifier,
parameters.offerAmount,
conduitKey,
accumulatorStruct
);
// Transfer native to recipients, return excess to caller & wrap up.
_transferEthAndFinalize(parameters.considerationAmount, parameters);
} else if (route == BasicOrderRouteType.ETH_TO_ERC1155) {
// Transfer ERC1155 to caller using offerer's conduit if applicable.
_transferERC1155(
parameters.offerToken,
offerer,
msg.sender,
parameters.offerIdentifier,
parameters.offerAmount,
conduitKey,
accumulatorStruct
);
// Transfer native to recipients, return excess to caller & wrap up.
_transferEthAndFinalize(parameters.considerationAmount, parameters);
} else if (route == BasicOrderRouteType.ERC20_TO_ERC721) {
// Transfer ERC721 to caller using offerer's conduit if applicable.
_transferERC721(
parameters.offerToken,
offerer,
msg.sender,
parameters.offerIdentifier,
parameters.offerAmount,
conduitKey,
accumulatorStruct
);
// Transfer ERC20 tokens to all recipients and wrap up.
_transferERC20AndFinalize(
msg.sender,
offerer,
parameters.considerationToken,
parameters.considerationAmount,
parameters,
false, // Send full amount indicated by all consideration items.
accumulatorStruct
);
} else if (route == BasicOrderRouteType.ERC20_TO_ERC1155) {
// Transfer ERC1155 to caller using offerer's conduit if applicable.
_transferERC1155(
parameters.offerToken,
offerer,
msg.sender,
parameters.offerIdentifier,
parameters.offerAmount,
conduitKey,
accumulatorStruct
);
// Transfer ERC20 tokens to all recipients and wrap up.
_transferERC20AndFinalize(
msg.sender,
offerer,
parameters.considerationToken,
parameters.considerationAmount,
parameters,
false, // Send full amount indicated by all consideration items.
accumulatorStruct
);
} else if (route == BasicOrderRouteType.ERC721_TO_ERC20) {
// Transfer ERC721 to offerer using caller's conduit if applicable.
_transferERC721(
parameters.considerationToken,
msg.sender,
offerer,
parameters.considerationIdentifier,
parameters.considerationAmount,
conduitKey,
accumulatorStruct
);
// Transfer ERC20 tokens to all recipients and wrap up.
_transferERC20AndFinalize(
offerer,
msg.sender,
parameters.offerToken,
parameters.offerAmount,
parameters,
true, // Reduce amount sent to fulfiller by additional amounts.
accumulatorStruct
);
} else {
// route == BasicOrderRouteType.ERC1155_TO_ERC20
// Transfer ERC1155 to offerer using caller's conduit if applicable.
_transferERC1155(
parameters.considerationToken,
msg.sender,
offerer,
parameters.considerationIdentifier,
parameters.considerationAmount,
conduitKey,
accumulatorStruct
);
// Transfer ERC20 tokens to all recipients and wrap up.
_transferERC20AndFinalize(
offerer,
msg.sender,
parameters.offerToken,
parameters.offerAmount,
parameters,
true, // Reduce amount sent to fulfiller by additional amounts.
accumulatorStruct
);
}
// Trigger any remaining accumulated transfers via call to the conduit.
_triggerIfArmed(accumulatorStruct);
return true;
}
/**
* @dev Internal function to calculate the order hash.
*
* @param hashes The array of offerItems and receivedItems
* hashes.
* @param parameters The parameters of the basic order.
* @param fulfillmentItemTypes The fulfillment's item type.
*/
function _hashOrder(
BasicFulfillmentHashes memory hashes,
BasicOrderParameters calldata parameters,
FulfillmentItemTypes memory fulfillmentItemTypes
) internal view returns (bytes32 orderHash) {
// Read offerer's current nonce from storage and place on the stack.
uint256 nonce = _getNonce(parameters.offerer);
// Hash the contents to get the orderHash
orderHash = keccak256(
abi.encode(
hashes.typeHash,
parameters.offerer,
parameters.zone,
hashes.offerItemsHash,
hashes.receivedItemsHash,
fulfillmentItemTypes.orderType,
parameters.startTime,
parameters.endTime,
parameters.zoneHash,
parameters.salt,
parameters.offererConduitKey,
nonce
)
);
}
/**
* @dev Internal function to prepare fulfillment of a basic order. This
* calculates the order hash, emits an OrderFulfilled event, and
* asserts basic order validity.
*
* @param parameters The parameters of the basic order.
* @param orderType The order type.
* @param receivedItemType The item type of the initial
* consideration item on the order.
* @param additionalRecipientsItemType The item type of any additional
* consideration item on the order.
* @param additionalRecipientsToken The ERC20 token contract address (if
* applicable) for any additional
* consideration item on the order.
* @param offeredItemType The item type of the offered item on
* the order.
*/
function _prepareBasicFulfillment(
BasicOrderParameters calldata parameters,
OrderType orderType,
ItemType receivedItemType,
ItemType additionalRecipientsItemType,
address additionalRecipientsToken,
ItemType offeredItemType
) internal {
// Ensure current timestamp falls between order start time and end time.
_verifyTime(parameters.startTime, parameters.endTime, true);
// Verify that calldata offsets for all dynamic types were produced by
// default encoding. This is only required on the optimized contract,
// but is included here to maintain parity.
_assertValidBasicOrderParameterOffsets();
// Ensure supplied consideration array length is not less than original.
_assertConsiderationLengthIsNotLessThanOriginalConsiderationLength(
parameters.additionalRecipients.length + 1,
parameters.totalOriginalAdditionalRecipients
);
// Memory to store hashes.
BasicFulfillmentHashes memory hashes;
// Store ItemType/Token parameters in a struct in memory to avoid stack issues.
FulfillmentItemTypes memory fulfillmentItemTypes = FulfillmentItemTypes(
orderType,
receivedItemType,
additionalRecipientsItemType,
additionalRecipientsToken,
offeredItemType
);
// Array of Received Items for use with OrderFulfilled event.
ReceivedItem[] memory consideration = new ReceivedItem[](
parameters.additionalRecipients.length + 1
);
{
// Load consideration item typehash from runtime and place on stack.
hashes.typeHash = _CONSIDERATION_ITEM_TYPEHASH;
// Create Consideration item.
ConsiderationItem memory primaryConsiderationItem = (
ConsiderationItem(
fulfillmentItemTypes.receivedItemType,
parameters.considerationToken,
parameters.considerationIdentifier,
parameters.considerationAmount,
parameters.considerationAmount,
parameters.offerer
)
);
// Array of all consideration item hashes.
hashes.considerationHashes = new bytes32[](
parameters.totalOriginalAdditionalRecipients + 1
);
// Hash contents.
hashes.considerationHashes[0] = keccak256(
abi.encode(
hashes.typeHash,
primaryConsiderationItem.itemType,
primaryConsiderationItem.token,
primaryConsiderationItem.identifierOrCriteria,
primaryConsiderationItem.startAmount,
primaryConsiderationItem.endAmount,
primaryConsiderationItem.recipient
)
);
// Declare memory for additionalReceivedItem, additionalRecipientItem.
ReceivedItem memory additionalReceivedItem;
ConsiderationItem memory additionalRecipientItem;
// Create Received item.
ReceivedItem memory primaryReceivedItem = ReceivedItem(
fulfillmentItemTypes.receivedItemType,
primaryConsiderationItem.token,
primaryConsiderationItem.identifierOrCriteria,
primaryConsiderationItem.endAmount,
primaryConsiderationItem.recipient
);
// Add the Received item to the
// OrderFulfilled ReceivedItem[].
consideration[0] = primaryReceivedItem;
/** Loop through all additionalRecipients, to generate
* ReceivedItems for OrderFulfilled Event and
* ConsiderationItems for hashing.
*/
for (
uint256 recipientCount = 0;
recipientCount < parameters.additionalRecipients.length;
recipientCount++
) {
// Get the next additionalRecipient.
AdditionalRecipient memory additionalRecipient = (
parameters.additionalRecipients[recipientCount]
);
// Create a Received item for each additional recipients.
additionalReceivedItem = ReceivedItem(
fulfillmentItemTypes.additionalRecipientsItemType,
fulfillmentItemTypes.additionalRecipientsToken,
0,
additionalRecipient.amount,
additionalRecipient.recipient
);
// Add additional Received items to the
// OrderFulfilled ReceivedItem[].
consideration[recipientCount + 1] = additionalReceivedItem;
// Skip hashing items not contained in the
// Original Recipients.
if (
recipientCount >=
parameters.totalOriginalAdditionalRecipients
) {
continue;
}
// Create a new consideration item for each additional recipient.
additionalRecipientItem = ConsiderationItem(
fulfillmentItemTypes.additionalRecipientsItemType,
fulfillmentItemTypes.additionalRecipientsToken,
0,
additionalRecipient.amount,
additionalRecipient.amount,
additionalRecipient.recipient
);
// Calculate the EIP712 ConsiderationItem hash for
// each additional recipients.
hashes.considerationHashes[recipientCount + 1] = keccak256(
abi.encode(
hashes.typeHash,
additionalRecipientItem.itemType,
additionalRecipientItem.token,
additionalRecipientItem.identifierOrCriteria,
additionalRecipientItem.startAmount,
additionalRecipientItem.endAmount,
additionalRecipientItem.recipient
)
);
}
/**
* The considerationHashes array now contains
* all consideration Item hashes.
*
* The consideration array now contains all received
* items excluding tips for OrderFulfilled Event.
*/
// Get hash of all consideration items.
hashes.receivedItemsHash = keccak256(
abi.encodePacked(hashes.considerationHashes)
);
// Get remainder of additionalRecipients for tips.
for (
uint256 additionalTips = parameters
.totalOriginalAdditionalRecipients;
additionalTips < parameters.additionalRecipients.length;
additionalTips++
) {
// Get the next additionalRecipient.
AdditionalRecipient memory additionalRecipient = (
parameters.additionalRecipients[additionalTips]
);
// Create the ReceivedItem.
additionalReceivedItem = ReceivedItem(
fulfillmentItemTypes.additionalRecipientsItemType,
fulfillmentItemTypes.additionalRecipientsToken,
0,
additionalRecipient.amount,
additionalRecipient.recipient
);
// Add additional received items to the
// OrderFulfilled ReceivedItem[].
consideration[additionalTips + 1] = additionalReceivedItem;
}
}
// Now let's handle the offer side.
// Write the offer to the Event SpentItem array.
SpentItem[] memory offer = new SpentItem[](1);
{
// Place offer item typehash on the stack.
hashes.typeHash = _OFFER_ITEM_TYPEHASH;
// Create Spent item.
SpentItem memory offerItem = SpentItem(
fulfillmentItemTypes.offeredItemType,
parameters.offerToken,
parameters.offerIdentifier,
parameters.offerAmount
);
// Add the offer item to the SpentItem array.
offer[0] = offerItem;
// Get the hash of the Spent item, treated as an Offer item.
bytes32[1] memory offerItemHashes = [
keccak256(
abi.encode(
hashes.typeHash,
offerItem.itemType,
offerItem.token,
offerItem.identifier,
offerItem.amount,
offerItem.amount //Assembly uses OfferItem instead of SpentItem.
)
)
];
// Get hash of all Spent items.
hashes.offerItemsHash = keccak256(
abi.encodePacked(offerItemHashes)
);
}
{
// Create the OrderComponent in order to derive
// the orderHash.
// Load order typehash from runtime code and place on stack.
hashes.typeHash = _ORDER_TYPEHASH;
// Derive the order hash.
hashes.orderHash = _hashOrder(
hashes,
parameters,
fulfillmentItemTypes
);
// Emit an event signifying that the order has been fulfilled.
emit OrderFulfilled(
hashes.orderHash,
parameters.offerer,
parameters.zone,
msg.sender,
offer,
consideration
);
}
// Determine whether order is restricted and, if so, that it is valid.
_assertRestrictedBasicOrderValidity(
hashes.orderHash,
parameters.zoneHash,
orderType,
parameters.offerer,
parameters.zone
);
// Verify and update the status of the derived order.
_validateBasicOrderAndUpdateStatus(
hashes.orderHash,
parameters.offerer,
parameters.signature
);
}
/**
* @dev Internal function to transfer Ether (or other native tokens) to a
* given recipient as part of basic order fulfillment. Note that
* proxies are not utilized for native tokens as the transferred amount
* must be provided as msg.value.
*
* @param amount The amount to transfer.
* @param parameters The parameters of the basic order in question.
*/
function _transferEthAndFinalize(
uint256 amount,
BasicOrderParameters calldata parameters
) internal {
// Put ether value supplied by the caller on the stack.
uint256 etherRemaining = msg.value;
// Iterate over each additional recipient.
for (uint256 i = 0; i < parameters.additionalRecipients.length; ++i) {
// Retrieve the additional recipient.
AdditionalRecipient calldata additionalRecipient = (
parameters.additionalRecipients[i]
);
// Read ether amount to transfer to recipient and place on stack.
uint256 additionalRecipientAmount = additionalRecipient.amount;
// Ensure that sufficient Ether is available.
if (additionalRecipientAmount > etherRemaining) {
revert InsufficientEtherSupplied();
}
// Transfer Ether to the additional recipient.
_transferEth(
additionalRecipient.recipient,
additionalRecipientAmount
);
// Reduce ether value available.
etherRemaining -= additionalRecipientAmount;
}
// Ensure that sufficient Ether is still available.
if (amount > etherRemaining) {
revert InsufficientEtherSupplied();
}
// Transfer Ether to the offerer.
_transferEth(parameters.offerer, amount);
// If any Ether remains after transfers, return it to the caller.
if (etherRemaining > amount) {
// Transfer remaining Ether to the caller.
_transferEth(payable(msg.sender), etherRemaining - amount);
}
}
/**
* @dev Internal function to transfer ERC20 tokens to a given recipient as
* part of basic order fulfillment. Note that proxies are not utilized
* for ERC20 tokens.
*
* @param from The originator of the ERC20 token transfer.
* @param to The recipient of the ERC20 token transfer.
* @param erc20Token The ERC20 token to transfer.
* @param amount The amount of ERC20 tokens to transfer.
* @param parameters The parameters of the order.
* @param fromOfferer Whether to decrement amount from the offered amount.
* @param accumulatorStruct A struct containing conduit transfer data and its
* corresponding conduitKey.
*/
function _transferERC20AndFinalize(
address from,
address to,
address erc20Token,
uint256 amount,
BasicOrderParameters calldata parameters,
bool fromOfferer,
AccumulatorStruct memory accumulatorStruct
) internal {
// Determine the appropriate conduit to utilize.
bytes32 conduitKey;
if (fromOfferer) {
conduitKey = parameters.offererConduitKey;
} else {
conduitKey = parameters.fulfillerConduitKey;
}
// Iterate over each additional recipient.
for (uint256 i = 0; i < parameters.additionalRecipients.length; ++i) {
// Retrieve the additional recipient.
AdditionalRecipient calldata additionalRecipient = (
parameters.additionalRecipients[i]
);
// Decrement the amount to transfer to fulfiller if indicated.
if (fromOfferer) {
amount -= additionalRecipient.amount;
}
// Transfer ERC20 tokens to additional recipient given approval.
_transferERC20(
erc20Token,
from,
additionalRecipient.recipient,
additionalRecipient.amount,
conduitKey,
accumulatorStruct
);
}
// Transfer ERC20 token amount (from account must have proper approval).
_transferERC20(
erc20Token,
from,
to,
amount,
conduitKey,
accumulatorStruct
);
}
}