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