clob-client-sdks 5.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +77 -0
  3. package/dist/client.d.ts +165 -0
  4. package/dist/client.js +954 -0
  5. package/dist/client.js.map +1 -0
  6. package/dist/config.d.ts +12 -0
  7. package/dist/config.js +28 -0
  8. package/dist/config.js.map +1 -0
  9. package/dist/constants.d.ts +3 -0
  10. package/dist/constants.js +7 -0
  11. package/dist/constants.js.map +1 -0
  12. package/dist/endpoints.d.ts +67 -0
  13. package/dist/endpoints.js +82 -0
  14. package/dist/endpoints.js.map +1 -0
  15. package/dist/errors.d.ts +9 -0
  16. package/dist/errors.js +15 -0
  17. package/dist/errors.js.map +1 -0
  18. package/dist/headers/index.d.ts +7 -0
  19. package/dist/headers/index.js +41 -0
  20. package/dist/headers/index.js.map +1 -0
  21. package/dist/http-helpers/index.d.ts +21 -0
  22. package/dist/http-helpers/index.js +166 -0
  23. package/dist/http-helpers/index.js.map +1 -0
  24. package/dist/index.d.ts +8 -0
  25. package/dist/index.js +9 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/order-builder/builder.d.ts +30 -0
  28. package/dist/order-builder/builder.js +52 -0
  29. package/dist/order-builder/builder.js.map +1 -0
  30. package/dist/order-builder/helpers.d.ts +51 -0
  31. package/dist/order-builder/helpers.js +279 -0
  32. package/dist/order-builder/helpers.js.map +1 -0
  33. package/dist/order-builder/index.d.ts +1 -0
  34. package/dist/order-builder/index.js +2 -0
  35. package/dist/order-builder/index.js.map +1 -0
  36. package/dist/rfq-client.d.ts +64 -0
  37. package/dist/rfq-client.js +397 -0
  38. package/dist/rfq-client.js.map +1 -0
  39. package/dist/rfq-deps.d.ts +45 -0
  40. package/dist/rfq-deps.js +2 -0
  41. package/dist/rfq-deps.js.map +1 -0
  42. package/dist/signing/constants.d.ts +14 -0
  43. package/dist/signing/constants.js +16 -0
  44. package/dist/signing/constants.js.map +1 -0
  45. package/dist/signing/eip712.d.ts +10 -0
  46. package/dist/signing/eip712.js +34 -0
  47. package/dist/signing/eip712.js.map +1 -0
  48. package/dist/signing/hmac.d.ts +9 -0
  49. package/dist/signing/hmac.js +58 -0
  50. package/dist/signing/hmac.js.map +1 -0
  51. package/dist/signing/index.d.ts +2 -0
  52. package/dist/signing/index.js +3 -0
  53. package/dist/signing/index.js.map +1 -0
  54. package/dist/types.d.ts +667 -0
  55. package/dist/types.js +37 -0
  56. package/dist/types.js.map +1 -0
  57. package/dist/utilities.d.ts +16 -0
  58. package/dist/utilities.js +93 -0
  59. package/dist/utilities.js.map +1 -0
  60. package/package.json +110 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Polymarket
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Polymarket CLOB Client
2
+
3
+ <a href='https://www.npmjs.com/package/@polymarket/clob-client'>
4
+ <img src='https://img.shields.io/npm/v/@polymarket/clob-client.svg' alt='NPM'/>
5
+ </a>
6
+
7
+ Typescript client for the Polymarket CLOB
8
+
9
+ ### Usage
10
+
11
+ ```ts
12
+ //npm install @polymarket/clob-client
13
+ //npm install ethers
14
+ //Client initialization example and dumping API Keys
15
+
16
+ import { ApiKeyCreds, ClobClient, OrderType, Side, } from "@polymarket/clob-client";
17
+ import { Wallet } from "@ethersproject/wallet";
18
+
19
+ const host = 'https://clob.polymarket.com';
20
+ const funder = '';//This is your Polymarket Profile Address, where you send UDSC to.
21
+ const signer = new Wallet(""); //This is your Private Key. If using email login export from https://reveal.magic.link/polymarket otherwise export from your Web3 Application
22
+
23
+
24
+ //In general don't create a new API key, always derive or createOrDerive
25
+ const creds = new ClobClient(host, 137, signer).createOrDeriveApiKey();
26
+
27
+ //0: Browser Wallet(Metamask, Coinbase Wallet, etc)
28
+ //1: Magic/Email Login
29
+ const signatureType = 1;
30
+ (async () => {
31
+ const clobClient = new ClobClient(host, 137, signer, await creds, signatureType, funder);
32
+ const resp2 = await clobClient.createAndPostOrder(
33
+ {
34
+ tokenID: "", //Use https://docs.polymarket.com/developers/gamma-markets-api/get-markets to grab a sample token
35
+ price: 0.01,
36
+ side: Side.BUY,
37
+ size: 5,
38
+ },
39
+ { tickSize: "0.001",negRisk: false }, //You'll need to adjust these based on the market. Get the tickSize and negRisk T/F from the get-markets above
40
+ //{ tickSize: "0.001",negRisk: true },
41
+
42
+ OrderType.GTC,
43
+ );
44
+ console.log(resp2)
45
+ })();
46
+ ```
47
+
48
+ See [examples](examples/) for more information
49
+
50
+ ### Error Handling
51
+
52
+ By default, API errors are returned as `{ error: "...", status: ... }` objects. To have the client throw errors instead, pass `throwOnError: true` as the last constructor argument:
53
+
54
+ ```ts
55
+ import { ClobClient, ApiError } from "@polymarket/clob-client";
56
+
57
+ const clobClient = new ClobClient(
58
+ host, 137, signer, await creds, signatureType, funder,
59
+ undefined, // geoBlockToken
60
+ undefined, // useServerTime
61
+ undefined, // builderConfig
62
+ undefined, // getSigner
63
+ undefined, // retryOnError
64
+ undefined, // tickSizeTtlMs
65
+ true, // throwOnError
66
+ );
67
+
68
+ try {
69
+ const book = await clobClient.getOrderBook(tokenID);
70
+ } catch (e) {
71
+ if (e instanceof ApiError) {
72
+ console.log(e.message); // "No orderbook exists for the requested token id"
73
+ console.log(e.status); // 404
74
+ console.log(e.data); // full error response object from the API
75
+ }
76
+ }
77
+ ```
@@ -0,0 +1,165 @@
1
+ import type { Wallet } from "@ethersproject/wallet";
2
+ import type { JsonRpcSigner } from "@ethersproject/providers";
3
+ import { SignatureType } from "@polymarket/order-utils";
4
+ import type { SignedOrder } from "@polymarket/order-utils";
5
+ import type { BuilderConfig } from "@polymarket/builder-signing-sdk";
6
+ import { OrderType, Side } from "./types.ts";
7
+ import type { ApiKeyCreds, ApiKeysResponse, Chain, CreateOrderOptions, MarketPrice, OpenOrderParams, OpenOrdersResponse, OrderMarketCancelParams, OrderBookSummary, OrderPayload, Trade, Notification, TradeParams, UserMarketOrder, UserOrder, BalanceAllowanceParams, BalanceAllowanceResponse, OrderScoringParams, OrderScoring, OpenOrder, TickSizes, TickSize, OrdersScoringParams, OrdersScoring, PriceHistoryFilterParams, PaginationPayload, MarketTradeEvent, DropNotificationParams, BookParams, UserEarning, RewardsPercentages, MarketReward, UserRewardsEarning, TotalUserEarning, NegRisk, BanStatus, PostOrdersArgs, FeeRates, BuilderTrade, BuilderApiKey, BuilderApiKeyResponse, ReadonlyApiKeyResponse, HeartbeatResponse } from "./types.ts";
8
+ import type { RequestOptions } from "./http-helpers/index.ts";
9
+ import { OrderBuilder } from "./order-builder/builder.ts";
10
+ import type { IRfqClient } from "./rfq-deps.ts";
11
+ export declare class ClobClient {
12
+ readonly host: string;
13
+ readonly chainId: Chain;
14
+ readonly signer?: Wallet | JsonRpcSigner;
15
+ readonly creds?: ApiKeyCreds;
16
+ readonly orderBuilder: OrderBuilder;
17
+ readonly tickSizes: TickSizes;
18
+ readonly negRisk: NegRisk;
19
+ readonly feeRates: FeeRates;
20
+ readonly geoBlockToken?: string;
21
+ readonly useServerTime?: boolean;
22
+ readonly builderConfig?: BuilderConfig;
23
+ readonly rfq: IRfqClient;
24
+ readonly retryOnError?: boolean;
25
+ readonly throwOnError: boolean;
26
+ private tickSizeTimestamps;
27
+ private readonly tickSizeTtlMs;
28
+ constructor(host: string, chainId: Chain, signer?: Wallet | JsonRpcSigner, creds?: ApiKeyCreds, signatureType?: SignatureType, funderAddress?: string, geoBlockToken?: string, useServerTime?: boolean, builderConfig?: BuilderConfig, getSigner?: () => Promise<Wallet | JsonRpcSigner> | (Wallet | JsonRpcSigner), retryOnError?: boolean, tickSizeTtlMs?: number, throwOnError?: boolean);
29
+ getOk(): Promise<any>;
30
+ getServerTime(): Promise<number>;
31
+ getSamplingSimplifiedMarkets(next_cursor?: string): Promise<PaginationPayload>;
32
+ getSamplingMarkets(next_cursor?: string): Promise<PaginationPayload>;
33
+ getSimplifiedMarkets(next_cursor?: string): Promise<PaginationPayload>;
34
+ getMarkets(next_cursor?: string): Promise<PaginationPayload>;
35
+ getMarket(conditionID: string): Promise<any>;
36
+ getOrderBook(tokenID: string): Promise<OrderBookSummary>;
37
+ getOrderBooks(params: BookParams[]): Promise<OrderBookSummary[]>;
38
+ getTickSize(tokenID: string): Promise<TickSize>;
39
+ /**
40
+ * Clears the tick size cache, forcing fresh fetches on the next access.
41
+ * @param tokenID - If provided, only clears the cache for this token. Otherwise clears all.
42
+ */
43
+ clearTickSizeCache(tokenID?: string): void;
44
+ getNegRisk(tokenID: string): Promise<boolean>;
45
+ getFeeRateBps(tokenID: string): Promise<number>;
46
+ /**
47
+ * Calculates the hash for the given orderbook
48
+ * @param orderbook
49
+ * @returns
50
+ */
51
+ getOrderBookHash(orderbook: OrderBookSummary): Promise<string>;
52
+ getMidpoint(tokenID: string): Promise<any>;
53
+ getMidpoints(params: BookParams[]): Promise<any>;
54
+ getPrice(tokenID: string, side: string): Promise<any>;
55
+ getPrices(params: BookParams[]): Promise<any>;
56
+ getSpread(tokenID: string): Promise<any>;
57
+ getSpreads(params: BookParams[]): Promise<any>;
58
+ getLastTradePrice(tokenID: string): Promise<any>;
59
+ getLastTradesPrices(params: BookParams[]): Promise<any>;
60
+ getPricesHistory(params: PriceHistoryFilterParams): Promise<MarketPrice[]>;
61
+ /**
62
+ * Creates a new API key for a user
63
+ * @param nonce
64
+ * @returns ApiKeyCreds
65
+ */
66
+ createApiKey(nonce?: number): Promise<ApiKeyCreds>;
67
+ /**
68
+ * Derives an existing API key for a user
69
+ * @param nonce
70
+ * @returns ApiKeyCreds
71
+ */
72
+ deriveApiKey(nonce?: number): Promise<ApiKeyCreds>;
73
+ createOrDeriveApiKey(nonce?: number): Promise<ApiKeyCreds>;
74
+ getApiKeys(): Promise<ApiKeysResponse>;
75
+ getClosedOnlyMode(): Promise<BanStatus>;
76
+ deleteApiKey(): Promise<any>;
77
+ /**
78
+ * Creates a new readonly API key for a user
79
+ * @returns ReadonlyApiKeyResponse
80
+ */
81
+ createReadonlyApiKey(): Promise<ReadonlyApiKeyResponse>;
82
+ getReadonlyApiKeys(): Promise<string[]>;
83
+ /**
84
+ * Deletes a readonly API key for a user
85
+ * @param key The readonly API key to delete
86
+ * @returns boolean
87
+ */
88
+ deleteReadonlyApiKey(key: string): Promise<boolean>;
89
+ /**
90
+ * Validates a readonly API key for a given address
91
+ * @param address The wallet address
92
+ * @param key The readonly API key to validate
93
+ * @returns string
94
+ */
95
+ validateReadonlyApiKey(address: string, key: string): Promise<string>;
96
+ getOrder(orderID: string): Promise<OpenOrder>;
97
+ getTrades(params?: TradeParams, only_first_page?: boolean, next_cursor?: string): Promise<Trade[]>;
98
+ getTradesPaginated(params?: TradeParams, next_cursor?: string): Promise<{
99
+ trades: Trade[];
100
+ next_cursor: string;
101
+ limit: number;
102
+ count: number;
103
+ }>;
104
+ getBuilderTrades(params?: TradeParams, next_cursor?: string): Promise<{
105
+ trades: BuilderTrade[];
106
+ next_cursor: string;
107
+ limit: number;
108
+ count: number;
109
+ }>;
110
+ getNotifications(): Promise<Notification[]>;
111
+ dropNotifications(params?: DropNotificationParams): Promise<void>;
112
+ getBalanceAllowance(params?: BalanceAllowanceParams): Promise<BalanceAllowanceResponse>;
113
+ updateBalanceAllowance(params?: BalanceAllowanceParams): Promise<void>;
114
+ createOrder(userOrder: UserOrder, options?: Partial<CreateOrderOptions>): Promise<SignedOrder>;
115
+ createMarketOrder(userMarketOrder: UserMarketOrder, options?: Partial<CreateOrderOptions>): Promise<SignedOrder>;
116
+ createAndPostOrder<T extends OrderType.GTC | OrderType.GTD = OrderType.GTC>(userOrder: UserOrder, options?: Partial<CreateOrderOptions>, orderType?: T, deferExec?: boolean, postOnly?: boolean): Promise<any>;
117
+ createAndPostMarketOrder<T extends OrderType.FOK | OrderType.FAK = OrderType.FOK>(userMarketOrder: UserMarketOrder, options?: Partial<CreateOrderOptions>, orderType?: T, deferExec?: boolean): Promise<any>;
118
+ getOpenOrders(params?: OpenOrderParams, only_first_page?: boolean, next_cursor?: string): Promise<OpenOrdersResponse>;
119
+ postOrder<T extends OrderType = OrderType.GTC>(order: SignedOrder, orderType?: T, deferExec?: boolean, postOnly?: boolean): Promise<any>;
120
+ postOrders(args: PostOrdersArgs[], deferExec?: boolean, defaultPostOnly?: boolean): Promise<any>;
121
+ cancelOrder(payload: OrderPayload): Promise<any>;
122
+ cancelOrders(ordersHashes: string[]): Promise<any>;
123
+ cancelAll(): Promise<any>;
124
+ /**
125
+ * Sends a heartbeat to the server to keep the session active.
126
+ *
127
+ * If heartbeats are started and one isn't sent within 10s, all orders will be cancelled.
128
+ * Requires Level 2 authentication.
129
+ *
130
+ * Pass the previously returned `heartbeat_id` to chain heartbeats.
131
+ * Pass `undefined`/`null` to start a new heartbeat chain.
132
+ */
133
+ postHeartbeat(heartbeatId?: string | null): Promise<HeartbeatResponse>;
134
+ cancelMarketOrders(payload: OrderMarketCancelParams): Promise<any>;
135
+ isOrderScoring(params?: OrderScoringParams): Promise<OrderScoring>;
136
+ areOrdersScoring(params?: OrdersScoringParams): Promise<OrdersScoring>;
137
+ getEarningsForUserForDay(date: string): Promise<UserEarning[]>;
138
+ getTotalEarningsForUserForDay(date: string): Promise<TotalUserEarning[]>;
139
+ getUserEarningsAndMarketsConfig(date: string, order_by?: string, position?: string, no_competition?: boolean): Promise<UserRewardsEarning[]>;
140
+ getRewardPercentages(): Promise<RewardsPercentages>;
141
+ getCurrentRewards(): Promise<MarketReward[]>;
142
+ getRawRewardsForMarket(conditionId: string): Promise<MarketReward[]>;
143
+ getMarketTradesEvents(conditionID: string): Promise<MarketTradeEvent[]>;
144
+ calculateMarketPrice(tokenID: string, side: Side, amount: number, orderType?: OrderType): Promise<number>;
145
+ createBuilderApiKey(): Promise<BuilderApiKey>;
146
+ getBuilderApiKeys(): Promise<BuilderApiKeyResponse[]>;
147
+ revokeBuilderApiKey(): Promise<any>;
148
+ protected _resolveTickSize(tokenID: string, tickSize?: TickSize): Promise<TickSize>;
149
+ protected get(endpoint: string, options?: RequestOptions): Promise<any>;
150
+ protected post(endpoint: string, options?: RequestOptions): Promise<any>;
151
+ protected put(endpoint: string, options?: RequestOptions): Promise<any>;
152
+ protected del(endpoint: string, options?: RequestOptions): Promise<any>;
153
+ private throwIfError;
154
+ private canL1Auth;
155
+ private canL2Auth;
156
+ private mustBuilderAuth;
157
+ private canBuilderAuth;
158
+ private _resolveFeeRateBps;
159
+ private _generateBuilderHeaders;
160
+ private _getBuilderHeaders;
161
+ /**
162
+ * Opportunistically updates the tick size cache from an order book response.
163
+ */
164
+ private updateTickSizeFromOrderBook;
165
+ }