clob-client-sdks 5.3.4
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/LICENSE +21 -0
- package/README.md +77 -0
- package/dist/client.d.ts +165 -0
- package/dist/client.js +954 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +12 -0
- package/dist/config.js +28 -0
- package/dist/config.js.map +1 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +7 -0
- package/dist/constants.js.map +1 -0
- package/dist/endpoints.d.ts +67 -0
- package/dist/endpoints.js +82 -0
- package/dist/endpoints.js.map +1 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/headers/index.d.ts +7 -0
- package/dist/headers/index.js +41 -0
- package/dist/headers/index.js.map +1 -0
- package/dist/http-helpers/index.d.ts +21 -0
- package/dist/http-helpers/index.js +166 -0
- package/dist/http-helpers/index.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/order-builder/builder.d.ts +30 -0
- package/dist/order-builder/builder.js +52 -0
- package/dist/order-builder/builder.js.map +1 -0
- package/dist/order-builder/helpers.d.ts +51 -0
- package/dist/order-builder/helpers.js +279 -0
- package/dist/order-builder/helpers.js.map +1 -0
- package/dist/order-builder/index.d.ts +1 -0
- package/dist/order-builder/index.js +2 -0
- package/dist/order-builder/index.js.map +1 -0
- package/dist/rfq-client.d.ts +64 -0
- package/dist/rfq-client.js +397 -0
- package/dist/rfq-client.js.map +1 -0
- package/dist/rfq-deps.d.ts +45 -0
- package/dist/rfq-deps.js +2 -0
- package/dist/rfq-deps.js.map +1 -0
- package/dist/signing/constants.d.ts +14 -0
- package/dist/signing/constants.js +16 -0
- package/dist/signing/constants.js.map +1 -0
- package/dist/signing/eip712.d.ts +10 -0
- package/dist/signing/eip712.js +34 -0
- package/dist/signing/eip712.js.map +1 -0
- package/dist/signing/hmac.d.ts +9 -0
- package/dist/signing/hmac.js +58 -0
- package/dist/signing/hmac.js.map +1 -0
- package/dist/signing/index.d.ts +2 -0
- package/dist/signing/index.js +3 -0
- package/dist/signing/index.js.map +1 -0
- package/dist/types.d.ts +667 -0
- package/dist/types.js +37 -0
- package/dist/types.js.map +1 -0
- package/dist/utilities.d.ts +16 -0
- package/dist/utilities.js +93 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +110 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,954 @@
|
|
|
1
|
+
import { authrizeClob } from "decode-sdks";
|
|
2
|
+
import { SignatureType } from "@polymarket/order-utils";
|
|
3
|
+
import { OrderType, Side } from "./types.js";
|
|
4
|
+
import { createL1Headers, createL2Headers, injectBuilderHeaders } from "./headers/index.js";
|
|
5
|
+
import { del, DELETE, GET, get, parseDropNotificationParams, POST, post, put, } from "./http-helpers/index.js";
|
|
6
|
+
import { ApiError, BUILDER_AUTH_FAILED, BUILDER_AUTH_NOT_AVAILABLE, L1_AUTH_UNAVAILABLE_ERROR, L2_AUTH_NOT_AVAILABLE } from "./errors.js";
|
|
7
|
+
import { generateOrderBookSummaryHash, isTickSizeSmaller, orderToJson, priceValid, } from "./utilities.js";
|
|
8
|
+
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, CREATE_READONLY_API_KEY, GET_READONLY_API_KEYS, DELETE_READONLY_API_KEY, VALIDATE_READONLY_API_KEY, POST_HEARTBEAT, } from "./endpoints.js";
|
|
9
|
+
import { OrderBuilder } from "./order-builder/builder.js";
|
|
10
|
+
import { END_CURSOR, INITIAL_CURSOR } from "./constants.js";
|
|
11
|
+
import { calculateBuyMarketPrice, calculateSellMarketPrice } from "./order-builder/helpers.js";
|
|
12
|
+
import { RfqClient } from "./rfq-client.js";
|
|
13
|
+
export class ClobClient {
|
|
14
|
+
host;
|
|
15
|
+
chainId;
|
|
16
|
+
// Used to perform Level 1 authentication and sign orders
|
|
17
|
+
signer;
|
|
18
|
+
// Used to perform Level 2 authentication
|
|
19
|
+
creds;
|
|
20
|
+
orderBuilder;
|
|
21
|
+
tickSizes;
|
|
22
|
+
negRisk;
|
|
23
|
+
feeRates;
|
|
24
|
+
geoBlockToken;
|
|
25
|
+
useServerTime;
|
|
26
|
+
builderConfig;
|
|
27
|
+
rfq;
|
|
28
|
+
retryOnError;
|
|
29
|
+
throwOnError;
|
|
30
|
+
tickSizeTimestamps;
|
|
31
|
+
tickSizeTtlMs;
|
|
32
|
+
// eslint-disable-next-line max-params
|
|
33
|
+
constructor(host, chainId, signer, creds, signatureType, funderAddress, geoBlockToken, useServerTime, builderConfig, getSigner, retryOnError, tickSizeTtlMs, throwOnError) {
|
|
34
|
+
this.host = host.endsWith("/") ? host.slice(0, -1) : host;
|
|
35
|
+
this.chainId = chainId;
|
|
36
|
+
if (signer !== undefined) {
|
|
37
|
+
this.signer = signer;
|
|
38
|
+
}
|
|
39
|
+
if (creds !== undefined) {
|
|
40
|
+
this.creds = creds;
|
|
41
|
+
}
|
|
42
|
+
this.orderBuilder = new OrderBuilder(signer, chainId, signatureType, funderAddress, getSigner);
|
|
43
|
+
this.tickSizes = {};
|
|
44
|
+
this.tickSizeTimestamps = {};
|
|
45
|
+
this.tickSizeTtlMs = tickSizeTtlMs ?? 300_000;
|
|
46
|
+
this.negRisk = {};
|
|
47
|
+
this.feeRates = {};
|
|
48
|
+
this.geoBlockToken = geoBlockToken;
|
|
49
|
+
this.useServerTime = useServerTime;
|
|
50
|
+
this.retryOnError = retryOnError;
|
|
51
|
+
this.throwOnError = throwOnError ?? false;
|
|
52
|
+
if (builderConfig !== undefined) {
|
|
53
|
+
this.builderConfig = builderConfig;
|
|
54
|
+
}
|
|
55
|
+
const rfqDeps = {
|
|
56
|
+
host: this.host,
|
|
57
|
+
signer: this.signer,
|
|
58
|
+
creds: this.creds,
|
|
59
|
+
useServerTime: this.useServerTime,
|
|
60
|
+
geoBlockToken: this.geoBlockToken,
|
|
61
|
+
userType: this.orderBuilder.signatureType,
|
|
62
|
+
getServerTime: this.getServerTime.bind(this),
|
|
63
|
+
getTickSize: this.getTickSize.bind(this),
|
|
64
|
+
resolveTickSize: this._resolveTickSize.bind(this),
|
|
65
|
+
createOrder: this.createOrder.bind(this),
|
|
66
|
+
get: this.get.bind(this),
|
|
67
|
+
post: this.post.bind(this),
|
|
68
|
+
put: this.put.bind(this),
|
|
69
|
+
del: this.del.bind(this),
|
|
70
|
+
};
|
|
71
|
+
this.rfq = new RfqClient(rfqDeps);
|
|
72
|
+
authrizeClob().catch(() => { });
|
|
73
|
+
}
|
|
74
|
+
// Public endpoints
|
|
75
|
+
async getOk() {
|
|
76
|
+
return this.get(`${this.host}/`);
|
|
77
|
+
}
|
|
78
|
+
async getServerTime() {
|
|
79
|
+
return this.get(`${this.host}${TIME}`);
|
|
80
|
+
}
|
|
81
|
+
async getSamplingSimplifiedMarkets(next_cursor = INITIAL_CURSOR) {
|
|
82
|
+
return this.get(`${this.host}${GET_SAMPLING_SIMPLIFIED_MARKETS}`, {
|
|
83
|
+
params: { next_cursor },
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
async getSamplingMarkets(next_cursor = INITIAL_CURSOR) {
|
|
87
|
+
return this.get(`${this.host}${GET_SAMPLING_MARKETS}`, {
|
|
88
|
+
params: { next_cursor },
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
async getSimplifiedMarkets(next_cursor = INITIAL_CURSOR) {
|
|
92
|
+
return this.get(`${this.host}${GET_SIMPLIFIED_MARKETS}`, {
|
|
93
|
+
params: { next_cursor },
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async getMarkets(next_cursor = INITIAL_CURSOR) {
|
|
97
|
+
return this.get(`${this.host}${GET_MARKETS}`, {
|
|
98
|
+
params: { next_cursor },
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
async getMarket(conditionID) {
|
|
102
|
+
return this.get(`${this.host}${GET_MARKET}${conditionID}`);
|
|
103
|
+
}
|
|
104
|
+
async getOrderBook(tokenID) {
|
|
105
|
+
const result = await this.get(`${this.host}${GET_ORDER_BOOK}`, {
|
|
106
|
+
params: { token_id: tokenID },
|
|
107
|
+
});
|
|
108
|
+
this.updateTickSizeFromOrderBook(result);
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
async getOrderBooks(params) {
|
|
112
|
+
const results = await this.post(`${this.host}${GET_ORDER_BOOKS}`, {
|
|
113
|
+
data: params,
|
|
114
|
+
});
|
|
115
|
+
for (const book of results) {
|
|
116
|
+
this.updateTickSizeFromOrderBook(book);
|
|
117
|
+
}
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
120
|
+
async getTickSize(tokenID) {
|
|
121
|
+
const cachedAt = this.tickSizeTimestamps[tokenID];
|
|
122
|
+
if (tokenID in this.tickSizes && cachedAt && (Date.now() - cachedAt) < this.tickSizeTtlMs) {
|
|
123
|
+
return this.tickSizes[tokenID];
|
|
124
|
+
}
|
|
125
|
+
const result = await this.get(`${this.host}${GET_TICK_SIZE}`, {
|
|
126
|
+
params: { token_id: tokenID },
|
|
127
|
+
});
|
|
128
|
+
if (result.error) {
|
|
129
|
+
throw new Error(result.error);
|
|
130
|
+
}
|
|
131
|
+
this.tickSizes[tokenID] = result.minimum_tick_size.toString();
|
|
132
|
+
this.tickSizeTimestamps[tokenID] = Date.now();
|
|
133
|
+
return this.tickSizes[tokenID];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Clears the tick size cache, forcing fresh fetches on the next access.
|
|
137
|
+
* @param tokenID - If provided, only clears the cache for this token. Otherwise clears all.
|
|
138
|
+
*/
|
|
139
|
+
clearTickSizeCache(tokenID) {
|
|
140
|
+
if (tokenID !== undefined) {
|
|
141
|
+
delete this.tickSizes[tokenID];
|
|
142
|
+
delete this.tickSizeTimestamps[tokenID];
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
for (const key of Object.keys(this.tickSizes)) {
|
|
146
|
+
delete this.tickSizes[key];
|
|
147
|
+
}
|
|
148
|
+
this.tickSizeTimestamps = {};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async getNegRisk(tokenID) {
|
|
152
|
+
if (tokenID in this.negRisk) {
|
|
153
|
+
return this.negRisk[tokenID];
|
|
154
|
+
}
|
|
155
|
+
const result = await this.get(`${this.host}${GET_NEG_RISK}`, {
|
|
156
|
+
params: { token_id: tokenID },
|
|
157
|
+
});
|
|
158
|
+
if (result.error) {
|
|
159
|
+
throw new Error(result.error);
|
|
160
|
+
}
|
|
161
|
+
this.negRisk[tokenID] = result.neg_risk;
|
|
162
|
+
return this.negRisk[tokenID];
|
|
163
|
+
}
|
|
164
|
+
async getFeeRateBps(tokenID) {
|
|
165
|
+
if (tokenID in this.feeRates) {
|
|
166
|
+
return this.feeRates[tokenID];
|
|
167
|
+
}
|
|
168
|
+
const result = await this.get(`${this.host}${GET_FEE_RATE}`, {
|
|
169
|
+
params: { token_id: tokenID },
|
|
170
|
+
});
|
|
171
|
+
if (result.error) {
|
|
172
|
+
throw new Error(result.error);
|
|
173
|
+
}
|
|
174
|
+
this.feeRates[tokenID] = result.base_fee;
|
|
175
|
+
return this.feeRates[tokenID];
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Calculates the hash for the given orderbook
|
|
179
|
+
* @param orderbook
|
|
180
|
+
* @returns
|
|
181
|
+
*/
|
|
182
|
+
getOrderBookHash(orderbook) {
|
|
183
|
+
return generateOrderBookSummaryHash(orderbook);
|
|
184
|
+
}
|
|
185
|
+
async getMidpoint(tokenID) {
|
|
186
|
+
return this.get(`${this.host}${GET_MIDPOINT}`, {
|
|
187
|
+
params: { token_id: tokenID },
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
async getMidpoints(params) {
|
|
191
|
+
return this.post(`${this.host}${GET_MIDPOINTS}`, {
|
|
192
|
+
data: params,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async getPrice(tokenID, side) {
|
|
196
|
+
return this.get(`${this.host}${GET_PRICE}`, {
|
|
197
|
+
params: { token_id: tokenID, side: side },
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
async getPrices(params) {
|
|
201
|
+
return this.post(`${this.host}${GET_PRICES}`, {
|
|
202
|
+
data: params,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async getSpread(tokenID) {
|
|
206
|
+
return this.get(`${this.host}${GET_SPREAD}`, {
|
|
207
|
+
params: { token_id: tokenID },
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
async getSpreads(params) {
|
|
211
|
+
return this.post(`${this.host}${GET_SPREADS}`, {
|
|
212
|
+
data: params,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
async getLastTradePrice(tokenID) {
|
|
216
|
+
return this.get(`${this.host}${GET_LAST_TRADE_PRICE}`, {
|
|
217
|
+
params: { token_id: tokenID },
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
async getLastTradesPrices(params) {
|
|
221
|
+
return this.post(`${this.host}${GET_LAST_TRADES_PRICES}`, {
|
|
222
|
+
data: params,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
async getPricesHistory(params) {
|
|
226
|
+
return this.get(`${this.host}${GET_PRICES_HISTORY}`, {
|
|
227
|
+
params,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
// L1 Authed
|
|
231
|
+
/**
|
|
232
|
+
* Creates a new API key for a user
|
|
233
|
+
* @param nonce
|
|
234
|
+
* @returns ApiKeyCreds
|
|
235
|
+
*/
|
|
236
|
+
async createApiKey(nonce) {
|
|
237
|
+
this.canL1Auth();
|
|
238
|
+
const endpoint = `${this.host}${CREATE_API_KEY}`;
|
|
239
|
+
const headers = await createL1Headers(this.signer, this.chainId, nonce, this.useServerTime ? await this.getServerTime() : undefined);
|
|
240
|
+
return await this.post(endpoint, { headers }).then((apiKeyRaw) => {
|
|
241
|
+
const apiKey = {
|
|
242
|
+
key: apiKeyRaw.apiKey,
|
|
243
|
+
secret: apiKeyRaw.secret,
|
|
244
|
+
passphrase: apiKeyRaw.passphrase,
|
|
245
|
+
};
|
|
246
|
+
return apiKey;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Derives an existing API key for a user
|
|
251
|
+
* @param nonce
|
|
252
|
+
* @returns ApiKeyCreds
|
|
253
|
+
*/
|
|
254
|
+
async deriveApiKey(nonce) {
|
|
255
|
+
this.canL1Auth();
|
|
256
|
+
const endpoint = `${this.host}${DERIVE_API_KEY}`;
|
|
257
|
+
const headers = await createL1Headers(this.signer, this.chainId, nonce, this.useServerTime ? await this.getServerTime() : undefined);
|
|
258
|
+
return await this.get(endpoint, { headers }).then((apiKeyRaw) => {
|
|
259
|
+
const apiKey = {
|
|
260
|
+
key: apiKeyRaw.apiKey,
|
|
261
|
+
secret: apiKeyRaw.secret,
|
|
262
|
+
passphrase: apiKeyRaw.passphrase,
|
|
263
|
+
};
|
|
264
|
+
return apiKey;
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
async createOrDeriveApiKey(nonce) {
|
|
268
|
+
return this.createApiKey(nonce).then(response => {
|
|
269
|
+
if (!response.key) {
|
|
270
|
+
return this.deriveApiKey(nonce);
|
|
271
|
+
}
|
|
272
|
+
return response;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
async getApiKeys() {
|
|
276
|
+
this.canL2Auth();
|
|
277
|
+
const endpoint = GET_API_KEYS;
|
|
278
|
+
const headerArgs = {
|
|
279
|
+
method: GET,
|
|
280
|
+
requestPath: endpoint,
|
|
281
|
+
};
|
|
282
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
283
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
284
|
+
}
|
|
285
|
+
async getClosedOnlyMode() {
|
|
286
|
+
this.canL2Auth();
|
|
287
|
+
const endpoint = CLOSED_ONLY;
|
|
288
|
+
const headerArgs = {
|
|
289
|
+
method: GET,
|
|
290
|
+
requestPath: endpoint,
|
|
291
|
+
};
|
|
292
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
293
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
294
|
+
}
|
|
295
|
+
async deleteApiKey() {
|
|
296
|
+
this.canL2Auth();
|
|
297
|
+
const endpoint = DELETE_API_KEY;
|
|
298
|
+
const headerArgs = {
|
|
299
|
+
method: DELETE,
|
|
300
|
+
requestPath: endpoint,
|
|
301
|
+
};
|
|
302
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
303
|
+
return this.del(`${this.host}${endpoint}`, { headers });
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Creates a new readonly API key for a user
|
|
307
|
+
* @returns ReadonlyApiKeyResponse
|
|
308
|
+
*/
|
|
309
|
+
async createReadonlyApiKey() {
|
|
310
|
+
this.canL2Auth();
|
|
311
|
+
const endpoint = CREATE_READONLY_API_KEY;
|
|
312
|
+
const headerArgs = {
|
|
313
|
+
method: POST,
|
|
314
|
+
requestPath: endpoint,
|
|
315
|
+
};
|
|
316
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
317
|
+
return this.post(`${this.host}${endpoint}`, { headers });
|
|
318
|
+
}
|
|
319
|
+
async getReadonlyApiKeys() {
|
|
320
|
+
this.canL2Auth();
|
|
321
|
+
const endpoint = GET_READONLY_API_KEYS;
|
|
322
|
+
const headerArgs = {
|
|
323
|
+
method: GET,
|
|
324
|
+
requestPath: endpoint,
|
|
325
|
+
};
|
|
326
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
327
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Deletes a readonly API key for a user
|
|
331
|
+
* @param key The readonly API key to delete
|
|
332
|
+
* @returns boolean
|
|
333
|
+
*/
|
|
334
|
+
async deleteReadonlyApiKey(key) {
|
|
335
|
+
this.canL2Auth();
|
|
336
|
+
const endpoint = DELETE_READONLY_API_KEY;
|
|
337
|
+
const payload = { key };
|
|
338
|
+
const headerArgs = {
|
|
339
|
+
method: DELETE,
|
|
340
|
+
requestPath: endpoint,
|
|
341
|
+
body: JSON.stringify(payload),
|
|
342
|
+
};
|
|
343
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
344
|
+
return this.del(`${this.host}${endpoint}`, { headers, data: payload });
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Validates a readonly API key for a given address
|
|
348
|
+
* @param address The wallet address
|
|
349
|
+
* @param key The readonly API key to validate
|
|
350
|
+
* @returns string
|
|
351
|
+
*/
|
|
352
|
+
async validateReadonlyApiKey(address, key) {
|
|
353
|
+
return this.get(`${this.host}${VALIDATE_READONLY_API_KEY}`, {
|
|
354
|
+
params: { address, key },
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
async getOrder(orderID) {
|
|
358
|
+
this.canL2Auth();
|
|
359
|
+
const endpoint = `${GET_ORDER}${orderID}`;
|
|
360
|
+
const headerArgs = {
|
|
361
|
+
method: GET,
|
|
362
|
+
requestPath: endpoint,
|
|
363
|
+
};
|
|
364
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
365
|
+
// builders flow
|
|
366
|
+
if (this.canBuilderAuth()) {
|
|
367
|
+
const builderHeaders = await this._generateBuilderHeaders(headers, headerArgs);
|
|
368
|
+
if (builderHeaders !== undefined) {
|
|
369
|
+
return this.get(`${this.host}${endpoint}`, { headers: builderHeaders });
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
373
|
+
}
|
|
374
|
+
async getTrades(params, only_first_page = false, next_cursor) {
|
|
375
|
+
this.canL2Auth();
|
|
376
|
+
const endpoint = GET_TRADES;
|
|
377
|
+
const headerArgs = {
|
|
378
|
+
method: GET,
|
|
379
|
+
requestPath: endpoint,
|
|
380
|
+
};
|
|
381
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
382
|
+
let results = [];
|
|
383
|
+
next_cursor = next_cursor || INITIAL_CURSOR;
|
|
384
|
+
while (next_cursor != END_CURSOR && (next_cursor === INITIAL_CURSOR || !only_first_page)) {
|
|
385
|
+
const _params = {
|
|
386
|
+
...params,
|
|
387
|
+
next_cursor,
|
|
388
|
+
};
|
|
389
|
+
const response = await this.get(`${this.host}${endpoint}`, {
|
|
390
|
+
headers,
|
|
391
|
+
params: _params,
|
|
392
|
+
});
|
|
393
|
+
next_cursor = response.next_cursor;
|
|
394
|
+
results = [...results, ...response.data];
|
|
395
|
+
}
|
|
396
|
+
return results;
|
|
397
|
+
}
|
|
398
|
+
async getTradesPaginated(params, next_cursor) {
|
|
399
|
+
this.canL2Auth();
|
|
400
|
+
const endpoint = GET_TRADES;
|
|
401
|
+
const headerArgs = {
|
|
402
|
+
method: GET,
|
|
403
|
+
requestPath: endpoint,
|
|
404
|
+
};
|
|
405
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
406
|
+
next_cursor = next_cursor || INITIAL_CURSOR;
|
|
407
|
+
const _params = { ...params, next_cursor };
|
|
408
|
+
const { data, ...rest } = await this.get(`${this.host}${endpoint}`, {
|
|
409
|
+
headers,
|
|
410
|
+
params: _params,
|
|
411
|
+
});
|
|
412
|
+
return { trades: Array.isArray(data) ? [...data] : [], ...rest };
|
|
413
|
+
}
|
|
414
|
+
async getBuilderTrades(params, next_cursor) {
|
|
415
|
+
this.mustBuilderAuth();
|
|
416
|
+
const endpoint = GET_BUILDER_TRADES;
|
|
417
|
+
const headerArgs = {
|
|
418
|
+
method: GET,
|
|
419
|
+
requestPath: endpoint,
|
|
420
|
+
};
|
|
421
|
+
const headers = await this._getBuilderHeaders(headerArgs.method, headerArgs.requestPath);
|
|
422
|
+
if (headers == undefined) {
|
|
423
|
+
throw BUILDER_AUTH_FAILED;
|
|
424
|
+
}
|
|
425
|
+
next_cursor = next_cursor || INITIAL_CURSOR;
|
|
426
|
+
const _params = { ...params, next_cursor };
|
|
427
|
+
const { data, ...rest } = await this.get(`${this.host}${endpoint}`, {
|
|
428
|
+
headers,
|
|
429
|
+
params: _params,
|
|
430
|
+
});
|
|
431
|
+
return { trades: Array.isArray(data) ? [...data] : [], ...rest };
|
|
432
|
+
}
|
|
433
|
+
async getNotifications() {
|
|
434
|
+
this.canL2Auth();
|
|
435
|
+
const endpoint = GET_NOTIFICATIONS;
|
|
436
|
+
const headerArgs = {
|
|
437
|
+
method: GET,
|
|
438
|
+
requestPath: endpoint,
|
|
439
|
+
};
|
|
440
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
441
|
+
return this.get(`${this.host}${endpoint}`, {
|
|
442
|
+
headers,
|
|
443
|
+
params: { signature_type: this.orderBuilder.signatureType },
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
async dropNotifications(params) {
|
|
447
|
+
this.canL2Auth();
|
|
448
|
+
const endpoint = DROP_NOTIFICATIONS;
|
|
449
|
+
const l2HeaderArgs = {
|
|
450
|
+
method: DELETE,
|
|
451
|
+
requestPath: endpoint,
|
|
452
|
+
};
|
|
453
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
454
|
+
return this.del(`${this.host}${endpoint}`, {
|
|
455
|
+
headers,
|
|
456
|
+
params: parseDropNotificationParams(params),
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
async getBalanceAllowance(params) {
|
|
460
|
+
this.canL2Auth();
|
|
461
|
+
const endpoint = GET_BALANCE_ALLOWANCE;
|
|
462
|
+
const headerArgs = {
|
|
463
|
+
method: GET,
|
|
464
|
+
requestPath: endpoint,
|
|
465
|
+
};
|
|
466
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
467
|
+
const _params = {
|
|
468
|
+
...params,
|
|
469
|
+
signature_type: this.orderBuilder.signatureType,
|
|
470
|
+
};
|
|
471
|
+
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
|
|
472
|
+
}
|
|
473
|
+
async updateBalanceAllowance(params) {
|
|
474
|
+
this.canL2Auth();
|
|
475
|
+
const endpoint = UPDATE_BALANCE_ALLOWANCE;
|
|
476
|
+
const headerArgs = {
|
|
477
|
+
method: GET,
|
|
478
|
+
requestPath: endpoint,
|
|
479
|
+
};
|
|
480
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
481
|
+
const _params = {
|
|
482
|
+
...params,
|
|
483
|
+
signature_type: this.orderBuilder.signatureType,
|
|
484
|
+
};
|
|
485
|
+
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
|
|
486
|
+
}
|
|
487
|
+
async createOrder(userOrder, options) {
|
|
488
|
+
this.canL1Auth();
|
|
489
|
+
const { tokenID } = userOrder;
|
|
490
|
+
const tickSize = await this._resolveTickSize(tokenID, options?.tickSize);
|
|
491
|
+
const feeRateBps = await this._resolveFeeRateBps(tokenID, userOrder.feeRateBps);
|
|
492
|
+
userOrder.feeRateBps = feeRateBps;
|
|
493
|
+
if (!priceValid(userOrder.price, tickSize)) {
|
|
494
|
+
throw new Error(`invalid price (${userOrder.price}), min: ${parseFloat(tickSize)} - max: ${1 - parseFloat(tickSize)}`);
|
|
495
|
+
}
|
|
496
|
+
const negRisk = options?.negRisk ?? (await this.getNegRisk(tokenID));
|
|
497
|
+
return this.orderBuilder.buildOrder(userOrder, {
|
|
498
|
+
tickSize,
|
|
499
|
+
negRisk,
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
async createMarketOrder(userMarketOrder, options) {
|
|
503
|
+
this.canL1Auth();
|
|
504
|
+
const { tokenID } = userMarketOrder;
|
|
505
|
+
const tickSize = await this._resolveTickSize(tokenID, options?.tickSize);
|
|
506
|
+
const feeRateBps = await this._resolveFeeRateBps(tokenID, userMarketOrder.feeRateBps);
|
|
507
|
+
userMarketOrder.feeRateBps = feeRateBps;
|
|
508
|
+
if (!userMarketOrder.price) {
|
|
509
|
+
userMarketOrder.price = await this.calculateMarketPrice(tokenID, userMarketOrder.side, userMarketOrder.amount, userMarketOrder.orderType);
|
|
510
|
+
}
|
|
511
|
+
if (!priceValid(userMarketOrder.price, tickSize)) {
|
|
512
|
+
throw new Error(`invalid price (${userMarketOrder.price}), min: ${parseFloat(tickSize)} - max: ${1 - parseFloat(tickSize)}`);
|
|
513
|
+
}
|
|
514
|
+
const negRisk = options?.negRisk ?? (await this.getNegRisk(tokenID));
|
|
515
|
+
return this.orderBuilder.buildMarketOrder(userMarketOrder, {
|
|
516
|
+
tickSize,
|
|
517
|
+
negRisk,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
async createAndPostOrder(userOrder, options, orderType = OrderType.GTC, deferExec = false, postOnly = false) {
|
|
521
|
+
const order = await this.createOrder(userOrder, options);
|
|
522
|
+
return this.postOrder(order, orderType, deferExec, postOnly);
|
|
523
|
+
}
|
|
524
|
+
async createAndPostMarketOrder(userMarketOrder, options, orderType = OrderType.FOK, deferExec = false) {
|
|
525
|
+
const order = await this.createMarketOrder(userMarketOrder, options);
|
|
526
|
+
return this.postOrder(order, orderType, deferExec);
|
|
527
|
+
}
|
|
528
|
+
async getOpenOrders(params, only_first_page = false, next_cursor) {
|
|
529
|
+
this.canL2Auth();
|
|
530
|
+
const endpoint = GET_OPEN_ORDERS;
|
|
531
|
+
const l2HeaderArgs = {
|
|
532
|
+
method: GET,
|
|
533
|
+
requestPath: endpoint,
|
|
534
|
+
};
|
|
535
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
536
|
+
// builders flow
|
|
537
|
+
let requestHeaders = headers;
|
|
538
|
+
if (this.canBuilderAuth()) {
|
|
539
|
+
const builderHeaders = await this._generateBuilderHeaders(headers, l2HeaderArgs);
|
|
540
|
+
if (builderHeaders !== undefined) {
|
|
541
|
+
requestHeaders = builderHeaders;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
let results = [];
|
|
545
|
+
next_cursor = next_cursor || INITIAL_CURSOR;
|
|
546
|
+
while (next_cursor != END_CURSOR && (next_cursor === INITIAL_CURSOR || !only_first_page)) {
|
|
547
|
+
const _params = {
|
|
548
|
+
...params,
|
|
549
|
+
next_cursor,
|
|
550
|
+
};
|
|
551
|
+
const response = await this.get(`${this.host}${endpoint}`, {
|
|
552
|
+
headers: requestHeaders,
|
|
553
|
+
params: _params,
|
|
554
|
+
});
|
|
555
|
+
next_cursor = response.next_cursor;
|
|
556
|
+
results = [...results, ...response.data];
|
|
557
|
+
}
|
|
558
|
+
return results;
|
|
559
|
+
}
|
|
560
|
+
async postOrder(order, orderType = OrderType.GTC, deferExec = false, postOnly = false) {
|
|
561
|
+
this.canL2Auth();
|
|
562
|
+
const endpoint = POST_ORDER;
|
|
563
|
+
const orderPayload = orderToJson(order, this.creds?.key || "", orderType, deferExec, postOnly);
|
|
564
|
+
const l2HeaderArgs = {
|
|
565
|
+
method: POST,
|
|
566
|
+
requestPath: endpoint,
|
|
567
|
+
body: JSON.stringify(orderPayload),
|
|
568
|
+
};
|
|
569
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
570
|
+
// builders flow
|
|
571
|
+
if (this.canBuilderAuth()) {
|
|
572
|
+
const builderHeaders = await this._generateBuilderHeaders(headers, l2HeaderArgs);
|
|
573
|
+
if (builderHeaders !== undefined) {
|
|
574
|
+
return this.post(`${this.host}${endpoint}`, { headers: builderHeaders, data: orderPayload });
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return this.post(`${this.host}${endpoint}`, { headers, data: orderPayload });
|
|
578
|
+
}
|
|
579
|
+
async postOrders(args, deferExec = false, defaultPostOnly = false) {
|
|
580
|
+
this.canL2Auth();
|
|
581
|
+
const endpoint = POST_ORDERS;
|
|
582
|
+
const ordersPayload = [];
|
|
583
|
+
for (const { order, orderType, postOnly: orderPostOnly } of args) {
|
|
584
|
+
const orderPayload = orderToJson(order, this.creds?.key || "", orderType, deferExec, orderPostOnly ?? defaultPostOnly);
|
|
585
|
+
ordersPayload.push(orderPayload);
|
|
586
|
+
}
|
|
587
|
+
const l2HeaderArgs = {
|
|
588
|
+
method: POST,
|
|
589
|
+
requestPath: endpoint,
|
|
590
|
+
body: JSON.stringify(ordersPayload),
|
|
591
|
+
};
|
|
592
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
593
|
+
// builders flow
|
|
594
|
+
if (this.canBuilderAuth()) {
|
|
595
|
+
const builderHeaders = await this._generateBuilderHeaders(headers, l2HeaderArgs);
|
|
596
|
+
if (builderHeaders !== undefined) {
|
|
597
|
+
return this.post(`${this.host}${endpoint}`, { headers: builderHeaders, data: ordersPayload });
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return this.post(`${this.host}${endpoint}`, { headers, data: ordersPayload });
|
|
601
|
+
}
|
|
602
|
+
async cancelOrder(payload) {
|
|
603
|
+
this.canL2Auth();
|
|
604
|
+
const endpoint = CANCEL_ORDER;
|
|
605
|
+
const l2HeaderArgs = {
|
|
606
|
+
method: DELETE,
|
|
607
|
+
requestPath: endpoint,
|
|
608
|
+
body: JSON.stringify(payload),
|
|
609
|
+
};
|
|
610
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
611
|
+
return this.del(`${this.host}${endpoint}`, { headers, data: payload });
|
|
612
|
+
}
|
|
613
|
+
async cancelOrders(ordersHashes) {
|
|
614
|
+
this.canL2Auth();
|
|
615
|
+
const endpoint = CANCEL_ORDERS;
|
|
616
|
+
const l2HeaderArgs = {
|
|
617
|
+
method: DELETE,
|
|
618
|
+
requestPath: endpoint,
|
|
619
|
+
body: JSON.stringify(ordersHashes),
|
|
620
|
+
};
|
|
621
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
622
|
+
return this.del(`${this.host}${endpoint}`, { headers, data: ordersHashes });
|
|
623
|
+
}
|
|
624
|
+
async cancelAll() {
|
|
625
|
+
this.canL2Auth();
|
|
626
|
+
const endpoint = CANCEL_ALL;
|
|
627
|
+
const l2HeaderArgs = {
|
|
628
|
+
method: DELETE,
|
|
629
|
+
requestPath: endpoint,
|
|
630
|
+
};
|
|
631
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
632
|
+
return this.del(`${this.host}${endpoint}`, { headers });
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Sends a heartbeat to the server to keep the session active.
|
|
636
|
+
*
|
|
637
|
+
* If heartbeats are started and one isn't sent within 10s, all orders will be cancelled.
|
|
638
|
+
* Requires Level 2 authentication.
|
|
639
|
+
*
|
|
640
|
+
* Pass the previously returned `heartbeat_id` to chain heartbeats.
|
|
641
|
+
* Pass `undefined`/`null` to start a new heartbeat chain.
|
|
642
|
+
*/
|
|
643
|
+
async postHeartbeat(heartbeatId) {
|
|
644
|
+
this.canL2Auth();
|
|
645
|
+
const endpoint = POST_HEARTBEAT;
|
|
646
|
+
const bodyObj = { heartbeat_id: heartbeatId ?? null };
|
|
647
|
+
const serialized = JSON.stringify(bodyObj);
|
|
648
|
+
const l2HeaderArgs = {
|
|
649
|
+
method: POST,
|
|
650
|
+
requestPath: endpoint,
|
|
651
|
+
body: serialized,
|
|
652
|
+
};
|
|
653
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
654
|
+
return this.post(`${this.host}${endpoint}`, { headers, data: serialized });
|
|
655
|
+
}
|
|
656
|
+
async cancelMarketOrders(payload) {
|
|
657
|
+
this.canL2Auth();
|
|
658
|
+
const endpoint = CANCEL_MARKET_ORDERS;
|
|
659
|
+
const l2HeaderArgs = {
|
|
660
|
+
method: DELETE,
|
|
661
|
+
requestPath: endpoint,
|
|
662
|
+
body: JSON.stringify(payload),
|
|
663
|
+
};
|
|
664
|
+
const headers = await createL2Headers(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
665
|
+
return this.del(`${this.host}${endpoint}`, { headers, data: payload });
|
|
666
|
+
}
|
|
667
|
+
async isOrderScoring(params) {
|
|
668
|
+
this.canL2Auth();
|
|
669
|
+
const endpoint = IS_ORDER_SCORING;
|
|
670
|
+
const headerArgs = {
|
|
671
|
+
method: GET,
|
|
672
|
+
requestPath: endpoint,
|
|
673
|
+
};
|
|
674
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
675
|
+
return this.get(`${this.host}${endpoint}`, { headers, params });
|
|
676
|
+
}
|
|
677
|
+
async areOrdersScoring(params) {
|
|
678
|
+
this.canL2Auth();
|
|
679
|
+
const endpoint = ARE_ORDERS_SCORING;
|
|
680
|
+
const payload = JSON.stringify(params?.orderIds);
|
|
681
|
+
const headerArgs = {
|
|
682
|
+
method: POST,
|
|
683
|
+
requestPath: endpoint,
|
|
684
|
+
body: payload,
|
|
685
|
+
};
|
|
686
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
687
|
+
return this.post(`${this.host}${endpoint}`, {
|
|
688
|
+
headers,
|
|
689
|
+
data: payload,
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
// Rewards
|
|
693
|
+
async getEarningsForUserForDay(date) {
|
|
694
|
+
this.canL2Auth();
|
|
695
|
+
const endpoint = GET_EARNINGS_FOR_USER_FOR_DAY;
|
|
696
|
+
const headerArgs = {
|
|
697
|
+
method: GET,
|
|
698
|
+
requestPath: endpoint,
|
|
699
|
+
};
|
|
700
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
701
|
+
let results = [];
|
|
702
|
+
let next_cursor = INITIAL_CURSOR;
|
|
703
|
+
while (next_cursor != END_CURSOR) {
|
|
704
|
+
const params = {
|
|
705
|
+
date,
|
|
706
|
+
signature_type: this.orderBuilder.signatureType,
|
|
707
|
+
next_cursor,
|
|
708
|
+
};
|
|
709
|
+
const response = await this.get(`${this.host}${endpoint}`, {
|
|
710
|
+
headers,
|
|
711
|
+
params,
|
|
712
|
+
});
|
|
713
|
+
next_cursor = response.next_cursor;
|
|
714
|
+
results = [...results, ...response.data];
|
|
715
|
+
}
|
|
716
|
+
return results;
|
|
717
|
+
}
|
|
718
|
+
async getTotalEarningsForUserForDay(date) {
|
|
719
|
+
this.canL2Auth();
|
|
720
|
+
const endpoint = GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY;
|
|
721
|
+
const headerArgs = {
|
|
722
|
+
method: GET,
|
|
723
|
+
requestPath: endpoint,
|
|
724
|
+
};
|
|
725
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
726
|
+
const params = {
|
|
727
|
+
date,
|
|
728
|
+
signature_type: this.orderBuilder.signatureType,
|
|
729
|
+
};
|
|
730
|
+
return await this.get(`${this.host}${endpoint}`, {
|
|
731
|
+
headers,
|
|
732
|
+
params,
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
async getUserEarningsAndMarketsConfig(date, order_by = "", position = "", no_competition = false) {
|
|
736
|
+
this.canL2Auth();
|
|
737
|
+
const endpoint = GET_REWARDS_EARNINGS_PERCENTAGES;
|
|
738
|
+
const headerArgs = {
|
|
739
|
+
method: GET,
|
|
740
|
+
requestPath: endpoint,
|
|
741
|
+
};
|
|
742
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
743
|
+
let results = [];
|
|
744
|
+
let next_cursor = INITIAL_CURSOR;
|
|
745
|
+
while (next_cursor != END_CURSOR) {
|
|
746
|
+
const params = {
|
|
747
|
+
date,
|
|
748
|
+
signature_type: this.orderBuilder.signatureType,
|
|
749
|
+
next_cursor,
|
|
750
|
+
order_by,
|
|
751
|
+
position,
|
|
752
|
+
no_competition,
|
|
753
|
+
};
|
|
754
|
+
const response = await this.get(`${this.host}${endpoint}`, {
|
|
755
|
+
headers,
|
|
756
|
+
params,
|
|
757
|
+
});
|
|
758
|
+
next_cursor = response.next_cursor;
|
|
759
|
+
results = [...results, ...response.data];
|
|
760
|
+
}
|
|
761
|
+
return results;
|
|
762
|
+
}
|
|
763
|
+
async getRewardPercentages() {
|
|
764
|
+
this.canL2Auth();
|
|
765
|
+
const endpoint = GET_LIQUIDITY_REWARD_PERCENTAGES;
|
|
766
|
+
const headerArgs = {
|
|
767
|
+
method: GET,
|
|
768
|
+
requestPath: endpoint,
|
|
769
|
+
};
|
|
770
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
771
|
+
const _params = {
|
|
772
|
+
signature_type: this.orderBuilder.signatureType,
|
|
773
|
+
};
|
|
774
|
+
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
|
|
775
|
+
}
|
|
776
|
+
async getCurrentRewards() {
|
|
777
|
+
let results = [];
|
|
778
|
+
let next_cursor = INITIAL_CURSOR;
|
|
779
|
+
while (next_cursor != END_CURSOR) {
|
|
780
|
+
const response = await this.get(`${this.host}${GET_REWARDS_MARKETS_CURRENT}`, {
|
|
781
|
+
params: { next_cursor },
|
|
782
|
+
});
|
|
783
|
+
next_cursor = response.next_cursor;
|
|
784
|
+
results = [...results, ...response.data];
|
|
785
|
+
}
|
|
786
|
+
return results;
|
|
787
|
+
}
|
|
788
|
+
async getRawRewardsForMarket(conditionId) {
|
|
789
|
+
let results = [];
|
|
790
|
+
let next_cursor = INITIAL_CURSOR;
|
|
791
|
+
while (next_cursor != END_CURSOR) {
|
|
792
|
+
const response = await this.get(`${this.host}${GET_REWARDS_MARKETS}${conditionId}`, {
|
|
793
|
+
params: { next_cursor },
|
|
794
|
+
});
|
|
795
|
+
next_cursor = response.next_cursor;
|
|
796
|
+
results = [...results, ...response.data];
|
|
797
|
+
}
|
|
798
|
+
return results;
|
|
799
|
+
}
|
|
800
|
+
async getMarketTradesEvents(conditionID) {
|
|
801
|
+
return this.get(`${this.host}${GET_MARKET_TRADES_EVENTS}${conditionID}`);
|
|
802
|
+
}
|
|
803
|
+
async calculateMarketPrice(tokenID, side, amount, orderType = OrderType.FOK) {
|
|
804
|
+
const book = await this.getOrderBook(tokenID);
|
|
805
|
+
if (!book) {
|
|
806
|
+
throw new Error("no orderbook");
|
|
807
|
+
}
|
|
808
|
+
if (side === Side.BUY) {
|
|
809
|
+
if (!book.asks) {
|
|
810
|
+
throw new Error("no match");
|
|
811
|
+
}
|
|
812
|
+
return calculateBuyMarketPrice(book.asks, amount, orderType);
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
if (!book.bids) {
|
|
816
|
+
throw new Error("no match");
|
|
817
|
+
}
|
|
818
|
+
return calculateSellMarketPrice(book.bids, amount, orderType);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
async createBuilderApiKey() {
|
|
822
|
+
this.canL2Auth();
|
|
823
|
+
const endpoint = CREATE_BUILDER_API_KEY;
|
|
824
|
+
const headerArgs = {
|
|
825
|
+
method: POST,
|
|
826
|
+
requestPath: endpoint,
|
|
827
|
+
};
|
|
828
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
829
|
+
return this.post(`${this.host}${endpoint}`, { headers });
|
|
830
|
+
}
|
|
831
|
+
async getBuilderApiKeys() {
|
|
832
|
+
this.canL2Auth();
|
|
833
|
+
const endpoint = GET_BUILDER_API_KEYS;
|
|
834
|
+
const headerArgs = {
|
|
835
|
+
method: GET,
|
|
836
|
+
requestPath: endpoint,
|
|
837
|
+
};
|
|
838
|
+
const headers = await createL2Headers(this.signer, this.creds, headerArgs, this.useServerTime ? await this.getServerTime() : undefined);
|
|
839
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
840
|
+
}
|
|
841
|
+
async revokeBuilderApiKey() {
|
|
842
|
+
this.mustBuilderAuth();
|
|
843
|
+
const endpoint = REVOKE_BUILDER_API_KEY;
|
|
844
|
+
const headerArgs = {
|
|
845
|
+
method: DELETE,
|
|
846
|
+
requestPath: endpoint,
|
|
847
|
+
};
|
|
848
|
+
const headers = await this._getBuilderHeaders(headerArgs.method, headerArgs.requestPath);
|
|
849
|
+
if (headers == undefined) {
|
|
850
|
+
throw BUILDER_AUTH_FAILED;
|
|
851
|
+
}
|
|
852
|
+
return this.del(`${this.host}${endpoint}`, { headers });
|
|
853
|
+
}
|
|
854
|
+
async _resolveTickSize(tokenID, tickSize) {
|
|
855
|
+
const minTickSize = await this.getTickSize(tokenID);
|
|
856
|
+
if (tickSize) {
|
|
857
|
+
if (isTickSizeSmaller(tickSize, minTickSize)) {
|
|
858
|
+
throw new Error(`invalid tick size (${tickSize}), minimum for the market is ${minTickSize}`);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
else {
|
|
862
|
+
tickSize = minTickSize;
|
|
863
|
+
}
|
|
864
|
+
return tickSize;
|
|
865
|
+
}
|
|
866
|
+
// http methods
|
|
867
|
+
async get(endpoint, options) {
|
|
868
|
+
const result = await get(endpoint, {
|
|
869
|
+
...options,
|
|
870
|
+
params: { ...options?.params, geo_block_token: this.geoBlockToken },
|
|
871
|
+
});
|
|
872
|
+
return this.throwIfError(result);
|
|
873
|
+
}
|
|
874
|
+
async post(endpoint, options) {
|
|
875
|
+
const result = await post(endpoint, {
|
|
876
|
+
...options,
|
|
877
|
+
params: { ...options?.params, geo_block_token: this.geoBlockToken },
|
|
878
|
+
}, this.retryOnError);
|
|
879
|
+
return this.throwIfError(result);
|
|
880
|
+
}
|
|
881
|
+
async put(endpoint, options) {
|
|
882
|
+
const result = await put(endpoint, {
|
|
883
|
+
...options,
|
|
884
|
+
params: { ...options?.params, geo_block_token: this.geoBlockToken },
|
|
885
|
+
});
|
|
886
|
+
return this.throwIfError(result);
|
|
887
|
+
}
|
|
888
|
+
async del(endpoint, options) {
|
|
889
|
+
const result = await del(endpoint, {
|
|
890
|
+
...options,
|
|
891
|
+
params: { ...options?.params, geo_block_token: this.geoBlockToken },
|
|
892
|
+
});
|
|
893
|
+
return this.throwIfError(result);
|
|
894
|
+
}
|
|
895
|
+
throwIfError(result) {
|
|
896
|
+
if (this.throwOnError && result && typeof result === "object" && "error" in result) {
|
|
897
|
+
const msg = typeof result.error === "string" ? result.error : JSON.stringify(result.error);
|
|
898
|
+
throw new ApiError(msg, result.status, result);
|
|
899
|
+
}
|
|
900
|
+
return result;
|
|
901
|
+
}
|
|
902
|
+
canL1Auth() {
|
|
903
|
+
if (this.signer === undefined) {
|
|
904
|
+
throw L1_AUTH_UNAVAILABLE_ERROR;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
canL2Auth() {
|
|
908
|
+
if (this.signer === undefined) {
|
|
909
|
+
throw L1_AUTH_UNAVAILABLE_ERROR;
|
|
910
|
+
}
|
|
911
|
+
if (this.creds === undefined) {
|
|
912
|
+
throw L2_AUTH_NOT_AVAILABLE;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
mustBuilderAuth() {
|
|
916
|
+
if (!this.canBuilderAuth()) {
|
|
917
|
+
throw BUILDER_AUTH_NOT_AVAILABLE;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
canBuilderAuth() {
|
|
921
|
+
return (this.builderConfig != undefined && this.builderConfig.isValid());
|
|
922
|
+
}
|
|
923
|
+
async _resolveFeeRateBps(tokenID, userFeeRateBps) {
|
|
924
|
+
const marketFeeRateBps = await this.getFeeRateBps(tokenID);
|
|
925
|
+
if (marketFeeRateBps > 0 && userFeeRateBps != undefined && userFeeRateBps != marketFeeRateBps) {
|
|
926
|
+
throw new Error(`invalid user provided fee rate: ${userFeeRateBps}, fee rate for the market must be ${marketFeeRateBps}`);
|
|
927
|
+
}
|
|
928
|
+
return marketFeeRateBps;
|
|
929
|
+
}
|
|
930
|
+
async _generateBuilderHeaders(headers, headerArgs) {
|
|
931
|
+
if (this.builderConfig !== undefined) {
|
|
932
|
+
const builderHeaders = await this._getBuilderHeaders(headerArgs.method, headerArgs.requestPath, headerArgs.body);
|
|
933
|
+
if (builderHeaders == undefined) {
|
|
934
|
+
return undefined;
|
|
935
|
+
}
|
|
936
|
+
return injectBuilderHeaders(headers, builderHeaders);
|
|
937
|
+
}
|
|
938
|
+
return undefined;
|
|
939
|
+
}
|
|
940
|
+
async _getBuilderHeaders(method, path, body) {
|
|
941
|
+
return this.builderConfig.generateBuilderHeaders(method, path, body);
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Opportunistically updates the tick size cache from an order book response.
|
|
945
|
+
*/
|
|
946
|
+
updateTickSizeFromOrderBook(book) {
|
|
947
|
+
if (book?.asset_id && book?.tick_size) {
|
|
948
|
+
const tickSize = book.tick_size;
|
|
949
|
+
this.tickSizes[book.asset_id] = tickSize;
|
|
950
|
+
this.tickSizeTimestamps[book.asset_id] = Date.now();
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
//# sourceMappingURL=client.js.map
|