@imtbl/sdk 0.31.1 → 0.31.2-alpha
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.
- package/.eslintrc +13 -0
- package/.gitignore +8 -0
- package/blockchain_data.js +1 -0
- package/checkout_sdk.js +9 -0
- package/config.js +1 -0
- package/dist/browser/checkout/sdk.js +2 -2
- package/dist/browser/checkout/widgets.js +1 -1
- package/dist/index.browser.js +4 -4
- package/dist/index.browser.js.map +1 -1
- package/immutablex_client.js +1 -0
- package/index.cjs +9 -0
- package/index.d.ts +18603 -0
- package/index.js +9 -0
- package/module-release.json +30 -0
- package/orderbook.js +1 -0
- package/package.json +24 -18
- package/passport.js +1 -0
- package/provider.js +1 -0
- package/rollup.config.js +158 -0
- package/scripts/copyBrowserBundles.js +62 -0
- package/scripts/generate-package-list.sh +5 -0
- package/scripts/generateIndex.js +44 -0
- package/scripts/updateDependencies.js +137 -0
- package/scripts/updateExports.js +62 -0
- package/src/blockchain_data.ts +1 -0
- package/src/checkout_sdk.ts +1 -0
- package/src/checkout_widgets.ts +1 -0
- package/src/config.ts +1 -0
- package/src/immutablex_client.ts +1 -0
- package/src/index.ts +7 -0
- package/src/orderbook.ts +1 -0
- package/src/passport.ts +1 -0
- package/src/provider.ts +1 -0
- package/tsconfig.json +27 -0
- package/typedoc.json +4 -0
- package/workspace-packages.json +31 -0
- package/dist/blockchain_data.js +0 -4010
- package/dist/checkout_sdk.js +0 -26016
- package/dist/config.js +0 -37
- package/dist/immutablex_client.js +0 -523
- package/dist/index.cjs +0 -34452
- package/dist/index.js +0 -34424
- package/dist/orderbook.js +0 -1583
- package/dist/passport.js +0 -9159
- package/dist/provider.js +0 -1165
- /package/{dist/blockchain_data.d.ts → blockchain_data.d.ts} +0 -0
- /package/{dist/checkout_sdk.d.ts → checkout_sdk.d.ts} +0 -0
- /package/{dist/config.d.ts → config.d.ts} +0 -0
- /package/{dist/immutablex_client.d.ts → immutablex_client.d.ts} +0 -0
- /package/{dist/index.d.ts → index.d.cts} +0 -0
- /package/{dist/orderbook.d.ts → orderbook.d.ts} +0 -0
- /package/{dist/passport.d.ts → passport.d.ts} +0 -0
- /package/{dist/provider.d.ts → provider.d.ts} +0 -0
package/dist/orderbook.js
DELETED
|
@@ -1,1583 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import FormData from 'form-data';
|
|
3
|
-
import { providers, BigNumber, constants as constants$1 } from 'ethers';
|
|
4
|
-
import { Seaport as Seaport$1 } from '@opensea/seaport-js';
|
|
5
|
-
|
|
6
|
-
var Environment;
|
|
7
|
-
(function (Environment) {
|
|
8
|
-
Environment["PRODUCTION"] = "production";
|
|
9
|
-
Environment["SANDBOX"] = "sandbox";
|
|
10
|
-
})(Environment || (Environment = {}));
|
|
11
|
-
|
|
12
|
-
class BaseHttpRequest {
|
|
13
|
-
config;
|
|
14
|
-
constructor(config) {
|
|
15
|
-
this.config = config;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
class ApiError extends Error {
|
|
20
|
-
url;
|
|
21
|
-
status;
|
|
22
|
-
statusText;
|
|
23
|
-
body;
|
|
24
|
-
request;
|
|
25
|
-
constructor(request, response, message) {
|
|
26
|
-
super(message);
|
|
27
|
-
this.name = 'ApiError';
|
|
28
|
-
this.url = response.url;
|
|
29
|
-
this.status = response.status;
|
|
30
|
-
this.statusText = response.statusText;
|
|
31
|
-
this.body = response.body;
|
|
32
|
-
this.request = request;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/* istanbul ignore file */
|
|
37
|
-
/* tslint:disable */
|
|
38
|
-
/* eslint-disable */
|
|
39
|
-
class CancelError extends Error {
|
|
40
|
-
constructor(message) {
|
|
41
|
-
super(message);
|
|
42
|
-
this.name = 'CancelError';
|
|
43
|
-
}
|
|
44
|
-
get isCancelled() {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
class CancelablePromise {
|
|
49
|
-
[Symbol.toStringTag];
|
|
50
|
-
_isResolved;
|
|
51
|
-
_isRejected;
|
|
52
|
-
_isCancelled;
|
|
53
|
-
_cancelHandlers;
|
|
54
|
-
_promise;
|
|
55
|
-
_resolve;
|
|
56
|
-
_reject;
|
|
57
|
-
constructor(executor) {
|
|
58
|
-
this._isResolved = false;
|
|
59
|
-
this._isRejected = false;
|
|
60
|
-
this._isCancelled = false;
|
|
61
|
-
this._cancelHandlers = [];
|
|
62
|
-
this._promise = new Promise((resolve, reject) => {
|
|
63
|
-
this._resolve = resolve;
|
|
64
|
-
this._reject = reject;
|
|
65
|
-
const onResolve = (value) => {
|
|
66
|
-
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
this._isResolved = true;
|
|
70
|
-
this._resolve?.(value);
|
|
71
|
-
};
|
|
72
|
-
const onReject = (reason) => {
|
|
73
|
-
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
this._isRejected = true;
|
|
77
|
-
this._reject?.(reason);
|
|
78
|
-
};
|
|
79
|
-
const onCancel = (cancelHandler) => {
|
|
80
|
-
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
this._cancelHandlers.push(cancelHandler);
|
|
84
|
-
};
|
|
85
|
-
Object.defineProperty(onCancel, 'isResolved', {
|
|
86
|
-
get: () => this._isResolved,
|
|
87
|
-
});
|
|
88
|
-
Object.defineProperty(onCancel, 'isRejected', {
|
|
89
|
-
get: () => this._isRejected,
|
|
90
|
-
});
|
|
91
|
-
Object.defineProperty(onCancel, 'isCancelled', {
|
|
92
|
-
get: () => this._isCancelled,
|
|
93
|
-
});
|
|
94
|
-
return executor(onResolve, onReject, onCancel);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
then(onFulfilled, onRejected) {
|
|
98
|
-
return this._promise.then(onFulfilled, onRejected);
|
|
99
|
-
}
|
|
100
|
-
catch(onRejected) {
|
|
101
|
-
return this._promise.catch(onRejected);
|
|
102
|
-
}
|
|
103
|
-
finally(onFinally) {
|
|
104
|
-
return this._promise.finally(onFinally);
|
|
105
|
-
}
|
|
106
|
-
cancel() {
|
|
107
|
-
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
this._isCancelled = true;
|
|
111
|
-
if (this._cancelHandlers.length) {
|
|
112
|
-
try {
|
|
113
|
-
for (const cancelHandler of this._cancelHandlers) {
|
|
114
|
-
cancelHandler();
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
catch (error) {
|
|
118
|
-
console.warn('Cancellation threw an error', error);
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
this._cancelHandlers.length = 0;
|
|
123
|
-
this._reject?.(new CancelError('Request aborted'));
|
|
124
|
-
}
|
|
125
|
-
get isCancelled() {
|
|
126
|
-
return this._isCancelled;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/* istanbul ignore file */
|
|
131
|
-
/* tslint:disable */
|
|
132
|
-
/* eslint-disable */
|
|
133
|
-
const isDefined = (value) => {
|
|
134
|
-
return value !== undefined && value !== null;
|
|
135
|
-
};
|
|
136
|
-
const isString = (value) => {
|
|
137
|
-
return typeof value === 'string';
|
|
138
|
-
};
|
|
139
|
-
const isStringWithValue = (value) => {
|
|
140
|
-
return isString(value) && value !== '';
|
|
141
|
-
};
|
|
142
|
-
const isBlob = (value) => {
|
|
143
|
-
return (typeof value === 'object' &&
|
|
144
|
-
typeof value.type === 'string' &&
|
|
145
|
-
typeof value.stream === 'function' &&
|
|
146
|
-
typeof value.arrayBuffer === 'function' &&
|
|
147
|
-
typeof value.constructor === 'function' &&
|
|
148
|
-
typeof value.constructor.name === 'string' &&
|
|
149
|
-
/^(Blob|File)$/.test(value.constructor.name) &&
|
|
150
|
-
/^(Blob|File)$/.test(value[Symbol.toStringTag]));
|
|
151
|
-
};
|
|
152
|
-
const isFormData = (value) => {
|
|
153
|
-
return value instanceof FormData;
|
|
154
|
-
};
|
|
155
|
-
const isSuccess = (status) => {
|
|
156
|
-
return status >= 200 && status < 300;
|
|
157
|
-
};
|
|
158
|
-
const base64 = (str) => {
|
|
159
|
-
try {
|
|
160
|
-
return btoa(str);
|
|
161
|
-
}
|
|
162
|
-
catch (err) {
|
|
163
|
-
// @ts-ignore
|
|
164
|
-
return Buffer.from(str).toString('base64');
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
const getQueryString = (params) => {
|
|
168
|
-
const qs = [];
|
|
169
|
-
const append = (key, value) => {
|
|
170
|
-
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
171
|
-
};
|
|
172
|
-
const process = (key, value) => {
|
|
173
|
-
if (isDefined(value)) {
|
|
174
|
-
if (Array.isArray(value)) {
|
|
175
|
-
value.forEach(v => {
|
|
176
|
-
process(key, v);
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
else if (typeof value === 'object') {
|
|
180
|
-
Object.entries(value).forEach(([k, v]) => {
|
|
181
|
-
process(`${key}[${k}]`, v);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
append(key, value);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
190
|
-
process(key, value);
|
|
191
|
-
});
|
|
192
|
-
if (qs.length > 0) {
|
|
193
|
-
return `?${qs.join('&')}`;
|
|
194
|
-
}
|
|
195
|
-
return '';
|
|
196
|
-
};
|
|
197
|
-
const getUrl = (config, options) => {
|
|
198
|
-
const encoder = config.ENCODE_PATH || encodeURI;
|
|
199
|
-
const path = options.url
|
|
200
|
-
.replace('{api-version}', config.VERSION)
|
|
201
|
-
.replace(/{(.*?)}/g, (substring, group) => {
|
|
202
|
-
if (options.path?.hasOwnProperty(group)) {
|
|
203
|
-
return encoder(String(options.path[group]));
|
|
204
|
-
}
|
|
205
|
-
return substring;
|
|
206
|
-
});
|
|
207
|
-
const url = `${config.BASE}${path}`;
|
|
208
|
-
if (options.query) {
|
|
209
|
-
return `${url}${getQueryString(options.query)}`;
|
|
210
|
-
}
|
|
211
|
-
return url;
|
|
212
|
-
};
|
|
213
|
-
const getFormData = (options) => {
|
|
214
|
-
if (options.formData) {
|
|
215
|
-
const formData = new FormData();
|
|
216
|
-
const process = (key, value) => {
|
|
217
|
-
if (isString(value) || isBlob(value)) {
|
|
218
|
-
formData.append(key, value);
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
formData.append(key, JSON.stringify(value));
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
Object.entries(options.formData)
|
|
225
|
-
.filter(([_, value]) => isDefined(value))
|
|
226
|
-
.forEach(([key, value]) => {
|
|
227
|
-
if (Array.isArray(value)) {
|
|
228
|
-
value.forEach(v => process(key, v));
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
process(key, value);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
return formData;
|
|
235
|
-
}
|
|
236
|
-
return undefined;
|
|
237
|
-
};
|
|
238
|
-
const resolve = async (options, resolver) => {
|
|
239
|
-
if (typeof resolver === 'function') {
|
|
240
|
-
return resolver(options);
|
|
241
|
-
}
|
|
242
|
-
return resolver;
|
|
243
|
-
};
|
|
244
|
-
const getHeaders = async (config, options, formData) => {
|
|
245
|
-
const token = await resolve(options, config.TOKEN);
|
|
246
|
-
const username = await resolve(options, config.USERNAME);
|
|
247
|
-
const password = await resolve(options, config.PASSWORD);
|
|
248
|
-
const additionalHeaders = await resolve(options, config.HEADERS);
|
|
249
|
-
const formHeaders = typeof formData?.getHeaders === 'function' && formData?.getHeaders() || {};
|
|
250
|
-
const headers = Object.entries({
|
|
251
|
-
Accept: 'application/json',
|
|
252
|
-
...additionalHeaders,
|
|
253
|
-
...options.headers,
|
|
254
|
-
...formHeaders,
|
|
255
|
-
})
|
|
256
|
-
.filter(([_, value]) => isDefined(value))
|
|
257
|
-
.reduce((headers, [key, value]) => ({
|
|
258
|
-
...headers,
|
|
259
|
-
[key]: String(value),
|
|
260
|
-
}), {});
|
|
261
|
-
if (isStringWithValue(token)) {
|
|
262
|
-
headers['Authorization'] = `Bearer ${token}`;
|
|
263
|
-
}
|
|
264
|
-
if (isStringWithValue(username) && isStringWithValue(password)) {
|
|
265
|
-
const credentials = base64(`${username}:${password}`);
|
|
266
|
-
headers['Authorization'] = `Basic ${credentials}`;
|
|
267
|
-
}
|
|
268
|
-
if (options.body) {
|
|
269
|
-
if (options.mediaType) {
|
|
270
|
-
headers['Content-Type'] = options.mediaType;
|
|
271
|
-
}
|
|
272
|
-
else if (isBlob(options.body)) {
|
|
273
|
-
headers['Content-Type'] = options.body.type || 'application/octet-stream';
|
|
274
|
-
}
|
|
275
|
-
else if (isString(options.body)) {
|
|
276
|
-
headers['Content-Type'] = 'text/plain';
|
|
277
|
-
}
|
|
278
|
-
else if (!isFormData(options.body)) {
|
|
279
|
-
headers['Content-Type'] = 'application/json';
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return headers;
|
|
283
|
-
};
|
|
284
|
-
const getRequestBody = (options) => {
|
|
285
|
-
if (options.body) {
|
|
286
|
-
return options.body;
|
|
287
|
-
}
|
|
288
|
-
return undefined;
|
|
289
|
-
};
|
|
290
|
-
const sendRequest = async (config, options, url, body, formData, headers, onCancel) => {
|
|
291
|
-
const source = axios.CancelToken.source();
|
|
292
|
-
const requestConfig = {
|
|
293
|
-
url,
|
|
294
|
-
headers,
|
|
295
|
-
data: body ?? formData,
|
|
296
|
-
method: options.method,
|
|
297
|
-
withCredentials: config.WITH_CREDENTIALS,
|
|
298
|
-
cancelToken: source.token,
|
|
299
|
-
};
|
|
300
|
-
onCancel(() => source.cancel('The user aborted a request.'));
|
|
301
|
-
try {
|
|
302
|
-
return await axios.request(requestConfig);
|
|
303
|
-
}
|
|
304
|
-
catch (error) {
|
|
305
|
-
const axiosError = error;
|
|
306
|
-
if (axiosError.response) {
|
|
307
|
-
return axiosError.response;
|
|
308
|
-
}
|
|
309
|
-
throw error;
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
const getResponseHeader = (response, responseHeader) => {
|
|
313
|
-
if (responseHeader) {
|
|
314
|
-
const content = response.headers[responseHeader];
|
|
315
|
-
if (isString(content)) {
|
|
316
|
-
return content;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
return undefined;
|
|
320
|
-
};
|
|
321
|
-
const getResponseBody = (response) => {
|
|
322
|
-
if (response.status !== 204) {
|
|
323
|
-
return response.data;
|
|
324
|
-
}
|
|
325
|
-
return undefined;
|
|
326
|
-
};
|
|
327
|
-
const catchErrorCodes = (options, result) => {
|
|
328
|
-
const errors = {
|
|
329
|
-
400: 'Bad Request',
|
|
330
|
-
401: 'Unauthorized',
|
|
331
|
-
403: 'Forbidden',
|
|
332
|
-
404: 'Not Found',
|
|
333
|
-
500: 'Internal Server Error',
|
|
334
|
-
502: 'Bad Gateway',
|
|
335
|
-
503: 'Service Unavailable',
|
|
336
|
-
...options.errors,
|
|
337
|
-
};
|
|
338
|
-
const error = errors[result.status];
|
|
339
|
-
if (error) {
|
|
340
|
-
throw new ApiError(options, result, error);
|
|
341
|
-
}
|
|
342
|
-
if (!result.ok) {
|
|
343
|
-
throw new ApiError(options, result, 'Generic Error');
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
/**
|
|
347
|
-
* Request method
|
|
348
|
-
* @param config The OpenAPI configuration object
|
|
349
|
-
* @param options The request options from the service
|
|
350
|
-
* @returns CancelablePromise<T>
|
|
351
|
-
* @throws ApiError
|
|
352
|
-
*/
|
|
353
|
-
const request = (config, options) => {
|
|
354
|
-
return new CancelablePromise(async (resolve, reject, onCancel) => {
|
|
355
|
-
try {
|
|
356
|
-
const url = getUrl(config, options);
|
|
357
|
-
const formData = getFormData(options);
|
|
358
|
-
const body = getRequestBody(options);
|
|
359
|
-
const headers = await getHeaders(config, options, formData);
|
|
360
|
-
if (!onCancel.isCancelled) {
|
|
361
|
-
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
|
|
362
|
-
const responseBody = getResponseBody(response);
|
|
363
|
-
const responseHeader = getResponseHeader(response, options.responseHeader);
|
|
364
|
-
const result = {
|
|
365
|
-
url,
|
|
366
|
-
ok: isSuccess(response.status),
|
|
367
|
-
status: response.status,
|
|
368
|
-
statusText: response.statusText,
|
|
369
|
-
body: responseHeader ?? responseBody,
|
|
370
|
-
};
|
|
371
|
-
catchErrorCodes(options, result);
|
|
372
|
-
resolve(result.body);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
catch (error) {
|
|
376
|
-
reject(error);
|
|
377
|
-
}
|
|
378
|
-
});
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
class AxiosHttpRequest extends BaseHttpRequest {
|
|
382
|
-
constructor(config) {
|
|
383
|
-
super(config);
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Request method
|
|
387
|
-
* @param options The request options from the service
|
|
388
|
-
* @returns CancelablePromise<T>
|
|
389
|
-
* @throws ApiError
|
|
390
|
-
*/
|
|
391
|
-
request(options) {
|
|
392
|
-
return request(this.config, options);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
class OrdersService {
|
|
397
|
-
httpRequest;
|
|
398
|
-
constructor(httpRequest) {
|
|
399
|
-
this.httpRequest = httpRequest;
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Cancel one or more orders
|
|
403
|
-
* Cancel one or more orders
|
|
404
|
-
* @returns CancelOrdersResult Orders cancellation response.
|
|
405
|
-
* @throws ApiError
|
|
406
|
-
*/
|
|
407
|
-
cancelOrders({ chainName, requestBody, }) {
|
|
408
|
-
return this.httpRequest.request({
|
|
409
|
-
method: 'POST',
|
|
410
|
-
url: '/v1/chains/{chain_name}/orders/cancel',
|
|
411
|
-
path: {
|
|
412
|
-
'chain_name': chainName,
|
|
413
|
-
},
|
|
414
|
-
body: requestBody,
|
|
415
|
-
mediaType: 'application/json',
|
|
416
|
-
errors: {
|
|
417
|
-
400: `Bad Request (400)`,
|
|
418
|
-
401: `Unauthorised Request (401)`,
|
|
419
|
-
404: `The specified resource was not found (404)`,
|
|
420
|
-
429: `Too Many Requests (429)`,
|
|
421
|
-
500: `Internal Server Error (500)`,
|
|
422
|
-
501: `Not Implemented Error (501)`,
|
|
423
|
-
},
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* List all listings
|
|
428
|
-
* List all listings
|
|
429
|
-
* @returns ListListingsResult OK response.
|
|
430
|
-
* @throws ApiError
|
|
431
|
-
*/
|
|
432
|
-
listListings({ chainName, status, sellItemContractAddress, buyItemContractAddress, sellItemTokenId, fromUpdatedAt, pageSize, sortBy, sortDirection, pageCursor, }) {
|
|
433
|
-
return this.httpRequest.request({
|
|
434
|
-
method: 'GET',
|
|
435
|
-
url: '/v1/chains/{chain_name}/orders/listings',
|
|
436
|
-
path: {
|
|
437
|
-
'chain_name': chainName,
|
|
438
|
-
},
|
|
439
|
-
query: {
|
|
440
|
-
'status': status,
|
|
441
|
-
'sell_item_contract_address': sellItemContractAddress,
|
|
442
|
-
'buy_item_contract_address': buyItemContractAddress,
|
|
443
|
-
'sell_item_token_id': sellItemTokenId,
|
|
444
|
-
'from_updated_at': fromUpdatedAt,
|
|
445
|
-
'page_size': pageSize,
|
|
446
|
-
'sort_by': sortBy,
|
|
447
|
-
'sort_direction': sortDirection,
|
|
448
|
-
'page_cursor': pageCursor,
|
|
449
|
-
},
|
|
450
|
-
errors: {
|
|
451
|
-
400: `Bad Request (400)`,
|
|
452
|
-
404: `The specified resource was not found (404)`,
|
|
453
|
-
500: `Internal Server Error (500)`,
|
|
454
|
-
},
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
/**
|
|
458
|
-
* Create a listing
|
|
459
|
-
* Create a listing
|
|
460
|
-
* @returns ListingResult Created response.
|
|
461
|
-
* @throws ApiError
|
|
462
|
-
*/
|
|
463
|
-
createListing({ chainName, requestBody, }) {
|
|
464
|
-
return this.httpRequest.request({
|
|
465
|
-
method: 'POST',
|
|
466
|
-
url: '/v1/chains/{chain_name}/orders/listings',
|
|
467
|
-
path: {
|
|
468
|
-
'chain_name': chainName,
|
|
469
|
-
},
|
|
470
|
-
body: requestBody,
|
|
471
|
-
mediaType: 'application/json',
|
|
472
|
-
errors: {
|
|
473
|
-
400: `Bad Request (400)`,
|
|
474
|
-
404: `The specified resource was not found (404)`,
|
|
475
|
-
500: `Internal Server Error (500)`,
|
|
476
|
-
},
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Get a single listing by ID
|
|
481
|
-
* Get a single listing by ID
|
|
482
|
-
* @returns ListingResult OK response.
|
|
483
|
-
* @throws ApiError
|
|
484
|
-
*/
|
|
485
|
-
getListing({ chainName, listingId, }) {
|
|
486
|
-
return this.httpRequest.request({
|
|
487
|
-
method: 'GET',
|
|
488
|
-
url: '/v1/chains/{chain_name}/orders/listings/{listing_id}',
|
|
489
|
-
path: {
|
|
490
|
-
'chain_name': chainName,
|
|
491
|
-
'listing_id': listingId,
|
|
492
|
-
},
|
|
493
|
-
errors: {
|
|
494
|
-
400: `Bad Request (400)`,
|
|
495
|
-
404: `The specified resource was not found (404)`,
|
|
496
|
-
500: `Internal Server Error (500)`,
|
|
497
|
-
},
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
/**
|
|
501
|
-
* Retrieve fulfillment data for orders
|
|
502
|
-
* Retrieve signed fulfillment data based on the list of order IDs and corresponding fees.
|
|
503
|
-
* @returns any Successful response
|
|
504
|
-
* @throws ApiError
|
|
505
|
-
*/
|
|
506
|
-
fulfillmentData({ chainName, requestBody, }) {
|
|
507
|
-
return this.httpRequest.request({
|
|
508
|
-
method: 'POST',
|
|
509
|
-
url: '/v1/chains/{chain_name}/orders/fulfillment-data',
|
|
510
|
-
path: {
|
|
511
|
-
'chain_name': chainName,
|
|
512
|
-
},
|
|
513
|
-
body: requestBody,
|
|
514
|
-
mediaType: 'application/json',
|
|
515
|
-
errors: {
|
|
516
|
-
400: `Bad Request (400)`,
|
|
517
|
-
404: `The specified resource was not found (404)`,
|
|
518
|
-
500: `Internal Server Error (500)`,
|
|
519
|
-
},
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
/**
|
|
523
|
-
* List all trades
|
|
524
|
-
* List all trades
|
|
525
|
-
* @returns ListTradeResult OK response.
|
|
526
|
-
* @throws ApiError
|
|
527
|
-
*/
|
|
528
|
-
listTrades({ chainName, accountAddress, fromIndexedAt, pageSize, sortBy, sortDirection, pageCursor, }) {
|
|
529
|
-
return this.httpRequest.request({
|
|
530
|
-
method: 'GET',
|
|
531
|
-
url: '/v1/chains/{chain_name}/trades',
|
|
532
|
-
path: {
|
|
533
|
-
'chain_name': chainName,
|
|
534
|
-
},
|
|
535
|
-
query: {
|
|
536
|
-
'account_address': accountAddress,
|
|
537
|
-
'from_indexed_at': fromIndexedAt,
|
|
538
|
-
'page_size': pageSize,
|
|
539
|
-
'sort_by': sortBy,
|
|
540
|
-
'sort_direction': sortDirection,
|
|
541
|
-
'page_cursor': pageCursor,
|
|
542
|
-
},
|
|
543
|
-
errors: {
|
|
544
|
-
400: `Bad Request (400)`,
|
|
545
|
-
404: `The specified resource was not found (404)`,
|
|
546
|
-
500: `Internal Server Error (500)`,
|
|
547
|
-
},
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
/**
|
|
551
|
-
* Get a single trade by ID
|
|
552
|
-
* Get a single trade by ID
|
|
553
|
-
* @returns TradeResult OK response.
|
|
554
|
-
* @throws ApiError
|
|
555
|
-
*/
|
|
556
|
-
getTrade({ chainName, tradeId, }) {
|
|
557
|
-
return this.httpRequest.request({
|
|
558
|
-
method: 'GET',
|
|
559
|
-
url: '/v1/chains/{chain_name}/trades/{trade_id}',
|
|
560
|
-
path: {
|
|
561
|
-
'chain_name': chainName,
|
|
562
|
-
'trade_id': tradeId,
|
|
563
|
-
},
|
|
564
|
-
errors: {
|
|
565
|
-
400: `Bad Request (400)`,
|
|
566
|
-
404: `The specified resource was not found (404)`,
|
|
567
|
-
500: `Internal Server Error (500)`,
|
|
568
|
-
},
|
|
569
|
-
});
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
class OrderBookClient {
|
|
574
|
-
orders;
|
|
575
|
-
request;
|
|
576
|
-
constructor(config, HttpRequest = AxiosHttpRequest) {
|
|
577
|
-
this.request = new HttpRequest({
|
|
578
|
-
BASE: config?.BASE ?? 'https://api.sandbox.immutable.com',
|
|
579
|
-
VERSION: config?.VERSION ?? '1.0.0',
|
|
580
|
-
WITH_CREDENTIALS: config?.WITH_CREDENTIALS ?? false,
|
|
581
|
-
CREDENTIALS: config?.CREDENTIALS ?? 'include',
|
|
582
|
-
TOKEN: config?.TOKEN,
|
|
583
|
-
USERNAME: config?.USERNAME,
|
|
584
|
-
PASSWORD: config?.PASSWORD,
|
|
585
|
-
HEADERS: config?.HEADERS,
|
|
586
|
-
ENCODE_PATH: config?.ENCODE_PATH,
|
|
587
|
-
});
|
|
588
|
-
this.orders = new OrdersService(this.request);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
/* istanbul ignore file */
|
|
593
|
-
/* tslint:disable */
|
|
594
|
-
/* eslint-disable */
|
|
595
|
-
var CancelledOrderStatus;
|
|
596
|
-
(function (CancelledOrderStatus) {
|
|
597
|
-
(function (cancellation_type) {
|
|
598
|
-
cancellation_type["ON_CHAIN"] = "ON_CHAIN";
|
|
599
|
-
cancellation_type["OFF_CHAIN"] = "OFF_CHAIN";
|
|
600
|
-
})(CancelledOrderStatus.cancellation_type || (CancelledOrderStatus.cancellation_type = {}));
|
|
601
|
-
})(CancelledOrderStatus || (CancelledOrderStatus = {}));
|
|
602
|
-
|
|
603
|
-
/* istanbul ignore file */
|
|
604
|
-
/* tslint:disable */
|
|
605
|
-
/* eslint-disable */
|
|
606
|
-
var FailedOrderCancellation;
|
|
607
|
-
(function (FailedOrderCancellation) {
|
|
608
|
-
(function (reason_code) {
|
|
609
|
-
reason_code["FILLED"] = "FILLED";
|
|
610
|
-
})(FailedOrderCancellation.reason_code || (FailedOrderCancellation.reason_code = {}));
|
|
611
|
-
})(FailedOrderCancellation || (FailedOrderCancellation = {}));
|
|
612
|
-
|
|
613
|
-
/* istanbul ignore file */
|
|
614
|
-
/* tslint:disable */
|
|
615
|
-
/* eslint-disable */
|
|
616
|
-
var Fee;
|
|
617
|
-
(function (Fee) {
|
|
618
|
-
(function (fee_type) {
|
|
619
|
-
fee_type["ROYALTY"] = "ROYALTY";
|
|
620
|
-
fee_type["MAKER_ECOSYSTEM"] = "MAKER_ECOSYSTEM";
|
|
621
|
-
fee_type["TAKER_ECOSYSTEM"] = "TAKER_ECOSYSTEM";
|
|
622
|
-
fee_type["PROTOCOL"] = "PROTOCOL";
|
|
623
|
-
})(Fee.fee_type || (Fee.fee_type = {}));
|
|
624
|
-
})(Fee || (Fee = {}));
|
|
625
|
-
|
|
626
|
-
/* istanbul ignore file */
|
|
627
|
-
/* tslint:disable */
|
|
628
|
-
/* eslint-disable */
|
|
629
|
-
/**
|
|
630
|
-
* The Order status
|
|
631
|
-
*/
|
|
632
|
-
var OrderStatusName;
|
|
633
|
-
(function (OrderStatusName) {
|
|
634
|
-
OrderStatusName["PENDING"] = "PENDING";
|
|
635
|
-
OrderStatusName["ACTIVE"] = "ACTIVE";
|
|
636
|
-
OrderStatusName["INACTIVE"] = "INACTIVE";
|
|
637
|
-
OrderStatusName["FILLED"] = "FILLED";
|
|
638
|
-
OrderStatusName["EXPIRED"] = "EXPIRED";
|
|
639
|
-
OrderStatusName["CANCELLED"] = "CANCELLED";
|
|
640
|
-
})(OrderStatusName || (OrderStatusName = {}));
|
|
641
|
-
|
|
642
|
-
/* istanbul ignore file */
|
|
643
|
-
/* tslint:disable */
|
|
644
|
-
/* eslint-disable */
|
|
645
|
-
var ProtocolData;
|
|
646
|
-
(function (ProtocolData) {
|
|
647
|
-
(function (order_type) {
|
|
648
|
-
order_type["FULL_RESTRICTED"] = "FULL_RESTRICTED";
|
|
649
|
-
})(ProtocolData.order_type || (ProtocolData.order_type = {}));
|
|
650
|
-
})(ProtocolData || (ProtocolData = {}));
|
|
651
|
-
|
|
652
|
-
var FeeType;
|
|
653
|
-
(function (FeeType) {
|
|
654
|
-
FeeType["MAKER_ECOSYSTEM"] = "MAKER_ECOSYSTEM";
|
|
655
|
-
FeeType["TAKER_ECOSYSTEM"] = "TAKER_ECOSYSTEM";
|
|
656
|
-
FeeType["PROTOCOL"] = "PROTOCOL";
|
|
657
|
-
FeeType["ROYALTY"] = "ROYALTY";
|
|
658
|
-
})(FeeType || (FeeType = {}));
|
|
659
|
-
var TransactionPurpose;
|
|
660
|
-
(function (TransactionPurpose) {
|
|
661
|
-
TransactionPurpose["APPROVAL"] = "APPROVAL";
|
|
662
|
-
TransactionPurpose["FULFILL_ORDER"] = "FULFILL_ORDER";
|
|
663
|
-
TransactionPurpose["CANCEL"] = "CANCEL";
|
|
664
|
-
})(TransactionPurpose || (TransactionPurpose = {}));
|
|
665
|
-
var SignablePurpose;
|
|
666
|
-
(function (SignablePurpose) {
|
|
667
|
-
SignablePurpose["CREATE_LISTING"] = "CREATE_LISTING";
|
|
668
|
-
SignablePurpose["OFF_CHAIN_CANCELLATION"] = "OFF_CHAIN_CANCELLATION";
|
|
669
|
-
})(SignablePurpose || (SignablePurpose = {}));
|
|
670
|
-
var ActionType;
|
|
671
|
-
(function (ActionType) {
|
|
672
|
-
ActionType["TRANSACTION"] = "TRANSACTION";
|
|
673
|
-
ActionType["SIGNABLE"] = "SIGNABLE";
|
|
674
|
-
})(ActionType || (ActionType = {}));
|
|
675
|
-
|
|
676
|
-
function mapFromOpenApiOrder(order) {
|
|
677
|
-
const buyItems = order.buy.map((item) => {
|
|
678
|
-
if (item.item_type === 'ERC20') {
|
|
679
|
-
return {
|
|
680
|
-
type: 'ERC20',
|
|
681
|
-
contractAddress: item.contract_address,
|
|
682
|
-
amount: item.amount,
|
|
683
|
-
};
|
|
684
|
-
}
|
|
685
|
-
if (item.item_type === 'NATIVE') {
|
|
686
|
-
return {
|
|
687
|
-
type: 'NATIVE',
|
|
688
|
-
amount: item.amount,
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
throw new Error('Buy items must be either ERC20 or NATIVE');
|
|
692
|
-
});
|
|
693
|
-
const sellItems = order.sell.map((item) => {
|
|
694
|
-
if (item.item_type === 'ERC721') {
|
|
695
|
-
return {
|
|
696
|
-
type: 'ERC721',
|
|
697
|
-
contractAddress: item.contract_address,
|
|
698
|
-
tokenId: item.token_id,
|
|
699
|
-
};
|
|
700
|
-
}
|
|
701
|
-
throw new Error('Sell items must ERC721');
|
|
702
|
-
});
|
|
703
|
-
return {
|
|
704
|
-
id: order.id,
|
|
705
|
-
accountAddress: order.account_address,
|
|
706
|
-
buy: buyItems,
|
|
707
|
-
sell: sellItems,
|
|
708
|
-
fees: order.fees.map((fee) => ({
|
|
709
|
-
amount: fee.amount,
|
|
710
|
-
recipient: fee.recipient,
|
|
711
|
-
type: fee.fee_type,
|
|
712
|
-
})),
|
|
713
|
-
chain: order.chain,
|
|
714
|
-
createdAt: order.created_at,
|
|
715
|
-
endAt: order.end_at,
|
|
716
|
-
protocolData: {
|
|
717
|
-
counter: order.protocol_data.counter,
|
|
718
|
-
orderType: order.protocol_data.order_type,
|
|
719
|
-
seaportAddress: order.protocol_data.seaport_address,
|
|
720
|
-
seaportVersion: order.protocol_data.seaport_version,
|
|
721
|
-
zoneAddress: order.protocol_data.zone_address,
|
|
722
|
-
},
|
|
723
|
-
salt: order.salt,
|
|
724
|
-
signature: order.signature,
|
|
725
|
-
startAt: order.start_at,
|
|
726
|
-
status: order.status,
|
|
727
|
-
updatedAt: order.updated_at,
|
|
728
|
-
};
|
|
729
|
-
}
|
|
730
|
-
function mapFromOpenApiTrade(trade) {
|
|
731
|
-
const buyItems = trade.buy.map((item) => {
|
|
732
|
-
if (item.item_type === 'ERC20') {
|
|
733
|
-
return {
|
|
734
|
-
type: 'ERC20',
|
|
735
|
-
contractAddress: item.contract_address,
|
|
736
|
-
amount: item.amount,
|
|
737
|
-
};
|
|
738
|
-
}
|
|
739
|
-
if (item.item_type === 'NATIVE') {
|
|
740
|
-
return {
|
|
741
|
-
type: 'NATIVE',
|
|
742
|
-
amount: item.amount,
|
|
743
|
-
};
|
|
744
|
-
}
|
|
745
|
-
throw new Error('Buy items must be either ERC20 or NATIVE');
|
|
746
|
-
});
|
|
747
|
-
const sellItems = trade.sell.map((item) => {
|
|
748
|
-
if (item.item_type === 'ERC721') {
|
|
749
|
-
return {
|
|
750
|
-
type: 'ERC721',
|
|
751
|
-
contractAddress: item.contract_address,
|
|
752
|
-
tokenId: item.token_id,
|
|
753
|
-
};
|
|
754
|
-
}
|
|
755
|
-
throw new Error('Sell items must ERC721');
|
|
756
|
-
});
|
|
757
|
-
return {
|
|
758
|
-
id: trade.id,
|
|
759
|
-
orderId: trade.order_id,
|
|
760
|
-
buy: buyItems,
|
|
761
|
-
sell: sellItems,
|
|
762
|
-
buyerFees: trade.buyer_fees.map((fee) => ({
|
|
763
|
-
amount: fee.amount,
|
|
764
|
-
recipient: fee.recipient,
|
|
765
|
-
type: fee.fee_type,
|
|
766
|
-
})),
|
|
767
|
-
chain: trade.chain,
|
|
768
|
-
indexedAt: trade.indexed_at,
|
|
769
|
-
blockchainMetadata: {
|
|
770
|
-
blockNumber: trade.blockchain_metadata.block_number,
|
|
771
|
-
logIndex: trade.blockchain_metadata.log_index,
|
|
772
|
-
transactionHash: trade.blockchain_metadata.transaction_hash,
|
|
773
|
-
transactionIndex: trade.blockchain_metadata.transaction_index,
|
|
774
|
-
},
|
|
775
|
-
buyerAddress: trade.buyer_address,
|
|
776
|
-
makerAddress: trade.maker_address,
|
|
777
|
-
sellerAddress: trade.seller_address,
|
|
778
|
-
takerAddress: trade.taker_address,
|
|
779
|
-
};
|
|
780
|
-
}
|
|
781
|
-
function mapFromOpenApiPage(page) {
|
|
782
|
-
return {
|
|
783
|
-
nextCursor: page.next_cursor,
|
|
784
|
-
previousCursor: page.previous_cursor,
|
|
785
|
-
};
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
/* eslint-disable */
|
|
789
|
-
// TODO: Resolve these from seaport-js.
|
|
790
|
-
// There is some bundling issue that is preventing this from working
|
|
791
|
-
const SEAPORT_CONTRACT_NAME = 'ImmutableSeaport';
|
|
792
|
-
// export const SEAPORT_CONTRACT_VERSION_V1_4 = '1.4';
|
|
793
|
-
const SEAPORT_CONTRACT_VERSION_V1_5 = '1.5';
|
|
794
|
-
const EIP_712_ORDER_TYPE = {
|
|
795
|
-
OrderComponents: [
|
|
796
|
-
{ name: 'offerer', type: 'address' },
|
|
797
|
-
{ name: 'zone', type: 'address' },
|
|
798
|
-
{ name: 'offer', type: 'OfferItem[]' },
|
|
799
|
-
{ name: 'consideration', type: 'ConsiderationItem[]' },
|
|
800
|
-
{ name: 'orderType', type: 'uint8' },
|
|
801
|
-
{ name: 'startTime', type: 'uint256' },
|
|
802
|
-
{ name: 'endTime', type: 'uint256' },
|
|
803
|
-
{ name: 'zoneHash', type: 'bytes32' },
|
|
804
|
-
{ name: 'salt', type: 'uint256' },
|
|
805
|
-
{ name: 'conduitKey', type: 'bytes32' },
|
|
806
|
-
{ name: 'counter', type: 'uint256' },
|
|
807
|
-
],
|
|
808
|
-
OfferItem: [
|
|
809
|
-
{ name: 'itemType', type: 'uint8' },
|
|
810
|
-
{ name: 'token', type: 'address' },
|
|
811
|
-
{ name: 'identifierOrCriteria', type: 'uint256' },
|
|
812
|
-
{ name: 'startAmount', type: 'uint256' },
|
|
813
|
-
{ name: 'endAmount', type: 'uint256' },
|
|
814
|
-
],
|
|
815
|
-
ConsiderationItem: [
|
|
816
|
-
{ name: 'itemType', type: 'uint8' },
|
|
817
|
-
{ name: 'token', type: 'address' },
|
|
818
|
-
{ name: 'identifierOrCriteria', type: 'uint256' },
|
|
819
|
-
{ name: 'startAmount', type: 'uint256' },
|
|
820
|
-
{ name: 'endAmount', type: 'uint256' },
|
|
821
|
-
{ name: 'recipient', type: 'address' },
|
|
822
|
-
],
|
|
823
|
-
};
|
|
824
|
-
var OrderType;
|
|
825
|
-
(function (OrderType) {
|
|
826
|
-
OrderType[OrderType["FULL_OPEN"] = 0] = "FULL_OPEN";
|
|
827
|
-
OrderType[OrderType["PARTIAL_OPEN"] = 1] = "PARTIAL_OPEN";
|
|
828
|
-
OrderType[OrderType["FULL_RESTRICTED"] = 2] = "FULL_RESTRICTED";
|
|
829
|
-
OrderType[OrderType["PARTIAL_RESTRICTED"] = 3] = "PARTIAL_RESTRICTED";
|
|
830
|
-
})(OrderType || (OrderType = {}));
|
|
831
|
-
var ItemType;
|
|
832
|
-
(function (ItemType) {
|
|
833
|
-
ItemType[ItemType["NATIVE"] = 0] = "NATIVE";
|
|
834
|
-
ItemType[ItemType["ERC20"] = 1] = "ERC20";
|
|
835
|
-
ItemType[ItemType["ERC721"] = 2] = "ERC721";
|
|
836
|
-
ItemType[ItemType["ERC1155"] = 3] = "ERC1155";
|
|
837
|
-
ItemType[ItemType["ERC721_WITH_CRITERIA"] = 4] = "ERC721_WITH_CRITERIA";
|
|
838
|
-
ItemType[ItemType["ERC1155_WITH_CRITERIA"] = 5] = "ERC1155_WITH_CRITERIA";
|
|
839
|
-
})(ItemType || (ItemType = {}));
|
|
840
|
-
var Side;
|
|
841
|
-
(function (Side) {
|
|
842
|
-
Side[Side["OFFER"] = 0] = "OFFER";
|
|
843
|
-
Side[Side["CONSIDERATION"] = 1] = "CONSIDERATION";
|
|
844
|
-
})(Side || (Side = {}));
|
|
845
|
-
var BasicOrderRouteType;
|
|
846
|
-
(function (BasicOrderRouteType) {
|
|
847
|
-
BasicOrderRouteType[BasicOrderRouteType["ETH_TO_ERC721"] = 0] = "ETH_TO_ERC721";
|
|
848
|
-
BasicOrderRouteType[BasicOrderRouteType["ETH_TO_ERC1155"] = 1] = "ETH_TO_ERC1155";
|
|
849
|
-
BasicOrderRouteType[BasicOrderRouteType["ERC20_TO_ERC721"] = 2] = "ERC20_TO_ERC721";
|
|
850
|
-
BasicOrderRouteType[BasicOrderRouteType["ERC20_TO_ERC1155"] = 3] = "ERC20_TO_ERC1155";
|
|
851
|
-
BasicOrderRouteType[BasicOrderRouteType["ERC721_TO_ERC20"] = 4] = "ERC721_TO_ERC20";
|
|
852
|
-
BasicOrderRouteType[BasicOrderRouteType["ERC1155_TO_ERC20"] = 5] = "ERC1155_TO_ERC20";
|
|
853
|
-
})(BasicOrderRouteType || (BasicOrderRouteType = {}));
|
|
854
|
-
|
|
855
|
-
function getOrderComponentsFromMessage(orderMessage) {
|
|
856
|
-
const data = JSON.parse(orderMessage);
|
|
857
|
-
const orderComponents = data.message;
|
|
858
|
-
orderComponents.salt = BigNumber.from(orderComponents.salt).toHexString();
|
|
859
|
-
return orderComponents;
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
// Add 20% more gas than estimate to prevent out of gas errors
|
|
863
|
-
// This can always be overwritten by the user signing the transaction
|
|
864
|
-
function prepareTransaction(transactionMethods,
|
|
865
|
-
// chainId is required for EIP155
|
|
866
|
-
chainId) {
|
|
867
|
-
return async () => {
|
|
868
|
-
const transaction = await transactionMethods.buildTransaction();
|
|
869
|
-
transaction.gasLimit = await transactionMethods.estimateGas();
|
|
870
|
-
transaction.gasLimit = transaction.gasLimit
|
|
871
|
-
.add(transaction.gasLimit.div(5));
|
|
872
|
-
transaction.chainId = chainId;
|
|
873
|
-
return transaction;
|
|
874
|
-
};
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
function mapImmutableOrderToSeaportOrderComponents(order, counter, zoneAddress) {
|
|
878
|
-
const considerationItems = order.buy.map((buyItem) => {
|
|
879
|
-
switch (buyItem.item_type) {
|
|
880
|
-
case 'NATIVE':
|
|
881
|
-
return {
|
|
882
|
-
startAmount: buyItem.amount,
|
|
883
|
-
endAmount: buyItem.amount,
|
|
884
|
-
itemType: ItemType.NATIVE,
|
|
885
|
-
recipient: order.account_address,
|
|
886
|
-
token: constants$1.AddressZero,
|
|
887
|
-
identifierOrCriteria: '0',
|
|
888
|
-
};
|
|
889
|
-
case 'ERC20':
|
|
890
|
-
return {
|
|
891
|
-
startAmount: buyItem.amount,
|
|
892
|
-
endAmount: buyItem.amount,
|
|
893
|
-
itemType: ItemType.ERC20,
|
|
894
|
-
recipient: order.account_address,
|
|
895
|
-
token: buyItem.contract_address || constants$1.AddressZero,
|
|
896
|
-
identifierOrCriteria: '0',
|
|
897
|
-
};
|
|
898
|
-
default: // ERC721
|
|
899
|
-
return {
|
|
900
|
-
startAmount: '1',
|
|
901
|
-
endAmount: '1',
|
|
902
|
-
itemType: ItemType.ERC721,
|
|
903
|
-
recipient: order.account_address,
|
|
904
|
-
token: buyItem.contract_address || constants$1.AddressZero,
|
|
905
|
-
identifierOrCriteria: '0',
|
|
906
|
-
};
|
|
907
|
-
}
|
|
908
|
-
});
|
|
909
|
-
const fees = order.fees.map((fee) => ({
|
|
910
|
-
amount: fee.amount,
|
|
911
|
-
itemType: order.buy[0].item_type === 'ERC20' ? ItemType.ERC20 : ItemType.NATIVE,
|
|
912
|
-
recipient: fee.recipient,
|
|
913
|
-
token: order.buy[0].item_type === 'ERC20'
|
|
914
|
-
? order.buy[0].contract_address
|
|
915
|
-
: constants$1.AddressZero,
|
|
916
|
-
identifierOrCriteria: '0',
|
|
917
|
-
}));
|
|
918
|
-
return {
|
|
919
|
-
orderComponents: {
|
|
920
|
-
conduitKey: constants$1.HashZero,
|
|
921
|
-
consideration: [...considerationItems],
|
|
922
|
-
offer: order.sell.map((sellItem) => {
|
|
923
|
-
const erc721Item = sellItem;
|
|
924
|
-
return {
|
|
925
|
-
startAmount: '1',
|
|
926
|
-
endAmount: '1',
|
|
927
|
-
itemType: ItemType.ERC721,
|
|
928
|
-
token: erc721Item.contract_address,
|
|
929
|
-
identifierOrCriteria: erc721Item.token_id,
|
|
930
|
-
};
|
|
931
|
-
}),
|
|
932
|
-
counter,
|
|
933
|
-
endTime: Math.round(new Date(order.end_at).getTime() / 1000).toString(),
|
|
934
|
-
startTime: Math.round(new Date(order.start_at).getTime() / 1000).toString(),
|
|
935
|
-
salt: order.salt,
|
|
936
|
-
offerer: order.account_address,
|
|
937
|
-
zone: zoneAddress,
|
|
938
|
-
// this should be the fee exclusive number of items the user signed for
|
|
939
|
-
totalOriginalConsiderationItems: considerationItems.length,
|
|
940
|
-
orderType: OrderType.FULL_RESTRICTED,
|
|
941
|
-
zoneHash: constants$1.HashZero,
|
|
942
|
-
},
|
|
943
|
-
tips: fees,
|
|
944
|
-
};
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
class Seaport {
|
|
948
|
-
seaportLibFactory;
|
|
949
|
-
provider;
|
|
950
|
-
seaportContractAddress;
|
|
951
|
-
zoneContractAddress;
|
|
952
|
-
constructor(seaportLibFactory, provider, seaportContractAddress, zoneContractAddress) {
|
|
953
|
-
this.seaportLibFactory = seaportLibFactory;
|
|
954
|
-
this.provider = provider;
|
|
955
|
-
this.seaportContractAddress = seaportContractAddress;
|
|
956
|
-
this.zoneContractAddress = zoneContractAddress;
|
|
957
|
-
}
|
|
958
|
-
async prepareSeaportOrder(offerer, listingItem, considerationItem, orderStart, orderExpiry) {
|
|
959
|
-
const { actions: seaportActions } = await this.createSeaportOrder(offerer, listingItem, considerationItem, orderStart, orderExpiry);
|
|
960
|
-
const listingActions = [];
|
|
961
|
-
const approvalAction = seaportActions.find((action) => action.type === 'approval');
|
|
962
|
-
if (approvalAction) {
|
|
963
|
-
listingActions.push({
|
|
964
|
-
type: ActionType.TRANSACTION,
|
|
965
|
-
purpose: TransactionPurpose.APPROVAL,
|
|
966
|
-
buildTransaction: prepareTransaction(approvalAction.transactionMethods, (await this.provider.getNetwork()).chainId),
|
|
967
|
-
});
|
|
968
|
-
}
|
|
969
|
-
const createAction = seaportActions.find((action) => action.type === 'create');
|
|
970
|
-
if (!createAction) {
|
|
971
|
-
throw new Error('No create order action found');
|
|
972
|
-
}
|
|
973
|
-
const orderMessageToSign = await createAction.getMessageToSign();
|
|
974
|
-
const orderComponents = getOrderComponentsFromMessage(orderMessageToSign);
|
|
975
|
-
listingActions.push({
|
|
976
|
-
type: ActionType.SIGNABLE,
|
|
977
|
-
purpose: SignablePurpose.CREATE_LISTING,
|
|
978
|
-
message: await this.getTypedDataFromOrderComponents(orderComponents),
|
|
979
|
-
});
|
|
980
|
-
return {
|
|
981
|
-
actions: listingActions,
|
|
982
|
-
orderComponents,
|
|
983
|
-
orderHash: this.getSeaportLib().getOrderHash(orderComponents),
|
|
984
|
-
};
|
|
985
|
-
}
|
|
986
|
-
async fulfillOrder(order, account, extraData) {
|
|
987
|
-
const { orderComponents, tips } = this.mapImmutableOrderToSeaportOrderComponents(order);
|
|
988
|
-
const seaportLib = this.getSeaportLib(order);
|
|
989
|
-
const { actions: seaportActions } = await seaportLib.fulfillOrders({
|
|
990
|
-
accountAddress: account,
|
|
991
|
-
fulfillOrderDetails: [
|
|
992
|
-
{
|
|
993
|
-
order: {
|
|
994
|
-
parameters: orderComponents,
|
|
995
|
-
signature: order.signature,
|
|
996
|
-
},
|
|
997
|
-
extraData,
|
|
998
|
-
tips,
|
|
999
|
-
},
|
|
1000
|
-
],
|
|
1001
|
-
});
|
|
1002
|
-
const fulfillmentActions = [];
|
|
1003
|
-
const approvalAction = seaportActions.find((action) => action.type === 'approval');
|
|
1004
|
-
if (approvalAction) {
|
|
1005
|
-
fulfillmentActions.push({
|
|
1006
|
-
type: ActionType.TRANSACTION,
|
|
1007
|
-
buildTransaction: prepareTransaction(approvalAction.transactionMethods, (await this.provider.getNetwork()).chainId),
|
|
1008
|
-
purpose: TransactionPurpose.APPROVAL,
|
|
1009
|
-
});
|
|
1010
|
-
}
|
|
1011
|
-
const fulfilOrderAction = seaportActions.find((action) => action.type === 'exchange');
|
|
1012
|
-
if (!fulfilOrderAction) {
|
|
1013
|
-
throw new Error('No exchange action found');
|
|
1014
|
-
}
|
|
1015
|
-
fulfillmentActions.push({
|
|
1016
|
-
type: ActionType.TRANSACTION,
|
|
1017
|
-
buildTransaction: prepareTransaction(fulfilOrderAction.transactionMethods, (await this.provider.getNetwork()).chainId),
|
|
1018
|
-
purpose: TransactionPurpose.FULFILL_ORDER,
|
|
1019
|
-
});
|
|
1020
|
-
return {
|
|
1021
|
-
actions: fulfillmentActions,
|
|
1022
|
-
expiration: Seaport.getExpirationISOTimeFromExtraData(extraData),
|
|
1023
|
-
order: mapFromOpenApiOrder(order),
|
|
1024
|
-
};
|
|
1025
|
-
}
|
|
1026
|
-
async fulfillBulkOrders(fulfillingOrders, account) {
|
|
1027
|
-
const fulfillOrderDetails = fulfillingOrders.map((o) => {
|
|
1028
|
-
const { orderComponents, tips } = this.mapImmutableOrderToSeaportOrderComponents(o.order);
|
|
1029
|
-
return {
|
|
1030
|
-
order: {
|
|
1031
|
-
parameters: orderComponents,
|
|
1032
|
-
signature: o.order.signature,
|
|
1033
|
-
},
|
|
1034
|
-
extraData: o.extra_data,
|
|
1035
|
-
tips,
|
|
1036
|
-
};
|
|
1037
|
-
});
|
|
1038
|
-
const { actions: seaportActions } = await this.getSeaportLib().fulfillOrders({
|
|
1039
|
-
fulfillOrderDetails,
|
|
1040
|
-
accountAddress: account,
|
|
1041
|
-
});
|
|
1042
|
-
const fulfillmentActions = [];
|
|
1043
|
-
const approvalAction = seaportActions.find((action) => action.type === 'approval');
|
|
1044
|
-
if (approvalAction) {
|
|
1045
|
-
fulfillmentActions.push({
|
|
1046
|
-
type: ActionType.TRANSACTION,
|
|
1047
|
-
buildTransaction: prepareTransaction(approvalAction.transactionMethods, (await this.provider.getNetwork()).chainId),
|
|
1048
|
-
purpose: TransactionPurpose.APPROVAL,
|
|
1049
|
-
});
|
|
1050
|
-
}
|
|
1051
|
-
const fulfilOrderAction = seaportActions.find((action) => action.type === 'exchange');
|
|
1052
|
-
if (!fulfilOrderAction) {
|
|
1053
|
-
throw new Error('No exchange action found');
|
|
1054
|
-
}
|
|
1055
|
-
fulfillmentActions.push({
|
|
1056
|
-
type: ActionType.TRANSACTION,
|
|
1057
|
-
buildTransaction: prepareTransaction(fulfilOrderAction.transactionMethods, (await this.provider.getNetwork()).chainId),
|
|
1058
|
-
purpose: TransactionPurpose.FULFILL_ORDER,
|
|
1059
|
-
});
|
|
1060
|
-
return {
|
|
1061
|
-
actions: fulfillmentActions,
|
|
1062
|
-
// return the shortest expiration out of all extraData - they should be very close
|
|
1063
|
-
expiration: fulfillOrderDetails
|
|
1064
|
-
.map((d) => Seaport.getExpirationISOTimeFromExtraData(d.extraData))
|
|
1065
|
-
.reduce((p, c) => (new Date(p) < new Date(c) ? p : c)),
|
|
1066
|
-
};
|
|
1067
|
-
}
|
|
1068
|
-
async cancelOrders(orders, account) {
|
|
1069
|
-
const orderComponents = orders.map((order) => this.mapImmutableOrderToSeaportOrderComponents(order).orderComponents);
|
|
1070
|
-
const seaportLib = this.getSeaportLib(orders[0]);
|
|
1071
|
-
const cancellationTransaction = await seaportLib.cancelOrders(orderComponents, account);
|
|
1072
|
-
return {
|
|
1073
|
-
type: ActionType.TRANSACTION,
|
|
1074
|
-
buildTransaction: prepareTransaction(cancellationTransaction, (await this.provider.getNetwork()).chainId),
|
|
1075
|
-
purpose: TransactionPurpose.CANCEL,
|
|
1076
|
-
};
|
|
1077
|
-
}
|
|
1078
|
-
mapImmutableOrderToSeaportOrderComponents(order) {
|
|
1079
|
-
const orderCounter = order.protocol_data.counter;
|
|
1080
|
-
return mapImmutableOrderToSeaportOrderComponents(order, orderCounter, this.zoneContractAddress);
|
|
1081
|
-
}
|
|
1082
|
-
createSeaportOrder(offerer, listingItem, considerationItem, orderStart, orderExpiry) {
|
|
1083
|
-
const seaportLib = this.getSeaportLib();
|
|
1084
|
-
return seaportLib.createOrder({
|
|
1085
|
-
allowPartialFills: false,
|
|
1086
|
-
offer: [
|
|
1087
|
-
{
|
|
1088
|
-
itemType: ItemType.ERC721,
|
|
1089
|
-
token: listingItem.contractAddress,
|
|
1090
|
-
identifier: listingItem.tokenId,
|
|
1091
|
-
},
|
|
1092
|
-
],
|
|
1093
|
-
consideration: [
|
|
1094
|
-
{
|
|
1095
|
-
token: considerationItem.type === 'ERC20' ? considerationItem.contractAddress : undefined,
|
|
1096
|
-
amount: considerationItem.amount,
|
|
1097
|
-
recipient: offerer,
|
|
1098
|
-
},
|
|
1099
|
-
],
|
|
1100
|
-
startTime: (orderStart.getTime() / 1000).toFixed(0),
|
|
1101
|
-
endTime: (orderExpiry.getTime() / 1000).toFixed(0),
|
|
1102
|
-
zone: this.zoneContractAddress,
|
|
1103
|
-
restrictedByZone: true,
|
|
1104
|
-
}, offerer);
|
|
1105
|
-
}
|
|
1106
|
-
async getTypedDataFromOrderComponents(orderComponents) {
|
|
1107
|
-
const { chainId } = await this.provider.getNetwork();
|
|
1108
|
-
const domainData = {
|
|
1109
|
-
name: SEAPORT_CONTRACT_NAME,
|
|
1110
|
-
version: SEAPORT_CONTRACT_VERSION_V1_5,
|
|
1111
|
-
chainId,
|
|
1112
|
-
verifyingContract: this.seaportContractAddress,
|
|
1113
|
-
};
|
|
1114
|
-
return {
|
|
1115
|
-
domain: domainData,
|
|
1116
|
-
types: EIP_712_ORDER_TYPE,
|
|
1117
|
-
value: orderComponents,
|
|
1118
|
-
};
|
|
1119
|
-
}
|
|
1120
|
-
getSeaportLib(order) {
|
|
1121
|
-
const seaportAddress = order?.protocol_data?.seaport_address ?? this.seaportContractAddress;
|
|
1122
|
-
const seaportVersion = SEAPORT_CONTRACT_VERSION_V1_5;
|
|
1123
|
-
// if (order?.protocol_data?.seaport_version === SEAPORT_CONTRACT_VERSION_V1_5) {
|
|
1124
|
-
// seaportVersion = SEAPORT_CONTRACT_VERSION_V1_5;
|
|
1125
|
-
// }
|
|
1126
|
-
return this.seaportLibFactory.create(seaportVersion, seaportAddress);
|
|
1127
|
-
}
|
|
1128
|
-
static getExpirationISOTimeFromExtraData(extraData) {
|
|
1129
|
-
// Expirtaion bytes in SIP7 extra data [21:29]
|
|
1130
|
-
// In hex string -> [21 * 2 + 2 (0x) : 29 * 2]
|
|
1131
|
-
// In JS slice (start, end_inclusive), (44,60)
|
|
1132
|
-
// 8 bytes uint64 epoch time in seconds
|
|
1133
|
-
const expirationHex = extraData.slice(44, 60);
|
|
1134
|
-
const expirationInSeconds = parseInt(expirationHex, 16);
|
|
1135
|
-
return new Date(expirationInSeconds * 1000).toISOString();
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
|
|
1139
|
-
class ImmutableApiClient {
|
|
1140
|
-
orderbookService;
|
|
1141
|
-
chainName;
|
|
1142
|
-
seaportAddress;
|
|
1143
|
-
constructor(orderbookService, chainName, seaportAddress) {
|
|
1144
|
-
this.orderbookService = orderbookService;
|
|
1145
|
-
this.chainName = chainName;
|
|
1146
|
-
this.seaportAddress = seaportAddress;
|
|
1147
|
-
}
|
|
1148
|
-
async fulfillmentData(requests) {
|
|
1149
|
-
return this.orderbookService.fulfillmentData({
|
|
1150
|
-
chainName: this.chainName,
|
|
1151
|
-
requestBody: requests,
|
|
1152
|
-
});
|
|
1153
|
-
}
|
|
1154
|
-
async getListing(listingId) {
|
|
1155
|
-
return this.orderbookService.getListing({
|
|
1156
|
-
chainName: this.chainName,
|
|
1157
|
-
listingId,
|
|
1158
|
-
});
|
|
1159
|
-
}
|
|
1160
|
-
async getTrade(tradeId) {
|
|
1161
|
-
return this.orderbookService.getTrade({
|
|
1162
|
-
chainName: this.chainName,
|
|
1163
|
-
tradeId,
|
|
1164
|
-
});
|
|
1165
|
-
}
|
|
1166
|
-
async listListings(listOrderParams) {
|
|
1167
|
-
return this.orderbookService.listListings({
|
|
1168
|
-
chainName: this.chainName,
|
|
1169
|
-
...listOrderParams,
|
|
1170
|
-
});
|
|
1171
|
-
}
|
|
1172
|
-
async listTrades(listTradesParams) {
|
|
1173
|
-
return this.orderbookService.listTrades({
|
|
1174
|
-
chainName: this.chainName,
|
|
1175
|
-
...listTradesParams,
|
|
1176
|
-
});
|
|
1177
|
-
}
|
|
1178
|
-
async cancelOrders(orderIds, accountAddress, signature) {
|
|
1179
|
-
return this.orderbookService.cancelOrders({
|
|
1180
|
-
chainName: this.chainName,
|
|
1181
|
-
requestBody: {
|
|
1182
|
-
account_address: accountAddress,
|
|
1183
|
-
orders: orderIds,
|
|
1184
|
-
signature,
|
|
1185
|
-
},
|
|
1186
|
-
});
|
|
1187
|
-
}
|
|
1188
|
-
async createListing({ orderHash, orderComponents, orderSignature, makerFees, }) {
|
|
1189
|
-
if (orderComponents.offer.length !== 1) {
|
|
1190
|
-
throw new Error('Only one item can be listed at a time');
|
|
1191
|
-
}
|
|
1192
|
-
if (Number(orderComponents.offer[0].itemType) !== ItemType.ERC721) {
|
|
1193
|
-
throw new Error('Only ERC721 tokens can be listed');
|
|
1194
|
-
}
|
|
1195
|
-
const orderTypes = [
|
|
1196
|
-
...orderComponents.consideration.map((c) => c.itemType),
|
|
1197
|
-
];
|
|
1198
|
-
const isSameConsiderationType = new Set(orderTypes).size === 1;
|
|
1199
|
-
if (!isSameConsiderationType) {
|
|
1200
|
-
throw new Error('All consideration items must be of the same type');
|
|
1201
|
-
}
|
|
1202
|
-
return this.orderbookService.createListing({
|
|
1203
|
-
chainName: this.chainName,
|
|
1204
|
-
requestBody: {
|
|
1205
|
-
order_hash: orderHash,
|
|
1206
|
-
account_address: orderComponents.offerer,
|
|
1207
|
-
buy: [
|
|
1208
|
-
{
|
|
1209
|
-
item_type: Number(orderComponents.consideration[0].itemType)
|
|
1210
|
-
=== ItemType.NATIVE
|
|
1211
|
-
? 'NATIVE'
|
|
1212
|
-
: 'ERC20',
|
|
1213
|
-
amount: orderComponents.consideration[0].startAmount,
|
|
1214
|
-
contract_address: orderComponents.consideration[0].token,
|
|
1215
|
-
},
|
|
1216
|
-
],
|
|
1217
|
-
fees: makerFees.map((x) => ({
|
|
1218
|
-
amount: x.amount,
|
|
1219
|
-
fee_type: FeeType.MAKER_ECOSYSTEM,
|
|
1220
|
-
recipient: x.recipient,
|
|
1221
|
-
})),
|
|
1222
|
-
end_at: new Date(parseInt(`${orderComponents.endTime.toString()}000`, 10)).toISOString(),
|
|
1223
|
-
protocol_data: {
|
|
1224
|
-
order_type: ProtocolData.order_type.FULL_RESTRICTED,
|
|
1225
|
-
zone_address: orderComponents.zone,
|
|
1226
|
-
seaport_address: this.seaportAddress,
|
|
1227
|
-
seaport_version: SEAPORT_CONTRACT_VERSION_V1_5,
|
|
1228
|
-
counter: orderComponents.counter.toString(),
|
|
1229
|
-
},
|
|
1230
|
-
salt: orderComponents.salt,
|
|
1231
|
-
sell: [
|
|
1232
|
-
{
|
|
1233
|
-
contract_address: orderComponents.offer[0].token,
|
|
1234
|
-
token_id: orderComponents.offer[0].identifierOrCriteria,
|
|
1235
|
-
item_type: 'ERC721',
|
|
1236
|
-
},
|
|
1237
|
-
],
|
|
1238
|
-
signature: orderSignature,
|
|
1239
|
-
start_at: new Date(parseInt(`${orderComponents.startTime.toString()}000`, 10)).toISOString(),
|
|
1240
|
-
},
|
|
1241
|
-
});
|
|
1242
|
-
}
|
|
1243
|
-
}
|
|
1244
|
-
|
|
1245
|
-
class ImmutableApiClientFactory {
|
|
1246
|
-
chainName;
|
|
1247
|
-
seaportAddress;
|
|
1248
|
-
orderbookClient;
|
|
1249
|
-
constructor(apiEndpoint, chainName, seaportAddress) {
|
|
1250
|
-
this.chainName = chainName;
|
|
1251
|
-
this.seaportAddress = seaportAddress;
|
|
1252
|
-
this.orderbookClient = new OrderBookClient({
|
|
1253
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1254
|
-
BASE: apiEndpoint,
|
|
1255
|
-
});
|
|
1256
|
-
}
|
|
1257
|
-
create() {
|
|
1258
|
-
return new ImmutableApiClient(this.orderbookClient.orders, this.chainName, this.seaportAddress);
|
|
1259
|
-
}
|
|
1260
|
-
}
|
|
1261
|
-
|
|
1262
|
-
const TESTNET_CHAIN_NAME = 'imtbl-zkevm-testnet';
|
|
1263
|
-
const MAINNET_CHAIN_NAME = 'imtbl-zkevm-mainnet';
|
|
1264
|
-
function getOrderbookConfig(environment) {
|
|
1265
|
-
switch (environment) {
|
|
1266
|
-
case Environment.SANDBOX:
|
|
1267
|
-
return {
|
|
1268
|
-
seaportContractAddress: '0x2CFA8f64e1B49A2DF28532D1D30Cda45117cF778',
|
|
1269
|
-
zoneContractAddress: '0x1Bb4Fb11Ba021Bd0104F0Ee8E5F5c728Bc83d7F1',
|
|
1270
|
-
apiEndpoint: 'https://api.sandbox.immutable.com',
|
|
1271
|
-
chainName: TESTNET_CHAIN_NAME,
|
|
1272
|
-
provider: new providers.JsonRpcProvider('https://rpc.testnet.immutable.com'),
|
|
1273
|
-
};
|
|
1274
|
-
// not yet deployed
|
|
1275
|
-
case Environment.PRODUCTION:
|
|
1276
|
-
return {
|
|
1277
|
-
seaportContractAddress: '',
|
|
1278
|
-
zoneContractAddress: '',
|
|
1279
|
-
apiEndpoint: 'https://api.immutable.com',
|
|
1280
|
-
chainName: MAINNET_CHAIN_NAME,
|
|
1281
|
-
provider: new providers.JsonRpcProvider('https://rpc.immutable.com'),
|
|
1282
|
-
};
|
|
1283
|
-
default:
|
|
1284
|
-
return null;
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
|
|
1288
|
-
class SeaportLibFactory {
|
|
1289
|
-
defaultSeaportContractAddress;
|
|
1290
|
-
provider;
|
|
1291
|
-
constructor(defaultSeaportContractAddress, provider) {
|
|
1292
|
-
this.defaultSeaportContractAddress = defaultSeaportContractAddress;
|
|
1293
|
-
this.provider = provider;
|
|
1294
|
-
}
|
|
1295
|
-
create(orderSeaportVersion, orderSeaportAddress) {
|
|
1296
|
-
const seaportVersion = orderSeaportVersion ?? SEAPORT_CONTRACT_VERSION_V1_5;
|
|
1297
|
-
const seaportContractAddress = orderSeaportAddress ?? this.defaultSeaportContractAddress;
|
|
1298
|
-
return new Seaport$1(this.provider, {
|
|
1299
|
-
seaportVersion,
|
|
1300
|
-
balanceAndApprovalChecksOnOrderCreation: true,
|
|
1301
|
-
overrides: {
|
|
1302
|
-
contractAddress: seaportContractAddress,
|
|
1303
|
-
},
|
|
1304
|
-
});
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
|
|
1308
|
-
/**
|
|
1309
|
-
* zkEVM orderbook SDK
|
|
1310
|
-
* @constructor
|
|
1311
|
-
* @param {OrderbookModuleConfiguration} config - Configuration for Immutable services.
|
|
1312
|
-
*/
|
|
1313
|
-
class Orderbook {
|
|
1314
|
-
apiClient;
|
|
1315
|
-
seaport;
|
|
1316
|
-
orderbookConfig;
|
|
1317
|
-
constructor(config) {
|
|
1318
|
-
const obConfig = getOrderbookConfig(config.baseConfig.environment);
|
|
1319
|
-
const finalConfig = {
|
|
1320
|
-
...obConfig,
|
|
1321
|
-
...config.overrides,
|
|
1322
|
-
};
|
|
1323
|
-
if (!finalConfig) {
|
|
1324
|
-
throw new Error('Orderbook configuration not passed, please specify the environment under config.baseConfig.environment');
|
|
1325
|
-
}
|
|
1326
|
-
this.orderbookConfig = finalConfig;
|
|
1327
|
-
const { apiEndpoint, chainName } = this.orderbookConfig;
|
|
1328
|
-
if (!apiEndpoint) {
|
|
1329
|
-
throw new Error('API endpoint must be provided');
|
|
1330
|
-
}
|
|
1331
|
-
this.apiClient = new ImmutableApiClientFactory(apiEndpoint, chainName, this.orderbookConfig.seaportContractAddress).create();
|
|
1332
|
-
const seaportLibFactory = new SeaportLibFactory(this.orderbookConfig.seaportContractAddress, this.orderbookConfig.provider);
|
|
1333
|
-
this.seaport = new Seaport(seaportLibFactory, this.orderbookConfig.provider, this.orderbookConfig.seaportContractAddress, this.orderbookConfig.zoneContractAddress);
|
|
1334
|
-
}
|
|
1335
|
-
/**
|
|
1336
|
-
* Return the configuration for the orderbook module.
|
|
1337
|
-
* @return {OrderbookModuleConfiguration} The configuration for the orderbook module.
|
|
1338
|
-
*/
|
|
1339
|
-
config() {
|
|
1340
|
-
return this.orderbookConfig;
|
|
1341
|
-
}
|
|
1342
|
-
/**
|
|
1343
|
-
* Get an order by ID
|
|
1344
|
-
* @param {string} listingId - The listingId to find.
|
|
1345
|
-
* @return {ListingResult} The returned order result.
|
|
1346
|
-
*/
|
|
1347
|
-
async getListing(listingId) {
|
|
1348
|
-
const apiListing = await this.apiClient.getListing(listingId);
|
|
1349
|
-
return {
|
|
1350
|
-
result: mapFromOpenApiOrder(apiListing.result),
|
|
1351
|
-
};
|
|
1352
|
-
}
|
|
1353
|
-
/**
|
|
1354
|
-
* Get a trade by ID
|
|
1355
|
-
* @param {string} tradeId - The tradeId to find.
|
|
1356
|
-
* @return {TradeResult} The returned order result.
|
|
1357
|
-
*/
|
|
1358
|
-
async getTrade(tradeId) {
|
|
1359
|
-
const apiListing = await this.apiClient.getTrade(tradeId);
|
|
1360
|
-
return {
|
|
1361
|
-
result: mapFromOpenApiTrade(apiListing.result),
|
|
1362
|
-
};
|
|
1363
|
-
}
|
|
1364
|
-
/**
|
|
1365
|
-
* List orders. This method is used to get a list of orders filtered by conditions specified
|
|
1366
|
-
* in the params object.
|
|
1367
|
-
* @param {ListListingsParams} listOrderParams - Filtering, ordering and page parameters.
|
|
1368
|
-
* @return {ListListingsResult} The paged orders.
|
|
1369
|
-
*/
|
|
1370
|
-
async listListings(listOrderParams) {
|
|
1371
|
-
const apiListings = await this.apiClient.listListings(listOrderParams);
|
|
1372
|
-
return {
|
|
1373
|
-
page: mapFromOpenApiPage(apiListings.page),
|
|
1374
|
-
result: apiListings.result.map(mapFromOpenApiOrder),
|
|
1375
|
-
};
|
|
1376
|
-
}
|
|
1377
|
-
/**
|
|
1378
|
-
* List trades. This method is used to get a list of trades filtered by conditions specified
|
|
1379
|
-
* in the params object
|
|
1380
|
-
* @param {ListTradesParams} listTradesParams - Filtering, ordering and page parameters.
|
|
1381
|
-
* @return {ListTradesResult} The paged trades.
|
|
1382
|
-
*/
|
|
1383
|
-
async listTrades(listTradesParams) {
|
|
1384
|
-
const apiListings = await this.apiClient.listTrades(listTradesParams);
|
|
1385
|
-
return {
|
|
1386
|
-
page: mapFromOpenApiPage(apiListings.page),
|
|
1387
|
-
result: apiListings.result.map(mapFromOpenApiTrade),
|
|
1388
|
-
};
|
|
1389
|
-
}
|
|
1390
|
-
/**
|
|
1391
|
-
* Get required transactions and messages for signing prior to creating a listing
|
|
1392
|
-
* through the createListing method
|
|
1393
|
-
* @param {PrepareListingParams} prepareListingParams - Details about the listing to be created.
|
|
1394
|
-
* @return {PrepareListingResponse} PrepareListingResponse includes
|
|
1395
|
-
* the unsigned approval transaction, the typed order message for signing and
|
|
1396
|
-
* the order components that can be submitted to `createListing` with a signature.
|
|
1397
|
-
*/
|
|
1398
|
-
async prepareListing({ makerAddress, sell, buy, orderExpiry, }) {
|
|
1399
|
-
return this.seaport.prepareSeaportOrder(makerAddress, sell, buy,
|
|
1400
|
-
// Default order start to now
|
|
1401
|
-
new Date(),
|
|
1402
|
-
// Default order expiry to 2 years from now
|
|
1403
|
-
orderExpiry || new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 2));
|
|
1404
|
-
}
|
|
1405
|
-
/**
|
|
1406
|
-
* Create an order
|
|
1407
|
-
* @param {CreateListingParams} createListingParams - create an order with the given params.
|
|
1408
|
-
* @return {ListingResult} The result of the order created in the Immutable services.
|
|
1409
|
-
*/
|
|
1410
|
-
async createListing(createListingParams) {
|
|
1411
|
-
const apiListingResponse = await this.apiClient.createListing({
|
|
1412
|
-
...createListingParams,
|
|
1413
|
-
});
|
|
1414
|
-
return {
|
|
1415
|
-
result: mapFromOpenApiOrder(apiListingResponse.result),
|
|
1416
|
-
};
|
|
1417
|
-
}
|
|
1418
|
-
/**
|
|
1419
|
-
* Get unsigned transactions that can be submitted to fulfil an open order. If the approval
|
|
1420
|
-
* transaction exists it must be signed and submitted to the chain before the fulfilment
|
|
1421
|
-
* transaction can be submitted or it will be reverted.
|
|
1422
|
-
* @param {string} listingId - The listingId to fulfil.
|
|
1423
|
-
* @param {string} fulfillerAddress - The address of the account fulfilling the order.
|
|
1424
|
-
* @return {FulfillOrderResponse} Approval and fulfilment transactions.
|
|
1425
|
-
*/
|
|
1426
|
-
async fulfillOrder(listingId, takerAddress, takerFees) {
|
|
1427
|
-
const fulfillmentDataRes = await this.apiClient.fulfillmentData([
|
|
1428
|
-
{
|
|
1429
|
-
order_id: listingId,
|
|
1430
|
-
fees: takerFees.map((fee) => ({
|
|
1431
|
-
amount: fee.amount,
|
|
1432
|
-
fee_type: FeeType.TAKER_ECOSYSTEM,
|
|
1433
|
-
recipient: fee.recipient,
|
|
1434
|
-
})),
|
|
1435
|
-
},
|
|
1436
|
-
]);
|
|
1437
|
-
if (fulfillmentDataRes.result.unfulfillable_orders?.length > 0) {
|
|
1438
|
-
throw new Error(`Unable to prepare fulfillment date: ${fulfillmentDataRes.result.unfulfillable_orders[0].reason}`);
|
|
1439
|
-
}
|
|
1440
|
-
else if (fulfillmentDataRes.result.fulfillable_orders?.length !== 1) {
|
|
1441
|
-
throw new Error('unexpected fulfillable order result length');
|
|
1442
|
-
}
|
|
1443
|
-
const extraData = fulfillmentDataRes.result.fulfillable_orders[0].extra_data;
|
|
1444
|
-
const orderResult = fulfillmentDataRes.result.fulfillable_orders[0].order;
|
|
1445
|
-
if (orderResult.status.name !== OrderStatusName.ACTIVE) {
|
|
1446
|
-
throw new Error(`Cannot fulfil order that is not active. Current status: ${orderResult.status}`);
|
|
1447
|
-
}
|
|
1448
|
-
return this.seaport.fulfillOrder(orderResult, takerAddress, extraData);
|
|
1449
|
-
}
|
|
1450
|
-
async fulfillBulkOrders(listings, takerAddress) {
|
|
1451
|
-
const fulfillmentDataRes = await this.apiClient.fulfillmentData(listings.map((listingRequest) => ({
|
|
1452
|
-
order_id: listingRequest.listingId,
|
|
1453
|
-
fees: listingRequest.takerFees.map((fee) => ({
|
|
1454
|
-
amount: fee.amount,
|
|
1455
|
-
fee_type: FeeType.TAKER_ECOSYSTEM,
|
|
1456
|
-
recipient: fee.recipient,
|
|
1457
|
-
})),
|
|
1458
|
-
})));
|
|
1459
|
-
try {
|
|
1460
|
-
return {
|
|
1461
|
-
...(await this.seaport.fulfillBulkOrders(fulfillmentDataRes.result.fulfillable_orders, takerAddress)),
|
|
1462
|
-
fulfillableOrders: fulfillmentDataRes.result.fulfillable_orders.map((o) => mapFromOpenApiOrder(o.order)),
|
|
1463
|
-
unfulfillableOrders: fulfillmentDataRes.result.unfulfillable_orders.map((o) => ({
|
|
1464
|
-
orderId: o.order_id,
|
|
1465
|
-
reason: o.reason,
|
|
1466
|
-
})),
|
|
1467
|
-
sufficientBalance: true,
|
|
1468
|
-
};
|
|
1469
|
-
}
|
|
1470
|
-
catch (e) {
|
|
1471
|
-
// if insufficient balance error, we return FulfillBulkOrdersInsufficientBalanceResponse
|
|
1472
|
-
if (String(e).includes('The fulfiller does not have the balances needed to fulfill.')) {
|
|
1473
|
-
return {
|
|
1474
|
-
fulfillableOrders: fulfillmentDataRes.result.fulfillable_orders.map((o) => mapFromOpenApiOrder(o.order)),
|
|
1475
|
-
unfulfillableOrders: fulfillmentDataRes.result.unfulfillable_orders.map((o) => ({
|
|
1476
|
-
orderId: o.order_id,
|
|
1477
|
-
reason: o.reason,
|
|
1478
|
-
})),
|
|
1479
|
-
sufficientBalance: false,
|
|
1480
|
-
};
|
|
1481
|
-
}
|
|
1482
|
-
// if some other error is thrown,
|
|
1483
|
-
// there likely is a race condition of the original order validity
|
|
1484
|
-
// we throw the error back out
|
|
1485
|
-
throw e;
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
/**
|
|
1489
|
-
* Cancelling orders is a gasless alternative to on-chain cancellation exposed with
|
|
1490
|
-
* `cancelOrdersOnChain`. For the orderbook to authenticate the cancellation, the creator
|
|
1491
|
-
* of the orders must sign an EIP712 message containing the orderIds
|
|
1492
|
-
* @param {string} orderIds - The orderIds to attempt to cancel.
|
|
1493
|
-
* @return {PrepareCancelOrdersResponse} The signable action to cancel the orders.
|
|
1494
|
-
*/
|
|
1495
|
-
async prepareOrderCancellations(orderIds) {
|
|
1496
|
-
const network = await this.orderbookConfig.provider.getNetwork();
|
|
1497
|
-
const domain = {
|
|
1498
|
-
name: 'imtbl-order-book',
|
|
1499
|
-
chainId: network.chainId,
|
|
1500
|
-
verifyingContract: this.orderbookConfig.seaportContractAddress,
|
|
1501
|
-
};
|
|
1502
|
-
const types = {
|
|
1503
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1504
|
-
CancelPayload: [
|
|
1505
|
-
{ name: 'orders', type: 'Order[]' },
|
|
1506
|
-
],
|
|
1507
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1508
|
-
Order: [
|
|
1509
|
-
{ name: 'id', type: 'string' },
|
|
1510
|
-
],
|
|
1511
|
-
};
|
|
1512
|
-
const cancelMessage = {
|
|
1513
|
-
orders: orderIds.map((id) => ({ id })),
|
|
1514
|
-
};
|
|
1515
|
-
return {
|
|
1516
|
-
signableAction: {
|
|
1517
|
-
purpose: SignablePurpose.OFF_CHAIN_CANCELLATION,
|
|
1518
|
-
type: ActionType.SIGNABLE,
|
|
1519
|
-
message: {
|
|
1520
|
-
domain,
|
|
1521
|
-
types,
|
|
1522
|
-
value: cancelMessage,
|
|
1523
|
-
},
|
|
1524
|
-
},
|
|
1525
|
-
};
|
|
1526
|
-
}
|
|
1527
|
-
/**
|
|
1528
|
-
* Cancelling orders is a gasless alternative to on-chain cancellation exposed with
|
|
1529
|
-
* `cancelOrdersOnChain`. Orders cancelled this way cannot be fulfilled and will be removed
|
|
1530
|
-
* from the orderbook. If there is pending fulfillment data outstanding for the order, its
|
|
1531
|
-
* cancellation will be pending until the fulfillment window has passed.
|
|
1532
|
-
* `prepareOffchainOrderCancellations` can be used to get the signable action that is signed
|
|
1533
|
-
* to get the signature required for this call.
|
|
1534
|
-
* @param {string[]} orderIds - The orderIds to attempt to cancel.
|
|
1535
|
-
* @param {string} accountAddress - The address of the account cancelling the orders.
|
|
1536
|
-
* @param {string} accountAddress - The address of the account cancelling the orders.
|
|
1537
|
-
* @return {CancelOrdersResult} The result of the off-chain cancellation request
|
|
1538
|
-
*/
|
|
1539
|
-
async cancelOrders(orderIds, accountAddress, signature) {
|
|
1540
|
-
return this.apiClient.cancelOrders(orderIds, accountAddress, signature);
|
|
1541
|
-
}
|
|
1542
|
-
/**
|
|
1543
|
-
* Get an unsigned order cancellation transaction. Orders can only be cancelled by
|
|
1544
|
-
* the account that created them. All of the orders must be from the same seaport contract.
|
|
1545
|
-
* If trying to cancel orders from multiple seaport contracts, group the orderIds by seaport
|
|
1546
|
-
* contract and call this method for each group.
|
|
1547
|
-
* @param {string[]} orderIds - The orderIds to cancel.
|
|
1548
|
-
* @param {string} accountAddress - The address of the account cancelling the order.
|
|
1549
|
-
* @return {CancelOrdersOnChainResponse} The unsigned cancel order action
|
|
1550
|
-
*/
|
|
1551
|
-
async cancelOrdersOnChain(orderIds, accountAddress) {
|
|
1552
|
-
const orderResults = await Promise.all(orderIds.map((id) => this.apiClient.getListing(id)));
|
|
1553
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
1554
|
-
for (const orderResult of orderResults) {
|
|
1555
|
-
if (orderResult.result.status.name !== OrderStatusName.ACTIVE
|
|
1556
|
-
&& orderResult.result.status.name !== OrderStatusName.INACTIVE
|
|
1557
|
-
&& orderResult.result.status.name !== OrderStatusName.PENDING) {
|
|
1558
|
-
throw new Error(`Cannot cancel order with status ${orderResult.result.status}`);
|
|
1559
|
-
}
|
|
1560
|
-
if (orderResult.result.account_address !== accountAddress.toLowerCase()) {
|
|
1561
|
-
throw new Error(`Only account ${orderResult.result.account_address} can cancel order ${orderResult.result.id}`);
|
|
1562
|
-
}
|
|
1563
|
-
}
|
|
1564
|
-
const orders = orderResults.map((orderResult) => orderResult.result);
|
|
1565
|
-
const seaportAddresses = orders.map((o) => o.protocol_data.seaport_address);
|
|
1566
|
-
const distinctSeaportAddresses = new Set(...[seaportAddresses]);
|
|
1567
|
-
if (distinctSeaportAddresses.size !== 1) {
|
|
1568
|
-
throw new Error('Cannot cancel multiple orders from different seaport contracts. Please group your orderIds accordingly');
|
|
1569
|
-
}
|
|
1570
|
-
const cancellationAction = await this.seaport.cancelOrders(orders, accountAddress);
|
|
1571
|
-
return { cancellationAction };
|
|
1572
|
-
}
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
// Consumers might want an estimated gas limit for fulfilling an order
|
|
1576
|
-
// without calling the transaction builder. This is an estimate that
|
|
1577
|
-
// should work for all fulfillment scenarios.
|
|
1578
|
-
const ESTIMATED_FULFILLMENT_GAS_GWEI = 400000;
|
|
1579
|
-
const constants = {
|
|
1580
|
-
estimatedFulfillmentGasGwei: ESTIMATED_FULFILLMENT_GAS_GWEI,
|
|
1581
|
-
};
|
|
1582
|
-
|
|
1583
|
-
export { ActionType, FeeType, OrderStatusName, Orderbook, SignablePurpose, TransactionPurpose, constants };
|