@opensea/seaport-js 1.0.1-beta.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/lib/abi/ERC1155.d.ts +29 -29
  2. package/lib/abi/ERC1155.js +319 -0
  3. package/lib/abi/ERC1155.js.map +1 -0
  4. package/lib/abi/ERC20.d.ts +29 -29
  5. package/lib/abi/ERC20.js +317 -0
  6. package/lib/abi/ERC20.js.map +1 -0
  7. package/lib/abi/ERC721.d.ts +29 -29
  8. package/lib/abi/ERC721.js +337 -0
  9. package/lib/abi/ERC721.js.map +1 -0
  10. package/lib/abi/Seaport.d.ts +112 -112
  11. package/lib/abi/Seaport.js +2585 -0
  12. package/lib/abi/Seaport.js.map +1 -0
  13. package/lib/constants.d.ts +49 -49
  14. package/lib/constants.js +73 -0
  15. package/lib/constants.js.map +1 -0
  16. package/lib/index.d.ts +2 -2
  17. package/lib/index.js +6 -2
  18. package/lib/index.js.map +1 -1
  19. package/lib/seaport.d.ts +181 -181
  20. package/lib/seaport.js +720 -0
  21. package/lib/seaport.js.map +1 -0
  22. package/lib/typechain/ERC1155.d.ts +189 -0
  23. package/lib/typechain/ERC1155.js +3 -0
  24. package/lib/typechain/ERC1155.js.map +1 -0
  25. package/lib/typechain/ERC20.d.ts +209 -0
  26. package/lib/typechain/ERC20.js +3 -0
  27. package/lib/typechain/ERC20.js.map +1 -0
  28. package/lib/typechain/ERC721.d.ts +220 -0
  29. package/lib/typechain/ERC721.js +3 -0
  30. package/lib/typechain/ERC721.js.map +1 -0
  31. package/lib/typechain/Seaport.d.ts +686 -0
  32. package/lib/typechain/Seaport.js +3 -0
  33. package/lib/typechain/Seaport.js.map +1 -0
  34. package/lib/typechain/common.d.ts +21 -0
  35. package/lib/typechain/common.js +3 -0
  36. package/lib/typechain/common.js.map +1 -0
  37. package/lib/types.d.ts +207 -207
  38. package/lib/types.js +3 -0
  39. package/lib/types.js.map +1 -0
  40. package/lib/utils/approval.d.ts +9 -9
  41. package/lib/utils/approval.js +108 -0
  42. package/lib/utils/approval.js.map +1 -0
  43. package/lib/utils/balance.d.ts +4 -4
  44. package/lib/utils/balance.js +86 -0
  45. package/lib/utils/balance.js.map +1 -0
  46. package/lib/utils/balanceAndApprovalCheck.d.ts +108 -108
  47. package/lib/utils/balanceAndApprovalCheck.js +322 -0
  48. package/lib/utils/balanceAndApprovalCheck.js.map +1 -0
  49. package/lib/utils/criteria.d.ts +14 -13
  50. package/lib/utils/criteria.js +91 -0
  51. package/lib/utils/criteria.js.map +1 -0
  52. package/lib/utils/fulfill.d.ts +84 -84
  53. package/lib/utils/fulfill.js +573 -0
  54. package/lib/utils/fulfill.js.map +1 -0
  55. package/lib/utils/gcd.d.ts +3 -3
  56. package/lib/utils/gcd.js +25 -0
  57. package/lib/utils/gcd.js.map +1 -0
  58. package/lib/utils/item.d.ts +29 -30
  59. package/lib/utils/item.js +136 -0
  60. package/lib/utils/item.js.map +1 -0
  61. package/lib/utils/match.d.ts +49 -49
  62. package/lib/utils/match.js +56 -0
  63. package/lib/utils/match.js.map +1 -0
  64. package/lib/utils/merkletree.d.ts +11 -11
  65. package/lib/utils/merkletree.js +32 -0
  66. package/lib/utils/merkletree.js.map +1 -0
  67. package/lib/utils/order.d.ts +37 -37
  68. package/lib/utils/order.js +224 -0
  69. package/lib/utils/order.js.map +1 -0
  70. package/lib/utils/usecase.d.ts +4 -4
  71. package/lib/utils/usecase.js +158 -0
  72. package/lib/utils/usecase.js.map +1 -0
  73. package/package.json +3 -5
  74. package/lib/index.esm.mjs +0 -2
  75. package/lib/index.esm.mjs.map +0 -1
  76. package/lib/index.modern.mjs +0 -2
  77. package/lib/index.modern.mjs.map +0 -1
  78. package/lib/index.umd.js +0 -2
  79. package/lib/index.umd.js.map +0 -1
package/lib/seaport.js ADDED
@@ -0,0 +1,720 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
51
+ if (!m) return o;
52
+ var i = m.call(o), r, ar = [], e;
53
+ try {
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
62
+ }
63
+ return ar;
64
+ };
65
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
+ if (ar || !(i in from)) {
68
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
+ ar[i] = from[i];
70
+ }
71
+ }
72
+ return to.concat(ar || Array.prototype.slice.call(from));
73
+ };
74
+ Object.defineProperty(exports, "__esModule", { value: true });
75
+ exports.Seaport = void 0;
76
+ var multicall_1 = require("@0xsequence/multicall");
77
+ var ethers_1 = require("ethers");
78
+ var utils_1 = require("ethers/lib/utils");
79
+ var Seaport_1 = require("./abi/Seaport");
80
+ var constants_1 = require("./constants");
81
+ var approval_1 = require("./utils/approval");
82
+ var balanceAndApprovalCheck_1 = require("./utils/balanceAndApprovalCheck");
83
+ var fulfill_1 = require("./utils/fulfill");
84
+ var item_1 = require("./utils/item");
85
+ var order_1 = require("./utils/order");
86
+ var usecase_1 = require("./utils/usecase");
87
+ var Seaport = /** @class */ (function () {
88
+ /**
89
+ * @param provider - The provider to use for web3-related calls
90
+ * @param considerationConfig - A config to provide flexibility in the usage of Seaport
91
+ */
92
+ function Seaport(provider, _a) {
93
+ var _b;
94
+ var _c = _a === void 0 ? {} : _a, overrides = _c.overrides,
95
+ // Five minute buffer
96
+ _d = _c.ascendingAmountFulfillmentBuffer,
97
+ // Five minute buffer
98
+ ascendingAmountFulfillmentBuffer = _d === void 0 ? 300 : _d, _e = _c.balanceAndApprovalChecksOnOrderCreation, balanceAndApprovalChecksOnOrderCreation = _e === void 0 ? true : _e, conduitKeyToConduit = _c.conduitKeyToConduit;
99
+ var _f, _g;
100
+ /**
101
+ * Calculates the order hash of order components so we can forgo executing a request to the contract
102
+ * This saves us RPC calls and latency.
103
+ */
104
+ this.getOrderHash = function (orderComponents) {
105
+ var offerItemTypeString = "OfferItem(uint8 itemType,address token,uint256 identifierOrCriteria,uint256 startAmount,uint256 endAmount)";
106
+ var considerationItemTypeString = "ConsiderationItem(uint8 itemType,address token,uint256 identifierOrCriteria,uint256 startAmount,uint256 endAmount,address recipient)";
107
+ var orderComponentsPartialTypeString = "OrderComponents(address offerer,address zone,OfferItem[] offer,ConsiderationItem[] consideration,uint8 orderType,uint256 startTime,uint256 endTime,bytes32 zoneHash,uint256 salt,bytes32 conduitKey,uint256 counter)";
108
+ var orderTypeString = "".concat(orderComponentsPartialTypeString).concat(considerationItemTypeString).concat(offerItemTypeString);
109
+ var offerItemTypeHash = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.toUtf8Bytes(offerItemTypeString));
110
+ var considerationItemTypeHash = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.toUtf8Bytes(considerationItemTypeString));
111
+ var orderTypeHash = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.toUtf8Bytes(orderTypeString));
112
+ var offerHash = ethers_1.ethers.utils.keccak256("0x" +
113
+ orderComponents.offer
114
+ .map(function (offerItem) {
115
+ return ethers_1.ethers.utils
116
+ .keccak256("0x" +
117
+ [
118
+ offerItemTypeHash.slice(2),
119
+ offerItem.itemType.toString().padStart(64, "0"),
120
+ offerItem.token.slice(2).padStart(64, "0"),
121
+ ethers_1.ethers.BigNumber.from(offerItem.identifierOrCriteria)
122
+ .toHexString()
123
+ .slice(2)
124
+ .padStart(64, "0"),
125
+ ethers_1.ethers.BigNumber.from(offerItem.startAmount)
126
+ .toHexString()
127
+ .slice(2)
128
+ .padStart(64, "0"),
129
+ ethers_1.ethers.BigNumber.from(offerItem.endAmount)
130
+ .toHexString()
131
+ .slice(2)
132
+ .padStart(64, "0"),
133
+ ].join(""))
134
+ .slice(2);
135
+ })
136
+ .join(""));
137
+ var considerationHash = ethers_1.ethers.utils.keccak256("0x" +
138
+ orderComponents.consideration
139
+ .map(function (considerationItem) {
140
+ return ethers_1.ethers.utils
141
+ .keccak256("0x" +
142
+ [
143
+ considerationItemTypeHash.slice(2),
144
+ considerationItem.itemType.toString().padStart(64, "0"),
145
+ considerationItem.token.slice(2).padStart(64, "0"),
146
+ ethers_1.ethers.BigNumber.from(considerationItem.identifierOrCriteria)
147
+ .toHexString()
148
+ .slice(2)
149
+ .padStart(64, "0"),
150
+ ethers_1.ethers.BigNumber.from(considerationItem.startAmount)
151
+ .toHexString()
152
+ .slice(2)
153
+ .padStart(64, "0"),
154
+ ethers_1.ethers.BigNumber.from(considerationItem.endAmount)
155
+ .toHexString()
156
+ .slice(2)
157
+ .padStart(64, "0"),
158
+ considerationItem.recipient.slice(2).padStart(64, "0"),
159
+ ].join(""))
160
+ .slice(2);
161
+ })
162
+ .join(""));
163
+ var derivedOrderHash = ethers_1.ethers.utils.keccak256("0x" +
164
+ [
165
+ orderTypeHash.slice(2),
166
+ orderComponents.offerer.slice(2).padStart(64, "0"),
167
+ orderComponents.zone.slice(2).padStart(64, "0"),
168
+ offerHash.slice(2),
169
+ considerationHash.slice(2),
170
+ orderComponents.orderType.toString().padStart(64, "0"),
171
+ ethers_1.ethers.BigNumber.from(orderComponents.startTime)
172
+ .toHexString()
173
+ .slice(2)
174
+ .padStart(64, "0"),
175
+ ethers_1.ethers.BigNumber.from(orderComponents.endTime)
176
+ .toHexString()
177
+ .slice(2)
178
+ .padStart(64, "0"),
179
+ orderComponents.zoneHash.slice(2),
180
+ orderComponents.salt.slice(2).padStart(64, "0"),
181
+ orderComponents.conduitKey.slice(2).padStart(64, "0"),
182
+ ethers_1.ethers.BigNumber.from(orderComponents.counter)
183
+ .toHexString()
184
+ .slice(2)
185
+ .padStart(64, "0"),
186
+ ].join(""));
187
+ return derivedOrderHash;
188
+ };
189
+ this.provider = provider;
190
+ this.multicallProvider = new multicall_1.providers.MulticallProvider(provider);
191
+ this.contract = new ethers_1.Contract((_f = overrides === null || overrides === void 0 ? void 0 : overrides.contractAddress) !== null && _f !== void 0 ? _f : constants_1.CROSS_CHAIN_SEAPORT_ADDRESS, Seaport_1.SeaportABI, this.multicallProvider);
192
+ this.config = {
193
+ ascendingAmountFulfillmentBuffer: ascendingAmountFulfillmentBuffer,
194
+ balanceAndApprovalChecksOnOrderCreation: balanceAndApprovalChecksOnOrderCreation,
195
+ conduitKeyToConduit: __assign(__assign(__assign({}, constants_1.KNOWN_CONDUIT_KEYS_TO_CONDUIT), (_b = {}, _b[constants_1.NO_CONDUIT] = this.contract.address, _b)), conduitKeyToConduit),
196
+ };
197
+ this.defaultConduitKey = (_g = overrides === null || overrides === void 0 ? void 0 : overrides.defaultConduitKey) !== null && _g !== void 0 ? _g : constants_1.NO_CONDUIT;
198
+ }
199
+ /**
200
+ * Returns the corresponding order type based on whether it allows partial fills and is restricted by zone
201
+ *
202
+ * @param input
203
+ * @param input.allowPartialFills Whether or not the order can be partially filled
204
+ * @param input.restrictedByZone Whether or not the order can only be filled/cancelled by the zone
205
+ * @returns the order type
206
+ */
207
+ Seaport.prototype._getOrderTypeFromOrderOptions = function (_a) {
208
+ var allowPartialFills = _a.allowPartialFills, restrictedByZone = _a.restrictedByZone;
209
+ if (allowPartialFills) {
210
+ return restrictedByZone
211
+ ? constants_1.OrderType.PARTIAL_RESTRICTED
212
+ : constants_1.OrderType.PARTIAL_OPEN;
213
+ }
214
+ return restrictedByZone ? constants_1.OrderType.FULL_RESTRICTED : constants_1.OrderType.FULL_OPEN;
215
+ };
216
+ /**
217
+ * Returns a use case that will create an order.
218
+ * The use case will contain the list of actions necessary to finish creating an order.
219
+ * The list of actions will either be an approval if approvals are necessary
220
+ * or a signature request that will then be supplied into the final Order struct, ready to be fulfilled.
221
+ *
222
+ * @param input
223
+ * @param input.conduitKey The conduitKey key to derive where to source your approvals from. Defaults to 0 which refers to the Seaport contract.
224
+ * Another special value is address(1) will refer to the legacy proxy. All other must derive to the specified address.
225
+ * @param input.zone The zone of the order. Defaults to the zero address.
226
+ * @param input.startTime The start time of the order. Defaults to the current unix time.
227
+ * @param input.endTime The end time of the order. Defaults to "never end".
228
+ * It is HIGHLY recommended to pass in an explicit end time
229
+ * @param input.offer The items you are willing to offer. This is a condensed version of the Seaport struct OfferItem for convenience
230
+ * @param input.consideration The items that will go to their respective recipients upon receiving your offer.
231
+ * @param input.counter The counter from which to create the order with. Automatically fetched from the contract if not provided
232
+ * @param input.allowPartialFills Whether to allow the order to be partially filled
233
+ * @param input.restrictedByZone Whether the order should be restricted by zone
234
+ * @param input.fees Convenience array to apply fees onto the order. The fees will be deducted from the
235
+ * existing consideration items and then tacked on as new consideration items
236
+ * @param input.salt Random salt
237
+ * @param input.offerer The order's creator address. Defaults to the first address on the provider.
238
+ * @param accountAddress Optional address for which to create the order with
239
+ * @returns a use case containing the list of actions needed to be performed in order to create the order
240
+ */
241
+ Seaport.prototype.createOrder = function (_a, accountAddress) {
242
+ var _b;
243
+ var _c = _a.conduitKey, conduitKey = _c === void 0 ? this.defaultConduitKey : _c, _d = _a.zone, zone = _d === void 0 ? ethers_1.ethers.constants.AddressZero : _d, _e = _a.startTime, startTime = _e === void 0 ? Math.floor(Date.now() / 1000).toString() : _e, _f = _a.endTime, endTime = _f === void 0 ? constants_1.MAX_INT.toString() : _f, offer = _a.offer, consideration = _a.consideration, counter = _a.counter, allowPartialFills = _a.allowPartialFills, restrictedByZone = _a.restrictedByZone, fees = _a.fees, _g = _a.salt, salt = _g === void 0 ? (0, order_1.generateRandomSalt)() : _g;
244
+ return __awaiter(this, void 0, void 0, function () {
245
+ var signer, offerer, offerItems, considerationItems, currencies, totalCurrencyAmount, operator, _h, resolvedCounter, balancesAndApprovals, orderType, considerationItemsWithFees, orderParameters, checkBalancesAndApprovals, insufficientApprovals, approvalActions, _j, createOrderAction, actions;
246
+ var _this = this;
247
+ return __generator(this, function (_k) {
248
+ switch (_k.label) {
249
+ case 0: return [4 /*yield*/, this.provider.getSigner(accountAddress)];
250
+ case 1:
251
+ signer = _k.sent();
252
+ return [4 /*yield*/, signer.getAddress()];
253
+ case 2:
254
+ offerer = _k.sent();
255
+ offerItems = offer.map(order_1.mapInputItemToOfferItem);
256
+ considerationItems = __spreadArray([], __read(consideration.map(function (consideration) {
257
+ var _a;
258
+ return (__assign(__assign({}, (0, order_1.mapInputItemToOfferItem)(consideration)), { recipient: (_a = consideration.recipient) !== null && _a !== void 0 ? _a : offerer }));
259
+ })), false);
260
+ if (!(0, order_1.areAllCurrenciesSame)({
261
+ offer: offerItems,
262
+ consideration: considerationItems,
263
+ })) {
264
+ throw new Error("All currency tokens in the order must be the same token");
265
+ }
266
+ currencies = __spreadArray(__spreadArray([], __read(offerItems), false), __read(considerationItems), false).filter(item_1.isCurrencyItem);
267
+ totalCurrencyAmount = (0, order_1.totalItemsAmount)(currencies);
268
+ operator = this.config.conduitKeyToConduit[conduitKey];
269
+ return [4 /*yield*/, Promise.all([
270
+ counter !== null && counter !== void 0 ? counter : this.getCounter(offerer),
271
+ (0, balanceAndApprovalCheck_1.getBalancesAndApprovals)({
272
+ owner: offerer,
273
+ items: offerItems,
274
+ criterias: [],
275
+ multicallProvider: this.multicallProvider,
276
+ operator: operator,
277
+ }),
278
+ ])];
279
+ case 3:
280
+ _h = __read.apply(void 0, [_k.sent(), 2]), resolvedCounter = _h[0], balancesAndApprovals = _h[1];
281
+ orderType = this._getOrderTypeFromOrderOptions({
282
+ allowPartialFills: allowPartialFills,
283
+ restrictedByZone: restrictedByZone,
284
+ });
285
+ considerationItemsWithFees = __spreadArray(__spreadArray([], __read((0, order_1.deductFees)(considerationItems, fees)), false), __read((currencies.length
286
+ ? (_b = fees === null || fees === void 0 ? void 0 : fees.map(function (fee) {
287
+ return (0, order_1.feeToConsiderationItem)({
288
+ fee: fee,
289
+ token: currencies[0].token,
290
+ baseAmount: totalCurrencyAmount.startAmount,
291
+ baseEndAmount: totalCurrencyAmount.endAmount,
292
+ });
293
+ })) !== null && _b !== void 0 ? _b : []
294
+ : [])), false);
295
+ orderParameters = {
296
+ offerer: offerer,
297
+ zone: zone,
298
+ // TODO: Placeholder
299
+ zoneHash: (0, utils_1.formatBytes32String)(resolvedCounter.toString()),
300
+ startTime: startTime,
301
+ endTime: endTime,
302
+ orderType: orderType,
303
+ offer: offerItems,
304
+ consideration: considerationItemsWithFees,
305
+ totalOriginalConsiderationItems: considerationItemsWithFees.length,
306
+ salt: salt,
307
+ conduitKey: conduitKey,
308
+ };
309
+ checkBalancesAndApprovals = this.config.balanceAndApprovalChecksOnOrderCreation;
310
+ insufficientApprovals = checkBalancesAndApprovals
311
+ ? (0, balanceAndApprovalCheck_1.validateOfferBalancesAndApprovals)({
312
+ offer: offerItems,
313
+ criterias: [],
314
+ balancesAndApprovals: balancesAndApprovals,
315
+ throwOnInsufficientBalances: checkBalancesAndApprovals,
316
+ operator: operator,
317
+ })
318
+ : [];
319
+ if (!checkBalancesAndApprovals) return [3 /*break*/, 5];
320
+ return [4 /*yield*/, (0, approval_1.getApprovalActions)(insufficientApprovals, signer)];
321
+ case 4:
322
+ _j = _k.sent();
323
+ return [3 /*break*/, 6];
324
+ case 5:
325
+ _j = [];
326
+ _k.label = 6;
327
+ case 6:
328
+ approvalActions = _j;
329
+ createOrderAction = {
330
+ type: "create",
331
+ getMessageToSign: function () {
332
+ return _this._getMessageToSign(orderParameters, resolvedCounter);
333
+ },
334
+ createOrder: function () { return __awaiter(_this, void 0, void 0, function () {
335
+ var signature;
336
+ return __generator(this, function (_a) {
337
+ switch (_a.label) {
338
+ case 0: return [4 /*yield*/, this.signOrder(orderParameters, resolvedCounter, offerer)];
339
+ case 1:
340
+ signature = _a.sent();
341
+ return [2 /*return*/, {
342
+ parameters: __assign(__assign({}, orderParameters), { counter: resolvedCounter }),
343
+ signature: signature,
344
+ }];
345
+ }
346
+ });
347
+ }); },
348
+ };
349
+ actions = __spreadArray(__spreadArray([], __read(approvalActions), false), [createOrderAction], false);
350
+ return [2 /*return*/, {
351
+ actions: actions,
352
+ executeAllActions: function () {
353
+ return (0, usecase_1.executeAllActions)(actions);
354
+ },
355
+ }];
356
+ }
357
+ });
358
+ });
359
+ };
360
+ /**
361
+ * Returns the domain data used when signing typed data
362
+ * @returns domain data
363
+ */
364
+ Seaport.prototype._getDomainData = function () {
365
+ return __awaiter(this, void 0, void 0, function () {
366
+ var chainId;
367
+ return __generator(this, function (_a) {
368
+ switch (_a.label) {
369
+ case 0: return [4 /*yield*/, this.provider.getNetwork()];
370
+ case 1:
371
+ chainId = (_a.sent()).chainId;
372
+ return [2 /*return*/, {
373
+ name: constants_1.SEAPORT_CONTRACT_NAME,
374
+ version: constants_1.SEAPORT_CONTRACT_VERSION,
375
+ chainId: chainId,
376
+ verifyingContract: this.contract.address,
377
+ }];
378
+ }
379
+ });
380
+ });
381
+ };
382
+ /**
383
+ * Returns a raw message to be signed using EIP-712
384
+ * @param orderParameters order parameter struct
385
+ * @param counter counter of the order
386
+ * @returns JSON string of the message to be signed
387
+ */
388
+ Seaport.prototype._getMessageToSign = function (orderParameters, counter) {
389
+ return __awaiter(this, void 0, void 0, function () {
390
+ var domainData, orderComponents;
391
+ return __generator(this, function (_a) {
392
+ switch (_a.label) {
393
+ case 0: return [4 /*yield*/, this._getDomainData()];
394
+ case 1:
395
+ domainData = _a.sent();
396
+ orderComponents = __assign(__assign({}, orderParameters), { counter: counter });
397
+ return [2 /*return*/, JSON.stringify(utils_1._TypedDataEncoder.getPayload(domainData, constants_1.EIP_712_ORDER_TYPE, orderComponents))];
398
+ }
399
+ });
400
+ });
401
+ };
402
+ /**
403
+ * Submits a request to your provider to sign the order. Signed orders are used for off-chain order books.
404
+ * @param orderParameters standard order parameter struct
405
+ * @param counter counter of the offerer
406
+ * @param accountAddress optional account address from which to sign the order with.
407
+ * @returns the order signature
408
+ */
409
+ Seaport.prototype.signOrder = function (orderParameters, counter, accountAddress) {
410
+ return __awaiter(this, void 0, void 0, function () {
411
+ var signer, domainData, orderComponents, signature;
412
+ return __generator(this, function (_a) {
413
+ switch (_a.label) {
414
+ case 0:
415
+ signer = this.provider.getSigner(accountAddress);
416
+ return [4 /*yield*/, this._getDomainData()];
417
+ case 1:
418
+ domainData = _a.sent();
419
+ orderComponents = __assign(__assign({}, orderParameters), { counter: counter });
420
+ return [4 /*yield*/, signer._signTypedData(domainData, constants_1.EIP_712_ORDER_TYPE, orderComponents)];
421
+ case 2:
422
+ signature = _a.sent();
423
+ // Use EIP-2098 compact signatures to save gas. https://eips.ethereum.org/EIPS/eip-2098
424
+ return [2 /*return*/, ethers_1.ethers.utils.splitSignature(signature).compact];
425
+ }
426
+ });
427
+ });
428
+ };
429
+ /**
430
+ * Cancels a list of orders so that they are no longer fulfillable.
431
+ *
432
+ * @param orders list of order components
433
+ * @param accountAddress optional account address from which to cancel the orders from.
434
+ * @returns the set of transaction methods that can be used
435
+ */
436
+ Seaport.prototype.cancelOrders = function (orders, accountAddress) {
437
+ var signer = this.provider.getSigner(accountAddress);
438
+ return (0, usecase_1.getTransactionMethods)(this.contract.connect(signer), "cancel", [
439
+ orders,
440
+ ]);
441
+ };
442
+ /**
443
+ * Bulk cancels all existing orders for a given account
444
+ * @param offerer the account to bulk cancel orders on
445
+ * @returns the set of transaction methods that can be used
446
+ */
447
+ Seaport.prototype.bulkCancelOrders = function (offerer) {
448
+ var signer = this.provider.getSigner(offerer);
449
+ return (0, usecase_1.getTransactionMethods)(this.contract.connect(signer), "incrementCounter", []);
450
+ };
451
+ /**
452
+ * Approves a list of orders on-chain. This allows accounts to fulfill the order without requiring
453
+ * a signature. Can also check if an order is valid using `callStatic`
454
+ * @param orders list of order structs
455
+ * @param accountAddress optional account address to approve orders.
456
+ * @returns the set of transaction methods that can be used
457
+ */
458
+ Seaport.prototype.validate = function (orders, accountAddress) {
459
+ var signer = this.provider.getSigner(accountAddress);
460
+ return (0, usecase_1.getTransactionMethods)(this.contract.connect(signer), "validate", [
461
+ orders,
462
+ ]);
463
+ };
464
+ /**
465
+ * Returns the order status given an order hash
466
+ * @param orderHash the hash of the order
467
+ * @returns an order status struct
468
+ */
469
+ Seaport.prototype.getOrderStatus = function (orderHash) {
470
+ return this.contract.getOrderStatus(orderHash);
471
+ };
472
+ /**
473
+ * Gets the counter of a given offerer
474
+ * @param offerer the offerer to get the counter of
475
+ * @returns counter as a number
476
+ */
477
+ Seaport.prototype.getCounter = function (offerer) {
478
+ return this.contract
479
+ .getCounter(offerer)
480
+ .then(function (counter) { return counter.toNumber(); });
481
+ };
482
+ /**
483
+ * Fulfills an order through either the basic method or the standard method
484
+ * Units to fill are denominated by the max possible size of the order, which is the greatest common denominator (GCD).
485
+ * We expose a helper to get this: getMaximumSizeForOrder
486
+ * i.e. If the maximum size of an order is 4, supplying 2 as the units to fulfill will fill half of the order: ;
487
+ * @param input
488
+ * @param input.order The standard order struct
489
+ * @param input.unitsToFill the number of units to fill for the given order. Only used if you wish to partially fill an order
490
+ * @param input.offerCriteria an array of criteria with length equal to the number of offer criteria items
491
+ * @param input.considerationCriteria an array of criteria with length equal to the number of consideration criteria items
492
+ * @param input.tips an array of optional condensed consideration items to be added onto a fulfillment
493
+ * @param input.extraData extra data supplied to the order
494
+ * @param input.accountAddress optional address from which to fulfill the order from
495
+ * @param input.conduitKey the conduitKey to source approvals from
496
+ * @param input.recipientAddress optional recipient to forward the offer to as opposed to the fulfiller.
497
+ * Defaults to the zero address which means the offer goes to the fulfiller
498
+ * @returns a use case containing the set of approval actions and fulfillment action
499
+ */
500
+ Seaport.prototype.fulfillOrder = function (_a) {
501
+ var order = _a.order, unitsToFill = _a.unitsToFill, _b = _a.offerCriteria, offerCriteria = _b === void 0 ? [] : _b, _c = _a.considerationCriteria, considerationCriteria = _c === void 0 ? [] : _c, _d = _a.tips, tips = _d === void 0 ? [] : _d, _e = _a.extraData, extraData = _e === void 0 ? "0x" : _e, accountAddress = _a.accountAddress, _f = _a.conduitKey, conduitKey = _f === void 0 ? this.defaultConduitKey : _f, _g = _a.recipientAddress, recipientAddress = _g === void 0 ? ethers_1.ethers.constants.AddressZero : _g;
502
+ return __awaiter(this, void 0, void 0, function () {
503
+ var orderParameters, offerer, offer, consideration, fulfiller, fulfillerAddress, offererOperator, fulfillerOperator, _h, offererBalancesAndApprovals, fulfillerBalancesAndApprovals, currentBlock, orderStatus, currentBlockTimestamp, totalFilled, totalSize, sanitizedOrder, timeBasedItemParams, tipConsiderationItems, isRecipientSelf;
504
+ return __generator(this, function (_j) {
505
+ switch (_j.label) {
506
+ case 0:
507
+ orderParameters = order.parameters;
508
+ offerer = orderParameters.offerer, offer = orderParameters.offer, consideration = orderParameters.consideration;
509
+ return [4 /*yield*/, this.provider.getSigner(accountAddress)];
510
+ case 1:
511
+ fulfiller = _j.sent();
512
+ return [4 /*yield*/, fulfiller.getAddress()];
513
+ case 2:
514
+ fulfillerAddress = _j.sent();
515
+ offererOperator = this.config.conduitKeyToConduit[orderParameters.conduitKey];
516
+ fulfillerOperator = this.config.conduitKeyToConduit[conduitKey];
517
+ return [4 /*yield*/, Promise.all([
518
+ (0, balanceAndApprovalCheck_1.getBalancesAndApprovals)({
519
+ owner: offerer,
520
+ items: offer,
521
+ criterias: offerCriteria,
522
+ multicallProvider: this.multicallProvider,
523
+ operator: offererOperator,
524
+ }),
525
+ // Get fulfiller balances and approvals of all items in the set, as offer items
526
+ // may be received by the fulfiller for standard fulfills
527
+ (0, balanceAndApprovalCheck_1.getBalancesAndApprovals)({
528
+ owner: fulfillerAddress,
529
+ items: __spreadArray(__spreadArray([], __read(offer), false), __read(consideration), false),
530
+ criterias: __spreadArray(__spreadArray([], __read(offerCriteria), false), __read(considerationCriteria), false),
531
+ multicallProvider: this.multicallProvider,
532
+ operator: fulfillerOperator,
533
+ }),
534
+ this.multicallProvider.getBlock("latest"),
535
+ this.getOrderStatus(this.getOrderHash(orderParameters)),
536
+ ])];
537
+ case 3:
538
+ _h = __read.apply(void 0, [_j.sent(), 4]), offererBalancesAndApprovals = _h[0], fulfillerBalancesAndApprovals = _h[1], currentBlock = _h[2], orderStatus = _h[3];
539
+ currentBlockTimestamp = currentBlock.timestamp;
540
+ totalFilled = orderStatus.totalFilled, totalSize = orderStatus.totalSize;
541
+ sanitizedOrder = (0, fulfill_1.validateAndSanitizeFromOrderStatus)(order, orderStatus);
542
+ timeBasedItemParams = {
543
+ startTime: sanitizedOrder.parameters.startTime,
544
+ endTime: sanitizedOrder.parameters.endTime,
545
+ currentBlockTimestamp: currentBlockTimestamp,
546
+ ascendingAmountTimestampBuffer: this.config.ascendingAmountFulfillmentBuffer,
547
+ };
548
+ tipConsiderationItems = tips.map(function (tip) { return (__assign(__assign({}, (0, order_1.mapInputItemToOfferItem)(tip)), { recipient: tip.recipient })); });
549
+ isRecipientSelf = recipientAddress === ethers_1.ethers.constants.AddressZero;
550
+ // We use basic fulfills as they are more optimal for simple and "hot" use cases
551
+ // We cannot use basic fulfill if user is trying to partially fill though.
552
+ if (!unitsToFill &&
553
+ isRecipientSelf &&
554
+ (0, fulfill_1.shouldUseBasicFulfill)(sanitizedOrder.parameters, totalFilled)) {
555
+ // TODO: Use fulfiller proxy if there are approvals needed directly, but none needed for proxy
556
+ return [2 /*return*/, (0, fulfill_1.fulfillBasicOrder)({
557
+ order: sanitizedOrder,
558
+ seaportContract: this.contract,
559
+ offererBalancesAndApprovals: offererBalancesAndApprovals,
560
+ fulfillerBalancesAndApprovals: fulfillerBalancesAndApprovals,
561
+ timeBasedItemParams: timeBasedItemParams,
562
+ conduitKey: conduitKey,
563
+ offererOperator: offererOperator,
564
+ fulfillerOperator: fulfillerOperator,
565
+ signer: fulfiller,
566
+ tips: tipConsiderationItems,
567
+ })];
568
+ }
569
+ // Else, we fallback to the standard fulfill order
570
+ return [2 /*return*/, (0, fulfill_1.fulfillStandardOrder)({
571
+ order: sanitizedOrder,
572
+ unitsToFill: unitsToFill,
573
+ totalFilled: totalFilled,
574
+ totalSize: totalSize.eq(0)
575
+ ? (0, item_1.getMaximumSizeForOrder)(sanitizedOrder)
576
+ : totalSize,
577
+ offerCriteria: offerCriteria,
578
+ considerationCriteria: considerationCriteria,
579
+ tips: tipConsiderationItems,
580
+ extraData: extraData,
581
+ seaportContract: this.contract,
582
+ offererBalancesAndApprovals: offererBalancesAndApprovals,
583
+ fulfillerBalancesAndApprovals: fulfillerBalancesAndApprovals,
584
+ timeBasedItemParams: timeBasedItemParams,
585
+ conduitKey: conduitKey,
586
+ signer: fulfiller,
587
+ offererOperator: offererOperator,
588
+ fulfillerOperator: fulfillerOperator,
589
+ recipientAddress: recipientAddress,
590
+ })];
591
+ }
592
+ });
593
+ });
594
+ };
595
+ /**
596
+ * Fulfills an order through best-effort fashion. Orders that fail will not revert the whole transaction
597
+ * unless there's an issue with approvals or balance checks
598
+ * @param input
599
+ * @param input.fulfillOrderDetails list of helper order details
600
+ * @param input.accountAddress the account to fulfill orders on
601
+ * @param input.conduitKey the key from which to source approvals from
602
+ * @param input.recipientAddress optional recipient to forward the offer to as opposed to the fulfiller.
603
+ * Defaults to the zero address which means the offer goes to the fulfiller
604
+ * @returns a use case containing the set of approval actions and fulfillment action
605
+ */
606
+ Seaport.prototype.fulfillOrders = function (_a) {
607
+ var fulfillOrderDetails = _a.fulfillOrderDetails, accountAddress = _a.accountAddress, _b = _a.conduitKey, conduitKey = _b === void 0 ? this.defaultConduitKey : _b, _c = _a.recipientAddress, recipientAddress = _c === void 0 ? ethers_1.ethers.constants.AddressZero : _c;
608
+ return __awaiter(this, void 0, void 0, function () {
609
+ var fulfiller, fulfillerAddress, allOffererOperators, fulfillerOperator, allOfferItems, allConsiderationItems, allOfferCriteria, allConsiderationCriteria, _d, offerersBalancesAndApprovals, fulfillerBalancesAndApprovals, currentBlock, orderStatuses, ordersMetadata;
610
+ var _this = this;
611
+ return __generator(this, function (_e) {
612
+ switch (_e.label) {
613
+ case 0: return [4 /*yield*/, this.provider.getSigner(accountAddress)];
614
+ case 1:
615
+ fulfiller = _e.sent();
616
+ return [4 /*yield*/, fulfiller.getAddress()];
617
+ case 2:
618
+ fulfillerAddress = _e.sent();
619
+ allOffererOperators = fulfillOrderDetails.map(function (_a) {
620
+ var order = _a.order;
621
+ return _this.config.conduitKeyToConduit[order.parameters.conduitKey];
622
+ });
623
+ fulfillerOperator = this.config.conduitKeyToConduit[conduitKey];
624
+ allOfferItems = fulfillOrderDetails.flatMap(function (_a) {
625
+ var order = _a.order;
626
+ return order.parameters.offer;
627
+ });
628
+ allConsiderationItems = fulfillOrderDetails.flatMap(function (_a) {
629
+ var order = _a.order;
630
+ return order.parameters.consideration;
631
+ });
632
+ allOfferCriteria = fulfillOrderDetails.flatMap(function (_a) {
633
+ var _b = _a.offerCriteria, offerCriteria = _b === void 0 ? [] : _b;
634
+ return offerCriteria;
635
+ });
636
+ allConsiderationCriteria = fulfillOrderDetails.flatMap(function (_a) {
637
+ var _b = _a.considerationCriteria, considerationCriteria = _b === void 0 ? [] : _b;
638
+ return considerationCriteria;
639
+ });
640
+ return [4 /*yield*/, Promise.all([
641
+ Promise.all(fulfillOrderDetails.map(function (_a, i) {
642
+ var order = _a.order, _b = _a.offerCriteria, offerCriteria = _b === void 0 ? [] : _b;
643
+ return (0, balanceAndApprovalCheck_1.getBalancesAndApprovals)({
644
+ owner: order.parameters.offerer,
645
+ items: order.parameters.offer,
646
+ criterias: offerCriteria,
647
+ operator: allOffererOperators[i],
648
+ multicallProvider: _this.multicallProvider,
649
+ });
650
+ })),
651
+ // Get fulfiller balances and approvals of all items in the set, as offer items
652
+ // may be received by the fulfiller for standard fulfills
653
+ (0, balanceAndApprovalCheck_1.getBalancesAndApprovals)({
654
+ owner: fulfillerAddress,
655
+ items: __spreadArray(__spreadArray([], __read(allOfferItems), false), __read(allConsiderationItems), false),
656
+ criterias: __spreadArray(__spreadArray([], __read(allOfferCriteria), false), __read(allConsiderationCriteria), false),
657
+ operator: fulfillerOperator,
658
+ multicallProvider: this.multicallProvider,
659
+ }),
660
+ this.multicallProvider.getBlock("latest"),
661
+ Promise.all(fulfillOrderDetails.map(function (_a) {
662
+ var order = _a.order;
663
+ return _this.getOrderStatus(_this.getOrderHash(order.parameters));
664
+ })),
665
+ ])];
666
+ case 3:
667
+ _d = __read.apply(void 0, [_e.sent(), 4]), offerersBalancesAndApprovals = _d[0], fulfillerBalancesAndApprovals = _d[1], currentBlock = _d[2], orderStatuses = _d[3];
668
+ ordersMetadata = fulfillOrderDetails.map(function (orderDetails, index) {
669
+ var _a, _b, _c, _d, _e;
670
+ return ({
671
+ order: orderDetails.order,
672
+ unitsToFill: orderDetails.unitsToFill,
673
+ orderStatus: orderStatuses[index],
674
+ offerCriteria: (_a = orderDetails.offerCriteria) !== null && _a !== void 0 ? _a : [],
675
+ considerationCriteria: (_b = orderDetails.considerationCriteria) !== null && _b !== void 0 ? _b : [],
676
+ tips: (_d = (_c = orderDetails.tips) === null || _c === void 0 ? void 0 : _c.map(function (tip) { return (__assign(__assign({}, (0, order_1.mapInputItemToOfferItem)(tip)), { recipient: tip.recipient })); })) !== null && _d !== void 0 ? _d : [],
677
+ extraData: (_e = orderDetails.extraData) !== null && _e !== void 0 ? _e : "0x",
678
+ offererBalancesAndApprovals: offerersBalancesAndApprovals[index],
679
+ offererOperator: allOffererOperators[index],
680
+ });
681
+ });
682
+ return [2 /*return*/, (0, fulfill_1.fulfillAvailableOrders)({
683
+ ordersMetadata: ordersMetadata,
684
+ seaportContract: this.contract,
685
+ fulfillerBalancesAndApprovals: fulfillerBalancesAndApprovals,
686
+ currentBlockTimestamp: currentBlock.timestamp,
687
+ ascendingAmountTimestampBuffer: this.config.ascendingAmountFulfillmentBuffer,
688
+ fulfillerOperator: fulfillerOperator,
689
+ signer: fulfiller,
690
+ conduitKey: conduitKey,
691
+ recipientAddress: recipientAddress,
692
+ })];
693
+ }
694
+ });
695
+ });
696
+ };
697
+ /**
698
+ * NOTE: Largely incomplete. Does NOT do any balance or approval checks.
699
+ * Just exposes the bare bones matchOrders where clients will have to supply
700
+ * their own overrides as needed.
701
+ * @param input
702
+ * @param input.orders the list of orders to match
703
+ * @param input.fulfillments the list of fulfillments to match offer and considerations
704
+ * @param input.overrides any overrides the client wants, will need to pass in value for matching orders with ETH.
705
+ * @param input.accountAddress Optional address for which to match the order with
706
+ * @returns set of transaction methods for matching orders
707
+ */
708
+ Seaport.prototype.matchOrders = function (_a) {
709
+ var orders = _a.orders, fulfillments = _a.fulfillments, overrides = _a.overrides, accountAddress = _a.accountAddress;
710
+ var signer = this.provider.getSigner(accountAddress);
711
+ return (0, usecase_1.getTransactionMethods)(this.contract.connect(signer), "matchOrders", [
712
+ orders,
713
+ fulfillments,
714
+ overrides,
715
+ ]);
716
+ };
717
+ return Seaport;
718
+ }());
719
+ exports.Seaport = Seaport;
720
+ //# sourceMappingURL=seaport.js.map