# LFJ Liquidity Book Auto-Rebalancer Monad mainnet MON/USDC concentrated liquidity auto-rebalancer for LFJ (Liquidity Book) DEX. Maintains a narrow (3-10 bin) LP position around the active price. When price moves out of range, automatically removes liquidity and re-adds at the new active bin to keep earning rewards. ## Features - Auto-rebalance on price movement out of range - Single-sided or dual-sided liquidity (uses whatever tokens are available) - Real-time dashboard with SSE - Configurable bin count, position size, slippage, cooldown - Rebalance history and stats tracking (SQLite) - Bark / Ntfy / Pushover push notifications - Docker deployment ready ## Quick Start ```bash # Install pnpm install # Configure cp .env.example .env.local # Edit .env.local with your PRIVATE_KEY and RPC # Run pnpm dev ``` Open http://localhost:3000, go to **Config** page, click **Start**. ## Environment Variables ```env # Required PRIVATE_KEY=0x... MONAD_RPC_URL=https://your-rpc-endpoint # Contracts (pre-filled defaults) LB_ROUTER_ADDRESS=0x18556DA13313f3532c54711497A8FedAC273220E LB_PAIR_ADDRESS=0x5afd3ec861f6104af26e8755abcc1f876de77620 # Strategy NUM_BINS=3 # 3-10 bins POSITION_SIZE_USD=50 # USD value per position POLL_INTERVAL_MS=3000 # polling frequency REBALANCE_COOLDOWN_MS=10000 # min time between rebalances SLIPPAGE_BPS=50 # slippage tolerance (0.5%) MAX_DAILY_REBALANCES=50 # Notifications (optional) NOTIFY_PROVIDER=bark # bark | ntfy | pushover NOTIFY_ENDPOINT=https://api.day.app/YOUR_KEY ``` ## How It Works ``` Every 3s: 1. Read active bin from LBPair 2. If no position → open new position 3. If active bin outside position range: a. Remove all liquidity (removeLiquidityNATIVE) b. Swap to rebalance if needed c. Open new position at current active bin (addLiquidityNATIVE) d. Log event + send notification ``` ## Dashboard Pages | Page | Description | |------|-------------| | `/` | Real-time status: engine state, price, position range, wallet balances, bin chart | | `/config` | Strategy settings, engine control (start/pause/stop), emergency withdraw | | `/history` | Rebalance event history table with stats | ## Tech Stack - Next.js 16 (App Router) + TypeScript - viem for Monad chain interaction - SQLite (better-sqlite3) for persistence - Tailwind CSS + shadcn/ui - Server-Sent Events for real-time updates ## Docker Deployment ```bash docker compose up -d --build ``` Data persisted in `./data/rebalancer.db`. ## Project Structure ``` src/ lib/ config.ts # Environment config + types chain/ # viem clients, ABIs, on-chain operations engine/ # Rebalancer engine (poll loop, state machine) db/ # SQLite schema + queries notifications/ # Push notification providers app/ page.tsx # Dashboard config/page.tsx # Configuration history/page.tsx # History api/ # SSE, control, history API routes hooks/ useEngineStatus.ts # SSE client hook ``` ## Key Contracts | Contract | Address | Chain | |----------|---------|-------| | LBPair (MON/USDC) | `0x5afd...7620` | Monad (143) | | LBRouter V2.2 | `0x1855...220E` | Monad (143) | | WMON (tokenX) | `0x3bd3...433A` | Monad (143) | | USDC (tokenY) | `0x7547...b603` | Monad (143) | Bin step: 5 (0.05% per bin)