prefetch

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 45 Imported by: 0

Documentation

Overview

Package prefetch builds wasm bots preloaded with necessary information

Index

Constants

View Source
const (
	EnvNameOrcaPoolBudget        = "ORCA_POOL_BUDGET"
	EnvNameRaydiumAmmPoolBudget  = "RAYDIUM_AMM_POOL_BUDGET"
	EnvNameRaydiumClmmPoolBudget = "RAYDIUM_CLMM_POOL_BUDGET"
	EnvNameRaydiumCpmmPoolBudget = "RAYDIUM_CPMM_POOL_BUDGET"
	EnvNamePumpswapPoolBudget    = "PUMPSWAP_POOL_BUDGET"
	EnvNameTrackedAccountsBudget = "TRACKED_ACCOUNTS_BUDGET"
	EnvNameRouterTokenBudget     = "ROUTER_TOKEN_BUDGET"
	// Not a budget (doesn't affect what gets admitted/subscribed at
	// build time) -- the probe size, in raw lamports, arbv1's periodic
	// "trade router check" diagnostic uses at runtime. Grouped into the
	// same default-env mechanism below since it needs the same
	// "optimizer sets a default, still overridable" treatment. See
	// catscope-rust-bot's build.rs trade_router_probe_lamports doc
	// comment for the full reasoning (100 SOL was too large a probe --
	// legitimately craters against thin-but-real pools -- lowered to a
	// more realistic 0.1 SOL).
	EnvNameTradeRouterProbeLamports = "TRADE_ROUTER_PROBE_LAMPORTS"
	// Which bundler `Wallet::drain_and_send()` requests from the host
	// when a single `evaluate()` tick produces more than one
	// transaction (`transactionprocessor::batch`'s own `bundler:
	// option<u8>` parameter) -- real bundler identity (Jito vs
	// Astralane, etc.) is a host-side concern catscope-rust-bot never
	// interprets, just plumbs through as a raw u8. See build.rs's
	// `bundler` doc comment.
	EnvNameBundler = "BUNDLER"
	// Temporary kill switch (2026-09-04): when "true", catscope-rust-bot's
	// build.rs generates an empty PHOENIX_MARKETS regardless of what's in
	// prefetch.db's phoenix_market table, so no build has any real
	// Phoenix market to find -- disables dispersion's short-index leg
	// and testperpv1's perp checks alike.
	// Added after a real, live-confirmed incident: the trading wallet's
	// Phoenix Eternal trader account was found frozen on-chain
	// (TraderCapabilityFlags denies DepositCollateral/WithdrawCollateral/
	// RiskIncreasingTrade), with no documented self-service unfreeze
	// path, so every Phoenix-touching cycle was doomed to fail. See
	// build.rs's phoenix_disabled doc comment for the full reasoning and
	// the real, on-chain-confirmed trader account
	// (DbbMev69Fr5ha3Q6M3vmfQxtzmFsy2MFA2jG387TDzRv). Revert (remove this
	// default, or override it to "false") once the trader account is
	// confirmed unfrozen.
	EnvNameDisablePhoenix = "DISABLE_PHOENIX"
)

Router/pool subscription budget env vars read by catscope-rust-bot's build.rs. Set here explicitly (not just left to build.rs's own smaller fallback values) so the optimizer is the one source of truth for them going forward, and so a future caller can override any of them by setting the env var on the optimizer process itself (picked up by the os.Environ() merge below) or via a StaticLoader.Env implementation (which runs after these defaults, so it always wins).

Variables

This section is empty.

Functions

func CreatePrefetchOnlyChat added in v0.4.16

func CreatePrefetchOnlyChat(
	ctx context.Context,
	model string,
	baseURL string,
	store *store.DB,
	feePayerPath string,
) (shell.ChatBuilder, error)

Types

type BotImage

type BotImage struct {
	// contains filtered or unexported fields
}

func (*BotImage) Path

func (bi *BotImage) Path() string

type Edge added in v0.4.16

type Edge struct {
	To      sgo.PublicKey
	Weight  float64
	Pool    sgo.PublicKey
	Program string
}

Edge is one directed hop in a PriceGraph: crossing it converts 1 unit of the source mint into exp(-Weight) units of To (raw token units, no decimals normalization).

type Prefetcher

type Prefetcher struct {
	// contains filtered or unexported fields
}

Prefetcher holds a catscope client and an sqlite database.

func Create

func Create(ctx context.Context, wg *sync.WaitGroup, stateClient state.Client, storeDB *store.DB) (*Prefetcher, error)

func (*Prefetcher) AppendPrompt added in v0.4.16

func (pf *Prefetcher) AppendPrompt(tools []tool.BaseTool) ([]tool.BaseTool, error)

func (*Prefetcher) Build

func (pf *Prefetcher) Build(ctx context.Context, targetRepositoryPath string, liquidityLoader StaticLoader) (*BotImage, error)

Build an image. Take the data from prefetch (e.g. Orca pool data) and write the Json files into the Rust code base so that those files can be statically compiled into the web assembly binary.

func (*Prefetcher) State

func (pf *Prefetcher) State() state.Client

func (*Prefetcher) Store added in v0.4.16

func (pf *Prefetcher) Store() *store.DB

func (*Prefetcher) Tools added in v0.4.16

func (pf *Prefetcher) Tools() ([]tool.BaseTool, error)

type PriceGraph added in v0.4.16

type PriceGraph struct {
	// contains filtered or unexported fields
}

PriceGraph is a directed multigraph of mints connected by pool prices. Edge weight is -log(price), so summing weights around a cycle is negative exactly when compounding the trades around that cycle is profitable — a Bellman-Ford negative cycle in this graph is an arbitrage loop.

func BuildUnifiedGraph added in v0.4.16

func BuildUnifiedGraph(db *sql.DB, n int) (*PriceGraph, error)

BuildUnifiedGraph builds a PriceGraph from the top n liquidity pools of Raydium's AMM v4, CPMM, and CLMM programs, plus Orca's Whirlpools.

Prices are raw-token-unit spot prices (no decimals normalization, no fee adjustment) — good enough to rank pools and hunt for gross arbitrage cycles, not a substitute for a real execution-price simulation.

func NewPriceGraph added in v0.4.16

func NewPriceGraph() *PriceGraph

NewPriceGraph returns an empty graph.

func (*PriceGraph) AddPool added in v0.4.16

func (g *PriceGraph) AddPool(mintA, mintB, pool sgo.PublicKey, program string, priceAToB float64)

AddPool adds both directions of a two-sided pool: mintA -> mintB at priceAToB (units of mintB per unit of mintA, raw token units), plus the reciprocal edge back. Pools with a non-positive or non-finite price are silently skipped.

func (*PriceGraph) BellmanFord added in v0.4.16

func (g *PriceGraph) BellmanFord() (cycle []sgo.PublicKey, ok bool)

BellmanFord looks for a negative-weight cycle anywhere in the graph — not just ones reachable from one chosen node — by seeding every node's distance at 0 before the standard relaxation passes. That's equivalent to adding a virtual source with a zero-weight edge to every node and running Bellman-Ford from it, so a cycle is found regardless of which mints it touches. If found, ok is true and cycle lists the mints around the loop in trade order (cycle[0] == cycle[len(cycle)-1]).

func (*PriceGraph) EdgeCount added in v0.4.16

func (g *PriceGraph) EdgeCount() int

func (*PriceGraph) NodeCount added in v0.4.16

func (g *PriceGraph) NodeCount() int

NodeCount and EdgeCount report the graph's size.

type StaticLoader

type StaticLoader interface {
	// modify arguments to cargo build
	Args([]string) ([]string, error)
	// put in files that can be statically compiled into the wasm bot
	Load(string, *trading.TradingPair) error
	// Env set environmental variables
	Env(map[string]string) error
}

Source Files

  • graph.go
  • harness.go
  • prefetch.go
  • prompttool.go
  • rust.go

Directories

Path Synopsis
Package alt persists Address Lookup Table contents to prefetch.db -- see cmd/alt.go, the only writer.
Package alt persists Address Lookup Table contents to prefetch.db -- see cmd/alt.go, the only writer.
Package drift preloads Drift v2 SpotMarket accounts (global market data only -- User, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load, and PerpMarket is skipped since it has no mint/vault of its own -- perp markets settle in the program's quote asset).
Package drift preloads Drift v2 SpotMarket accounts (global market data only -- User, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load, and PerpMarket is skipped since it has no mint/vault of its own -- perp markets settle in the program's quote asset).
Package jet preloads Jet Protocol V1 lending pool reserve accounts (global market data only -- Obligation, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load).
Package jet preloads Jet Protocol V1 lending pool reserve accounts (global market data only -- Obligation, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load).
Package kamino preloads Kamino Lending reserve accounts.
Package kamino preloads Kamino Lending reserve accounts.
Package liquidity generates router.json for the catscope-rust-bot build.
Package liquidity generates router.json for the catscope-rust-bot build.
Package lstyield estimates real Solana liquid-staking-token (LST) staking yield from a periodic SOL-per-LST exchange-rate timeseries, so an LST-collateral leverage-loop strategy (originally built as leveragedloopv1's Time-Expanded DAG, since removed) has a real profitability signal to work from.
Package lstyield estimates real Solana liquid-staking-token (LST) staking yield from a periodic SOL-per-LST exchange-rate timeseries, so an LST-collateral leverage-loop strategy (originally built as leveragedloopv1's Time-Expanded DAG, since removed) has a real profitability signal to work from.
Package marginfi preloads marginfi-v2 Bank accounts (global market data only -- MarginfiAccount, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load).
Package marginfi preloads marginfi-v2 Bank accounts (global market data only -- MarginfiAccount, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load).
Package mintinfo provides a small shared cache of SPL Mint decimals, populated opportunistically by every prefetcher that discovers a mint (Orca, Raydium AMM/CPMM/CLMM) so a mint referenced by many pools (USDC, wSOL) is only ever fetched once per run, not once per pool.
Package mintinfo provides a small shared cache of SPL Mint decimals, populated opportunistically by every prefetcher that discovers a mint (Orca, Raydium AMM/CPMM/CLMM) so a mint referenced by many pools (USDC, wSOL) is only ever fetched once per run, not once per pool.
Package obligation records this bot's own Solend/Kamino lending obligation state (deposits/borrows, across multimodelv1's pair/ directional/hawkes trade types, since removed) into prefetch.db, the same skip-if-unchanged shape optimizer/prefetch/pnl uses for wallet token balances -- see this package's schema.sql for the real motivation (there was previously no way to see this state without manually deriving each obligation's address and parsing its raw account bytes).
Package obligation records this bot's own Solend/Kamino lending obligation state (deposits/borrows, across multimodelv1's pair/ directional/hawkes trade types, since removed) into prefetch.db, the same skip-if-unchanged shape optimizer/prefetch/pnl uses for wallet token balances -- see this package's schema.sql for the real motivation (there was previously no way to see this state without manually deriving each obligation's address and parsing its raw account bytes).
Package orca preloads Orca Whirlpool trading pools.
Package orca preloads Orca Whirlpool trading pools.
Package phoenix preloads Phoenix perpetuals market identity (symbol, asset_id, market_account) into prefetch.db.
Package phoenix preloads Phoenix perpetuals market identity (symbol, asset_id, market_account) into prefetch.db.
Package pnl records a trading wallet's token position history into prefetch.db and computes mark-to-market PnL from it: the market value of a position at its earliest known snapshot vs.
Package pnl records a trading wallet's token position history into prefetch.db and computes mark-to-market PnL from it: the market value of a position at its earliest known snapshot vs.
Package pricefeed polls the Jupiter Price API v3 for a configurable list of token mints and publishes price updates on a channel.
Package pricefeed polls the Jupiter Price API v3 for a configurable list of token mints and publishes price updates on a channel.
Package pumpfun preloads Pump.fun bonding-curve state.
Package pumpfun preloads Pump.fun bonding-curve state.
Package pumpswap preloads PumpSwap AMM pool state.
Package pumpswap preloads PumpSwap AMM pool state.
Package raydium preloads Raydium AMM v4 trading pools.
Package raydium preloads Raydium AMM v4 trading pools.
amm
Package amm tracks amm
Package amm tracks amm
clmm
Package clmm tracks clmm
Package clmm tracks clmm
cpmm
Package cpmm tracks cpmm
Package cpmm tracks cpmm
Package sanctum preloads Sanctum S Controller LST pool state.
Package sanctum preloads Sanctum S Controller LST pool state.
Package solend preloads Solend/Save token-lending reserve accounts (global market data only -- Obligation, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load).
Package solend preloads Solend/Save token-lending reserve accounts (global market data only -- Obligation, the per-wallet position account, is not fetched here since there is no bounded set to bulk-load).
Package trading holds common structs
Package trading holds common structs

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL