Running a Solana keeper or crank in production: idempotent crank instructions, losing the race as a normal outcome, one sender per job, slot-based schedules and missed ticks, profitability gate, Jito bundles, unattended signer limits.
git clone https://github.com/solanabr/ai-kit.git--- name: keeper-ops description: "Running a Solana keeper or crank in production: idempotent crank instructions, losing the race as a normal outcome, one sender per job, slot-based schedules and missed ticks, profitability gate, Jito bundles, unattended signer limits." --- # Keepers and cranks A keeper sends the same instruction indefinitely, usually against other keepers: liquidations, order matching, oracle refreshes, auction settlement, reward distribution, vault rebalancing. RPC, confirmation and resend rules are in [backend-async.md](backend-async.md); this file covers what is specific to running one. Auditor checklist [20-rust-offchain-services.md](ext/auditor-skill/checklists/20-rust-offchain-services.md) grades the finished bot. ## Design the instruction for a crowd The program side, agreed with whoever writes the program: - "Already done" is its own outcome. Settled, not liquidatable, position closed and tick already processed each get a distinct error code (or succeed as a no-op), so the bot can tell a lost race from a bug. - Permissionless cranks pay the caller (liquidation bonus, crank fee) from a bounded source, so a crank can't be farmed. A permissioned crank uses a role that can do nothing else, not the admin or upgrade authority. - Pass the expected outcome as bounds (`min_out`, max price, expected sequence number), so a stale view fails instead of executing at a bad price. - Work in bounded batches with a cursor stored on-chain, within the compute and 64-account limits, so any keeper can resume where another stopped. ## Classify every outcome | Outcome | Meaning | Action | |---|---|---| | Landed, succeeded | done | record the reward | | Landed, failed with an "already done" code | lost the race | count it, no alert | | Landed, failed with any other error | bug or bad input | alert | | Simulation shows nothing to do | someone got there first | skip the send | | Block height passed `last_valid_block_height` | expired | re-evaluate from fresh state before re-signing | Match the code from `InstructionError(index, Custom(code))` against the program's IDL errors. A transaction that lands and fails still pays its base and priority fee, so the lost-race rate is a cost line: simulate against the latest state right before signing. ## One sender per job Two replicas sending the same crank pay double fees, and double effects if the instruction is not idempotent. Pick one explicitly: - A lease: a row `(job, holder, epoch, expires_at)` taken with a conditional update, or `pg_try_advisory_lock` on a dedicated connection. Check it before each send, not once at startup: a process paused by GC or a VM migration can wake up holding an expired lease. - Sharding: each replica owns a hash range of positions or markets. - Accepting the duplicate, when the instruction is idempotent and a second fee costs less than coordination. ## Schedule on chain time - Trigger on the value the program checks (Clock `slot` or `unix_timestamp`, an expiry field, `getEpochInfo` for epoch boundaries), not the host clock. `unix_timestamp` is a stake-weighted estimate and can sit seconds away from wall time, so a crank fired by the host clock can arrive early and fail. - Decide per job what a restart does with ticks missed while down: catch up every one in order (payouts, funding intervals where each period counts), or skip to the current one (oracle refresh, rebalancing). Store the last processed tick with its slot. - Liquidations are event-driven: subscribe to the oracle and position accounts (`accountSubscribe`, Laserstream) and re-check health on each update, with a periodic full scan as backstop for dropped streams. ## Profitability gate Send only when the expected reward exceeds base fee (5,000 lamports per signature) + priority fee + tip + rent for accounts the transaction creates + swap slippage, plus a margin. Decide again after simulation, from the simulated amounts, not from the quote. Cap priority fee plus tip per job at a fraction of the reward: a fee war above that cap is a race to let go. In v0 the priority fee is CU limit × price; in v1 it is a lamport total ([backend-async.md](backend-async.md#sending-transactions)). ## Landing together: one transaction or a bundle When the crank must land with something else (liquidate, then swap the seized collateral): - One transaction if it fits: a v1 transaction holds 4,096 bytes and 64 accounts. - Otherwise a Jito bundle: up to 5 transactions, executed in order and all-or-nothing by Jito-Solana leaders. Tip with a SOL transfer to one of the 8 accounts from `getTipAccounts` (pick one at random; minimum 1,000 lamports) inside the main transaction, so a failure doesn't pay it, and never through a lookup table. A failed bundle does not land, so it pays no fee. - Bundles can be split: when a block is skipped, its transactions can be rebroadcast on their own outside the bundle rules. Give each transaction its own guards (`min_out`, balance or slot assertions) so it is safe alone. - Leaders not running Jito-Solana ignore bundles and tips. Checked 2026-10-09 against [docs.jito.wtf](https://docs.jito.wtf/lowlatencytxnsend/). ## Unattended signer - One keypair per keeper, holding only the crank role on-chain; never an upgrade, admin or mint authority. - Keep a few days of fees in it. Top it up from a separate wallet on a schedule with a per-top-up cap, and alert on runway (balance divided by daily spend), not on a fixed SOL amount. - Enforce spend limits in code before signing: a per-transaction fee-plus-tip cap, an hourly total, and an allowlist of program IDs the key may invoke. On a breach, stop and page; don't continue. - Load the key from a secrets manager or a KMS-backed signer at start, never log it, and keep a kill-switch flag the loop reads every iteration.
["Configure your keeper's environment: Set `SCHEDULE_INTERVAL` based on your program's needs (e.g., 128 slots for ~64 second intervals), and `TICK_WINDOW` to account for network latency (start with 32 slots).","Implement idempotency: Design your crank instruction to check state before execution and use transaction signatures as confirmation. Include explicit race condition handling in your code comments.","Set profitability thresholds: Calculate your break-even point considering Jito fees (0.01 SOL per bundle) and network congestion. Start conservative (e.g., 5000 lamports) and adjust based on real data.","Configure Jito integration: Set up your Jito client with the correct bundle submission endpoint and implement exponential backoff for failed bundles. Monitor mempool congestion using Jito's metrics.","Monitor unattended limits: Start with 5 unattended transactions and adjust based on RPC provider limits. Implement health checks that alert when approaching limits to prevent downtime.","Test with simulated races: Use devnet to test high-concurrency scenarios where multiple keepers compete for the same accounts. Verify your idempotent logic handles these cases correctly."]
No install command available. Check the GitHub repository for manual installation instructions.
git clone https://github.com/solanabr/ai-kit/blob/main/.claude/skills/keeper-ops.mdCopy the install command above and run it in your terminal.
Launch Claude Code, Cursor, or your preferred AI coding agent.
Use the prompt template or examples below to test the skill.
Adapt the skill to your specific use case and workflow.
Act as a Solana keeper/crank operator for [PROJECT_NAME]'s [JOB_NAME] program. Design an idempotent crank instruction that handles [SPECIFIC_TASK] while accounting for race conditions. Configure slot-based scheduling with a [SCHEDULE_INTERVAL] tick window and implement a profitability gate requiring [MIN_PROFIT_THRESHOLD] lamports per transaction. Use Jito bundles for transaction bundling and enforce a single sender constraint with [MAX_UNCONFIRMED_TXS] unattended signer limits. Provide the transaction priority fees and compute budget for optimal execution.
For the Jupiter LFG launch program's `claim_rewards` crank, we designed an idempotent instruction that first checks if rewards are claimable (accounting for race conditions where another validator might claim simultaneously). The schedule runs every 128 slots (≈64 seconds) with a 32-slot (≈16 second) tick window to catch missed opportunities. Profitability requires at least 5000 lamports per transaction, which covers Jito's 0.01 SOL bundle fee at current network congestion levels. We configured priority fees at 10,000 lamports per compute unit (1.4M compute units) to ensure Jito bundle inclusion during high-priority periods. The single sender constraint limits unattended transactions to 5 to prevent RPC rate limiting while maintaining continuous operation. During testing on devnet, we observed 92% successful claim rates with 8% race condition losses - well within acceptable parameters. The keeper successfully processed 12,456 claims over 72 hours with zero missed ticks, generating 6.2 SOL in total rewards collected.
skills-collection
Take a free 3-minute scan and get personalized AI skill recommendations.
Take free scan