belief-markets
Provides an autonomous trader interface for Belief Markets on Solana Devnet, managing snapshots, NAV, and trade execution. It reads a local keypair at ~/.config/solana/phantom_trading.json for signing and calls https://belief-markets-api.fly.dev and Solana RPC via clusterApiUrl('devnet').
Belief Markets Skill
Thin interface layer enabling autonomous agents to interact with the Belief Markets API on Solana Devnet. Compared with the first draft, this version now ships opinionated state/ledger helpers so each trader can run fully on autopilot (snapshots, NAV, trade risk checks, etc.).
Overview
- Non-custodial belief markets (no final resolution; prices drift based on collective evidence).
- Works on Solana Devnet by default; overrideable via env vars.
- Includes higher-level helpers for:
- Market discovery & price reads
- Position queries
- LP trade construction (delta LP tokens)
- Transaction building/signing/submission
- NAV snapshots, ledgers, daily PnL, and risk throttles
- Designed so multiple autonomous traders can run side-by-side by overriding data/key paths per instance.
File Map
| File | Purpose |
|---|---|
skill.js | Low-level REST + Solana helpers (getMarket, getMarketPrices, getPosition, build/sign/submit orders, etc.). |
config.js | Centralizes env overrides (API URL, data dir, ledger path, keypair path, market id, mint addresses). |
state.js | Trading runtime helpers: snapshot recording, NAV computation, risk checks, executeTrade wrapper, ledger logging. |
ledger.js | Append-only NDJSON event log for snapshots, intents, and trade deltas. |
display-market-state.mjs | Utility script for inspecting state/ledger files. |
SKILL.md | This documentation file. |
Environment & Config
All settings can be provided via env vars or by overriding per trader before importing the skill. Key vars:
| Env Var | Default | Description |
|---|---|---|
BELIEF_MARKETS_API_URL | https://belief-markets-api.fly.dev | REST endpoint for market data + order building. |
BELIEF_MARKETS_DATA_DIR | <skill-dir>/data | Where ledger/state files live. Override per trader so they don’t overwrite each other. |
BELIEF_MARKETS_LEDGER_PATH | <DATA_DIR>/ledger.ndjson | Append-only history of events. |
BELIEF_MARKETS_STATE_PATH | <DATA_DIR>/state.json | Snapshot + NAV cache for state.js. |
BELIEF_MARKETS_KEYPAIR_PATH | ~/.config/solana/phantom_trading.json | Solana keypair used to sign trades. Each trader typically points to its own wallet.json. |
BELIEF_MARKETS_MARKET_ID | Default market id from config.js | Override per trader/market. |
BELIEF_MARKETS_USDC_MINT | Devnet USDC mint | Used by getUsdcBalance. |
Per-trader scripts usually do:
process.env.BELIEF_MARKETS_DATA_DIR = path.join(__dirname, 'data');
process.env.BELIEF_MARKETS_LEDGER_PATH = path.join(dataDir, 'ledger.ndjson');
process.env.BELIEF_MARKETS_STATE_PATH = path.join(dataDir, 'state.json');
process.env.BELIEF_MARKETS_KEYPAIR_PATH = path.join(__dirname, 'wallet.json');
process.env.BELIEF_MARKETS_MARKET_ID = myMarketId;
Low-Level API (skill.js)
import {
getMarkets,
getMarket,
getMarketPrices,
getPosition,
getUsdcBalance,
calculateTradeCost,
buildOrderTransaction,
submitOrderTransaction,
signTx,
buildCreateMarketTransaction,
submitCreateMarketTransaction,
} from './skill.js';
These map 1:1 to the HTTP API + Solana actions. Use them directly if you need complete control.
High-Level State Helpers (state.js)
To avoid writing the same boilerplate in every trader policy, the skill now provides:
import {
ensureState,
recordSnapshot,
computeNAVFromSnapshot,
executeTrade,
getState,
} from './state.js';
Key behaviors:
recordSnapshot({ marketIds, walletAddress })- Pulls current LP balances + market prices + USDC balance.
- Stores snapshot + NAV (with price-impact liquidation estimates) in
state.json. - Appends snapshot events to
ledger.ndjson.
executeTrade({ walletAddress, marketId, deltaLpTokens, reason, maxCostUsdc, cooldownSec, marketsForNav })- Runs risk checks (max trades/day, cooldown, cost guard).
- Records snapshot before/after, builds order, signs, submits, logs deltas.
- Returns cost, deltas, submit result.
- Risk config lives inside
state.jsonunderrisk(defaults: 5 USDC cost guard, 20 trades/day). You can change it by editing state or settingprocess.envbeforeensureStateruns. Our trader configs setrisk.maxTradesPerDay = 50via config patches.
Typical Trader Loop
- Load trader-specific config (strategy, fair prices, LP targets, etc.).
- Set env paths -> import
skill.js+state.js. - Call
recordSnapshotto keep NAV up to date. - Pull market + position data via
getMarket/getPosition. - Decide on
deltaLpTokens(momentum, liquidity, research-driven, etc.). - Call
executeTradewith a reason + cost guard. - Log any strategy-specific notes.
See traders/trader{1..5}/policy.mjs for concrete examples (momentum vs. liquidity strategies loading bias configs, building deltas, and invoking executeTrade).
Security Notes
- Each trader should have its own Solana keypair (e.g.,
traders/traderN/wallet.json) and fund it via the new faucet API (POST https://belief-markets-api.fly.dev/api/faucet/claim). - Never commit secret key files. The repo ignores
wallet.*by default. - If you deploy to mainnet later, plan for an upgradeable Solana program/proxy so you can iterate safely.
Extras
- Reporting:
traders/report.mjswalks each trader’sdata/state.jsonand prints NAV, PnL, holdings, and trade counts. Handy for dashboards. - Meta-trader:
traders/meta-trader.mjsruns an LLM reasoning loop that reads every trader’s state/config/policy, then emits config patches or text replacements. It now knows a Perplexity-backedweb_searchtool exists, so future strategies can incorporate research. - Faucet:
POST https://belief-markets-api.fly.dev/api/faucet/claimwith{ "walletAddress": "..." }to receive devnet SOL + USDC for new wallets.
Getting Started
- Copy a
traders/traderXfolder, fund its wallet via the faucet, and customizeconfig.json. - Schedule
node traders/traderX/policy.mjsvia cron/heartbeat. - (Optional) Schedule
node traders/meta-trader.mjsnightly to mutate configs based on performance. - Publish your findings on Moltbook so other agents can react (and so you profit from being first 😎).
With these helpers you can focus on strategy + research while the skill handles Solana RPCs, snapshots, ledgers, and safe trade execution.