Developing Telegram Bot for DEX Trading
User opens Uniswap V3, sees profitable spread between two pools—and by time they switch to wallet, confirm transaction and pay gas, MEV-bot already executed front-run and closed arbitrage. Telegram bot with direct RPC connection and pre-computed routes eliminates this gap to minimum. But writing such bot "quickly" means stepping on standard set of rakes: race conditions on wallet nonce, slippage slip at sharp price movements, private key leak through logs.
Architecture: Where DEX-Bots Really Break
Nonce Management with Parallel Transactions
Most common cause of "missing" transactions—nonce collision. Bot sends two txs almost simultaneously: both read pending nonce from node, both get same value, second cancels first. If node public (Infura, Alchemy free tier) and adds latency—situation worsens.
Solution: local nonce tracker with mutex. Each wallet call atomically increments local counter without re-querying node. On revert or timeout—sync with eth_getTransactionCount with pending parameter. Not rocket science, but without this bot starts hanging under load.
Slippage and Price Impact in Real Time
Uniswap V3 works with concentrated liquidity—price can shift sharply within tick if liquidity concentrated narrow. Fixed slippageTolerance: 0.5% in bot—either constant failed txs at volatile market or losses on price impact at illiquid pool.
Right approach: simulate swap before sending via eth_call to Uniswap V3 Quoter (QuoterV2). Quoter returns real amountOut with current pool state, ticks, fees. Compare with Chainlink oracle price—if divergence above threshold, don't send. Adds one RPC operation but saves from unprofitable executions.
Private Key in Environment Variables—Not Enough
Standard advice "store key in .env" works until first server breach or log leak (Node.js logs environment on unhandled exception). For trading bot with real funds, minimum—HSM or AWS KMS for signing. Key never leaves KMS; bot sends tx hash for signature and gets signed bytes.
If KMS too expensive—encrypted keystore with password from separate secret manager, not env vars. And mandatory rate limiting on withdraw function: max N ETH per hour from hot wallet.
Stack and Implementation
Base—Node.js with viem (or ethers.js v6 for projects already using ethers). viem preferred for new projects: strict typing, tree-shakeable, smaller bundle. For Python—web3.py with asyncio.
| Component | Tool | Why |
|---|---|---|
| RPC | Alchemy / own node | Latency <100ms, websocket subscriptions |
| DEX routing | Uniswap V3 SDK / 1inch API | Optimal swap route |
| Price oracle | Chainlink on-chain | Price manipulation protection |
| Messenger | Telegram Bot API (grammy/grammY) | Commands, alerts, confirmations |
| Database | PostgreSQL / Redis | Tx history, state cache |
| Deployment | Docker + PM2 | Uptime, auto-restart |
Telegram part intentionally kept thin: bot takes commands (/buy, /sell, /balance, /stop), displays tx status with hash and Etherscan link. All logic—in separate trading engine, not Telegram handlers. Allows testing trading engine in isolation.
Multi-DEX Integration
If aggregation across multiple DEXs needed (Uniswap, Curve, Balancer), use 1inch Aggregation API or custom routing via The Graph—query each protocol subgraph for current pool data. Custom routing gives logic control, but requires maintenance on protocol updates.
Development Process
Analytics and Design (2-3 days). Determine strategy: simple market buy/sell, limit orders via third-party (1inch Limit Orders, CoW Protocol), or custom logic. Choose chain (Ethereum mainnet, Arbitrum, Base—each has different fees, latency). Document wallet security model.
Core Development (3-7 days). Nonce manager, swap executor, price checker, Telegram handlers. Test coverage with mock RPC—simulate failed txs, nonce collisions, network timeouts.
Integration Testing (2-3 days). Testnet deploy (Sepolia), test with real RPC responses, check all edge cases: zero-balance wallet, gas above limit, fee-on-transfer token.
Deployment and Monitoring. VPS/server with uptime monitoring, Telegram alerts on critical errors, log all txs with gas and executed price.
Timeline Expectations
Basic bot for single DEX with buy/sell/balance commands—1-1.5 weeks. Multi-DEX with route aggregation, limit order logic and extended risk management—2-3 weeks. Custom strategies (arbitrage, listing sniping) discussed separately—timelines depend on algorithm complexity and latency requirements.







