Orders
Order operations allow you to create new orders, query your existing orders, and cancel orders on the CLOB.
Methods
Section titled “Methods”Create Order
Section titled “Create Order”Create a new order:
const order = await client.order.createOrder({ price: 0.5, side: "BUY", size: 10, tokenId: "your-token-id", expiration: 1000000000, taker: "public",});Parameters:
price(number): Price per share, must be between 0.0 and 1.0side(string): Order side, either"BUY"or"SELL"size(number): Number of shares to tradetokenId(string): The token ID for the market outcomeexpiration(number): Unix timestamp when the order expirestaker(string): Either"public"for any taker, or a specific Ethereum address
Returns: Order
List Orders
Section titled “List Orders”Get all your open orders:
const openOrders = await client.order.list();Get orders with filters:
const filteredOrders = await client.order.list({ marketId: "specific-market-id", status: "LIVE",});Parameters:
marketId(string, optional): Filter by specific marketstatus(string, optional): Filter by order status ("LIVE","FILLED","CANCELLED")
Returns: Order[]
Cancel Order
Section titled “Cancel Order”Cancel a specific order:
await client.order.cancel({ orderId: "your-order-id"});Parameters:
orderId(string): The ID of the order to cancel
Returns: void
Cancel All Orders
Section titled “Cancel All Orders”Cancel all open orders:
await client.order.cancelAll();Cancel all orders for a specific market:
await client.order.cancelAll({ marketId: "specific-market-id"});Parameters:
marketId(string, optional): Cancel only orders for this market
Returns: void
Order Type
Section titled “Order Type”interface Order { orderId: string; marketId: string; tokenId: string; side: "BUY" | "SELL"; price: string; size: string; filled: string; remaining: string; status: "LIVE" | "FILLED" | "CANCELLED" | "EXPIRED"; createdAt: number; updatedAt: number; // ... additional fields}Example Usage
Section titled “Example Usage”Create a Buy Order
Section titled “Create a Buy Order”import { Clob, createConnectedWallet } from "@dicedhq/clob";
const wallet = createConnectedWallet({ privateKey: process.env.PRIVATE_KEY, chain: "polygon",});
const credentials = { key: process.env.POLYMARKET_API_KEY, secret: process.env.POLYMARKET_SECRET, passphrase: process.env.POLYMARKET_PASSPHRASE,};
const client = new Clob({ wallet, credentials });
// Create a buy orderconst order = await client.order.createOrder({ price: 0.55, // Buy at 55 cents side: "BUY", size: 100, // 100 shares tokenId: "0x1234567890abcdef...", expiration: Math.floor(Date.now() / 1000) + 86400, // 24 hours from now taker: "public",});
console.log(`Order created: ${order.orderId}`);console.log(`Status: ${order.status}`);Create a Sell Order
Section titled “Create a Sell Order”// Create a sell orderconst order = await client.order.createOrder({ price: 0.65, // Sell at 65 cents side: "SELL", size: 50, // 50 shares tokenId: "0x1234567890abcdef...", expiration: Math.floor(Date.now() / 1000) + 86400, taker: "public",});
console.log(`Sell order created: ${order.orderId}`);List Your Orders
Section titled “List Your Orders”// Get all open ordersconst openOrders = await client.order.list();
console.log(`You have ${openOrders.length} open orders`);
for (const order of openOrders) { console.log(`Order ${order.orderId}:`); console.log(` Side: ${order.side}`); console.log(` Price: ${order.price}`); console.log(` Size: ${order.size}`); console.log(` Filled: ${order.filled}`); console.log(` Remaining: ${order.remaining}`);}Filter Orders by Market
Section titled “Filter Orders by Market”// Get orders for a specific marketconst marketOrders = await client.order.list({ marketId: "0xabcdef1234567890...",});
console.log(`${marketOrders.length} orders for this market`);
// Group by sideconst buyOrders = marketOrders.filter(o => o.side === "BUY");const sellOrders = marketOrders.filter(o => o.side === "SELL");
console.log(`Buy orders: ${buyOrders.length}`);console.log(`Sell orders: ${sellOrders.length}`);Cancel a Specific Order
Section titled “Cancel a Specific Order”// Cancel an order by IDconst orderId = "0x9876543210fedcba...";
try { await client.order.cancel({ orderId }); console.log(`Order ${orderId} cancelled successfully`);} catch (error) { console.error("Failed to cancel order:", error.message);}Cancel All Orders
Section titled “Cancel All Orders”// Cancel all your open ordersawait client.order.cancelAll();console.log("All orders cancelled");
// Or cancel orders for a specific market onlyawait client.order.cancelAll({ marketId: "0xabcdef1234567890..."});console.log("Market orders cancelled");Place Order with Error Handling
Section titled “Place Order with Error Handling”import { ValidationError, AuthenticationError, RateLimitError} from "@dicedhq/clob";
async function placeOrder(orderParams) { try { const order = await client.order.createOrder(orderParams); console.log("Order placed successfully:", order.orderId); return order; } catch (error) { if (error instanceof ValidationError) { console.error("Invalid order parameters:", error.message); // Fix parameters and retry } else if (error instanceof AuthenticationError) { console.error("Authentication failed:", error.message); // Check credentials } else if (error instanceof RateLimitError) { console.error("Rate limited, retrying after:", error.retryAfter); await new Promise(resolve => setTimeout(resolve, error.retryAfter * 1000) ); return placeOrder(orderParams); // Retry } else { console.error("Unexpected error:", error); } throw error; }}
// Use the functionawait placeOrder({ price: 0.5, side: "BUY", size: 10, tokenId: "0x1234567890abcdef...", expiration: Math.floor(Date.now() / 1000) + 86400, taker: "public",});Calculate Order Expiration
Section titled “Calculate Order Expiration”// Helper function to create expiration timestampfunction getExpirationTimestamp(hours: number): number { const now = Math.floor(Date.now() / 1000); return now + (hours * 3600);}
// Create order expiring in 48 hoursconst order = await client.order.createOrder({ price: 0.6, side: "BUY", size: 25, tokenId: "0x1234567890abcdef...", expiration: getExpirationTimestamp(48), taker: "public",});Monitor Order Status
Section titled “Monitor Order Status”async function waitForOrderFill(orderId: string, timeoutMs: number = 60000) { const startTime = Date.now();
while (Date.now() - startTime < timeoutMs) { const orders = await client.order.list(); const order = orders.find(o => o.orderId === orderId);
if (!order) { throw new Error("Order not found"); }
if (order.status === "FILLED") { console.log("Order filled!"); return order; }
if (order.status === "CANCELLED" || order.status === "EXPIRED") { throw new Error(`Order ${order.status.toLowerCase()}`); }
console.log(`Order status: ${order.status}, filled: ${order.filled}/${order.size}`); await new Promise(resolve => setTimeout(resolve, 5000)); // Check every 5 seconds }
throw new Error("Timeout waiting for order to fill");}
// Place order and wait for it to fillconst order = await client.order.createOrder({ price: 0.5, side: "BUY", size: 10, tokenId: "0x1234567890abcdef...", expiration: getExpirationTimestamp(24), taker: "public",});
console.log("Order placed:", order.orderId);await waitForOrderFill(order.orderId);Error Handling
Section titled “Error Handling”import { ApiError, ValidationError, AuthenticationError, RateLimitError, NetworkError} from "@dicedhq/clob";
try { const order = await client.order.createOrder({ price: 0.5, side: "BUY", size: 10, tokenId: "0x1234567890abcdef...", expiration: Math.floor(Date.now() / 1000) + 86400, taker: "public", });} catch (error) { if (error instanceof ValidationError) { console.error("Validation error:", error.message); } else if (error instanceof AuthenticationError) { console.error("Authentication failed:", error.message); } else if (error instanceof RateLimitError) { console.error("Rate limited, retry after:", error.retryAfter); } else if (error instanceof NetworkError) { console.error("Network error:", error.message); } else if (error instanceof ApiError) { console.error("API error:", error.message); }}