Real-time monitoring and position management system for the WETH/USDC pool on Shadow DEX.
- Real-time Data Fetching: Polls Shadow DEX API every 15 seconds
- Price Calculation: Calculates actual price from sqrtPrice using Uniswap V3 formula
- 15-Second Candles: Creates OHLC candles with liquidity data
- Position Management: Automatically opens positions and monitors price ranges
- Rebalancing Logic: Detects when price moves out of ±0.1% range
- CSV Export: Saves all candle and position data to CSV files
- Live Dashboard: Real-time web interface with charts and metrics
- Create 15s Candles: Captures pool liquidity data every 15 seconds
- Define Range: Sets ±0.1% range at the open of each candle
- Distribution: Calculates % distribution of WETH & USDC
- Open Position: Opens position at candle open
- Monitor Position:
- UP: Price > Upper Range
- DOWN: Price < Lower Range
- Rebalance: When out of range, triggers rebalance (A for UP, B for DOWN)
- Next Candle: Opens new position on next 15-second candle after rebalance
- Install Dependencies:
npm install- Configure Environment:
cp .env.example .env
# Edit .env with your configurationRequired variables:
SONIC_RPC_URL: Your Sonic RPC endpointPOOL_ADDRESS: WETH/USDC pool address
Optional variables:
WEBHOOK_URL: Real-time webhook for AI scientist/analysisPRIVATE_KEY: For automated trading (Phase 2)SWAP_HELPER_ADDRESS: Deployed swap helper contract
Test swaps and liquidity operations with small amounts (~$0.50):
# Test a swap
npm run test:swap-buy # Swap USDC → WETH
npm run test:swap-sell # Swap WETH → USDC
# Test liquidity
npm run test:add-liq # Add ~$0.50 liquidity
npm run test:positions # View all positions
# Run all tests
npm run test:allSee TESTING_TRANSACTIONS.md for complete guide.
- Start the Application:
npm start-
Open Dashboard:
- Navigate to
http://localhost:3000/index.html - Dashboard updates automatically every 5 seconds
- Navigate to
-
Monitor Console:
- The terminal shows real-time logs of:
- Price updates
- Candle creation
- Position status
- Rebalancing events
- The terminal shows real-time logs of:
Two CSV files are created in the root directory:
-
candles.csv:
- Timestamp
- Open, High, Low, Close prices
- Liquidity
- WETH Amount
- USDC Amount
-
positions.csv:
- Timestamp
- Status (Position Open, Out of Range - UP/DOWN, Waiting for Rebalance)
- Current Price
- Upper Range, Lower Range
- WETH %, USDC %
- Rebalance Type (A or B)
The backend provides the following endpoints:
GET /api/current- Current candle, position, and recent tick dataGET /api/candles- Last 15 minutes of candlesGET /api/positions- Position historyGET /api/all-data- Complete dataset (last hour)
Real-time position data can be sent to an external webhook (e.g., AI scientist endpoint) by configuring WEBHOOK_URL in .env.
Webhook Payload (sent on every position update):
{
"timestamp": 1234567890,
"status": "Monitoring|Price-UP|Price-DOWN|Open-UP|Open-DOWN",
"upper_range": 3100.5,
"lower_range": 3090.5,
"open": 3095.0,
"high": 3097.0,
"low": 3094.0,
"close": 3096.0,
"weth_pct": 51.2,
"usdc_pct": 48.8,
"rebalance_type": "N/A|Rebalance UP|Rebalance DOWN",
"pool_address": "0x6fb30f3fcb864d49cdff15061ed5c6adfee40b40",
"network": "sonic"
}Setup:
- Add
WEBHOOK_URL=https://your-endpoint.com/api/positionsto.env - Webhook will auto-send on every position update (every 10 seconds)
- 5-second timeout with error logging
- Non-blocking - won't stop data collection if webhook fails
- Current Price: Real-time WETH/USDC price
- Position Status: Shows if position is open, out of range, or waiting
- Range Indicators: Visual upper and lower range boundaries
- Token Distribution: Live WETH/USDC percentage breakdown
- 15s Candle Data: Current candle OHLC values
- Price Chart: Last hour of price data with range lines
Edit sonic-execution.js to modify:
const POOL_ADDRESS = '0x6fb30f3fcb864d49cdff15061ed5c6adfee40b40'; // Pool to monitor
const FETCH_INTERVAL = 15000; // 15 seconds
const RANGE_PERCENTAGE = 0.1; // 0.1% rangeThe price is calculated from the pool's sqrtPrice value:
price = (sqrtPrice / 2^96)^2 * 10^(decimals1 - decimals0)Where:
sqrtPrice: Square root price from the pool- USDC decimals: 6
- WETH decimals: 18
Token distribution is calculated based on:
- Reserve values in the pool
- Current price
- Total value locked (TVL)
USDC % = (USDC_value / Total_value) * 100
WETH % = (WETH_value_in_USDC / Total_value) * 100To run the monitor for extended periods (e.g., multiple hours):
- Use
nohupor a process manager likepm2:
npm install -g pm2
pm2 start sonic-execution.js --name sonic-monitor
pm2 logs sonic-monitor- To stop:
pm2 stop sonic-monitorAfter running for an hour or more, you can analyze the CSV files:
- Excel/Google Sheets: Open the CSV files directly
- Python: Use pandas for analysis
- Custom Analysis: The data is structured for easy processing
- Pool Address:
0x6fb30f3fcb864d49cdff15061ed5c6adfee40b40 - DEX: Shadow (Sonic Network)
- Tokens: WETH/USDC
- Fee Tier: Dynamic (shown in API response)
- API Not Responding: Check if Shadow API is accessible
- No Data in Dashboard: Ensure backend is running on port 3000
- CSV Files Not Created: Check write permissions in directory
- Chart Not Updating: Check browser console for CORS or fetch errors
Future enhancements could include:
- Actual on-chain transaction execution for rebalancing
- Multiple pool monitoring
- Alert system (email/Telegram)
- Historical data analysis and backtesting
- Advanced charting with TradingView
MIT