@polymarket/clob-client 5.0.0 → 5.1.0

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 (52) hide show
  1. package/package.json +2 -2
  2. package/dist/client.d.ts +0 -118
  3. package/dist/client.js +0 -776
  4. package/dist/client.js.map +0 -1
  5. package/dist/config.d.ts +0 -12
  6. package/dist/config.js +0 -28
  7. package/dist/config.js.map +0 -1
  8. package/dist/constants.d.ts +0 -3
  9. package/dist/constants.js +0 -7
  10. package/dist/constants.js.map +0 -1
  11. package/dist/endpoints.d.ts +0 -51
  12. package/dist/endpoints.js +0 -63
  13. package/dist/endpoints.js.map +0 -1
  14. package/dist/errors.d.ts +0 -4
  15. package/dist/errors.js +0 -5
  16. package/dist/errors.js.map +0 -1
  17. package/dist/headers/index.d.ts +0 -7
  18. package/dist/headers/index.js +0 -41
  19. package/dist/headers/index.js.map +0 -1
  20. package/dist/http-helpers/index.d.ts +0 -18
  21. package/dist/http-helpers/index.js +0 -105
  22. package/dist/http-helpers/index.js.map +0 -1
  23. package/dist/index.d.ts +0 -5
  24. package/dist/index.js +0 -6
  25. package/dist/index.js.map +0 -1
  26. package/dist/order-builder/builder.d.ts +0 -30
  27. package/dist/order-builder/builder.js +0 -52
  28. package/dist/order-builder/builder.js.map +0 -1
  29. package/dist/order-builder/helpers.d.ts +0 -51
  30. package/dist/order-builder/helpers.js +0 -279
  31. package/dist/order-builder/helpers.js.map +0 -1
  32. package/dist/order-builder/index.d.ts +0 -1
  33. package/dist/order-builder/index.js +0 -2
  34. package/dist/order-builder/index.js.map +0 -1
  35. package/dist/signing/constants.d.ts +0 -14
  36. package/dist/signing/constants.js +0 -16
  37. package/dist/signing/constants.js.map +0 -1
  38. package/dist/signing/eip712.d.ts +0 -10
  39. package/dist/signing/eip712.js +0 -34
  40. package/dist/signing/eip712.js.map +0 -1
  41. package/dist/signing/hmac.d.ts +0 -9
  42. package/dist/signing/hmac.js +0 -52
  43. package/dist/signing/hmac.js.map +0 -1
  44. package/dist/signing/index.d.ts +0 -2
  45. package/dist/signing/index.js +0 -3
  46. package/dist/signing/index.js.map +0 -1
  47. package/dist/types.d.ts +0 -445
  48. package/dist/types.js +0 -31
  49. package/dist/types.js.map +0 -1
  50. package/dist/utilities.d.ts +0 -16
  51. package/dist/utilities.js +0 -89
  52. package/dist/utilities.js.map +0 -1
package/dist/client.js DELETED
@@ -1,776 +0,0 @@
1
- import { SignatureType } from "@polymarket/order-utils";
2
- import { OrderType, Side } from "./types.js";
3
- import { createL1Headers, createL2Headers, injectBuilderHeaders } from "./headers/index.js";
4
- import { del, DELETE, GET, get, parseDropNotificationParams, POST, post, } from "./http-helpers/index.js";
5
- import { BUILDER_AUTH_FAILED, BUILDER_AUTH_NOT_AVAILABLE, L1_AUTH_UNAVAILABLE_ERROR, L2_AUTH_NOT_AVAILABLE } from "./errors.js";
6
- import { generateOrderBookSummaryHash, isTickSizeSmaller, orderToJson, priceValid, } from "./utilities.js";
7
- import { CANCEL_ALL, CANCEL_ORDER, CREATE_API_KEY, GET_API_KEYS, CLOSED_ONLY, GET_ORDER, POST_ORDER, TIME, GET_TRADES, GET_ORDER_BOOK, DELETE_API_KEY, GET_MIDPOINT, GET_PRICE, GET_OPEN_ORDERS, DERIVE_API_KEY, GET_LAST_TRADE_PRICE, GET_MARKETS, GET_MARKET, GET_PRICES_HISTORY, GET_NOTIFICATIONS, DROP_NOTIFICATIONS, CANCEL_ORDERS, CANCEL_MARKET_ORDERS, GET_BALANCE_ALLOWANCE, IS_ORDER_SCORING, GET_TICK_SIZE, GET_NEG_RISK, ARE_ORDERS_SCORING, GET_SIMPLIFIED_MARKETS, GET_SAMPLING_SIMPLIFIED_MARKETS, GET_SAMPLING_MARKETS, GET_MARKET_TRADES_EVENTS, GET_ORDER_BOOKS, GET_MIDPOINTS, GET_PRICES, GET_LAST_TRADES_PRICES, GET_EARNINGS_FOR_USER_FOR_DAY, GET_LIQUIDITY_REWARD_PERCENTAGES, GET_REWARDS_MARKETS_CURRENT, GET_REWARDS_MARKETS, GET_REWARDS_EARNINGS_PERCENTAGES, GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY, GET_SPREAD, GET_SPREADS, UPDATE_BALANCE_ALLOWANCE, POST_ORDERS, GET_FEE_RATE, GET_BUILDER_TRADES, CREATE_BUILDER_API_KEY, GET_BUILDER_API_KEYS, REVOKE_BUILDER_API_KEY, } from "./endpoints.js";
8
- import { OrderBuilder } from "./order-builder/builder.js";
9
- import { END_CURSOR, INITIAL_CURSOR } from "./constants.js";
10
- import { calculateBuyMarketPrice, calculateSellMarketPrice } from "./order-builder/helpers.js";
11
- export class ClobClient {
12
- host;
13
- chainId;
14
- // Used to perform Level 1 authentication and sign orders
15
- signer;
16
- // Used to perform Level 2 authentication
17
- creds;
18
- orderBuilder;
19
- tickSizes;
20
- negRisk;
21
- feeRates;
22
- geoBlockToken;
23
- useServerTime;
24
- builderConfig;
25
- // eslint-disable-next-line max-params
26
- constructor(host, chainId, signer, creds, signatureType, funderAddress, geoBlockToken, useServerTime, builderConfig, getSigner) {
27
- this.host = host.endsWith("/") ? host.slice(0, -1) : host;
28
- this.chainId = chainId;
29
- if (signer !== undefined) {
30
- this.signer = signer;
31
- }
32
- if (creds !== undefined) {
33
- this.creds = creds;
34
- }
35
- this.orderBuilder = new OrderBuilder(signer, chainId, signatureType, funderAddress, getSigner);
36
- this.tickSizes = {};
37
- this.negRisk = {};
38
- this.feeRates = {};
39
- this.geoBlockToken = geoBlockToken;
40
- this.useServerTime = useServerTime;
41
- if (builderConfig !== undefined) {
42
- this.builderConfig = builderConfig;
43
- }
44
- }
45
- // Public endpoints
46
- async getOk() {
47
- return this.get(`${this.host}/`);
48
- }
49
- async getServerTime() {
50
- return this.get(`${this.host}${TIME}`);
51
- }
52
- async getSamplingSimplifiedMarkets(next_cursor = INITIAL_CURSOR) {
53
- return this.get(`${this.host}${GET_SAMPLING_SIMPLIFIED_MARKETS}`, {
54
- params: { next_cursor },
55
- });
56
- }
57
- async getSamplingMarkets(next_cursor = INITIAL_CURSOR) {
58
- return this.get(`${this.host}${GET_SAMPLING_MARKETS}`, {
59
- params: { next_cursor },
60
- });
61
- }
62
- async getSimplifiedMarkets(next_cursor = INITIAL_CURSOR) {
63
- return this.get(`${this.host}${GET_SIMPLIFIED_MARKETS}`, {
64
- params: { next_cursor },
65
- });
66
- }
67
- async getMarkets(next_cursor = INITIAL_CURSOR) {
68
- return this.get(`${this.host}${GET_MARKETS}`, {
69
- params: { next_cursor },
70
- });
71
- }
72
- async getMarket(conditionID) {
73
- return this.get(`${this.host}${GET_MARKET}${conditionID}`);
74
- }
75
- async getOrderBook(tokenID) {
76
- return this.get(`${this.host}${GET_ORDER_BOOK}`, {
77
- params: { token_id: tokenID },
78
- });
79
- }
80
- async getOrderBooks(params) {
81
- return this.post(`${this.host}${GET_ORDER_BOOKS}`, {
82
- data: params,
83
- });
84
- }
85
- async getTickSize(tokenID) {
86
- if (tokenID in this.tickSizes) {
87
- return this.tickSizes[tokenID];
88
- }
89
- const result = await this.get(`${this.host}${GET_TICK_SIZE}`, {
90
- params: { token_id: tokenID },
91
- });
92
- this.tickSizes[tokenID] = result.minimum_tick_size.toString();
93
- return this.tickSizes[tokenID];
94
- }
95
- async getNegRisk(tokenID) {
96
- if (tokenID in this.negRisk) {
97
- return this.negRisk[tokenID];
98
- }
99
- const result = await this.get(`${this.host}${GET_NEG_RISK}`, {
100
- params: { token_id: tokenID },
101
- });
102
- this.negRisk[tokenID] = result.neg_risk;
103
- return this.negRisk[tokenID];
104
- }
105
- async getFeeRateBps(tokenID) {
106
- if (tokenID in this.feeRates) {
107
- return this.feeRates[tokenID];
108
- }
109
- const result = await this.get(`${this.host}${GET_FEE_RATE}`, {
110
- params: { token_id: tokenID },
111
- });
112
- this.feeRates[tokenID] = result.base_fee;
113
- return this.feeRates[tokenID];
114
- }
115
- /**
116
- * Calculates the hash for the given orderbook
117
- * @param orderbook
118
- * @returns
119
- */
120
- getOrderBookHash(orderbook) {
121
- return generateOrderBookSummaryHash(orderbook);
122
- }
123
- async getMidpoint(tokenID) {
124
- return this.get(`${this.host}${GET_MIDPOINT}`, {
125
- params: { token_id: tokenID },
126
- });
127
- }
128
- async getMidpoints(params) {
129
- return this.post(`${this.host}${GET_MIDPOINTS}`, {
130
- data: params,
131
- });
132
- }
133
- async getPrice(tokenID, side) {
134
- return this.get(`${this.host}${GET_PRICE}`, {
135
- params: { token_id: tokenID, side: side },
136
- });
137
- }
138
- async getPrices(params) {
139
- return this.post(`${this.host}${GET_PRICES}`, {
140
- data: params,
141
- });
142
- }
143
- async getSpread(tokenID) {
144
- return this.get(`${this.host}${GET_SPREAD}`, {
145
- params: { token_id: tokenID },
146
- });
147
- }
148
- async getSpreads(params) {
149
- return this.post(`${this.host}${GET_SPREADS}`, {
150
- data: params,
151
- });
152
- }
153
- async getLastTradePrice(tokenID) {
154
- return this.get(`${this.host}${GET_LAST_TRADE_PRICE}`, {
155
- params: { token_id: tokenID },
156
- });
157
- }
158
- async getLastTradesPrices(params) {
159
- return this.post(`${this.host}${GET_LAST_TRADES_PRICES}`, {
160
- data: params,
161
- });
162
- }
163
- async getPricesHistory(params) {
164
- return this.get(`${this.host}${GET_PRICES_HISTORY}`, {
165
- params,
166
- });
167
- }
168
- // L1 Authed
169
- /**
170
- * Creates a new API key for a user
171
- * @param nonce
172
- * @returns ApiKeyCreds
173
- */
174
- async createApiKey(nonce) {
175
- this.canL1Auth();
176
- const endpoint = `${this.host}${CREATE_API_KEY}`;
177
- const headers = await createL1Headers(this.signer, this.chainId, nonce, this.useServerTime ? await this.getServerTime() : undefined);
178
- return await this.post(endpoint, { headers }).then((apiKeyRaw) => {
179
- const apiKey = {
180
- key: apiKeyRaw.apiKey,
181
- secret: apiKeyRaw.secret,
182
- passphrase: apiKeyRaw.passphrase,
183
- };
184
- return apiKey;
185
- });
186
- }
187
- /**
188
- * Derives an existing API key for a user
189
- * @param nonce
190
- * @returns ApiKeyCreds
191
- */
192
- async deriveApiKey(nonce) {
193
- this.canL1Auth();
194
- const endpoint = `${this.host}${DERIVE_API_KEY}`;
195
- const headers = await createL1Headers(this.signer, this.chainId, nonce, this.useServerTime ? await this.getServerTime() : undefined);
196
- return await this.get(endpoint, { headers }).then((apiKeyRaw) => {
197
- const apiKey = {
198
- key: apiKeyRaw.apiKey,
199
- secret: apiKeyRaw.secret,
200
- passphrase: apiKeyRaw.passphrase,
201
- };
202
- return apiKey;
203
- });
204
- }
205
- async createOrDeriveApiKey(nonce) {
206
- return this.createApiKey(nonce).then(response => {
207
- if (!response.key) {
208
- return this.deriveApiKey(nonce);
209
- }
210
- return response;
211
- });
212
- }
213
- async getApiKeys() {
214
- this.canL2Auth();
215
- const endpoint = GET_API_KEYS;
216
- const headerArgs = {
217
- method: GET,
218
- requestPath: endpoint,
219
- };
220
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
221
- return this.get(`${this.host}${endpoint}`, { headers });
222
- }
223
- async getClosedOnlyMode() {
224
- this.canL2Auth();
225
- const endpoint = CLOSED_ONLY;
226
- const headerArgs = {
227
- method: GET,
228
- requestPath: endpoint,
229
- };
230
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
231
- return this.get(`${this.host}${endpoint}`, { headers });
232
- }
233
- async deleteApiKey() {
234
- this.canL2Auth();
235
- const endpoint = DELETE_API_KEY;
236
- const headerArgs = {
237
- method: DELETE,
238
- requestPath: endpoint,
239
- };
240
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
241
- return this.del(`${this.host}${endpoint}`, { headers });
242
- }
243
- async getOrder(orderID) {
244
- this.canL2Auth();
245
- const endpoint = `${GET_ORDER}${orderID}`;
246
- const headerArgs = {
247
- method: GET,
248
- requestPath: endpoint,
249
- };
250
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
251
- return this.get(`${this.host}${endpoint}`, { headers });
252
- }
253
- async getTrades(params, only_first_page = false, next_cursor) {
254
- this.canL2Auth();
255
- const endpoint = GET_TRADES;
256
- const headerArgs = {
257
- method: GET,
258
- requestPath: endpoint,
259
- };
260
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
261
- let results = [];
262
- next_cursor = next_cursor || INITIAL_CURSOR;
263
- while (next_cursor != END_CURSOR && (next_cursor === INITIAL_CURSOR || !only_first_page)) {
264
- const _params = {
265
- ...params,
266
- next_cursor,
267
- };
268
- const response = await this.get(`${this.host}${endpoint}`, {
269
- headers,
270
- params: _params,
271
- });
272
- next_cursor = response.next_cursor;
273
- results = [...results, ...response.data];
274
- }
275
- return results;
276
- }
277
- async getTradesPaginated(params, next_cursor) {
278
- this.canL2Auth();
279
- const endpoint = GET_TRADES;
280
- const headerArgs = {
281
- method: GET,
282
- requestPath: endpoint,
283
- };
284
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
285
- next_cursor = next_cursor || INITIAL_CURSOR;
286
- const _params = { ...params, next_cursor };
287
- const { data, ...rest } = await this.get(`${this.host}${endpoint}`, {
288
- headers,
289
- params: _params,
290
- });
291
- return { trades: Array.isArray(data) ? [...data] : [], ...rest };
292
- }
293
- async getBuilderTrades(params, next_cursor) {
294
- this.mustBuilderAuth();
295
- const endpoint = GET_BUILDER_TRADES;
296
- const headerArgs = {
297
- method: GET,
298
- requestPath: endpoint,
299
- };
300
- const headers = await this._getBuilderHeaders(headerArgs.method, headerArgs.requestPath);
301
- if (headers == undefined) {
302
- throw BUILDER_AUTH_FAILED;
303
- }
304
- next_cursor = next_cursor || INITIAL_CURSOR;
305
- const _params = { ...params, next_cursor };
306
- const { data, ...rest } = await this.get(`${this.host}${endpoint}`, {
307
- headers,
308
- params: _params,
309
- });
310
- return { trades: Array.isArray(data) ? [...data] : [], ...rest };
311
- }
312
- async getNotifications() {
313
- this.canL2Auth();
314
- const endpoint = GET_NOTIFICATIONS;
315
- const headerArgs = {
316
- method: GET,
317
- requestPath: endpoint,
318
- };
319
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
320
- return this.get(`${this.host}${endpoint}`, {
321
- headers,
322
- params: { signature_type: this.orderBuilder.signatureType },
323
- });
324
- }
325
- async dropNotifications(params) {
326
- this.canL2Auth();
327
- const endpoint = DROP_NOTIFICATIONS;
328
- const l2HeaderArgs = {
329
- method: DELETE,
330
- requestPath: endpoint,
331
- };
332
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
333
- return this.del(`${this.host}${endpoint}`, {
334
- headers,
335
- params: parseDropNotificationParams(params),
336
- });
337
- }
338
- async getBalanceAllowance(params) {
339
- this.canL2Auth();
340
- const endpoint = GET_BALANCE_ALLOWANCE;
341
- const headerArgs = {
342
- method: GET,
343
- requestPath: endpoint,
344
- };
345
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
346
- const _params = {
347
- ...params,
348
- signature_type: this.orderBuilder.signatureType,
349
- };
350
- return this.get(`${this.host}${endpoint}`, { headers, params: _params });
351
- }
352
- async updateBalanceAllowance(params) {
353
- this.canL2Auth();
354
- const endpoint = UPDATE_BALANCE_ALLOWANCE;
355
- const headerArgs = {
356
- method: GET,
357
- requestPath: endpoint,
358
- };
359
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
360
- const _params = {
361
- ...params,
362
- signature_type: this.orderBuilder.signatureType,
363
- };
364
- return this.get(`${this.host}${endpoint}`, { headers, params: _params });
365
- }
366
- async createOrder(userOrder, options) {
367
- this.canL1Auth();
368
- const { tokenID } = userOrder;
369
- const tickSize = await this._resolveTickSize(tokenID, options?.tickSize);
370
- const feeRateBps = await this._resolveFeeRateBps(tokenID, userOrder.feeRateBps);
371
- userOrder.feeRateBps = feeRateBps;
372
- if (!priceValid(userOrder.price, tickSize)) {
373
- throw new Error(`invalid price (${userOrder.price}), min: ${parseFloat(tickSize)} - max: ${1 - parseFloat(tickSize)}`);
374
- }
375
- const negRisk = options?.negRisk ?? (await this.getNegRisk(tokenID));
376
- return this.orderBuilder.buildOrder(userOrder, {
377
- tickSize,
378
- negRisk,
379
- });
380
- }
381
- async createMarketOrder(userMarketOrder, options) {
382
- this.canL1Auth();
383
- const { tokenID } = userMarketOrder;
384
- const tickSize = await this._resolveTickSize(tokenID, options?.tickSize);
385
- const feeRateBps = await this._resolveFeeRateBps(tokenID, userMarketOrder.feeRateBps);
386
- userMarketOrder.feeRateBps = feeRateBps;
387
- if (!userMarketOrder.price) {
388
- userMarketOrder.price = await this.calculateMarketPrice(tokenID, userMarketOrder.side, userMarketOrder.amount, userMarketOrder.orderType);
389
- }
390
- if (!priceValid(userMarketOrder.price, tickSize)) {
391
- throw new Error(`invalid price (${userMarketOrder.price}), min: ${parseFloat(tickSize)} - max: ${1 - parseFloat(tickSize)}`);
392
- }
393
- const negRisk = options?.negRisk ?? (await this.getNegRisk(tokenID));
394
- return this.orderBuilder.buildMarketOrder(userMarketOrder, {
395
- tickSize,
396
- negRisk,
397
- });
398
- }
399
- async createAndPostOrder(userOrder, options, orderType = OrderType.GTC, deferExec = false) {
400
- const order = await this.createOrder(userOrder, options);
401
- return this.postOrder(order, orderType, deferExec);
402
- }
403
- async createAndPostMarketOrder(userMarketOrder, options, orderType = OrderType.FOK, deferExec = false) {
404
- const order = await this.createMarketOrder(userMarketOrder, options);
405
- return this.postOrder(order, orderType, deferExec);
406
- }
407
- async getOpenOrders(params, only_first_page = false, next_cursor) {
408
- this.canL2Auth();
409
- const endpoint = GET_OPEN_ORDERS;
410
- const l2HeaderArgs = {
411
- method: GET,
412
- requestPath: endpoint,
413
- };
414
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
415
- let results = [];
416
- next_cursor = next_cursor || INITIAL_CURSOR;
417
- while (next_cursor != END_CURSOR && (next_cursor === INITIAL_CURSOR || !only_first_page)) {
418
- const _params = {
419
- ...params,
420
- next_cursor,
421
- };
422
- const response = await this.get(`${this.host}${endpoint}`, {
423
- headers,
424
- params: _params,
425
- });
426
- next_cursor = response.next_cursor;
427
- results = [...results, ...response.data];
428
- }
429
- return results;
430
- }
431
- async postOrder(order, orderType = OrderType.GTC, deferExec = false) {
432
- this.canL2Auth();
433
- const endpoint = POST_ORDER;
434
- const orderPayload = orderToJson(order, this.creds?.key || "", orderType, deferExec);
435
- const l2HeaderArgs = {
436
- method: POST,
437
- requestPath: endpoint,
438
- body: JSON.stringify(orderPayload),
439
- };
440
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
441
- // builders flow
442
- if (this.canBuilderAuth()) {
443
- const builderHeaders = await this._generateBuilderHeaders(headers, l2HeaderArgs);
444
- if (builderHeaders !== undefined) {
445
- return this.post(`${this.host}${endpoint}`, { headers: builderHeaders, data: orderPayload });
446
- }
447
- }
448
- return this.post(`${this.host}${endpoint}`, { headers, data: orderPayload });
449
- }
450
- async postOrders(args, deferExec = false) {
451
- this.canL2Auth();
452
- const endpoint = POST_ORDERS;
453
- const ordersPayload = [];
454
- for (const { order, orderType } of args) {
455
- const orderPayload = orderToJson(order, this.creds?.key || "", orderType, deferExec);
456
- ordersPayload.push(orderPayload);
457
- }
458
- const l2HeaderArgs = {
459
- method: POST,
460
- requestPath: endpoint,
461
- body: JSON.stringify(ordersPayload),
462
- };
463
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
464
- // builders flow
465
- if (this.canBuilderAuth()) {
466
- const builderHeaders = await this._generateBuilderHeaders(headers, l2HeaderArgs);
467
- if (builderHeaders !== undefined) {
468
- return this.post(`${this.host}${endpoint}`, { headers: builderHeaders, data: ordersPayload });
469
- }
470
- }
471
- return this.post(`${this.host}${endpoint}`, { headers, data: ordersPayload });
472
- }
473
- async cancelOrder(payload) {
474
- this.canL2Auth();
475
- const endpoint = CANCEL_ORDER;
476
- const l2HeaderArgs = {
477
- method: DELETE,
478
- requestPath: endpoint,
479
- body: JSON.stringify(payload),
480
- };
481
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
482
- return this.del(`${this.host}${endpoint}`, { headers, data: payload });
483
- }
484
- async cancelOrders(ordersHashes) {
485
- this.canL2Auth();
486
- const endpoint = CANCEL_ORDERS;
487
- const l2HeaderArgs = {
488
- method: DELETE,
489
- requestPath: endpoint,
490
- body: JSON.stringify(ordersHashes),
491
- };
492
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
493
- return this.del(`${this.host}${endpoint}`, { headers, data: ordersHashes });
494
- }
495
- async cancelAll() {
496
- this.canL2Auth();
497
- const endpoint = CANCEL_ALL;
498
- const l2HeaderArgs = {
499
- method: DELETE,
500
- requestPath: endpoint,
501
- };
502
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
503
- return this.del(`${this.host}${endpoint}`, { headers });
504
- }
505
- async cancelMarketOrders(payload) {
506
- this.canL2Auth();
507
- const endpoint = CANCEL_MARKET_ORDERS;
508
- const l2HeaderArgs = {
509
- method: DELETE,
510
- requestPath: endpoint,
511
- body: JSON.stringify(payload),
512
- };
513
- const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
514
- return this.del(`${this.host}${endpoint}`, { headers, data: payload });
515
- }
516
- async isOrderScoring(params) {
517
- this.canL2Auth();
518
- const endpoint = IS_ORDER_SCORING;
519
- const headerArgs = {
520
- method: GET,
521
- requestPath: endpoint,
522
- };
523
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
524
- return this.get(`${this.host}${endpoint}`, { headers, params });
525
- }
526
- async areOrdersScoring(params) {
527
- this.canL2Auth();
528
- const endpoint = ARE_ORDERS_SCORING;
529
- const payload = JSON.stringify(params?.orderIds);
530
- const headerArgs = {
531
- method: POST,
532
- requestPath: endpoint,
533
- body: payload,
534
- };
535
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
536
- return this.post(`${this.host}${endpoint}`, {
537
- headers,
538
- data: payload,
539
- });
540
- }
541
- // Rewards
542
- async getEarningsForUserForDay(date) {
543
- this.canL2Auth();
544
- const endpoint = GET_EARNINGS_FOR_USER_FOR_DAY;
545
- const headerArgs = {
546
- method: GET,
547
- requestPath: endpoint,
548
- };
549
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
550
- let results = [];
551
- let next_cursor = INITIAL_CURSOR;
552
- while (next_cursor != END_CURSOR) {
553
- const params = {
554
- date,
555
- signature_type: this.orderBuilder.signatureType,
556
- next_cursor,
557
- };
558
- const response = await this.get(`${this.host}${endpoint}`, {
559
- headers,
560
- params,
561
- });
562
- next_cursor = response.next_cursor;
563
- results = [...results, ...response.data];
564
- }
565
- return results;
566
- }
567
- async getTotalEarningsForUserForDay(date) {
568
- this.canL2Auth();
569
- const endpoint = GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY;
570
- const headerArgs = {
571
- method: GET,
572
- requestPath: endpoint,
573
- };
574
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
575
- const params = {
576
- date,
577
- signature_type: this.orderBuilder.signatureType,
578
- };
579
- return await this.get(`${this.host}${endpoint}`, {
580
- headers,
581
- params,
582
- });
583
- }
584
- async getUserEarningsAndMarketsConfig(date, order_by = "", position = "", no_competition = false) {
585
- this.canL2Auth();
586
- const endpoint = GET_REWARDS_EARNINGS_PERCENTAGES;
587
- const headerArgs = {
588
- method: GET,
589
- requestPath: endpoint,
590
- };
591
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
592
- let results = [];
593
- let next_cursor = INITIAL_CURSOR;
594
- while (next_cursor != END_CURSOR) {
595
- const params = {
596
- date,
597
- signature_type: this.orderBuilder.signatureType,
598
- next_cursor,
599
- order_by,
600
- position,
601
- no_competition,
602
- };
603
- const response = await this.get(`${this.host}${endpoint}`, {
604
- headers,
605
- params,
606
- });
607
- next_cursor = response.next_cursor;
608
- results = [...results, ...response.data];
609
- }
610
- return results;
611
- }
612
- async getRewardPercentages() {
613
- this.canL2Auth();
614
- const endpoint = GET_LIQUIDITY_REWARD_PERCENTAGES;
615
- const headerArgs = {
616
- method: GET,
617
- requestPath: endpoint,
618
- };
619
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
620
- const _params = {
621
- signature_type: this.orderBuilder.signatureType,
622
- };
623
- return this.get(`${this.host}${endpoint}`, { headers, params: _params });
624
- }
625
- async getCurrentRewards() {
626
- let results = [];
627
- let next_cursor = INITIAL_CURSOR;
628
- while (next_cursor != END_CURSOR) {
629
- const response = await this.get(`${this.host}${GET_REWARDS_MARKETS_CURRENT}`, {
630
- params: { next_cursor },
631
- });
632
- next_cursor = response.next_cursor;
633
- results = [...results, ...response.data];
634
- }
635
- return results;
636
- }
637
- async getRawRewardsForMarket(conditionId) {
638
- let results = [];
639
- let next_cursor = INITIAL_CURSOR;
640
- while (next_cursor != END_CURSOR) {
641
- const response = await this.get(`${this.host}${GET_REWARDS_MARKETS}${conditionId}`, {
642
- params: { next_cursor },
643
- });
644
- next_cursor = response.next_cursor;
645
- results = [...results, ...response.data];
646
- }
647
- return results;
648
- }
649
- async getMarketTradesEvents(conditionID) {
650
- return this.get(`${this.host}${GET_MARKET_TRADES_EVENTS}${conditionID}`);
651
- }
652
- async calculateMarketPrice(tokenID, side, amount, orderType = OrderType.FOK) {
653
- const book = await this.getOrderBook(tokenID);
654
- if (!book) {
655
- throw new Error("no orderbook");
656
- }
657
- if (side === Side.BUY) {
658
- if (!book.asks) {
659
- throw new Error("no match");
660
- }
661
- return calculateBuyMarketPrice(book.asks, amount, orderType);
662
- }
663
- else {
664
- if (!book.bids) {
665
- throw new Error("no match");
666
- }
667
- return calculateSellMarketPrice(book.bids, amount, orderType);
668
- }
669
- }
670
- async createBuilderApiKey() {
671
- this.canL2Auth();
672
- const endpoint = CREATE_BUILDER_API_KEY;
673
- const headerArgs = {
674
- method: POST,
675
- requestPath: endpoint,
676
- };
677
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
678
- return this.post(`${this.host}${endpoint}`, { headers });
679
- }
680
- async getBuilderApiKeys() {
681
- this.canL2Auth();
682
- const endpoint = GET_BUILDER_API_KEYS;
683
- const headerArgs = {
684
- method: GET,
685
- requestPath: endpoint,
686
- };
687
- const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
688
- return this.get(`${this.host}${endpoint}`, { headers });
689
- }
690
- async revokeBuilderApiKey() {
691
- this.mustBuilderAuth();
692
- const endpoint = REVOKE_BUILDER_API_KEY;
693
- const headerArgs = {
694
- method: DELETE,
695
- requestPath: endpoint,
696
- };
697
- const headers = await this._getBuilderHeaders(headerArgs.method, headerArgs.requestPath);
698
- if (headers == undefined) {
699
- throw BUILDER_AUTH_FAILED;
700
- }
701
- return this.del(`${this.host}${endpoint}`, { headers });
702
- }
703
- canL1Auth() {
704
- if (this.signer === undefined) {
705
- throw L1_AUTH_UNAVAILABLE_ERROR;
706
- }
707
- }
708
- canL2Auth() {
709
- if (this.signer === undefined) {
710
- throw L1_AUTH_UNAVAILABLE_ERROR;
711
- }
712
- if (this.creds === undefined) {
713
- throw L2_AUTH_NOT_AVAILABLE;
714
- }
715
- }
716
- mustBuilderAuth() {
717
- if (!this.canBuilderAuth()) {
718
- throw BUILDER_AUTH_NOT_AVAILABLE;
719
- }
720
- }
721
- canBuilderAuth() {
722
- return (this.builderConfig != undefined && this.builderConfig.isValid());
723
- }
724
- async _resolveTickSize(tokenID, tickSize) {
725
- const minTickSize = await this.getTickSize(tokenID);
726
- if (tickSize) {
727
- if (isTickSizeSmaller(tickSize, minTickSize)) {
728
- throw new Error(`invalid tick size (${tickSize}), minimum for the market is ${minTickSize}`);
729
- }
730
- }
731
- else {
732
- tickSize = minTickSize;
733
- }
734
- return tickSize;
735
- }
736
- async _resolveFeeRateBps(tokenID, userFeeRateBps) {
737
- const marketFeeRateBps = await this.getFeeRateBps(tokenID);
738
- if (marketFeeRateBps > 0 && userFeeRateBps != undefined && userFeeRateBps != marketFeeRateBps) {
739
- throw new Error(`invalid user provided fee rate: ${userFeeRateBps}, fee rate for the market must be ${marketFeeRateBps}`);
740
- }
741
- return marketFeeRateBps;
742
- }
743
- async _generateBuilderHeaders(headers, headerArgs) {
744
- if (this.builderConfig !== undefined) {
745
- const builderHeaders = await this._getBuilderHeaders(headerArgs.method, headerArgs.requestPath, headerArgs.body);
746
- if (builderHeaders == undefined) {
747
- return undefined;
748
- }
749
- return injectBuilderHeaders(headers, builderHeaders);
750
- }
751
- return undefined;
752
- }
753
- async _getBuilderHeaders(method, path, body) {
754
- return this.builderConfig.generateBuilderHeaders(method, path, body);
755
- }
756
- // http methods
757
- async get(endpoint, options) {
758
- return get(endpoint, {
759
- ...options,
760
- params: { ...options?.params, geo_block_token: this.geoBlockToken },
761
- });
762
- }
763
- async post(endpoint, options) {
764
- return post(endpoint, {
765
- ...options,
766
- params: { ...options?.params, geo_block_token: this.geoBlockToken },
767
- });
768
- }
769
- async del(endpoint, options) {
770
- return del(endpoint, {
771
- ...options,
772
- params: { ...options?.params, geo_block_token: this.geoBlockToken },
773
- });
774
- }
775
- }
776
- //# sourceMappingURL=client.js.map