@polymarket/clob-client-v2 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/client.d.ts +125 -0
- package/dist/client.js +999 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +14 -0
- package/dist/config.js +37 -0
- package/dist/config.js.map +1 -0
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +13 -0
- package/dist/constants.js.map +1 -0
- package/dist/endpoints.d.ts +52 -0
- package/dist/endpoints.js +68 -0
- package/dist/endpoints.js.map +1 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +6 -0
- package/dist/errors.js.map +1 -0
- package/dist/headers/index.d.ts +5 -0
- package/dist/headers/index.js +43 -0
- package/dist/headers/index.js.map +1 -0
- package/dist/http-helpers/index.d.ts +18 -0
- package/dist/http-helpers/index.js +112 -0
- package/dist/http-helpers/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/order-builder/helpers/buildMarketOrderCreationArgs.d.ts +6 -0
- package/dist/order-builder/helpers/buildMarketOrderCreationArgs.js +32 -0
- package/dist/order-builder/helpers/buildMarketOrderCreationArgs.js.map +1 -0
- package/dist/order-builder/helpers/buildOrder.d.ts +15 -0
- package/dist/order-builder/helpers/buildOrder.js +36 -0
- package/dist/order-builder/helpers/buildOrder.js.map +1 -0
- package/dist/order-builder/helpers/buildOrderCreationArgs.d.ts +6 -0
- package/dist/order-builder/helpers/buildOrderCreationArgs.js +32 -0
- package/dist/order-builder/helpers/buildOrderCreationArgs.js.map +1 -0
- package/dist/order-builder/helpers/calculateBuyMarketPrice.d.ts +8 -0
- package/dist/order-builder/helpers/calculateBuyMarketPrice.js +38 -0
- package/dist/order-builder/helpers/calculateBuyMarketPrice.js.map +1 -0
- package/dist/order-builder/helpers/calculateSellMarketPrice.d.ts +8 -0
- package/dist/order-builder/helpers/calculateSellMarketPrice.js +38 -0
- package/dist/order-builder/helpers/calculateSellMarketPrice.js.map +1 -0
- package/dist/order-builder/helpers/createMarketOrder.d.ts +6 -0
- package/dist/order-builder/helpers/createMarketOrder.js +40 -0
- package/dist/order-builder/helpers/createMarketOrder.js.map +1 -0
- package/dist/order-builder/helpers/createOrder.d.ts +6 -0
- package/dist/order-builder/helpers/createOrder.js +40 -0
- package/dist/order-builder/helpers/createOrder.js.map +1 -0
- package/dist/order-builder/helpers/getMarketOrderRawAmounts.d.ts +6 -0
- package/dist/order-builder/helpers/getMarketOrderRawAmounts.js +41 -0
- package/dist/order-builder/helpers/getMarketOrderRawAmounts.js.map +1 -0
- package/dist/order-builder/helpers/getOrderRawAmounts.d.ts +6 -0
- package/dist/order-builder/helpers/getOrderRawAmounts.js +41 -0
- package/dist/order-builder/helpers/getOrderRawAmounts.js.map +1 -0
- package/dist/order-builder/helpers/index.d.ts +10 -0
- package/dist/order-builder/helpers/index.js +24 -0
- package/dist/order-builder/helpers/index.js.map +1 -0
- package/dist/order-builder/helpers/roundingConfig.d.ts +2 -0
- package/dist/order-builder/helpers/roundingConfig.js +26 -0
- package/dist/order-builder/helpers/roundingConfig.js.map +1 -0
- package/dist/order-builder/index.d.ts +1 -0
- package/dist/order-builder/index.js +5 -0
- package/dist/order-builder/index.js.map +1 -0
- package/dist/order-builder/orderBuilder.d.ts +29 -0
- package/dist/order-builder/orderBuilder.js +47 -0
- package/dist/order-builder/orderBuilder.js.map +1 -0
- package/dist/order-utils/abi/ExchangeV1.d.ts +1444 -0
- package/dist/order-utils/abi/ExchangeV1.js +1856 -0
- package/dist/order-utils/abi/ExchangeV1.js.map +1 -0
- package/dist/order-utils/abi/ExchangeV2.d.ts +1116 -0
- package/dist/order-utils/abi/ExchangeV2.js +1433 -0
- package/dist/order-utils/abi/ExchangeV2.js.map +1 -0
- package/dist/order-utils/exchangeOrderBuilderV1.d.ts +43 -0
- package/dist/order-utils/exchangeOrderBuilderV1.js +143 -0
- package/dist/order-utils/exchangeOrderBuilderV1.js.map +1 -0
- package/dist/order-utils/exchangeOrderBuilderV2.d.ts +43 -0
- package/dist/order-utils/exchangeOrderBuilderV2.js +114 -0
- package/dist/order-utils/exchangeOrderBuilderV2.js.map +1 -0
- package/dist/order-utils/index.d.ts +8 -0
- package/dist/order-utils/index.js +13 -0
- package/dist/order-utils/index.js.map +1 -0
- package/dist/order-utils/model/abi.d.ts +21 -0
- package/dist/order-utils/model/abi.js +3 -0
- package/dist/order-utils/model/abi.js.map +1 -0
- package/dist/order-utils/model/ctfExchangeV1TypedData.d.ts +6 -0
- package/dist/order-utils/model/ctfExchangeV1TypedData.js +20 -0
- package/dist/order-utils/model/ctfExchangeV1TypedData.js.map +1 -0
- package/dist/order-utils/model/ctfExchangeV2TypedData.d.ts +6 -0
- package/dist/order-utils/model/ctfExchangeV2TypedData.js +19 -0
- package/dist/order-utils/model/ctfExchangeV2TypedData.js.map +1 -0
- package/dist/order-utils/model/eip712.d.ts +29 -0
- package/dist/order-utils/model/eip712.js +10 -0
- package/dist/order-utils/model/eip712.js.map +1 -0
- package/dist/order-utils/model/order.d.ts +2 -0
- package/dist/order-utils/model/order.js +3 -0
- package/dist/order-utils/model/order.js.map +1 -0
- package/dist/order-utils/model/orderDataV1.d.ts +111 -0
- package/dist/order-utils/model/orderDataV1.js +3 -0
- package/dist/order-utils/model/orderDataV1.js.map +1 -0
- package/dist/order-utils/model/orderDataV2.d.ts +111 -0
- package/dist/order-utils/model/orderDataV2.js +3 -0
- package/dist/order-utils/model/orderDataV2.js.map +1 -0
- package/dist/order-utils/model/side.d.ts +8 -0
- package/dist/order-utils/model/side.js +14 -0
- package/dist/order-utils/model/side.js.map +1 -0
- package/dist/order-utils/model/signatureTypeV1.d.ts +14 -0
- package/dist/order-utils/model/signatureTypeV1.js +19 -0
- package/dist/order-utils/model/signatureTypeV1.js.map +1 -0
- package/dist/order-utils/model/signatureTypeV2.d.ts +18 -0
- package/dist/order-utils/model/signatureTypeV2.js +23 -0
- package/dist/order-utils/model/signatureTypeV2.js.map +1 -0
- package/dist/order-utils/utils.d.ts +1 -0
- package/dist/order-utils/utils.js +7 -0
- package/dist/order-utils/utils.js.map +1 -0
- package/dist/signing/constants.d.ts +14 -0
- package/dist/signing/constants.js +19 -0
- package/dist/signing/constants.js.map +1 -0
- package/dist/signing/eip712.d.ts +10 -0
- package/dist/signing/eip712.js +39 -0
- package/dist/signing/eip712.js.map +1 -0
- package/dist/signing/hmac.d.ts +9 -0
- package/dist/signing/hmac.js +32 -0
- package/dist/signing/hmac.js.map +1 -0
- package/dist/signing/index.d.ts +2 -0
- package/dist/signing/index.js +6 -0
- package/dist/signing/index.js.map +1 -0
- package/dist/types/clob.d.ts +377 -0
- package/dist/types/clob.js +31 -0
- package/dist/types/clob.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +8 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/ordersV1.d.ts +106 -0
- package/dist/types/ordersV1.js +3 -0
- package/dist/types/ordersV1.js.map +1 -0
- package/dist/types/ordersV2.d.ts +89 -0
- package/dist/types/ordersV2.js +3 -0
- package/dist/types/ordersV2.js.map +1 -0
- package/dist/types/unifiedOrder.d.ts +34 -0
- package/dist/types/unifiedOrder.js +12 -0
- package/dist/types/unifiedOrder.js.map +1 -0
- package/dist/utilities.d.ts +17 -0
- package/dist/utilities.js +120 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +43 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,999 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClobClient = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const endpoints_1 = require("./endpoints");
|
|
7
|
+
const errors_1 = require("./errors");
|
|
8
|
+
const headers_1 = require("./headers");
|
|
9
|
+
const http_helpers_1 = require("./http-helpers");
|
|
10
|
+
const order_builder_1 = require("./order-builder");
|
|
11
|
+
const helpers_1 = require("./order-builder/helpers");
|
|
12
|
+
const types_1 = require("./types");
|
|
13
|
+
const unifiedOrder_1 = require("./types/unifiedOrder");
|
|
14
|
+
const utilities_1 = require("./utilities");
|
|
15
|
+
class ClobClient {
|
|
16
|
+
// eslint-disable-next-line max-params
|
|
17
|
+
constructor(host, chainId, signer, creds, signatureType, funderAddress, geoBlockToken, useServerTime, builderConfig, getSigner) {
|
|
18
|
+
this.host = host.endsWith("/") ? host.slice(0, -1) : host;
|
|
19
|
+
this.chainId = chainId;
|
|
20
|
+
if (signer !== undefined) {
|
|
21
|
+
this.signer = signer;
|
|
22
|
+
}
|
|
23
|
+
if (creds !== undefined) {
|
|
24
|
+
this.creds = creds;
|
|
25
|
+
}
|
|
26
|
+
this.orderBuilder = new order_builder_1.OrderBuilder(signer, chainId, signatureType, funderAddress, getSigner);
|
|
27
|
+
this.tickSizes = {};
|
|
28
|
+
this.negRisk = {};
|
|
29
|
+
this.feeRates = {};
|
|
30
|
+
this.feeExponents = {};
|
|
31
|
+
this.builderFeeRates = {};
|
|
32
|
+
this.tokenConditionMap = {};
|
|
33
|
+
this.geoBlockToken = geoBlockToken;
|
|
34
|
+
this.useServerTime = useServerTime;
|
|
35
|
+
if (builderConfig !== undefined) {
|
|
36
|
+
this.builderConfig = builderConfig;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Public endpoints
|
|
40
|
+
getOk() {
|
|
41
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
return this.get(`${this.host}/`);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
getVersion() {
|
|
46
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
var _a;
|
|
48
|
+
const response = yield this.get(`${this.host}/version`);
|
|
49
|
+
// default to v2
|
|
50
|
+
return (_a = response === null || response === void 0 ? void 0 : response.version) !== null && _a !== void 0 ? _a : 2;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
getServerTime() {
|
|
54
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
return this.get(`${this.host}${endpoints_1.TIME}`);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
getSamplingSimplifiedMarkets() {
|
|
59
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (next_cursor = constants_1.INITIAL_CURSOR) {
|
|
60
|
+
return this.get(`${this.host}${endpoints_1.GET_SAMPLING_SIMPLIFIED_MARKETS}`, {
|
|
61
|
+
params: { next_cursor },
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
getSamplingMarkets() {
|
|
66
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (next_cursor = constants_1.INITIAL_CURSOR) {
|
|
67
|
+
return this.get(`${this.host}${endpoints_1.GET_SAMPLING_MARKETS}`, {
|
|
68
|
+
params: { next_cursor },
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
getSimplifiedMarkets() {
|
|
73
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (next_cursor = constants_1.INITIAL_CURSOR) {
|
|
74
|
+
return this.get(`${this.host}${endpoints_1.GET_SIMPLIFIED_MARKETS}`, {
|
|
75
|
+
params: { next_cursor },
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
getMarkets() {
|
|
80
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (next_cursor = constants_1.INITIAL_CURSOR) {
|
|
81
|
+
return this.get(`${this.host}${endpoints_1.GET_MARKETS}`, {
|
|
82
|
+
params: { next_cursor },
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
getMarket(conditionID) {
|
|
87
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
return this.get(`${this.host}${endpoints_1.GET_MARKET}${conditionID}`);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
getClobMarketInfo(conditionID) {
|
|
92
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
var _a, _b, _c, _d, _e, _f;
|
|
94
|
+
const result = yield this.get(`${this.host}${endpoints_1.GET_CLOB_MARKET}${conditionID}`);
|
|
95
|
+
if (!(result === null || result === void 0 ? void 0 : result.t)) {
|
|
96
|
+
throw new Error(`failed to fetch market info for condition id ${conditionID}`);
|
|
97
|
+
}
|
|
98
|
+
for (const token of result.t) {
|
|
99
|
+
if (!token)
|
|
100
|
+
continue;
|
|
101
|
+
const tokenId = token.t;
|
|
102
|
+
this.tokenConditionMap[tokenId] = conditionID;
|
|
103
|
+
this.tickSizes[tokenId] = result.mts.toString();
|
|
104
|
+
this.negRisk[tokenId] = result.nr;
|
|
105
|
+
this.feeRates[tokenId] = (_b = (_a = result.fd) === null || _a === void 0 ? void 0 : _a.r) !== null && _b !== void 0 ? _b : 0;
|
|
106
|
+
this.feeExponents[tokenId] = (_d = (_c = result.fd) === null || _c === void 0 ? void 0 : _c.e) !== null && _d !== void 0 ? _d : 0;
|
|
107
|
+
if (result.mbf !== undefined || result.tbf !== undefined) {
|
|
108
|
+
this.builderFeeRates[tokenId] = {
|
|
109
|
+
maker: ((_e = result.mbf) !== null && _e !== void 0 ? _e : 0) / constants_1.BUILDER_FEES_BPS,
|
|
110
|
+
taker: ((_f = result.tbf) !== null && _f !== void 0 ? _f : 0) / constants_1.BUILDER_FEES_BPS,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
getOrderBook(tokenID) {
|
|
118
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
return this.get(`${this.host}${endpoints_1.GET_ORDER_BOOK}`, {
|
|
120
|
+
params: { token_id: tokenID },
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
getOrderBooks(params) {
|
|
125
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
return this.post(`${this.host}${endpoints_1.GET_ORDER_BOOKS}`, {
|
|
127
|
+
data: params,
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
getTickSize(tokenID) {
|
|
132
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
133
|
+
if (tokenID in this.tickSizes) {
|
|
134
|
+
return this.tickSizes[tokenID];
|
|
135
|
+
}
|
|
136
|
+
if (tokenID in this.tokenConditionMap) {
|
|
137
|
+
yield this.getClobMarketInfo(this.tokenConditionMap[tokenID]);
|
|
138
|
+
return this.tickSizes[tokenID];
|
|
139
|
+
}
|
|
140
|
+
const result = yield this.get(`${this.host}${endpoints_1.GET_TICK_SIZE}`, {
|
|
141
|
+
params: { token_id: tokenID },
|
|
142
|
+
});
|
|
143
|
+
this.tickSizes[tokenID] = result.minimum_tick_size.toString();
|
|
144
|
+
return this.tickSizes[tokenID];
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
getNegRisk(tokenID) {
|
|
148
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
if (tokenID in this.negRisk) {
|
|
150
|
+
return this.negRisk[tokenID];
|
|
151
|
+
}
|
|
152
|
+
if (tokenID in this.tokenConditionMap) {
|
|
153
|
+
yield this.getClobMarketInfo(this.tokenConditionMap[tokenID]);
|
|
154
|
+
return this.negRisk[tokenID];
|
|
155
|
+
}
|
|
156
|
+
const result = yield this.get(`${this.host}${endpoints_1.GET_NEG_RISK}`, {
|
|
157
|
+
params: { token_id: tokenID },
|
|
158
|
+
});
|
|
159
|
+
this.negRisk[tokenID] = result.neg_risk;
|
|
160
|
+
return this.negRisk[tokenID];
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
getFeeRateBps(tokenID) {
|
|
164
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
if (tokenID in this.feeRates) {
|
|
166
|
+
return this.feeRates[tokenID];
|
|
167
|
+
}
|
|
168
|
+
if (tokenID in this.tokenConditionMap) {
|
|
169
|
+
yield this.getClobMarketInfo(this.tokenConditionMap[tokenID]);
|
|
170
|
+
return this.feeRates[tokenID];
|
|
171
|
+
}
|
|
172
|
+
const result = yield this.get(`${this.host}${endpoints_1.GET_FEE_RATE}`, {
|
|
173
|
+
params: { token_id: tokenID },
|
|
174
|
+
});
|
|
175
|
+
this.feeRates[tokenID] = result.base_fee;
|
|
176
|
+
return this.feeRates[tokenID];
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
getFeeExponent(tokenID) {
|
|
180
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
if (tokenID in this.feeExponents) {
|
|
182
|
+
return this.feeExponents[tokenID];
|
|
183
|
+
}
|
|
184
|
+
yield this._ensureMarketInfoCached(tokenID);
|
|
185
|
+
return this.feeExponents[tokenID];
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Calculates the hash for the given orderbook
|
|
190
|
+
* @param orderbook
|
|
191
|
+
* @returns
|
|
192
|
+
*/
|
|
193
|
+
getOrderBookHash(orderbook) {
|
|
194
|
+
return (0, utilities_1.generateOrderBookSummaryHash)(orderbook);
|
|
195
|
+
}
|
|
196
|
+
getMidpoint(tokenID) {
|
|
197
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
198
|
+
return this.get(`${this.host}${endpoints_1.GET_MIDPOINT}`, {
|
|
199
|
+
params: { token_id: tokenID },
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
getMidpoints(params) {
|
|
204
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
205
|
+
return this.post(`${this.host}${endpoints_1.GET_MIDPOINTS}`, {
|
|
206
|
+
data: params,
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
getPrice(tokenID, side) {
|
|
211
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
212
|
+
return this.get(`${this.host}${endpoints_1.GET_PRICE}`, {
|
|
213
|
+
params: { token_id: tokenID, side: side },
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
getPrices(params) {
|
|
218
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
219
|
+
return this.post(`${this.host}${endpoints_1.GET_PRICES}`, {
|
|
220
|
+
data: params,
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
getSpread(tokenID) {
|
|
225
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
return this.get(`${this.host}${endpoints_1.GET_SPREAD}`, {
|
|
227
|
+
params: { token_id: tokenID },
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
getSpreads(params) {
|
|
232
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
233
|
+
return this.post(`${this.host}${endpoints_1.GET_SPREADS}`, {
|
|
234
|
+
data: params,
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
getLastTradePrice(tokenID) {
|
|
239
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
240
|
+
return this.get(`${this.host}${endpoints_1.GET_LAST_TRADE_PRICE}`, {
|
|
241
|
+
params: { token_id: tokenID },
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
getLastTradesPrices(params) {
|
|
246
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
247
|
+
return this.post(`${this.host}${endpoints_1.GET_LAST_TRADES_PRICES}`, {
|
|
248
|
+
data: params,
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
getPricesHistory(params) {
|
|
253
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
254
|
+
return this.get(`${this.host}${endpoints_1.GET_PRICES_HISTORY}`, {
|
|
255
|
+
params,
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
// L1 Authed
|
|
260
|
+
/**
|
|
261
|
+
* Creates a new API key for a user
|
|
262
|
+
* @param nonce
|
|
263
|
+
* @returns ApiKeyCreds
|
|
264
|
+
*/
|
|
265
|
+
createApiKey(nonce) {
|
|
266
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
267
|
+
this.canL1Auth();
|
|
268
|
+
const endpoint = `${this.host}${endpoints_1.CREATE_API_KEY}`;
|
|
269
|
+
const headers = yield (0, headers_1.createL1Headers)(this.signer, this.chainId, nonce, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
270
|
+
return yield this.post(endpoint, { headers }).then((apiKeyRaw) => {
|
|
271
|
+
const apiKey = {
|
|
272
|
+
key: apiKeyRaw.apiKey,
|
|
273
|
+
secret: apiKeyRaw.secret,
|
|
274
|
+
passphrase: apiKeyRaw.passphrase,
|
|
275
|
+
};
|
|
276
|
+
return apiKey;
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Derives an existing API key for a user
|
|
282
|
+
* @param nonce
|
|
283
|
+
* @returns ApiKeyCreds
|
|
284
|
+
*/
|
|
285
|
+
deriveApiKey(nonce) {
|
|
286
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
287
|
+
this.canL1Auth();
|
|
288
|
+
const endpoint = `${this.host}${endpoints_1.DERIVE_API_KEY}`;
|
|
289
|
+
const headers = yield (0, headers_1.createL1Headers)(this.signer, this.chainId, nonce, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
290
|
+
return yield this.get(endpoint, { headers }).then((apiKeyRaw) => {
|
|
291
|
+
const apiKey = {
|
|
292
|
+
key: apiKeyRaw.apiKey,
|
|
293
|
+
secret: apiKeyRaw.secret,
|
|
294
|
+
passphrase: apiKeyRaw.passphrase,
|
|
295
|
+
};
|
|
296
|
+
return apiKey;
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
createOrDeriveApiKey(nonce) {
|
|
301
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
302
|
+
return this.createApiKey(nonce).then(response => {
|
|
303
|
+
if (!response.key) {
|
|
304
|
+
return this.deriveApiKey(nonce);
|
|
305
|
+
}
|
|
306
|
+
return response;
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
getApiKeys() {
|
|
311
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
312
|
+
this.canL2Auth();
|
|
313
|
+
const endpoint = endpoints_1.GET_API_KEYS;
|
|
314
|
+
const headerArgs = {
|
|
315
|
+
method: http_helpers_1.GET,
|
|
316
|
+
requestPath: endpoint,
|
|
317
|
+
};
|
|
318
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
319
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
getClosedOnlyMode() {
|
|
323
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
324
|
+
this.canL2Auth();
|
|
325
|
+
const endpoint = endpoints_1.CLOSED_ONLY;
|
|
326
|
+
const headerArgs = {
|
|
327
|
+
method: http_helpers_1.GET,
|
|
328
|
+
requestPath: endpoint,
|
|
329
|
+
};
|
|
330
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
331
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
deleteApiKey() {
|
|
335
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
336
|
+
this.canL2Auth();
|
|
337
|
+
const endpoint = endpoints_1.DELETE_API_KEY;
|
|
338
|
+
const headerArgs = {
|
|
339
|
+
method: http_helpers_1.DELETE,
|
|
340
|
+
requestPath: endpoint,
|
|
341
|
+
};
|
|
342
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
343
|
+
return this.del(`${this.host}${endpoint}`, { headers });
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
getOrder(orderID) {
|
|
347
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
348
|
+
this.canL2Auth();
|
|
349
|
+
const endpoint = `${endpoints_1.GET_ORDER}${orderID}`;
|
|
350
|
+
const headerArgs = {
|
|
351
|
+
method: http_helpers_1.GET,
|
|
352
|
+
requestPath: endpoint,
|
|
353
|
+
};
|
|
354
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
355
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
getTrades(params_1) {
|
|
359
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (params, only_first_page = false, next_cursor) {
|
|
360
|
+
this.canL2Auth();
|
|
361
|
+
const endpoint = endpoints_1.GET_TRADES;
|
|
362
|
+
const headerArgs = {
|
|
363
|
+
method: http_helpers_1.GET,
|
|
364
|
+
requestPath: endpoint,
|
|
365
|
+
};
|
|
366
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
367
|
+
let results = [];
|
|
368
|
+
next_cursor = next_cursor || constants_1.INITIAL_CURSOR;
|
|
369
|
+
while (next_cursor !== constants_1.END_CURSOR && (next_cursor === constants_1.INITIAL_CURSOR || !only_first_page)) {
|
|
370
|
+
const _params = Object.assign(Object.assign({}, params), { next_cursor });
|
|
371
|
+
const response = yield this.get(`${this.host}${endpoint}`, {
|
|
372
|
+
headers,
|
|
373
|
+
params: _params,
|
|
374
|
+
});
|
|
375
|
+
next_cursor = response.next_cursor;
|
|
376
|
+
results = [...results, ...response.data];
|
|
377
|
+
}
|
|
378
|
+
return results;
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
getTradesPaginated(params, next_cursor) {
|
|
382
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
383
|
+
this.canL2Auth();
|
|
384
|
+
const endpoint = endpoints_1.GET_TRADES;
|
|
385
|
+
const headerArgs = {
|
|
386
|
+
method: http_helpers_1.GET,
|
|
387
|
+
requestPath: endpoint,
|
|
388
|
+
};
|
|
389
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
390
|
+
next_cursor = next_cursor || constants_1.INITIAL_CURSOR;
|
|
391
|
+
const _params = Object.assign(Object.assign({}, params), { next_cursor });
|
|
392
|
+
const _a = yield this.get(`${this.host}${endpoint}`, {
|
|
393
|
+
headers,
|
|
394
|
+
params: _params,
|
|
395
|
+
}), { data } = _a, rest = tslib_1.__rest(_a, ["data"]);
|
|
396
|
+
return Object.assign({ trades: Array.isArray(data) ? [...data] : [] }, rest);
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
getBuilderTrades(params, next_cursor) {
|
|
400
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
401
|
+
if (!params.builder_code || params.builder_code === constants_1.bytes32Zero) {
|
|
402
|
+
throw new Error("builderCode is required and cannot be zero");
|
|
403
|
+
}
|
|
404
|
+
this.canL2Auth();
|
|
405
|
+
const endpoint = endpoints_1.GET_BUILDER_TRADES;
|
|
406
|
+
const headerArgs = {
|
|
407
|
+
method: http_helpers_1.GET,
|
|
408
|
+
requestPath: endpoint,
|
|
409
|
+
};
|
|
410
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
411
|
+
next_cursor = next_cursor || constants_1.INITIAL_CURSOR;
|
|
412
|
+
const _params = Object.assign(Object.assign({}, params), { next_cursor });
|
|
413
|
+
const _a = yield this.get(`${this.host}${endpoint}`, {
|
|
414
|
+
headers,
|
|
415
|
+
params: _params,
|
|
416
|
+
}), { data } = _a, rest = tslib_1.__rest(_a, ["data"]);
|
|
417
|
+
return Object.assign({ trades: Array.isArray(data) ? [...data] : [] }, rest);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
getNotifications() {
|
|
421
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
422
|
+
this.canL2Auth();
|
|
423
|
+
const endpoint = endpoints_1.GET_NOTIFICATIONS;
|
|
424
|
+
const headerArgs = {
|
|
425
|
+
method: http_helpers_1.GET,
|
|
426
|
+
requestPath: endpoint,
|
|
427
|
+
};
|
|
428
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
429
|
+
return this.get(`${this.host}${endpoint}`, {
|
|
430
|
+
headers,
|
|
431
|
+
params: { signature_type: this.orderBuilder.signatureType },
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
dropNotifications(params) {
|
|
436
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
437
|
+
this.canL2Auth();
|
|
438
|
+
const endpoint = endpoints_1.DROP_NOTIFICATIONS;
|
|
439
|
+
const l2HeaderArgs = {
|
|
440
|
+
method: http_helpers_1.DELETE,
|
|
441
|
+
requestPath: endpoint,
|
|
442
|
+
};
|
|
443
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
444
|
+
return this.del(`${this.host}${endpoint}`, {
|
|
445
|
+
headers,
|
|
446
|
+
params: (0, http_helpers_1.parseDropNotificationParams)(params),
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
getBalanceAllowance(params) {
|
|
451
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
452
|
+
this.canL2Auth();
|
|
453
|
+
const endpoint = endpoints_1.GET_BALANCE_ALLOWANCE;
|
|
454
|
+
const headerArgs = {
|
|
455
|
+
method: http_helpers_1.GET,
|
|
456
|
+
requestPath: endpoint,
|
|
457
|
+
};
|
|
458
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
459
|
+
const _params = Object.assign(Object.assign({}, params), { signature_type: this.orderBuilder.signatureType });
|
|
460
|
+
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
updateBalanceAllowance(params) {
|
|
464
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
465
|
+
this.canL2Auth();
|
|
466
|
+
const endpoint = endpoints_1.UPDATE_BALANCE_ALLOWANCE;
|
|
467
|
+
const headerArgs = {
|
|
468
|
+
method: http_helpers_1.GET,
|
|
469
|
+
requestPath: endpoint,
|
|
470
|
+
};
|
|
471
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
472
|
+
const _params = Object.assign(Object.assign({}, params), { signature_type: this.orderBuilder.signatureType });
|
|
473
|
+
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
createOrder(userOrder, options) {
|
|
477
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
478
|
+
var _a, _b;
|
|
479
|
+
this.canL1Auth();
|
|
480
|
+
const orderToSign = Object.assign({}, userOrder);
|
|
481
|
+
if (((_a = this.builderConfig) === null || _a === void 0 ? void 0 : _a.builderCode) && !orderToSign.builderCode) {
|
|
482
|
+
orderToSign.builderCode = this.builderConfig.builderCode;
|
|
483
|
+
}
|
|
484
|
+
const { tokenID } = orderToSign;
|
|
485
|
+
// const tickSize = await this._resolveTickSize(tokenID, options?.tickSize);
|
|
486
|
+
if (!(options === null || options === void 0 ? void 0 : options.tickSize)) {
|
|
487
|
+
throw new Error("tickSize is required in options");
|
|
488
|
+
}
|
|
489
|
+
const tickSize = options.tickSize;
|
|
490
|
+
if (!(0, utilities_1.priceValid)(orderToSign.price, tickSize)) {
|
|
491
|
+
throw new Error(`invalid price (${orderToSign.price}), min: ${parseFloat(tickSize)} - max: ${1 - parseFloat(tickSize)}`);
|
|
492
|
+
}
|
|
493
|
+
const negRisk = (_b = options === null || options === void 0 ? void 0 : options.negRisk) !== null && _b !== void 0 ? _b : (yield this.getNegRisk(tokenID));
|
|
494
|
+
const version = yield this.resolveVersion();
|
|
495
|
+
return this.orderBuilder.buildOrder(orderToSign, {
|
|
496
|
+
tickSize,
|
|
497
|
+
negRisk,
|
|
498
|
+
}, version);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
createMarketOrder(userMarketOrder, options) {
|
|
502
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
503
|
+
var _a, _b, _c, _d;
|
|
504
|
+
this.canL1Auth();
|
|
505
|
+
const { tokenID } = userMarketOrder;
|
|
506
|
+
yield this._ensureMarketInfoCached(tokenID);
|
|
507
|
+
const tickSize = yield this._resolveTickSize(tokenID, options === null || options === void 0 ? void 0 : options.tickSize);
|
|
508
|
+
if (!userMarketOrder.price) {
|
|
509
|
+
userMarketOrder.price = yield this.calculateMarketPrice(tokenID, userMarketOrder.side, userMarketOrder.amount, userMarketOrder.orderType);
|
|
510
|
+
}
|
|
511
|
+
if (!(0, utilities_1.priceValid)(userMarketOrder.price, tickSize)) {
|
|
512
|
+
throw new Error(`invalid price (${userMarketOrder.price}), min: ${parseFloat(tickSize)} - max: ${1 - parseFloat(tickSize)}`);
|
|
513
|
+
}
|
|
514
|
+
const orderToSign = Object.assign({}, userMarketOrder);
|
|
515
|
+
if (((_a = this.builderConfig) === null || _a === void 0 ? void 0 : _a.builderCode) && !orderToSign.builderCode) {
|
|
516
|
+
orderToSign.builderCode = this.builderConfig.builderCode;
|
|
517
|
+
}
|
|
518
|
+
if (orderToSign.side === types_1.Side.BUY && orderToSign.userUSDCBalance !== undefined) {
|
|
519
|
+
// biome-ignore lint/style/noNonNullAssertion: price is validated above
|
|
520
|
+
const price = orderToSign.price;
|
|
521
|
+
const builderTakerFeeRate = this.isBuilderOrder(orderToSign.builderCode)
|
|
522
|
+
? ((_c = (_b = this.builderFeeRates[tokenID]) === null || _b === void 0 ? void 0 : _b.taker) !== null && _c !== void 0 ? _c : 0)
|
|
523
|
+
: 0;
|
|
524
|
+
const platformFeeRate = this._calculatePlatformFeeRate(price, this.feeRates[tokenID], this.feeExponents[tokenID]);
|
|
525
|
+
// platform_fee = C * platform_fee_rate, C = amount / price
|
|
526
|
+
const platformFee = (orderToSign.amount / price) * platformFeeRate;
|
|
527
|
+
const totalCost = orderToSign.amount + platformFee + orderToSign.amount * builderTakerFeeRate;
|
|
528
|
+
if (orderToSign.userUSDCBalance <= totalCost) {
|
|
529
|
+
orderToSign.amount = this._calculateFeeAdjustedAmount(orderToSign.amount, price, this.feeRates[tokenID], this.feeExponents[tokenID], builderTakerFeeRate);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
const negRisk = (_d = options === null || options === void 0 ? void 0 : options.negRisk) !== null && _d !== void 0 ? _d : (yield this.getNegRisk(tokenID));
|
|
533
|
+
const version = yield this.resolveVersion();
|
|
534
|
+
return this.orderBuilder.buildMarketOrder(orderToSign, {
|
|
535
|
+
tickSize,
|
|
536
|
+
negRisk,
|
|
537
|
+
}, version);
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
createAndPostOrder(userOrder_1, options_1) {
|
|
541
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (userOrder, options, orderType = types_1.OrderType.GTC, deferExec = false) {
|
|
542
|
+
let postOrderResponse;
|
|
543
|
+
yield this._retryOnVersionUpdate(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
544
|
+
const order = yield this.createOrder(userOrder, options);
|
|
545
|
+
postOrderResponse = yield this.postOrder(order, orderType, deferExec);
|
|
546
|
+
}));
|
|
547
|
+
return postOrderResponse;
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
createAndPostMarketOrder(userMarketOrder_1, options_1) {
|
|
551
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (userMarketOrder, options, orderType = types_1.OrderType.FOK, deferExec = false) {
|
|
552
|
+
let postOrderMarketResponse;
|
|
553
|
+
yield this._retryOnVersionUpdate(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
554
|
+
const order = yield this.createMarketOrder(userMarketOrder, options);
|
|
555
|
+
postOrderMarketResponse = yield this.postOrder(order, orderType, deferExec);
|
|
556
|
+
}));
|
|
557
|
+
return postOrderMarketResponse;
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
getOpenOrders(params_1) {
|
|
561
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (params, only_first_page = false, next_cursor) {
|
|
562
|
+
this.canL2Auth();
|
|
563
|
+
const endpoint = endpoints_1.GET_OPEN_ORDERS;
|
|
564
|
+
const l2HeaderArgs = {
|
|
565
|
+
method: http_helpers_1.GET,
|
|
566
|
+
requestPath: endpoint,
|
|
567
|
+
};
|
|
568
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
569
|
+
let results = [];
|
|
570
|
+
next_cursor = next_cursor || constants_1.INITIAL_CURSOR;
|
|
571
|
+
while (next_cursor !== constants_1.END_CURSOR && (next_cursor === constants_1.INITIAL_CURSOR || !only_first_page)) {
|
|
572
|
+
const _params = Object.assign(Object.assign({}, params), { next_cursor });
|
|
573
|
+
const response = yield this.get(`${this.host}${endpoint}`, {
|
|
574
|
+
headers,
|
|
575
|
+
params: _params,
|
|
576
|
+
});
|
|
577
|
+
next_cursor = response.next_cursor;
|
|
578
|
+
results = [...results, ...response.data];
|
|
579
|
+
}
|
|
580
|
+
return results;
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
postOrder(order_1) {
|
|
584
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (order, orderType = types_1.OrderType.GTC, deferExec = false) {
|
|
585
|
+
var _a, _b;
|
|
586
|
+
this.canL2Auth();
|
|
587
|
+
const endpoint = endpoints_1.POST_ORDER;
|
|
588
|
+
const orderPayload = (0, unifiedOrder_1.isV2Order)(order)
|
|
589
|
+
? (0, utilities_1.orderToJsonV2)(order, ((_a = this.creds) === null || _a === void 0 ? void 0 : _a.key) || "", orderType, deferExec)
|
|
590
|
+
: (0, utilities_1.orderToJsonV1)(order, ((_b = this.creds) === null || _b === void 0 ? void 0 : _b.key) || "", orderType, deferExec);
|
|
591
|
+
const l2HeaderArgs = {
|
|
592
|
+
method: http_helpers_1.POST,
|
|
593
|
+
requestPath: endpoint,
|
|
594
|
+
body: JSON.stringify(orderPayload),
|
|
595
|
+
};
|
|
596
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
597
|
+
const res = yield this.post(`${this.host}${endpoint}`, {
|
|
598
|
+
headers,
|
|
599
|
+
data: orderPayload,
|
|
600
|
+
});
|
|
601
|
+
if (this._isOrderVersionMismatch(res))
|
|
602
|
+
yield this.resolveVersion(true);
|
|
603
|
+
return res;
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
postOrders(args_1) {
|
|
607
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (args, deferExec = false) {
|
|
608
|
+
var _a, _b;
|
|
609
|
+
this.canL2Auth();
|
|
610
|
+
const endpoint = endpoints_1.POST_ORDERS;
|
|
611
|
+
const ordersPayload = [];
|
|
612
|
+
for (const arg of args) {
|
|
613
|
+
const { order, orderType } = arg;
|
|
614
|
+
// Version-aware dispatch
|
|
615
|
+
const orderPayload = (0, unifiedOrder_1.isV2Order)(order)
|
|
616
|
+
? (0, utilities_1.orderToJsonV2)(order, ((_a = this.creds) === null || _a === void 0 ? void 0 : _a.key) || "", orderType, deferExec)
|
|
617
|
+
: (0, utilities_1.orderToJsonV1)(order, ((_b = this.creds) === null || _b === void 0 ? void 0 : _b.key) || "", orderType, deferExec);
|
|
618
|
+
ordersPayload.push(orderPayload);
|
|
619
|
+
}
|
|
620
|
+
const l2HeaderArgs = {
|
|
621
|
+
method: http_helpers_1.POST,
|
|
622
|
+
requestPath: endpoint,
|
|
623
|
+
body: JSON.stringify(ordersPayload),
|
|
624
|
+
};
|
|
625
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
626
|
+
const res = yield this.post(`${this.host}${endpoint}`, {
|
|
627
|
+
headers,
|
|
628
|
+
data: ordersPayload,
|
|
629
|
+
});
|
|
630
|
+
if (this._isOrderVersionMismatch(res))
|
|
631
|
+
yield this.resolveVersion(true);
|
|
632
|
+
return res;
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
cancelOrder(payload) {
|
|
636
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
637
|
+
this.canL2Auth();
|
|
638
|
+
const endpoint = endpoints_1.CANCEL_ORDER;
|
|
639
|
+
const l2HeaderArgs = {
|
|
640
|
+
method: http_helpers_1.DELETE,
|
|
641
|
+
requestPath: endpoint,
|
|
642
|
+
body: JSON.stringify(payload),
|
|
643
|
+
};
|
|
644
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
645
|
+
return this.del(`${this.host}${endpoint}`, { headers, data: payload });
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
cancelOrders(ordersHashes) {
|
|
649
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
650
|
+
this.canL2Auth();
|
|
651
|
+
const endpoint = endpoints_1.CANCEL_ORDERS;
|
|
652
|
+
const l2HeaderArgs = {
|
|
653
|
+
method: http_helpers_1.DELETE,
|
|
654
|
+
requestPath: endpoint,
|
|
655
|
+
body: JSON.stringify(ordersHashes),
|
|
656
|
+
};
|
|
657
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
658
|
+
return this.del(`${this.host}${endpoint}`, { headers, data: ordersHashes });
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
cancelAll() {
|
|
662
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
663
|
+
this.canL2Auth();
|
|
664
|
+
const endpoint = endpoints_1.CANCEL_ALL;
|
|
665
|
+
const l2HeaderArgs = {
|
|
666
|
+
method: http_helpers_1.DELETE,
|
|
667
|
+
requestPath: endpoint,
|
|
668
|
+
};
|
|
669
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
670
|
+
return this.del(`${this.host}${endpoint}`, { headers });
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
cancelMarketOrders(payload) {
|
|
674
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
675
|
+
this.canL2Auth();
|
|
676
|
+
const endpoint = endpoints_1.CANCEL_MARKET_ORDERS;
|
|
677
|
+
const l2HeaderArgs = {
|
|
678
|
+
method: http_helpers_1.DELETE,
|
|
679
|
+
requestPath: endpoint,
|
|
680
|
+
body: JSON.stringify(payload),
|
|
681
|
+
};
|
|
682
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, l2HeaderArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
683
|
+
return this.del(`${this.host}${endpoint}`, { headers, data: payload });
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
isOrderScoring(params) {
|
|
687
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
688
|
+
this.canL2Auth();
|
|
689
|
+
const endpoint = endpoints_1.IS_ORDER_SCORING;
|
|
690
|
+
const headerArgs = {
|
|
691
|
+
method: http_helpers_1.GET,
|
|
692
|
+
requestPath: endpoint,
|
|
693
|
+
};
|
|
694
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
695
|
+
return this.get(`${this.host}${endpoint}`, { headers, params });
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
areOrdersScoring(params) {
|
|
699
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
700
|
+
this.canL2Auth();
|
|
701
|
+
const endpoint = endpoints_1.ARE_ORDERS_SCORING;
|
|
702
|
+
const payload = JSON.stringify(params === null || params === void 0 ? void 0 : params.orderIds);
|
|
703
|
+
const headerArgs = {
|
|
704
|
+
method: http_helpers_1.POST,
|
|
705
|
+
requestPath: endpoint,
|
|
706
|
+
body: payload,
|
|
707
|
+
};
|
|
708
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
709
|
+
return this.post(`${this.host}${endpoint}`, {
|
|
710
|
+
headers,
|
|
711
|
+
data: payload,
|
|
712
|
+
});
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
// Rewards
|
|
716
|
+
getEarningsForUserForDay(date) {
|
|
717
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
718
|
+
this.canL2Auth();
|
|
719
|
+
const endpoint = endpoints_1.GET_EARNINGS_FOR_USER_FOR_DAY;
|
|
720
|
+
const headerArgs = {
|
|
721
|
+
method: http_helpers_1.GET,
|
|
722
|
+
requestPath: endpoint,
|
|
723
|
+
};
|
|
724
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
725
|
+
let results = [];
|
|
726
|
+
let next_cursor = constants_1.INITIAL_CURSOR;
|
|
727
|
+
while (next_cursor !== constants_1.END_CURSOR) {
|
|
728
|
+
const params = {
|
|
729
|
+
date,
|
|
730
|
+
signature_type: this.orderBuilder.signatureType,
|
|
731
|
+
next_cursor,
|
|
732
|
+
};
|
|
733
|
+
const response = yield this.get(`${this.host}${endpoint}`, {
|
|
734
|
+
headers,
|
|
735
|
+
params,
|
|
736
|
+
});
|
|
737
|
+
next_cursor = response.next_cursor;
|
|
738
|
+
results = [...results, ...response.data];
|
|
739
|
+
}
|
|
740
|
+
return results;
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
getTotalEarningsForUserForDay(date) {
|
|
744
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
745
|
+
this.canL2Auth();
|
|
746
|
+
const endpoint = endpoints_1.GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY;
|
|
747
|
+
const headerArgs = {
|
|
748
|
+
method: http_helpers_1.GET,
|
|
749
|
+
requestPath: endpoint,
|
|
750
|
+
};
|
|
751
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
752
|
+
const params = {
|
|
753
|
+
date,
|
|
754
|
+
signature_type: this.orderBuilder.signatureType,
|
|
755
|
+
};
|
|
756
|
+
return yield this.get(`${this.host}${endpoint}`, {
|
|
757
|
+
headers,
|
|
758
|
+
params,
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
getUserEarningsAndMarketsConfig(date_1) {
|
|
763
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (date, order_by = "", position = "", no_competition = false) {
|
|
764
|
+
this.canL2Auth();
|
|
765
|
+
const endpoint = endpoints_1.GET_REWARDS_EARNINGS_PERCENTAGES;
|
|
766
|
+
const headerArgs = {
|
|
767
|
+
method: http_helpers_1.GET,
|
|
768
|
+
requestPath: endpoint,
|
|
769
|
+
};
|
|
770
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
771
|
+
let results = [];
|
|
772
|
+
let next_cursor = constants_1.INITIAL_CURSOR;
|
|
773
|
+
while (next_cursor !== constants_1.END_CURSOR) {
|
|
774
|
+
const params = {
|
|
775
|
+
date,
|
|
776
|
+
signature_type: this.orderBuilder.signatureType,
|
|
777
|
+
next_cursor,
|
|
778
|
+
order_by,
|
|
779
|
+
position,
|
|
780
|
+
no_competition,
|
|
781
|
+
};
|
|
782
|
+
const response = yield this.get(`${this.host}${endpoint}`, {
|
|
783
|
+
headers,
|
|
784
|
+
params,
|
|
785
|
+
});
|
|
786
|
+
next_cursor = response.next_cursor;
|
|
787
|
+
results = [...results, ...response.data];
|
|
788
|
+
}
|
|
789
|
+
return results;
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
getRewardPercentages() {
|
|
793
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
794
|
+
this.canL2Auth();
|
|
795
|
+
const endpoint = endpoints_1.GET_LIQUIDITY_REWARD_PERCENTAGES;
|
|
796
|
+
const headerArgs = {
|
|
797
|
+
method: http_helpers_1.GET,
|
|
798
|
+
requestPath: endpoint,
|
|
799
|
+
};
|
|
800
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
801
|
+
const _params = {
|
|
802
|
+
signature_type: this.orderBuilder.signatureType,
|
|
803
|
+
};
|
|
804
|
+
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
getCurrentRewards() {
|
|
808
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
809
|
+
let results = [];
|
|
810
|
+
let next_cursor = constants_1.INITIAL_CURSOR;
|
|
811
|
+
while (next_cursor !== constants_1.END_CURSOR) {
|
|
812
|
+
const response = yield this.get(`${this.host}${endpoints_1.GET_REWARDS_MARKETS_CURRENT}`, {
|
|
813
|
+
params: { next_cursor },
|
|
814
|
+
});
|
|
815
|
+
next_cursor = response.next_cursor;
|
|
816
|
+
results = [...results, ...response.data];
|
|
817
|
+
}
|
|
818
|
+
return results;
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
getRawRewardsForMarket(conditionId) {
|
|
822
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
823
|
+
let results = [];
|
|
824
|
+
let next_cursor = constants_1.INITIAL_CURSOR;
|
|
825
|
+
while (next_cursor !== constants_1.END_CURSOR) {
|
|
826
|
+
const response = yield this.get(`${this.host}${endpoints_1.GET_REWARDS_MARKETS}${conditionId}`, {
|
|
827
|
+
params: { next_cursor },
|
|
828
|
+
});
|
|
829
|
+
next_cursor = response.next_cursor;
|
|
830
|
+
results = [...results, ...response.data];
|
|
831
|
+
}
|
|
832
|
+
return results;
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
getMarketTradesEvents(conditionID) {
|
|
836
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
837
|
+
return this.get(`${this.host}${endpoints_1.GET_MARKET_TRADES_EVENTS}${conditionID}`);
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
calculateMarketPrice(tokenID_1, side_1, amount_1) {
|
|
841
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (tokenID, side, amount, orderType = types_1.OrderType.FOK) {
|
|
842
|
+
const book = yield this.getOrderBook(tokenID);
|
|
843
|
+
if (!book) {
|
|
844
|
+
throw new Error("no orderbook");
|
|
845
|
+
}
|
|
846
|
+
if (side === types_1.Side.BUY) {
|
|
847
|
+
if (!book.asks) {
|
|
848
|
+
throw new Error("no match");
|
|
849
|
+
}
|
|
850
|
+
return (0, helpers_1.calculateBuyMarketPrice)(book.asks, amount, orderType);
|
|
851
|
+
}
|
|
852
|
+
else {
|
|
853
|
+
if (!book.bids) {
|
|
854
|
+
throw new Error("no match");
|
|
855
|
+
}
|
|
856
|
+
return (0, helpers_1.calculateSellMarketPrice)(book.bids, amount, orderType);
|
|
857
|
+
}
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
createBuilderApiKey() {
|
|
861
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
862
|
+
this.canL2Auth();
|
|
863
|
+
const endpoint = endpoints_1.CREATE_BUILDER_API_KEY;
|
|
864
|
+
const headerArgs = {
|
|
865
|
+
method: http_helpers_1.POST,
|
|
866
|
+
requestPath: endpoint,
|
|
867
|
+
};
|
|
868
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
869
|
+
return this.post(`${this.host}${endpoint}`, { headers });
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
getBuilderApiKeys() {
|
|
873
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
874
|
+
this.canL2Auth();
|
|
875
|
+
const endpoint = endpoints_1.GET_BUILDER_API_KEYS;
|
|
876
|
+
const headerArgs = {
|
|
877
|
+
method: http_helpers_1.GET,
|
|
878
|
+
requestPath: endpoint,
|
|
879
|
+
};
|
|
880
|
+
const headers = yield (0, headers_1.createL2Headers)(this.signer, this.creds, headerArgs, this.useServerTime ? yield this.getServerTime() : undefined);
|
|
881
|
+
return this.get(`${this.host}${endpoint}`, { headers });
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
canL1Auth() {
|
|
885
|
+
if (this.signer === undefined) {
|
|
886
|
+
throw errors_1.L1_AUTH_UNAVAILABLE_ERROR;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
canL2Auth() {
|
|
890
|
+
if (this.signer === undefined) {
|
|
891
|
+
throw errors_1.L1_AUTH_UNAVAILABLE_ERROR;
|
|
892
|
+
}
|
|
893
|
+
if (this.creds === undefined) {
|
|
894
|
+
throw errors_1.L2_AUTH_NOT_AVAILABLE;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
isBuilderOrder(builderCode) {
|
|
898
|
+
return builderCode !== undefined && builderCode !== constants_1.bytes32Zero;
|
|
899
|
+
}
|
|
900
|
+
_ensureMarketInfoCached(tokenID) {
|
|
901
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
902
|
+
if (tokenID in this.feeRates && tokenID in this.feeExponents)
|
|
903
|
+
return;
|
|
904
|
+
if (!(tokenID in this.tokenConditionMap)) {
|
|
905
|
+
const result = yield this.get(`${this.host}${endpoints_1.GET_MARKET_BY_TOKEN}${tokenID}`);
|
|
906
|
+
if (!(result === null || result === void 0 ? void 0 : result.condition_id)) {
|
|
907
|
+
throw new Error(`failed to resolve condition id for token ${tokenID}`);
|
|
908
|
+
}
|
|
909
|
+
this.tokenConditionMap[tokenID] = result.condition_id;
|
|
910
|
+
}
|
|
911
|
+
yield this.getClobMarketInfo(this.tokenConditionMap[tokenID]);
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
_resolveTickSize(tokenID, tickSize) {
|
|
915
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
916
|
+
const minTickSize = yield this.getTickSize(tokenID);
|
|
917
|
+
if (tickSize) {
|
|
918
|
+
if ((0, utilities_1.isTickSizeSmaller)(tickSize, minTickSize)) {
|
|
919
|
+
throw new Error(`invalid tick size (${tickSize}), minimum for the market is ${minTickSize}`);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
else {
|
|
923
|
+
tickSize = minTickSize;
|
|
924
|
+
}
|
|
925
|
+
return tickSize;
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
resolveVersion() {
|
|
929
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* (forceUpdate = false) {
|
|
930
|
+
// Use cached version if given
|
|
931
|
+
if (!forceUpdate && this.cachedVersion !== undefined) {
|
|
932
|
+
return this.cachedVersion;
|
|
933
|
+
}
|
|
934
|
+
// Query API and cache the result
|
|
935
|
+
const apiVersion = yield this.getVersion();
|
|
936
|
+
this.cachedVersion = apiVersion;
|
|
937
|
+
return apiVersion;
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
_calculatePlatformFeeRate(price, feeRate, feeExponent) {
|
|
941
|
+
// platform_fee_rate = rate * (p*(1-p))^exp
|
|
942
|
+
return feeRate * Math.pow((price * (1 - price)), feeExponent);
|
|
943
|
+
}
|
|
944
|
+
_calculateFeeAdjustedAmount(budget, price, feeRate, feeExponent, builderTakerFeeRate = 0) {
|
|
945
|
+
// effective + (effective/price)*feeRate*(p*(1-p))^exp + effective*builderRate = budget
|
|
946
|
+
// effective * (1 + (feeRate*(p*(1-p))^exp)/price + builderRate) = budget
|
|
947
|
+
const platformFeeRate = this._calculatePlatformFeeRate(price, feeRate, feeExponent);
|
|
948
|
+
return budget / (1 + platformFeeRate / price + builderTakerFeeRate);
|
|
949
|
+
}
|
|
950
|
+
// private async _resolveFeeRateBps(tokenID: string, userFeeRateBps?: number): Promise<number> {
|
|
951
|
+
// const marketFeeRateBps = await this.getFeeRateBps(tokenID);
|
|
952
|
+
// if (
|
|
953
|
+
// marketFeeRateBps > 0 &&
|
|
954
|
+
// userFeeRateBps !== undefined &&
|
|
955
|
+
// userFeeRateBps !== marketFeeRateBps
|
|
956
|
+
// ) {
|
|
957
|
+
// throw new Error(
|
|
958
|
+
// `invalid user provided fee rate: ${userFeeRateBps}, fee rate for the market must be ${marketFeeRateBps}`,
|
|
959
|
+
// );
|
|
960
|
+
// }
|
|
961
|
+
// return marketFeeRateBps;
|
|
962
|
+
// }
|
|
963
|
+
_retryOnVersionUpdate(retryFunc) {
|
|
964
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
965
|
+
const version = yield this.resolveVersion();
|
|
966
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
967
|
+
yield retryFunc();
|
|
968
|
+
// no need to retry if version is unchanged
|
|
969
|
+
if (version === (yield this.resolveVersion()))
|
|
970
|
+
break;
|
|
971
|
+
}
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
_isOrderVersionMismatch(resp) {
|
|
975
|
+
const error = resp === null || resp === void 0 ? void 0 : resp.error;
|
|
976
|
+
if (!error)
|
|
977
|
+
return false;
|
|
978
|
+
const message = typeof error === "string" ? error : JSON.stringify(error);
|
|
979
|
+
return message.includes(constants_1.ORDER_VERSION_MISMATCH_ERROR);
|
|
980
|
+
}
|
|
981
|
+
// http methods
|
|
982
|
+
get(endpoint, options) {
|
|
983
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
984
|
+
return (0, http_helpers_1.get)(endpoint, Object.assign(Object.assign({}, options), { params: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.params), { geo_block_token: this.geoBlockToken }) }));
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
post(endpoint, options) {
|
|
988
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
989
|
+
return (0, http_helpers_1.post)(endpoint, Object.assign(Object.assign({}, options), { params: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.params), { geo_block_token: this.geoBlockToken }) }));
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
del(endpoint, options) {
|
|
993
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
994
|
+
return (0, http_helpers_1.del)(endpoint, Object.assign(Object.assign({}, options), { params: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.params), { geo_block_token: this.geoBlockToken }) }));
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
exports.ClobClient = ClobClient;
|
|
999
|
+
//# sourceMappingURL=client.js.map
|