forked from ProjectOpenSea/seaport-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seaport.ts
829 lines (755 loc) · 27.6 KB
/
seaport.ts
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
import { providers as multicallProviders } from "@0xsequence/multicall";
import {
BigNumberish,
Contract,
ethers,
PayableOverrides,
providers,
} from "ethers";
import { formatBytes32String, _TypedDataEncoder } from "ethers/lib/utils";
import { SeaportABI } from "./abi/Seaport";
import {
SEAPORT_CONTRACT_NAME,
SEAPORT_CONTRACT_VERSION,
EIP_712_ORDER_TYPE,
KNOWN_CONDUIT_KEYS_TO_CONDUIT,
MAX_INT,
NO_CONDUIT,
OrderType,
CROSS_CHAIN_CONSIDERATION_ADDRESS,
} from "./constants";
import type { Seaport as SeaportContract } from "./typechain/Seaport";
import type {
SeaportConfig,
CreateOrderAction,
CreateOrderInput,
ExchangeAction,
InputCriteria,
Order,
OrderComponents,
OrderParameters,
OrderStatus,
OrderUseCase,
OrderWithNonce,
TipInputItem,
TransactionMethods,
Fulfillment,
} from "./types";
import { getApprovalActions } from "./utils/approval";
import {
getBalancesAndApprovals,
validateOfferBalancesAndApprovals,
} from "./utils/balanceAndApprovalCheck";
import {
fulfillAvailableOrders,
fulfillBasicOrder,
FulfillOrdersMetadata,
fulfillStandardOrder,
shouldUseBasicFulfill,
validateAndSanitizeFromOrderStatus,
} from "./utils/fulfill";
import { getMaximumSizeForOrder, isCurrencyItem } from "./utils/item";
import {
areAllCurrenciesSame,
deductFees,
feeToConsiderationItem,
generateRandomSalt,
mapInputItemToOfferItem,
totalItemsAmount,
} from "./utils/order";
import { executeAllActions, getTransactionMethods } from "./utils/usecase";
export class Seaport {
// Provides the raw interface to the contract for flexibility
public contract: SeaportContract;
private provider: providers.JsonRpcProvider;
// Use the multicall provider for reads for batching and performance optimisations
// NOTE: Do NOT await between sequential requests if you're intending to batch
// instead, use Promise.all() and map to fetch data in parallel
// https://www.npmjs.com/package/@0xsequence/multicall
private multicallProvider: multicallProviders.MulticallProvider;
private config: Required<Omit<SeaportConfig, "overrides">>;
private defaultConduitKey: string;
/**
* @param provider - The provider to use for web3-related calls
* @param considerationConfig - A config to provide flexibility in the usage of Seaport
*/
public constructor(
provider: providers.JsonRpcProvider,
{
overrides,
// Five minute buffer
ascendingAmountFulfillmentBuffer = 300,
balanceAndApprovalChecksOnOrderCreation = true,
conduitKeyToConduit,
}: SeaportConfig
) {
this.provider = provider;
this.multicallProvider = new multicallProviders.MulticallProvider(provider);
this.contract = new Contract(
overrides?.contractAddress ?? CROSS_CHAIN_CONSIDERATION_ADDRESS,
SeaportABI,
this.multicallProvider
) as SeaportContract;
this.config = {
ascendingAmountFulfillmentBuffer,
balanceAndApprovalChecksOnOrderCreation,
conduitKeyToConduit: {
...KNOWN_CONDUIT_KEYS_TO_CONDUIT,
[NO_CONDUIT]: this.contract.address,
...conduitKeyToConduit,
},
};
this.defaultConduitKey = overrides?.defaultConduitKey ?? NO_CONDUIT;
}
/**
* Returns the corresponding order type based on whether it allows partial fills and is restricted by zone
*
* @param input
* @param input.allowPartialFills Whether or not the order can be partially filled
* @param input.restrictedByZone Whether or not the order can only be filled/cancelled by the zone
* @returns the order type
*/
private _getOrderTypeFromOrderOptions({
allowPartialFills,
restrictedByZone,
}: Pick<CreateOrderInput, "allowPartialFills" | "restrictedByZone">) {
if (allowPartialFills) {
return restrictedByZone
? OrderType.PARTIAL_RESTRICTED
: OrderType.PARTIAL_OPEN;
}
return restrictedByZone ? OrderType.FULL_RESTRICTED : OrderType.FULL_OPEN;
}
/**
* Returns a use case that will create an order.
* The use case will contain the list of actions necessary to finish creating an order.
* The list of actions will either be an approval if approvals are necessary
* or a signature request that will then be supplied into the final Order struct, ready to be fulfilled.
*
* @param input
* @param input.conduitKey The conduitKey key to derive where to source your approvals from. Defaults to 0 which refers to the Seaport contract.
* Another special value is address(1) will refer to the legacy proxy. All other must derive to the specified address.
* @param input.zone The zone of the order. Defaults to the zero address.
* @param input.startTime The start time of the order. Defaults to the current unix time.
* @param input.endTime The end time of the order. Defaults to "never end".
* It is HIGHLY recommended to pass in an explicit end time
* @param input.offer The items you are willing to offer. This is a condensed version of the Seaport struct OfferItem for convenience
* @param input.consideration The items that will go to their respective recipients upon receiving your offer.
* @param input.nonce The nonce from which to create the order with. Automatically fetched from the contract if not provided
* @param input.allowPartialFills Whether to allow the order to be partially filled
* @param input.restrictedByZone Whether the order should be restricted by zone
* @param input.fees Convenience array to apply fees onto the order. The fees will be deducted from the
* existing consideration items and then tacked on as new consideration items
* @param input.salt Random salt
* @param input.offerer The order's creator address. Defaults to the first address on the provider.
* @param accountAddress Optional address for which to create the order with
* @returns a use case containing the list of actions needed to be performed in order to create the order
*/
public async createOrder(
{
conduitKey = this.defaultConduitKey,
zone = ethers.constants.AddressZero,
startTime = Math.floor(Date.now() / 1000).toString(),
endTime = MAX_INT.toString(),
offer,
consideration,
nonce,
allowPartialFills,
restrictedByZone,
fees,
salt = generateRandomSalt(),
}: CreateOrderInput,
accountAddress?: string
): Promise<OrderUseCase<CreateOrderAction>> {
const signer = await this.provider.getSigner(accountAddress);
const offerer = await signer.getAddress();
const offerItems = offer.map(mapInputItemToOfferItem);
const considerationItems = [
...consideration.map((consideration) => ({
...mapInputItemToOfferItem(consideration),
recipient: consideration.recipient ?? offerer,
})),
];
if (
!areAllCurrenciesSame({
offer: offerItems,
consideration: considerationItems,
})
) {
throw new Error(
"All currency tokens in the order must be the same token"
);
}
const currencies = [...offerItems, ...considerationItems].filter(
isCurrencyItem
);
const totalCurrencyAmount = totalItemsAmount(currencies);
const operator = this.config.conduitKeyToConduit[conduitKey];
const [resolvedNonce, balancesAndApprovals] = await Promise.all([
nonce ?? this.getNonce(offerer),
getBalancesAndApprovals({
owner: offerer,
items: offerItems,
criterias: [],
multicallProvider: this.multicallProvider,
operator,
}),
]);
const orderType = this._getOrderTypeFromOrderOptions({
allowPartialFills,
restrictedByZone,
});
const considerationItemsWithFees = [
...deductFees(considerationItems, fees),
...(currencies.length
? fees?.map((fee) =>
feeToConsiderationItem({
fee,
token: currencies[0].token,
baseAmount: totalCurrencyAmount.startAmount,
baseEndAmount: totalCurrencyAmount.endAmount,
})
) ?? []
: []),
];
const orderParameters: OrderParameters = {
offerer,
zone,
// TODO: Placeholder
zoneHash: formatBytes32String(resolvedNonce.toString()),
startTime,
endTime,
orderType,
offer: offerItems,
consideration: considerationItemsWithFees,
totalOriginalConsiderationItems: considerationItemsWithFees.length,
salt,
conduitKey,
};
const checkBalancesAndApprovals =
this.config.balanceAndApprovalChecksOnOrderCreation;
const insufficientApprovals = validateOfferBalancesAndApprovals({
offer: offerItems,
criterias: [],
balancesAndApprovals,
throwOnInsufficientBalances: checkBalancesAndApprovals,
operator,
});
const approvalActions = checkBalancesAndApprovals
? await getApprovalActions(insufficientApprovals, signer)
: [];
const createOrderAction = {
type: "create",
getMessageToSign: () => {
return this._getMessageToSign(orderParameters, resolvedNonce);
},
createOrder: async () => {
const signature = await this.signOrder(
orderParameters,
resolvedNonce,
offerer
);
return {
parameters: { ...orderParameters, nonce: resolvedNonce },
signature,
};
},
} as const;
const actions = [...approvalActions, createOrderAction] as const;
return {
actions,
executeAllActions: () =>
executeAllActions(actions) as Promise<OrderWithNonce>,
};
}
/**
* Returns the domain data used when signing typed data
* @returns domain data
*/
private async _getDomainData() {
const { chainId } = await this.provider.getNetwork();
return {
name: SEAPORT_CONTRACT_NAME,
version: SEAPORT_CONTRACT_VERSION,
chainId,
verifyingContract: this.contract.address,
};
}
/**
* Returns a raw message to be signed using EIP-712
* @param orderParameters order parameter struct
* @param nonce nonce of the order
* @returns JSON string of the message to be signed
*/
private async _getMessageToSign(
orderParameters: OrderParameters,
nonce: number
) {
const domainData = await this._getDomainData();
const orderComponents: OrderComponents = {
...orderParameters,
nonce,
};
return JSON.stringify(
_TypedDataEncoder.getPayload(
domainData,
EIP_712_ORDER_TYPE,
orderComponents
)
);
}
/**
* Submits a request to your provider to sign the order. Signed orders are used for off-chain order books.
* @param orderParameters standard order parameter struct
* @param nonce nonce of the offerer
* @param accountAddress optional account address from which to sign the order with.
* @returns the order signature
*/
public async signOrder(
orderParameters: OrderParameters,
nonce: number,
accountAddress?: string
): Promise<string> {
const signer = this.provider.getSigner(accountAddress);
const domainData = await this._getDomainData();
const orderComponents: OrderComponents = {
...orderParameters,
nonce,
};
const signature = await signer._signTypedData(
domainData,
EIP_712_ORDER_TYPE,
orderComponents
);
// Use EIP-2098 compact signatures to save gas. https://eips.ethereum.org/EIPS/eip-2098
return ethers.utils.splitSignature(signature).compact;
}
/**
* Cancels a list of orders so that they are no longer fulfillable.
*
* @param orders list of order components
* @param accountAddress optional account address from which to cancel the orders from.
* @returns the set of transaction methods that can be used
*/
public cancelOrders(
orders: OrderComponents[],
accountAddress?: string
): TransactionMethods {
const signer = this.provider.getSigner(accountAddress);
return getTransactionMethods(this.contract.connect(signer), "cancel", [
orders,
]);
}
/**
* Bulk cancels all existing orders for a given account
* @param offerer the account to bulk cancel orders on
* @returns the set of transaction methods that can be used
*/
public bulkCancelOrders(offerer?: string): TransactionMethods {
const signer = this.provider.getSigner(offerer);
return getTransactionMethods(
this.contract.connect(signer),
"incrementNonce",
[]
);
}
/**
* Approves a list of orders on-chain. This allows accounts to fulfill the order without requiring
* a signature
* @param orders list of order structs
* @param accountAddress optional account address to approve orders.
* @returns the set of transaction methods that can be used
*/
public approveOrders(
orders: Order[],
accountAddress?: string
): TransactionMethods {
const signer = this.provider.getSigner(accountAddress);
return getTransactionMethods(this.contract.connect(signer), "validate", [
orders,
]);
}
/**
* Returns the order status given an order hash
* @param orderHash the hash of the order
* @returns an order status struct
*/
public getOrderStatus(orderHash: string): Promise<OrderStatus> {
return this.contract.getOrderStatus(orderHash);
}
/**
* Gets the nonce of a given offerer
* @param offerer the offerer to get the nonce of
* @returns nonce as a number
*/
public getNonce(offerer: string): Promise<number> {
return this.contract.getNonce(offerer).then((nonce) => nonce.toNumber());
}
/**
* Calculates the order hash of order components so we can forgo executing a request to the contract
* This saves us RPC calls and latency.
*/
public getOrderHash = (orderComponents: OrderComponents): string => {
const offerItemTypeString =
"OfferItem(uint8 itemType,address token,uint256 identifierOrCriteria,uint256 startAmount,uint256 endAmount)";
const considerationItemTypeString =
"ConsiderationItem(uint8 itemType,address token,uint256 identifierOrCriteria,uint256 startAmount,uint256 endAmount,address recipient)";
const orderComponentsPartialTypeString =
"OrderComponents(address offerer,address zone,OfferItem[] offer,ConsiderationItem[] consideration,uint8 orderType,uint256 startTime,uint256 endTime,bytes32 zoneHash,uint256 salt,bytes32 conduitKey,uint256 nonce)";
const orderTypeString = `${orderComponentsPartialTypeString}${considerationItemTypeString}${offerItemTypeString}`;
const offerItemTypeHash = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(offerItemTypeString)
);
const considerationItemTypeHash = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(considerationItemTypeString)
);
const orderTypeHash = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(orderTypeString)
);
const offerHash = ethers.utils.keccak256(
"0x" +
orderComponents.offer
.map((offerItem) => {
return ethers.utils
.keccak256(
"0x" +
[
offerItemTypeHash.slice(2),
offerItem.itemType.toString().padStart(64, "0"),
offerItem.token.slice(2).padStart(64, "0"),
ethers.BigNumber.from(offerItem.identifierOrCriteria)
.toHexString()
.slice(2)
.padStart(64, "0"),
ethers.BigNumber.from(offerItem.startAmount)
.toHexString()
.slice(2)
.padStart(64, "0"),
ethers.BigNumber.from(offerItem.endAmount)
.toHexString()
.slice(2)
.padStart(64, "0"),
].join("")
)
.slice(2);
})
.join("")
);
const considerationHash = ethers.utils.keccak256(
"0x" +
orderComponents.consideration
.map((considerationItem) => {
return ethers.utils
.keccak256(
"0x" +
[
considerationItemTypeHash.slice(2),
considerationItem.itemType.toString().padStart(64, "0"),
considerationItem.token.slice(2).padStart(64, "0"),
ethers.BigNumber.from(
considerationItem.identifierOrCriteria
)
.toHexString()
.slice(2)
.padStart(64, "0"),
ethers.BigNumber.from(considerationItem.startAmount)
.toHexString()
.slice(2)
.padStart(64, "0"),
ethers.BigNumber.from(considerationItem.endAmount)
.toHexString()
.slice(2)
.padStart(64, "0"),
considerationItem.recipient.slice(2).padStart(64, "0"),
].join("")
)
.slice(2);
})
.join("")
);
const derivedOrderHash = ethers.utils.keccak256(
"0x" +
[
orderTypeHash.slice(2),
orderComponents.offerer.slice(2).padStart(64, "0"),
orderComponents.zone.slice(2).padStart(64, "0"),
offerHash.slice(2),
considerationHash.slice(2),
orderComponents.orderType.toString().padStart(64, "0"),
ethers.BigNumber.from(orderComponents.startTime)
.toHexString()
.slice(2)
.padStart(64, "0"),
ethers.BigNumber.from(orderComponents.endTime)
.toHexString()
.slice(2)
.padStart(64, "0"),
orderComponents.zoneHash.slice(2),
orderComponents.salt.slice(2).padStart(64, "0"),
orderComponents.conduitKey.slice(2).padStart(64, "0"),
ethers.BigNumber.from(orderComponents.nonce)
.toHexString()
.slice(2)
.padStart(64, "0"),
].join("")
);
return derivedOrderHash;
};
/**
* Fulfills an order through either the basic method or the standard method
* Units to fill are denominated by the max possible size of the order, which is the greatest common denominator (GCD).
* We expose a helper to get this: getMaximumSizeForOrder
* i.e. If the maximum size of an order is 4, supplying 2 as the units to fulfill will fill half of the order: ;
* @param input
* @param input.order The standard order struct
* @param input.unitsToFill the number of units to fill for the given order. Only used if you wish to partially fill an order
* @param input.offerCriteria an array of criteria with length equal to the number of offer criteria items
* @param input.considerationCriteria an array of criteria with length equal to the number of consideration criteria items
* @param input.tips an array of optional condensed consideration items to be added onto a fulfillment
* @param input.extraData extra data supplied to the order
* @param input.accountAddress optional address from which to fulfill the order from
* @param input.conduitKey the conduitKey to source approvals from
* @returns a use case containing the set of approval actions and fulfillment action
*/
public async fulfillOrder({
order,
unitsToFill,
offerCriteria = [],
considerationCriteria = [],
tips = [],
extraData = "0x",
accountAddress,
conduitKey = this.defaultConduitKey,
}: {
order: OrderWithNonce;
unitsToFill?: BigNumberish;
offerCriteria?: InputCriteria[];
considerationCriteria?: InputCriteria[];
tips?: TipInputItem[];
extraData?: string;
accountAddress?: string;
conduitKey?: string;
}): Promise<OrderUseCase<ExchangeAction>> {
const { parameters: orderParameters } = order;
const { offerer, offer, consideration } = orderParameters;
const fulfiller = await this.provider.getSigner(accountAddress);
const fulfillerAddress = await fulfiller.getAddress();
const offererOperator =
this.config.conduitKeyToConduit[orderParameters.conduitKey];
const fulfillerOperator = this.config.conduitKeyToConduit[conduitKey];
const [
offererBalancesAndApprovals,
fulfillerBalancesAndApprovals,
currentBlock,
orderStatus,
] = await Promise.all([
getBalancesAndApprovals({
owner: offerer,
items: offer,
criterias: offerCriteria,
multicallProvider: this.multicallProvider,
operator: offererOperator,
}),
// Get fulfiller balances and approvals of all items in the set, as offer items
// may be received by the fulfiller for standard fulfills
getBalancesAndApprovals({
owner: fulfillerAddress,
items: [...offer, ...consideration],
criterias: [...offerCriteria, ...considerationCriteria],
multicallProvider: this.multicallProvider,
operator: fulfillerOperator,
}),
this.multicallProvider.getBlock("latest"),
this.getOrderStatus(this.getOrderHash(orderParameters)),
]);
const currentBlockTimestamp = currentBlock.timestamp;
const { totalFilled, totalSize } = orderStatus;
const sanitizedOrder = validateAndSanitizeFromOrderStatus(
order,
orderStatus
);
const timeBasedItemParams = {
startTime: sanitizedOrder.parameters.startTime,
endTime: sanitizedOrder.parameters.endTime,
currentBlockTimestamp,
ascendingAmountTimestampBuffer:
this.config.ascendingAmountFulfillmentBuffer,
};
const tipConsiderationItems = tips.map((tip) => ({
...mapInputItemToOfferItem(tip),
recipient: tip.recipient,
}));
// We use basic fulfills as they are more optimal for simple and "hot" use cases
// We cannot use basic fulfill if user is trying to partially fill though.
if (
!unitsToFill &&
shouldUseBasicFulfill(sanitizedOrder.parameters, totalFilled)
) {
// TODO: Use fulfiller proxy if there are approvals needed directly, but none needed for proxy
return fulfillBasicOrder({
order: sanitizedOrder,
seaportContract: this.contract,
offererBalancesAndApprovals,
fulfillerBalancesAndApprovals,
timeBasedItemParams,
conduitKey,
offererOperator,
fulfillerOperator,
signer: fulfiller,
tips: tipConsiderationItems,
});
}
// Else, we fallback to the standard fulfill order
return fulfillStandardOrder({
order: sanitizedOrder,
unitsToFill,
totalFilled,
totalSize: totalSize.eq(0)
? getMaximumSizeForOrder(sanitizedOrder)
: totalSize,
offerCriteria,
considerationCriteria,
tips: tipConsiderationItems,
extraData,
seaportContract: this.contract,
offererBalancesAndApprovals,
fulfillerBalancesAndApprovals,
timeBasedItemParams,
conduitKey,
signer: fulfiller,
offererOperator,
fulfillerOperator,
});
}
/**
* Fulfills an order through best-effort fashion. Orders that fail will not revert the whole transaction
* unless there's an issue with approvals or balance checks
* @param input
* @param input.fulfillOrderDetails list of helper order details
* @param input.accountAddress the account to fulfill orders on
* @param input.conduitKey the key from which to source approvals from
* @returns a use case containing the set of approval actions and fulfillment action
*/
public async fulfillOrders({
fulfillOrderDetails,
accountAddress,
conduitKey = this.defaultConduitKey,
}: {
fulfillOrderDetails: {
order: OrderWithNonce;
unitsToFill?: BigNumberish;
offerCriteria?: InputCriteria[];
considerationCriteria?: InputCriteria[];
tips?: TipInputItem[];
extraData?: string;
}[];
accountAddress?: string;
conduitKey?: string;
}) {
const fulfiller = await this.provider.getSigner(accountAddress);
const fulfillerAddress = await fulfiller.getAddress();
const allOffererOperators = fulfillOrderDetails.map(
({ order }) =>
this.config.conduitKeyToConduit[order.parameters.conduitKey]
);
const fulfillerOperator = this.config.conduitKeyToConduit[conduitKey];
const allOfferItems = fulfillOrderDetails.flatMap(
({ order }) => order.parameters.offer
);
const allConsiderationItems = fulfillOrderDetails.flatMap(
({ order }) => order.parameters.consideration
);
const allOfferCriteria = fulfillOrderDetails.flatMap(
({ offerCriteria = [] }) => offerCriteria
);
const allConsiderationCriteria = fulfillOrderDetails.flatMap(
({ considerationCriteria = [] }) => considerationCriteria
);
const [
offerersBalancesAndApprovals,
fulfillerBalancesAndApprovals,
currentBlock,
orderStatuses,
] = await Promise.all([
Promise.all(
fulfillOrderDetails.map(({ order, offerCriteria = [] }, i) =>
getBalancesAndApprovals({
owner: order.parameters.offerer,
items: order.parameters.offer,
criterias: offerCriteria,
operator: allOffererOperators[i],
multicallProvider: this.multicallProvider,
})
)
),
// Get fulfiller balances and approvals of all items in the set, as offer items
// may be received by the fulfiller for standard fulfills
getBalancesAndApprovals({
owner: fulfillerAddress,
items: [...allOfferItems, ...allConsiderationItems],
criterias: [...allOfferCriteria, ...allConsiderationCriteria],
operator: fulfillerOperator,
multicallProvider: this.multicallProvider,
}),
this.multicallProvider.getBlock("latest"),
Promise.all(
fulfillOrderDetails.map(({ order }) =>
this.getOrderStatus(this.getOrderHash(order.parameters))
)
),
]);
const ordersMetadata: FulfillOrdersMetadata = fulfillOrderDetails.map(
(orderDetails, index) => ({
order: orderDetails.order,
unitsToFill: orderDetails.unitsToFill,
orderStatus: orderStatuses[index],
offerCriteria: orderDetails.offerCriteria ?? [],
considerationCriteria: orderDetails.considerationCriteria ?? [],
tips:
orderDetails.tips?.map((tip) => ({
...mapInputItemToOfferItem(tip),
recipient: tip.recipient,
})) ?? [],
extraData: orderDetails.extraData ?? "0x",
offererBalancesAndApprovals: offerersBalancesAndApprovals[index],
offererOperator: allOffererOperators[index],
})
);
return fulfillAvailableOrders({
ordersMetadata,
seaportContract: this.contract,
fulfillerBalancesAndApprovals,
currentBlockTimestamp: currentBlock.timestamp,
ascendingAmountTimestampBuffer:
this.config.ascendingAmountFulfillmentBuffer,
fulfillerOperator,
signer: fulfiller,
conduitKey,
});
}
/**
* NOTE: Largely incomplete. Does NOT do any balance or approval checks.
* Just exposes the bare bones matchOrders where clients will have to supply
* their own overrides as needed.
* @param input
* @param input.orders the list of orders to match
* @param input.fulfillments the list of fulfillments to match offer and considerations
* @param overrides any overrides the client wants, will need to pass in value for matching orders with ETH.
* @returns set of transaction methods for matching orders
*/
public matchOrders({
orders,
fulfillments,
overrides,
}: {
orders: OrderWithNonce[];
fulfillments: Fulfillment[];
overrides?: PayableOverrides;
}): TransactionMethods {
return getTransactionMethods(this.contract, "matchOrders", [
orders,
fulfillments,
overrides,
]);
}
}