Markdown
When building on-chain monitoring tools or automated security agents on Solana, latency and simplicity matter. Relying on slow RPC polling or heavy Anchor IDL setups just to watch liquidity changes in a DEX pool introduces unnecessary overhead.
In this post, I’ll walk through the architecture behind sentinel-solana—a lightweight, real-time monitoring agent written in TypeScript that decodes Raydium V4 pool buffers directly and tracks liquidity outflows over Helius WebSockets.
Architecture Overview
To keep the agent fast, modular, and easy to extend, the pipeline is split into two focused layers:
-
Binary Buffer Decoding Layer — Parses raw account data from Raydium V4 liquidity pools directly using Raydium's binary layout definitions (
LIQUIDITY_STATE_LAYOUT_V4) and converts reserve/accumulator fields intoBigIntfor exact arithmetic without floating-point rounding issues. - Real-Time WebSocket & Guard Layer — Establishes low-latency WebSocket subscriptions via Helius RPC, resolves the underlying token vaults, and evaluates real-time balance deltas to flag anomalous liquidity withdrawals.
Step 1: Resolving Raydium V4 Pool State & Vaults
A common gotcha when working with Solana AMMs is assuming token reserves live directly inside the main pool account. In Raydium V4, the pool account stores configuration flags, operational status, and swap accumulators, while the actual token reserves reside in two separate SPL Token accounts: baseVault and quoteVault.
On startup, sentinel-solana fetches the target pool account, validates the buffer length against the expected layout span, and extracts:
- Pool Status — To detect administrative freezes or unexpected state transitions.
-
Base & Quote Mints — To confirm the trading pair (
SOL/USDCin the demo). -
Vault Public Keys — Specifically the
quoteVaultaddress where quote liquidity is held.
Step 2: Dual WebSocket Subscriptions & Drop Detection
Once the vault addresses are resolved, the agent opens two concurrent onAccountChange WebSocket listeners with confirmed commitment:
- Pool Account Listener: Watches the main AMM account for status changes or unexpected pool freezes.
-
Quote Vault Listener: Decodes incoming SPL Token account buffer updates on the fly using
@solana/spl-token'sAccountLayoutand compares each new balance against the previous state usingBigIntprecision.
If a single update causes liquidity to drop past a configured percentage threshold, the sentinel immediately flags a critical withdrawal alert.
Live Mainnet Output
Running the monitor against the mainnet SOL/USDC pool (58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2) resolves the Quote Vault (HLmqeL62xR1QoZ1HKKbXRrdN1p3phKpxRMb2VVopvBBz) and streams every swap's balance change in real time:
text
Initializing Sentinel-Solana for pool: 58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2
Pool successfully decoded:
Base Mint: So11111111111111111111111111111111111111112
Quote Mint: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
Quote Vault (Liquidity): HLmqeL62xR1QoZ1HKKbXRrdN1p3phKpxRMb2VVopvBBz
WebSocket subscriptions active. Waiting for real-time events...
Quote Vault balance updated: 8015896570107
Quote Vault balance updated: 8015898213075
Quote Vault balance updated: 8015947713075
What's Next
This architecture provides a low-latency foundation that can be extended with:
Instant Alerting: Dispatching critical liquidity drop alerts directly to Telegram or Discord webhooks.
Dual-Vault Correlation: Tracking both baseVault and quoteVault simultaneously to distinguish normal arbitrage swaps from one-sided liquidity drains.
Custom Program Layouts: Applying the same binary buffer parsing approach with @coral-xyz/borsh to monitor custom Anchor or native Solana contracts.
How would you approach real-time anomaly detection and alerting on Solana AMM pools? Feel free to share your thoughts or ask questions about the architecture in the comments below.
Markdown
## What's Next
This architecture provides a low-latency foundation that can be extended with:
- **Instant Alerting**: Dispatching critical liquidity drop alerts directly to Telegram or Discord webhooks.
- **Dual-Vault Correlation**: Tracking both `baseVault` and `quoteVault` simultaneously to distinguish normal arbitrage swaps from one-sided liquidity drains.
- **Custom Program Layouts**: Applying the same binary buffer parsing approach with `@coral-xyz/borsh` to monitor custom Anchor or native Solana contracts.
How would you approach real-time anomaly detection and alerting on Solana AMM pools? Feel free to share your thoughts or ask questions about the architecture in the comments below.
Top comments (0)