Dflow kalshi trading

From x402agent/solana-clawd · View interactive version

Dflow kalshi trading is an AI agent skill from the GitHub repository x402agent/solana-clawd. Buy, sell, or redeem YES/NO outcome tokens on Kalshi prediction markets via DFlow. Use when the user wants to bet on an event, place a Kalshi order, take a YES or NO position, exit a Kalshi position, redeem winning outcome tokens after a market resolves, tune priority fees on a PM trade, or build a

How to use this skill

Skill file

DFlow Kalshi Trading

Buy, sell, and redeem YES/NO outcome tokens on Kalshi prediction markets. PM trades are imperative and asynchronous — submit, then poll until terminal.

Prerequisites

Choose your surface

If unclear, ask once: "From the command line, or wired into an app?"

Workflows

All three workflows assume the user already has a market ledger mint (CLI; the marketLedger field on the Metadata API market object) or an outcome mint (API; yesMint / noMint) in hand. If they only have a ticker / event name, defer to dflow-kalshi-market-scanner.

One market, two settlement rails. Every initialized Kalshi market on DFlow exposes both a USDC rail and a CASH rail in market.accounts — each with its own marketLedger, yesMint, and noMint. They share an orderbook (the top-level yesBid / yesAsk / volume24hFp are market-wide), but trades and holdings are rail-scoped: USDC-rail YES tokens are a different SPL mint from CASH-rail YES tokens and aren't fungible. Default to the USDC rail unless the user holds CASH, explicitly asks for CASH, or the active DFlow vault only has CASH. Don't write defensive "fall back to CASH if USDC rail missing" code — it never fires, and it hides the rail choice from the user. State the default at the top of the script instead.

Settlement mint constants (Solana, Token-2022 for CASH and the classic SPL token program for USDC):

Use these as the keys into market.accounts[mint] when picking a rail. There is no top-level market.settlementMint field on the /markets response (despite what some recipe snippets might suggest with market.accounts?.[market.settlementMint] — that pattern shows up in position-side code, where the rail is already known from the held outcome mint, not in market-discovery code). Key by the mint directly.

Buy (open or increase a YES/NO position)

  1. Confirm the buy gates (KYC + geo + maintenance window) are passable for this user — see Gotchas.
  2. Submit the order with the settlement mint as input (USDC or CASH) and the outcome mint as output.
  3. Poll status until terminal (closed / expired / failed).

"Buy N whole contracts" from a scan snapshot. Kalshi buys submit a USDC amount and get back as many whole contracts as it covers, refunding leftovers. When you're executing N contracts off a snapshotted yesAsk, compute Math.ceil(N * yesAsk * 1e6) atomic USDC and optionally add a small buffer (≤ ~1%, a few basis points is typically enough) so a tick-up in the ask between scan and submit doesn't leave you with N-1. Leftover stablecoin is refunded by the CLP, so over-funding slightly is cheap insurance. Don't over-fund by more than a percent or two — at some point it's no longer insurance, it's a different order size.

Sell (decrease or close)

Flip the mints — outcome in, settlement out. No KYC required. No --market / --side on the CLI; pass the outcome mint as the FROM positional and the CLI auto-resolves the settlement mint.

Redeem (post-settlement)

Once the market is determined / finalized and redemptionStatus: "open", redemption is just a regular sell of the winning side back to the settlement mint. No special flag, no KYC.

What to ASK the user (and what NOT to ask)

Trade shape — infer if unambiguous, confirm if not:

  1. Operation — buy / sell / redeem. Infer from intent ("bet on X" → buy YES; "cash out" → sell; "my YES tokens just won" → redeem). Don't make the user pick a mode.
  2. Market + side — for CLI: the marketLedger (from market.accounts[<settlement-mint>].marketLedger) plus --side yes|no. For API: the YES or NO outcome mint directly as outputMint.
  3. Settlement rail — USDC or CASH. Both exist on every initialized market; default to USDC unless the user says otherwise. This determines which marketLedger (CLI) or yesMint / noMint (API) you use.
  4. Amount in atomic units — every Kalshi mint is 6 decimals (8_000_000 = $8 of USDC; 10_000_000 = 10 outcome tokens). Buys submit settlement-mint amounts (USDC/CASH); sells/redeems submit outcome-token amounts.

Infra — always ask, never infer:

  1. API only — wallet pubkey (base58). Required for every /order call.
  2. API only — DFlow API key (only when the script is making direct HTTP calls to /order or other Trade API endpoints; pure CLI scripts don't need one — see the "two auth paths" gotcha). Ask with a clean, neutral question: "Do you have a DFlow API key?" Don't presuppose where the key lives — phrasings like "do you have it in env?" or "is DFLOW_API_KEY set?" nudge the user toward env-var defaults they didn't ask for. Surface the choice; don't silently fall back to env or to dev. It's one key for everything DFlow — same x-api-key unlocks the Trade API and the Metadata API, REST and WebSocket. If yes → prod host https://quote-api.dflow.net with x-api-key on every request. If no → dev host https://dev-quote-api.dflow.net (same features, rate-limited). Point them at https://pond.dflow.net/build/api-key for a prod key. When you generate a script that does its own HTTP, log the resolved host + key-presence at startup so the user can see which rails they're on.
  3. Priority fee (both surfaces) — "Any priority-fee preference, or just use DFlow's default?" Default on both surfaces = DFlow-auto, capped at 0.005 SOL (documented default on /order). Surface this explicitly so the user knows the lever exists for congested periods or cost-sensitive flows. Don't editorialize about what percentage of trades this covers — DFlow doesn't publish one and you don't know.
  1. Sponsored / gasless (API only — skip for CLI) — "Does the user need to hold SOL for this trade, or is your app covering fees?" Default = user pays everything. Two levers on /order, depending on what you want to cover:

Do NOT ask about:

Gotchas (the docs MCP won't volunteer these)

When something doesn't fit

For anything not covered above — full parameter lists, full error tables, response schemas, partial-fill handling, rare flags, new features — query the docs MCP (search_d_flow, query_docs_filesystem_d_flow). Don't guess.

For runnable code, point the user at the DFlow docs recipes (each links to the DFlow Cookbook Repo for clone-and-go): CODE0, CODE1, CODE2.

Sibling skills

Defer if the user pivots to:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.