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