@varla/polymarket 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/AGENTS.md +221 -10
  2. package/README.md +282 -95
  3. package/dist/index.js +467 -5101
  4. package/package.json +6 -6
package/README.md CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/@varla/polymarket.svg)](https://www.npmjs.com/package/@varla/polymarket)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
- [![CI](https://github.com/Varla-xyz/varla-api/actions/workflows/ci.yml/badge.svg)](https://github.com/Varla-xyz/varla-api/actions/workflows/ci.yml)
6
5
 
7
6
  **High-performance Polymarket SDK** — 139% faster than the official SDK.
8
7
 
@@ -63,7 +62,7 @@ import { GammaClient, ClobClient } from "@varla/polymarket";
63
62
 
64
63
  // Fetch markets from Gamma API
65
64
  const gamma = new GammaClient();
66
- const markets = await gamma.getMarkets({ limit: 10 });
65
+ const markets = await gamma.listMarkets({ active: true, limit: 10 });
67
66
 
68
67
  // Get orderbook from CLOB
69
68
  const clob = new ClobClient();
@@ -73,7 +72,6 @@ console.log("Best ask:", book.asks[0]);
73
72
 
74
73
  // Get prices
75
74
  const midpoint = await clob.getMidpoint(markets[0].clobTokenIds[0]);
76
- const spread = await clob.getSpread(markets[0].clobTokenIds[0]);
77
75
  ```
78
76
 
79
77
  ### Authenticated Trading
@@ -142,85 +140,295 @@ ws.subscribeMarket("0x5f65177b394277fd294cd75650044e32ba009a95...");
142
140
  ws.close();
143
141
  ```
144
142
 
145
- ### Order Persistence (SQLite)
143
+ ---
146
144
 
147
- Track orders locally for fast queries and restart recovery:
145
+ ## Complete API Reference
146
+
147
+ ### GammaClient (28 methods)
148
+
149
+ Market discovery, metadata, and content API.
148
150
 
149
151
  ```typescript
150
- import {
151
- ClobTradingClient,
152
- createSqliteOrderPersistence,
153
- createTrackedOrder,
154
- } from "@varla/polymarket";
155
-
156
- const persistence = createSqliteOrderPersistence("./orders.db");
157
-
158
- // Track placed orders
159
- const order = createTrackedOrder({
160
- orderId: response.orderID,
161
- market: "0x...",
162
- tokenId: "123456...",
163
- side: "BUY",
164
- price: 0.5,
165
- size: 100,
166
- orderType: "GTC",
167
- status: "OPEN",
168
- });
169
- await persistence.save(order);
152
+ import { GammaClient } from "@varla/polymarket";
153
+ const gamma = new GammaClient(opts?: GammaClientOpts);
154
+ ```
155
+
156
+ #### Markets
157
+
158
+ | Method | Parameters | Returns |
159
+ |--------|------------|---------|
160
+ | `listMarkets` | `{ active?, closed?, limit?, offset?, tag_id? }` | `Promise<GammaMarket[]>` |
161
+ | `getMarketsByConditionIds` | `conditionIds: string[]` | `Promise<GammaMarket[]>` |
162
+ | `getMarketById` | `id: string` | `Promise<GammaMarket>` |
163
+ | `getMarketBySlug` | `slug: string` | `Promise<GammaMarket>` |
164
+ | `getMarketTags` | `marketId: string` | `Promise<GammaTag[]>` |
165
+
166
+ #### Events
167
+
168
+ | Method | Parameters | Returns |
169
+ |--------|------------|---------|
170
+ | `listEvents` | `{ active?, closed?, limit?, offset?, tag_id?, series_id?, order?, ascending? }` | `Promise<GammaEvent[]>` |
171
+ | `getEventById` | `id: string` | `Promise<GammaEvent>` |
172
+ | `getEventBySlug` | `slug: string` | `Promise<GammaEvent>` |
173
+ | `getEventTags` | `eventId: string` | `Promise<GammaTag[]>` |
174
+
175
+ #### Tags
176
+
177
+ | Method | Parameters | Returns |
178
+ |--------|------------|---------|
179
+ | `listTags` | `{ limit?, offset? }` | `Promise<GammaTag[]>` |
180
+ | `getTagById` | `id: string` | `Promise<GammaTag>` |
181
+ | `getTagBySlug` | `slug: string` | `Promise<GammaTag>` |
182
+ | `getRelatedTagsById` | `id: string` | `Promise<GammaTag[]>` |
183
+ | `getRelatedTagsBySlug` | `slug: string` | `Promise<GammaTag[]>` |
184
+ | `getTagRelationshipsById` | `id: string` | `Promise<GammaTagRelationship[]>` |
185
+ | `getTagRelationshipsBySlug` | `slug: string` | `Promise<GammaTagRelationship[]>` |
186
+
187
+ #### Sports
188
+
189
+ | Method | Parameters | Returns |
190
+ |--------|------------|---------|
191
+ | `getSports` | — | `Promise<GammaSport[]>` |
192
+ | `listTeams` | `{ sport?, limit?, offset? }` | `Promise<GammaTeam[]>` |
193
+ | `getValidSportsMarketTypes` | — | `Promise<GammaSportsMarketType[]>` |
194
+
195
+ #### Series
196
+
197
+ | Method | Parameters | Returns |
198
+ |--------|------------|---------|
199
+ | `listSeries` | `{ limit?, offset? }` | `Promise<GammaSeries[]>` |
200
+ | `getSeriesById` | `id: string` | `Promise<GammaSeries>` |
201
+
202
+ #### Search
203
+
204
+ | Method | Parameters | Returns |
205
+ |--------|------------|---------|
206
+ | `search` | `query: string, { limit?, offset? }` | `Promise<GammaSearchResult>` |
207
+
208
+ #### Profiles
209
+
210
+ | Method | Parameters | Returns |
211
+ |--------|------------|---------|
212
+ | `getPublicProfile` | `address: string` | `Promise<GammaPublicProfile>` |
213
+
214
+ #### Comments
215
+
216
+ | Method | Parameters | Returns |
217
+ |--------|------------|---------|
218
+ | `listComments` | `{ eventId?, marketId?, limit?, offset? }` | `Promise<GammaComment[]>` |
219
+ | `getCommentById` | `id: string` | `Promise<GammaComment>` |
220
+ | `getCommentsByUser` | `address: string, { limit?, offset? }` | `Promise<GammaComment[]>` |
221
+
222
+ #### Builders
223
+
224
+ | Method | Parameters | Returns |
225
+ |--------|------------|---------|
226
+ | `getBuilderLeaderboard` | `{ period?, limit?, offset? }` | `Promise<GammaBuilderLeaderboardEntry[]>` |
227
+ | `getBuilderVolume` | `{ address?, startDate?, endDate? }` | `Promise<GammaBuilderVolumePoint[]>` |
228
+
229
+ #### Health
230
+
231
+ | Method | Parameters | Returns |
232
+ |--------|------------|---------|
233
+ | `healthCheck` | — | `Promise<{ status: string }>` |
234
+
235
+ ---
236
+
237
+ ### ClobClient (5 methods)
238
+
239
+ Public orderbook, prices, and trades (read-only with resilience).
240
+
241
+ ```typescript
242
+ import { ClobClient } from "@varla/polymarket";
243
+ const clob = new ClobClient(config?: Partial<ClobClientConfig>);
244
+ ```
245
+
246
+ | Method | Parameters | Returns |
247
+ |--------|------------|---------|
248
+ | `getMidpoint` | `tokenId: string` | `Promise<ClobMidpointResponse>` |
249
+ | `getBook` | `tokenId: string` | `Promise<ClobBookResponse>` |
250
+ | `getPricesHistory` | `{ tokenId, startTs, endTs, interval, fidelity? }` | `Promise<ClobPricesHistoryResponse>` |
251
+ | `getMetrics` | — | `ClobClientMetrics` |
252
+ | `getCircuitState` | — | `CircuitState` |
253
+ | `resetCircuitBreaker` | — | `void` |
254
+
255
+ ---
256
+
257
+ ### ClobTradingClient (30 methods)
258
+
259
+ Authenticated trading operations.
260
+
261
+ ```typescript
262
+ import { ClobTradingClient } from "@varla/polymarket";
263
+ const client = new ClobTradingClient(walletClient, config?: ClobTradingClientConfig);
264
+ ```
265
+
266
+ #### Initialization
267
+
268
+ | Method | Parameters | Returns |
269
+ |--------|------------|---------|
270
+ | `init` | — | `Promise<void>` |
271
+ | `isInitialized` | — | `boolean` |
272
+ | `getCredentials` | — | `ApiKeyCreds` |
273
+ | `setCredentials` | `creds: ApiKeyCreds` | `void` |
274
+
275
+ #### Orders
170
276
 
171
- // Query orders
172
- const openOrders = await persistence.getOpen();
173
- const marketOrders = await persistence.getByMarket("0x...");
277
+ | Method | Parameters | Returns |
278
+ |--------|------------|---------|
279
+ | `createOrder` | `params: OrderParams, options: MarketOptions` | `Promise<SignedOrder>` |
280
+ | `postOrder` | `signedOrder: SignedOrder, orderType?: OrderType` | `Promise<OrderResponse>` |
281
+ | `createAndPostOrder` | `params: OrderParams, options: MarketOptions, orderType?: OrderType` | `Promise<OrderResponse>` |
282
+ | `postOrders` | `signedOrders: SignedOrder[], orderType?: OrderType` | `Promise<BatchOrderResponse>` |
283
+ | `createAndPostOrders` | `ordersParams: Array<{params, options}>, orderType?: OrderType` | `Promise<BatchOrderResponse>` |
284
+ | `getOrder` | `orderId: string` | `Promise<Order>` |
285
+ | `getOpenOrders` | `filter?: OpenOrdersFilter` | `Promise<Order[]>` |
174
286
 
175
- // Cleanup old orders
176
- await persistence.prune(24 * 60 * 60 * 1000); // 24h
287
+ #### Market Orders
177
288
 
178
- persistence.close();
289
+ | Method | Parameters | Returns |
290
+ |--------|------------|---------|
291
+ | `marketBuy` | `params: MarketBuyParams, options: MarketOptions, timeInForce?: TimeInForce` | `Promise<OrderResponse>` |
292
+ | `marketSell` | `params: MarketSellParams, options: MarketOptions, timeInForce?: TimeInForce` | `Promise<OrderResponse>` |
293
+ | `getOrderBook` | `tokenId: string` | `Promise<OrderBook>` |
294
+
295
+ #### Cancellation
296
+
297
+ | Method | Parameters | Returns |
298
+ |--------|------------|---------|
299
+ | `cancelOrder` | `orderId: string` | `Promise<CancelResponse>` |
300
+ | `cancelOrders` | `orderIds: string[]` | `Promise<CancelResponse>` |
301
+ | `cancelMarketOrders` | `{ market?, asset_id? }` | `Promise<CancelResponse>` |
302
+ | `cancelAll` | — | `Promise<CancelResponse>` |
303
+
304
+ #### Account
305
+
306
+ | Method | Parameters | Returns |
307
+ |--------|------------|---------|
308
+ | `getClosedOnlyMode` | — | `Promise<{ closedOnly: boolean; reason?: string }>` |
309
+ | `getApiKeys` | — | `Promise<{ apiKeys: Array<{apiKey, createdAt}> }>` |
310
+ | `deleteApiKey` | — | `Promise<void>` |
311
+
312
+ #### Trades & History
313
+
314
+ | Method | Parameters | Returns |
315
+ |--------|------------|---------|
316
+ | `getTrades` | `filter?: TradeHistoryFilter` | `Promise<Trade[]>` |
317
+ | `heartbeat` | — | `Promise<HeartbeatResponse>` |
318
+
319
+ #### Rewards
320
+
321
+ | Method | Parameters | Returns |
322
+ |--------|------------|---------|
323
+ | `getEarningsForDay` | `date: string` | `Promise<UserEarnings>` |
324
+ | `getTotalEarningsForDay` | `date: string` | `Promise<UserEarnings>` |
325
+ | `getLiquidityRewardPercentages` | — | `Promise<LiquidityRewardPercentages>` |
326
+ | `getRewardsMarkets` | — | `Promise<RewardsMarket[]>` |
327
+
328
+ #### Builder API
329
+
330
+ | Method | Parameters | Returns |
331
+ |--------|------------|---------|
332
+ | `createBuilderApiKey` | — | `Promise<BuilderApiKey>` |
333
+ | `getBuilderApiKeys` | — | `Promise<BuilderApiKeyInfo[]>` |
334
+ | `revokeBuilderApiKey` | — | `Promise<void>` |
335
+ | `getBuilderTrades` | `filter?: BuilderTradesFilter, cursor?: string` | `Promise<BuilderTradesResponse>` |
336
+
337
+ ---
338
+
339
+ ### DataApiClient (16 methods)
340
+
341
+ Positions, trades, leaderboards, and analytics.
342
+
343
+ ```typescript
344
+ import { DataApiClient } from "@varla/polymarket";
345
+ const data = new DataApiClient(opts?: DataApiClientOpts);
179
346
  ```
180
347
 
181
- ## API Reference
348
+ #### Positions
349
+
350
+ | Method | Parameters | Returns |
351
+ |--------|------------|---------|
352
+ | `getCurrentPositions` | `{ address, conditionId?, limit?, offset? }` | `Promise<DataPosition[]>` |
353
+ | `getClosedPositions` | `{ address, limit?, offset? }` | `Promise<DataClosedPosition[]>` |
354
+ | `getTotalPositionValue` | `address: string` | `Promise<DataPositionValue>` |
355
+
356
+ #### Trades & Activity
357
+
358
+ | Method | Parameters | Returns |
359
+ |--------|------------|---------|
360
+ | `getTrades` | `{ address?, conditionId?, limit?, offset? }` | `Promise<DataTrade[]>` |
361
+ | `getUserActivity` | `{ address, limit?, offset? }` | `Promise<DataActivity[]>` |
362
+
363
+ #### Analytics
364
+
365
+ | Method | Parameters | Returns |
366
+ |--------|------------|---------|
367
+ | `getTopHolders` | `{ conditionId, outcomeIndex?, limit? }` | `Promise<DataTopHolder[]>` |
368
+ | `getTraderLeaderboard` | `{ period?, category?, order?, limit?, offset? }` | `Promise<DataLeaderboardEntry[]>` |
369
+ | `getOpenInterest` | `{ conditionId?, asset? }` | `Promise<DataOpenInterest>` |
370
+ | `getLiveVolume` | `{ eventId?, conditionId? }` | `Promise<DataVolume>` |
371
+
372
+ #### Pricing & Orderbook
182
373
 
183
- ### Clients
374
+ | Method | Parameters | Returns |
375
+ |--------|------------|---------|
376
+ | `getPriceHistory` | `{ tokenId, interval?, startTs?, endTs?, fidelity? }` | `Promise<DataPriceHistory>` |
377
+ | `getOrderBookSummary` | `tokenId: string` | `Promise<DataOrderBookSummary>` |
378
+ | `getOrderBookSummaries` | `tokenIds: string[]` | `Promise<DataOrderBookSummary[]>` |
379
+ | `getSpreads` | `tokenIds: string[]` | `Promise<DataSpread[]>` |
184
380
 
185
- | Client | Purpose |
186
- |--------|---------|
187
- | `GammaClient` | Market discovery, metadata, token IDs |
188
- | `ClobClient` | Public orderbook, prices, trades (read-only) |
189
- | `ClobTradingClient` | Authenticated trading operations |
190
- | `ClobWsClient` | Real-time WebSocket updates |
381
+ #### Misc
191
382
 
192
- ### ClobTradingClient Methods
383
+ | Method | Parameters | Returns |
384
+ |--------|------------|---------|
385
+ | `getTotalMarketsTraded` | `address: string` | `Promise<DataMarketsTraded>` |
386
+ | `getAccountingSnapshot` | `address: string` | `Promise<Response>` |
387
+ | `healthCheck` | — | `Promise<{ status: string }>` |
388
+
389
+ ---
390
+
391
+ ### BridgeClient (5 methods)
392
+
393
+ Deposit/withdrawal functionality through the Relay bridge.
193
394
 
194
395
  ```typescript
195
- // Initialization
196
- client.init() // Derive/create API key
197
- client.setCredentials(creds) // Set API key manually
198
-
199
- // Orders
200
- client.createOrder(params, options) // Create signed order
201
- client.postOrder(order, type) // Post to CLOB
202
- client.createAndPostOrder(params, options, type) // Combined
203
- client.getOrder(orderId) // Get order details
204
- client.getOpenOrders(filter?) // List open orders
205
-
206
- // Cancellation
207
- client.cancelOrder(orderId) // Cancel one order
208
- client.cancelOrders(orderIds) // Cancel multiple
209
- client.cancelMarketOrders({ market }) // Cancel by market
210
- client.cancelAll() // Cancel all orders
211
-
212
- // Account
213
- client.getClosedOnlyMode() // Check if account is restricted
396
+ import { BridgeClient } from "@varla/polymarket";
397
+ const bridge = new BridgeClient(opts?: BridgeClientOpts);
214
398
  ```
215
399
 
216
- ### Order Types
400
+ | Method | Parameters | Returns |
401
+ |--------|------------|---------|
402
+ | `createDepositAddresses` | `{ userAddress, chain, asset? }` | `Promise<BridgeDepositAddress>` |
403
+ | `createWithdrawalAddresses` | `{ userAddress, destinationAddress, chain, asset, amount }` | `Promise<BridgeWithdrawalAddress>` |
404
+ | `getQuote` | `{ fromChain, toChain, fromAsset, toAsset, amount }` | `Promise<BridgeQuote>` |
405
+ | `getSupportedAssets` | — | `Promise<BridgeSupportedAsset[]>` |
406
+ | `getTransactionStatus` | `transactionId: string` | `Promise<BridgeTransactionStatus>` |
407
+
408
+ ---
409
+
410
+ ### ClobWsClient (7 methods)
411
+
412
+ Real-time WebSocket updates.
217
413
 
218
414
  ```typescript
219
- type Side = "BUY" | "SELL";
220
- type OrderType = "GTC" | "GTD" | "FOK"; // Good-Til-Cancel, Good-Til-Date, Fill-Or-Kill
221
- type OrderStatus = "OPEN" | "MATCHED" | "FILLED" | "CANCELLED" | "EXPIRED";
415
+ import { ClobWsClient } from "@varla/polymarket";
416
+ const ws = new ClobWsClient(events?: ClobWsEvents, config?: ClobWsConfig);
222
417
  ```
223
418
 
419
+ | Method | Parameters | Returns |
420
+ |--------|------------|---------|
421
+ | `connect` | — | `void` |
422
+ | `close` | — | `void` |
423
+ | `subscribeMarket` | `market: string` | `void` |
424
+ | `unsubscribeMarket` | `market: string` | `void` |
425
+ | `subscribeMarkets` | `markets: string[]` | `void` |
426
+ | `getState` | — | `ClobWsState` |
427
+ | `isConnected` | — | `boolean` |
428
+ | `getSubscribedMarkets` | — | `string[]` |
429
+
430
+ ---
431
+
224
432
  ## Configuration
225
433
 
226
434
  ### Environment Variables
@@ -279,29 +487,6 @@ try {
279
487
  }
280
488
  ```
281
489
 
282
- ## Validation Utilities
283
-
284
- ```typescript
285
- import {
286
- validateOrderParams,
287
- roundToTickSize,
288
- isValidPrice,
289
- getMinimumOrderSize,
290
- } from "@varla/polymarket";
291
-
292
- // Validate before creating
293
- validateOrderParams(params, options); // throws OrderValidationError
294
-
295
- // Round to valid tick
296
- const price = roundToTickSize(0.505, "0.01"); // 0.51
297
-
298
- // Check price validity
299
- isValidPrice(0.505, "0.01"); // false
300
-
301
- // Get minimum shares for $1 order
302
- getMinimumOrderSize(0.5); // 2 shares
303
- ```
304
-
305
490
  ## Testing
306
491
 
307
492
  ```bash
@@ -337,17 +522,19 @@ POLYMARKET_LIVE_TESTS=1 bun test packages/polymarket/test/live
337
522
  └─────────────────────────────────────────────────────────────┘
338
523
 
339
524
  ┌──────────────────┐ ┌─────────────────┐ ┌────────────────┐
340
- OrderPersistence │ │ ClobWsClient │ │ GammaClient
341
- │ - SQLite/Memory │ │ - WebSocket │ │ - Markets
342
- │ - Order tracking │ │ - Book updates │ │ - Metadata
525
+ GammaClient │ │ DataApiClient │ │ BridgeClient
526
+ │ - 28 methods │ │ - 16 methods │ │ - 5 methods
527
+ │ - Markets, Tags │ │ - Positions │ │ - Deposits
528
+ │ - Events, Sports │ │ - Analytics │ │ - Withdrawals │
343
529
  └──────────────────┘ └─────────────────┘ └────────────────┘
344
- ```
345
530
 
346
- ## Related Packages
347
-
348
- - [`@varla/db`](../db) - SQLite persistence layer
349
- - [`@varla/rpc`](../rpc) - Resilient RPC client
350
- - [`@varla/runtime`](../runtime) - Service runtime utilities
531
+ ┌──────────────────┐ ┌─────────────────┐
532
+ │ ClobClient │ │ ClobWsClient │
533
+ - 5 methods │ │ - 7 methods │
534
+ - Circuit break │ │ - Real-time │
535
+ - Rate limiting │ │ - Auto-reconnect│
536
+ └──────────────────┘ └─────────────────┘
537
+ ```
351
538
 
352
539
  ## License
353
540