Documentation Index
Fetch the complete documentation index at: https://docs.lpagent.io/llms.txt
Use this file to discover all available pages before exploring further.
What You’ll Build
An auto-rebalancing bot that runs on a loop and:- Monitors your open LP positions every 60 seconds
- Detects when a position goes out of range (stops earning fees)
- Zap-Out — withdraws liquidity back to SOL
- Zap-In — re-enters with a new position centered around the current price
How Rebalancing Works
When the price moves outside your position’s bin range, the position stops earning fees. A rebalance fixes this by closing the old position and opening a new one centered around the current price.The Bot’s Decision Loop
Every check interval, the bot follows this flow:Key Decisions the Bot Makes
| Decision | How It’s Handled |
|---|---|
| Should I rebalance? | Yes, if inRange is false for the position |
| How much to reinvest? | Checks SOL balance after zap-out, reserves 0.05 SOL for fees |
| Where to place the new range? | Centers around the current active bin, with configurable range |
| What strategy to use? | Configurable: Spot, Curve, or BidAsk |
Prerequisites
- An LP Agent API key
- A Solana wallet with SOL for transaction fees
- Node.js >= 18
Configuration
Before looking at the code, here are the settings you’ll want to tune:| Setting | Default | Description |
|---|---|---|
CHECK_INTERVAL_MS | 60000 | How often to check positions (ms). 60s is a good starting point. |
SLIPPAGE_BPS | 500 | Slippage tolerance (500 = 5%) |
BIN_RANGE | 34 | Bins on each side of active bin. Wider = less rebalancing, lower APR. |
STRATEGY | Spot | Distribution: Spot (uniform), Curve (concentrated), or BidAsk (edges) |
ZAP_OUT_OUTPUT | allBaseToken | Withdraw to SOL so you can easily zap-in again |
POOL_FILTER | undefined | Only rebalance a specific pool, or undefined for all |
Customization Ideas
These snippets show how to extend the bot. Each one plugs into the rebalance loop.Add a Stop-Loss
Exit positions that have lost too much value — don’t throw good money after bad:Add Discord Notifications
Get alerted when a rebalance happens:Dynamic Bin Range
Use wider ranges for volatile pools, tighter for stable ones:Filter by Pool Type
Only rebalance DLMM positions (skip DAMM V2):Running in Production
With PM2
With Docker
Environment Variables
For production, never hardcode secrets:API Endpoints Used
| Endpoint | Purpose |
|---|---|
GET /lp-positions/opening | Fetch open positions and check inRange |
GET /token/balance | Check SOL balance for reinvestment |
GET /pools/{poolId}/info | Get active bin for new position range |
POST /position/decrease-tx | Generate zap-out transactions |
POST /position/landing-decrease-tx | Land zap-out transactions via Jito |
POST /pools/{poolId}/add-tx | Generate zap-in transactions |
POST /pools/landing-add-tx | Land zap-in transactions via Jito |
Tips
- Check interval: 60s is a good default. Too fast = unnecessary API calls. Too slow = missed fees while out of range.
- Gas costs: Each rebalance costs ~0.01-0.03 SOL. Factor this into profitability.
- Bin range: Wider range = fewer rebalances but lower fee APR. Find the sweet spot for your pair.
- Transaction landing: LP Agent’s landing endpoints use Jito bundles — no need to set up your own Jito integration.
Complete Bot Script
Here’s the full bot ready to copy-paste and run:Full auto-rebalancing bot script
Full auto-rebalancing bot script
Next Steps
- Zap-In & Zap-Out Tutorial — Understand the underlying API flows
- Copy LP Best Practices — Learn from top LPers
- Explore the full API Reference