amm

package
v0.4.21 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package amm tracks amm

Index

Constants

View Source
const SevenDaysOfSlots = 7 * 24 * 60 * 60 * 25 / 10 // 1,512,000 slots

SevenDaysOfSlots assumes ~400ms/slot (2.5 slots/sec), the same fallback bot/state/clock.go itself uses before real slot-timing data arrives -- see OldSlot in raydium/cpmm for the established precedent of this convention.

View Source
const Size = 752

Size is the exact byte length of a serialised AmmInfo account. AmmInfo is repr(C, packed) and loaded on-chain via bytemuck's Pod/Zeroable (see Loadable::load / load_checked in the Raydium source), which the program itself gates on account.data_len() == size_of::<AmmInfo>() — so 752 is the only size that is ever legitimately this account type. Since AmmInfo has no discriminator, anything looser (e.g. "at least Size") would also match other, unrelated account types the program happens to own.

Variables

View Source
var ProgramID = sgo.MustPublicKeyFromBase58("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8")
View Source
var Schema string

Functions

func DeriveVaultSigner added in v0.4.16

func DeriveVaultSigner(market sgo.PublicKey, nonce uint64, marketProgramID sgo.PublicKey) (sgo.PublicKey, error)

DeriveVaultSigner recomputes the market's vault-signer PDA. The nonce is stored on the market account itself (VaultSignerNonce), so this is a direct CreateProgramAddress call rather than a bump search.

func Download

func Download(
	parentCtx context.Context,
	stateClient state.Client,
	db *sql.DB,
	maxSubscriptionCount int,
	force bool,
	mintTracker *mintinfo.Tracker,
) error

func ExportJSON added in v0.4.16

func ExportJSON(db *sql.DB, path string) error

ExportJSON writes every raydium_amm_pool row whose mints both have a known mint_info decimals row (capped at embedCap, a non-binding safety ceiling -- see that constant's and knownDecimalsPoolQuery's own doc comments) to path, so catscope-rust-bot's build.rs can read this file instead of opening prefetch.db directly. Reuses PoolRow's own json tags verbatim (same scanPool shape TopPools/ReadPools use, just a different query).

func ReadPools added in v0.4.16

func ReadPools(db *sql.DB, fn func(*PoolRow) bool) error

ReadPools calls fn for each liquid AMM pool (both vault balances non-zero). Iteration stops early if fn returns false.

Types

type AmmInfo

type AmmInfo struct {
	Status             uint64 // bitmask: swap/deposit/withdraw/crank enabled
	Nonce              uint64 // bump used to derive amm_authority
	OrderNum           uint64
	Depth              uint64
	CoinDecimals       uint64
	PcDecimals         uint64
	State              uint64 // internal state machine
	ResetFlag          uint64
	MinSize            uint64
	VolMaxCutRatio     uint64
	AmountWave         uint64
	CoinLotSize        uint64 // mirrors OpenBook market
	PcLotSize          uint64
	MinPriceMultiplier uint64
	MaxPriceMultiplier uint64
	SysDecimalValue    uint64

	Fees      Fees
	StateData StateData

	CoinVault     sgo.PublicKey // pool-owned token account for base mint
	PcVault       sgo.PublicKey // pool-owned token account for quote mint
	CoinVaultMint sgo.PublicKey // base mint
	PcVaultMint   sgo.PublicKey // quote mint
	LpMint        sgo.PublicKey
	OpenOrders    sgo.PublicKey // pool's OpenOrders on OpenBook
	Market        sgo.PublicKey // OpenBook market
	MarketProgram sgo.PublicKey // OpenBook program ID
	TargetOrders  sgo.PublicKey
	// padding [u64; 8] in the real struct (64 bytes) — reserved, not two
	// more pubkeys. Owner and LpReserve below land on the correct real
	// offsets regardless, since this gap is the same size as the two
	// pubkeys previously (incorrectly) read here.
	Owner     sgo.PublicKey // admin (multisig)
	LpReserve uint64        // pool's minted LP token supply
}

AmmInfo mirrors the Raydium AMM v4 AmmInfo struct (repr(C, packed), 752 bytes). Source: https://github.com/raydium-io/raydium-amm/blob/master/program/src/state.rs

func Parse

func Parse(pubkeyID sgo.PublicKey, data []byte) (*AmmInfo, error)

Parse deserialises a raw account data slice into an AmmInfo.

type AmmInfoWithToken

type AmmInfoWithToken struct {
	Info      *AmmInfo
	CoinVault *sgotkn.Account
	PcVault   *sgotkn.Account
}

type Configuration

type Configuration struct {
	List []*Summary
}

type Fees

type Fees struct {
	MinSeparateNumerator   uint64 // 5
	MinSeparateDenominator uint64 // 10_000
	TradeFeeNumerator      uint64 // 25
	TradeFeeDenominator    uint64 // 10_000
	PnlNumerator           uint64 // 12  (protocol share of the swap fee)
	PnlDenominator         uint64 // 100
	SwapFeeNumerator       uint64 // 25  → 0.25% gross swap fee
	SwapFeeDenominator     uint64 // 10_000
}

Fees mirrors the Raydium AMM v4 Fees struct (repr(C, packed), 64 bytes).

type OpenBookMarket added in v0.4.16

type OpenBookMarket struct {
	Pubkey           sgo.PublicKey
	VaultSignerNonce uint64
	CoinVault        sgo.PublicKey
	PcVault          sgo.PublicKey
	EventQueue       sgo.PublicKey
	Bids             sgo.PublicKey
	Asks             sgo.PublicKey
}

OpenBookMarket is the subset of a Serum/OpenBook MarketState account needed to build a Raydium AMM v4 swap instruction.

func ParseOpenBookMarket added in v0.4.16

func ParseOpenBookMarket(pubkeyID sgo.PublicKey, data []byte) (*OpenBookMarket, error)

ParseOpenBookMarket deserialises a raw OpenBook/Serum market account.

type PoolRow added in v0.4.16

type PoolRow struct {
	Pubkey            sgo.PublicKey  `json:"pubkey"`
	CoinVault         sgo.PublicKey  `json:"coin_vault"`
	PcVault           sgo.PublicKey  `json:"pc_vault"`
	CoinMint          sgo.PublicKey  `json:"coin_mint"`
	PcMint            sgo.PublicKey  `json:"pc_mint"`
	CoinBalance       uint64         `json:"coin_balance"`
	PcBalance         uint64         `json:"pc_balance"`
	MarketBids        *sgo.PublicKey `json:"market_bids,omitempty"`
	MarketAsks        *sgo.PublicKey `json:"market_asks,omitempty"`
	MarketEventQueue  *sgo.PublicKey `json:"market_event_queue,omitempty"`
	MarketCoinVault   *sgo.PublicKey `json:"market_coin_vault,omitempty"`
	MarketPcVault     *sgo.PublicKey `json:"market_pc_vault,omitempty"`
	MarketVaultSigner *sgo.PublicKey `json:"market_vault_signer,omitempty"`
}

PoolRow is a flat view of an AMM v4 pool. CoinMint/PcMint (and thus a spot price of PcBalance/CoinBalance raw-unit tokens) are only populated once both vault accounts have been fetched — see liquidPoolQuery. The Market* fields (OpenBook market account children, populated once the pool's `market` account has been fetched) are nullable independently of CoinMint/PcMint -- catscope-rust-bot's build.rs needs them for its RAYDIUM_AMM_POOLS embed, which only requires MarketVaultSigner to be set, not CoinMint/PcMint. Json tags let this struct be exported directly (see export.go) instead of via a separate JSON-shaped type.

func TopPools added in v0.4.16

func TopPools(db *sql.DB, n int) ([]*PoolRow, error)

TopPools returns the n liquid AMM pools with the highest liquidity (coin_balance + pc_balance, raw token units), sorted descending.

type Query added in v0.4.16

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

func CreateQuery added in v0.4.16

func CreateQuery(db *sql.DB) (*Query, error)

CreateQuery prepares the top-pools-by-liquidity statement against raydium_amm_pool. Callers wrap the result as one dispatch case of a single, consolidated "get top pools" tool (see prefetch/prompttool.go) rather than exposing their own separate tool.

func (*Query) TopPools added in v0.4.16

func (q *Query) TopPools(ctx context.Context, count int) (string, error)

TopPools returns up to count raydium_amm_pool pubkeys, ranked by coin_balance+pc_balance descending, as a semicolon-joined "pubkey=..." list.

type StateData

type StateData struct {
	NeedTakePnlCoin uint64
	NeedTakePnlPc   uint64
	TotalPnlPc      uint64
	TotalPnlCoin    uint64
	PoolOpenTime    uint64
	// padding [u64; 2] in the real struct — reserved, not two real fields.
	OrderbookToInitTime uint64
	SwapCoinInAmount    U128
	SwapPcOutAmount     U128
	SwapAccPcFee        uint64
	SwapPcInAmount      U128
	SwapCoinOutAmount   U128
	SwapAccCoinFee      uint64
}

StateData mirrors the Raydium AMM v4 StateData struct (repr(C, packed), 144 bytes). All fields from OrderbookToInitTime onward (including the padding below) are deprecated on-chain — no longer read or updated by the program.

type Summary

type Summary struct {
	Pubkey      sgo.PublicKey
	Coin        sgo.PublicKey
	CoinBalance uint64
	Pc          sgo.PublicKey
	PcBalance   uint64
}

type U128

type U128 struct {
	Lo uint64
	Hi uint64
}

U128 holds a little-endian 128-bit unsigned integer split into two 64-bit halves.

Source Files

  • amm.go
  • event.go
  • export.go
  • openbook.go
  • query.go
  • read.go
  • schema.go

Jump to

Keyboard shortcuts

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