@polymarket/clob-client-v2 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +107 -1
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1 +1,107 @@
|
|
|
1
|
-
# Polymarket CLOB Client
|
|
1
|
+
# Polymarket CLOB Client V2
|
|
2
|
+
|
|
3
|
+
<a href='https://www.npmjs.com/package/@polymarket/clob-client-v2'>
|
|
4
|
+
<img src='https://img.shields.io/npm/v/@polymarket/clob-client-v2.svg' alt='NPM'/>
|
|
5
|
+
</a>
|
|
6
|
+
|
|
7
|
+
TypeScript client for the Polymarket CLOB (v2)
|
|
8
|
+
|
|
9
|
+
### Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// npm install @polymarket/clob-client-v2
|
|
13
|
+
// npm install viem
|
|
14
|
+
|
|
15
|
+
import { ApiKeyCreds, Chain, ClobClient, OrderType, Side } from "@polymarket/clob-client-v2";
|
|
16
|
+
import { createWalletClient, http } from "viem";
|
|
17
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
18
|
+
|
|
19
|
+
const host = "<polymarket-clob-host>";
|
|
20
|
+
const chainId = Chain.POLYGON; // or Chain.AMOY for testnet
|
|
21
|
+
|
|
22
|
+
const account = privateKeyToAccount("0x..."); // your private key
|
|
23
|
+
const walletClient = createWalletClient({ account, transport: http() });
|
|
24
|
+
|
|
25
|
+
// Step 1: obtain API credentials using your wallet (L1 auth)
|
|
26
|
+
const clobClient = new ClobClient({ host, chain: chainId, signer: walletClient });
|
|
27
|
+
const creds = await clobClient.createOrDeriveApiKey();
|
|
28
|
+
|
|
29
|
+
// Step 2: initialize a fully-authenticated client (L1 + L2)
|
|
30
|
+
const client = new ClobClient({ host, chain: chainId, signer: walletClient, creds });
|
|
31
|
+
|
|
32
|
+
// Place a resting limit buy (GTC)
|
|
33
|
+
const resp = await client.createAndPostOrder(
|
|
34
|
+
{
|
|
35
|
+
tokenID: "", // token ID of the market outcome — get from https://docs.polymarket.com
|
|
36
|
+
price: 0.4,
|
|
37
|
+
side: Side.BUY,
|
|
38
|
+
size: 100,
|
|
39
|
+
},
|
|
40
|
+
{ tickSize: "0.01" },
|
|
41
|
+
OrderType.GTC,
|
|
42
|
+
);
|
|
43
|
+
console.log(resp);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
See [examples](examples/) for more information.
|
|
47
|
+
|
|
48
|
+
### Market Orders
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
// Market buy — amount is in USDC
|
|
52
|
+
// OrderType.FOK: entire order must fill immediately or it is cancelled
|
|
53
|
+
// OrderType.FAK: fills as much as possible, remainder is cancelled
|
|
54
|
+
const resp = await client.createAndPostMarketOrder(
|
|
55
|
+
{
|
|
56
|
+
tokenID: "",
|
|
57
|
+
amount: 100, // USDC
|
|
58
|
+
side: Side.BUY,
|
|
59
|
+
orderType: OrderType.FOK,
|
|
60
|
+
},
|
|
61
|
+
{ tickSize: "0.01" },
|
|
62
|
+
OrderType.FOK,
|
|
63
|
+
);
|
|
64
|
+
console.log(resp);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Authentication
|
|
68
|
+
|
|
69
|
+
The client has two authentication levels:
|
|
70
|
+
|
|
71
|
+
**L1** — wallet signature (EIP-712). Required to create or derive API keys.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const client = new ClobClient({ host, chain: chainId, signer: walletClient });
|
|
75
|
+
const creds = await client.createOrDeriveApiKey();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**L2** — HMAC with API credentials. Required for order placement, cancellation, and account data.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const creds: ApiKeyCreds = {
|
|
82
|
+
key: process.env.CLOB_API_KEY,
|
|
83
|
+
secret: process.env.CLOB_SECRET,
|
|
84
|
+
passphrase: process.env.CLOB_PASS_PHRASE,
|
|
85
|
+
};
|
|
86
|
+
const client = new ClobClient({ host, chain: chainId, signer: walletClient, creds });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Error Handling
|
|
90
|
+
|
|
91
|
+
By default, API errors are returned as `{ error: "...", status: ... }` objects. To have the client throw instead, pass `throwOnError: true`:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { ApiError, ClobClient } from "@polymarket/clob-client-v2";
|
|
95
|
+
|
|
96
|
+
const client = new ClobClient({ host, chain: chainId, signer: walletClient, creds, throwOnError: true });
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const book = await client.getOrderBook(tokenID);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
if (e instanceof ApiError) {
|
|
102
|
+
console.log(e.message); // "No orderbook exists for the requested token id"
|
|
103
|
+
console.log(e.status); // 404
|
|
104
|
+
console.log(e.data); // full error response object
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|