|
| 1 | +# BNBChain AI Trading Assistant |
| 2 | + |
| 3 | +AI-powered trading assistant for BNB Chain ecosystem with real USDT→BNB swaps via PancakeSwap, technical analysis, and natural language interface |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- **Node.js** (v18 or higher) |
| 10 | +- **pnpm** package manager |
| 11 | +- **OpenAI API Key** for AI-powered analysis |
| 12 | +- **Private Key** for BNB Chain wallet operations with some BNB balance |
| 13 | +- **USDT Balance** at least 10 USDT on BNB Smart Chain (BSC) for trading |
| 14 | + |
| 15 | +### Installation |
| 16 | + |
| 17 | +1. **Install dependencies**: |
| 18 | + ```bash |
| 19 | + pnpm install |
| 20 | + ``` |
| 21 | + |
| 22 | +2. **Environment setup**: |
| 23 | + ```bash |
| 24 | + cp .env.example .env |
| 25 | + ``` |
| 26 | + |
| 27 | + Add your credentials to `.env`: |
| 28 | + ```env |
| 29 | + OPENAI_API_KEY=your_openai_api_key_here |
| 30 | + OPENAI_MODEL=gpt-4o |
| 31 | +
|
| 32 | + PRIVATE_KEY=your_bnb_chain_private_key_here |
| 33 | + ``` |
| 34 | + |
| 35 | +### Running the Project |
| 36 | + |
| 37 | +1. **Start the AI Trading Assistant**: |
| 38 | + ```bash |
| 39 | + pnpm start |
| 40 | + ``` |
| 41 | + |
| 42 | +2. **Start trading with natural language**: |
| 43 | + ``` |
| 44 | + > [email protected] start /Users/siew/DAOventures/ThirdFi/bnbchain-mcp |
| 45 | + > tsx src/index.ts |
| 46 | + |
| 47 | + MCP Client Started! |
| 48 | + Type your queries and I'll help you with trading analysis and blockchain operations! |
| 49 | + |
| 50 | + User: analysis bnb |
| 51 | + ⏳ Fetching market data and generating analysis... Please wait. |
| 52 | + 📡 Fetching BNB market data from CoinGecko... |
| 53 | + ✅ Received 180 data points |
| 54 | + 📊 Calculating technical indicators... |
| 55 | + ✅ Technical analysis complete |
| 56 | + 🤖 Generating AI analysis... |
| 57 | + 🔍 Extracting trading signal data... |
| 58 | + 🤖 Generating Trump-style analysis text... |
| 59 | + ✅ Analysis text generated |
| 60 | + ✅ Analysis generated successfully |
| 61 | + 📊 Trading Signal JSON: { |
| 62 | + asset: 'BNB', |
| 63 | + position: 'long', |
| 64 | + entry: 689.86, |
| 65 | + target: 704.21, |
| 66 | + stopLoss: 674.79, |
| 67 | + confidence: 7 |
| 68 | + } |
| 69 | + |
| 70 | + Assistant: |
| 71 | + [Calling tool performAnalysis with args {"token":"BNB"}] |
| 72 | + The analysis on BNB shows a **BULLISH** sentiment with very promising indicators: |
| 73 | + |
| 74 | + - **Current Price**: BNB is trading above key moving averages, signifying strong upward momentum. |
| 75 | + - **RSI**: Not in overbought territory, implying there's still room for growth. |
| 76 | + - **Bollinger Bands**: Indicating a squeeze, which often leads to significant price movement. |
| 77 | + - **MACD**: A bullish cross is likely, suggesting an increase in momentum. |
| 78 | + |
| 79 | + ### Trading Opportunity |
| 80 | + - **Entry Price**: $689.86 |
| 81 | + - **Target Price**: $704.21 |
| 82 | + - **Stop Loss**: $674.79 |
| 83 | + - **Confidence Score**: 7 out of 10 |
| 84 | + |
| 85 | + Would you like to proceed with a trade for 10 USDT? This would capitalize on the bullish momentum identified in the analysis. If you're unsure, I can check the current price for you. |
| 86 | + |
| 87 | + User: check the current price |
| 88 | + 📡 Fetching current price... Please wait. |
| 89 | + 📡 Fetching current price for BNB... |
| 90 | + ✅ Current price fetched: $691.75 |
| 91 | + |
| 92 | + Assistant: |
| 93 | + [Calling tool getCurrentPrice with args {"token":"BNB"}] |
| 94 | + 💰 **Current BNB Price:** $691.75 USD |
| 95 | + |
| 96 | + The price has moved slightly since the analysis. Would you like to proceed with the trade at the updated price of $691.75, or do you have any other questions? |
| 97 | + |
| 98 | + User: proceed with the trade |
| 99 | + 🔍 Checking USDT balance and executing trade... Please wait. |
| 100 | + 🔍 Checking USDT balance... |
| 101 | + 🔍 Getting user's wallet address... |
| 102 | + ✅ User address: 0x7719E324D758Dc4B140D215FF8b999F5Ac1C7F45 |
| 103 | + ✅ USDT Balance: 10 |
| 104 | + 🚀 Executing trade for 10 USDT worth of BNB... |
| 105 | + 🔍 Getting user's wallet address... |
| 106 | + ✅ User address: 0x7719E324D758Dc4B140D215FF8b999F5Ac1C7F45 |
| 107 | + 📊 Fetching current BNB price for swap calculation... |
| 108 | + 🚀 Starting USDT to BNB swap... |
| 109 | + 💰 Amount: 10 USDT |
| 110 | + 📊 BNB Price: $691.75 |
| 111 | + 🔍 Checking USDT allowance... |
| 112 | + ✅ Approving USDT for Permit2... |
| 113 | + ✅ USDT approval completed |
| 114 | + ✍️ Generating Permit2 signature... |
| 115 | + 🔄 Executing swap via Universal Router... |
| 116 | + ⏳ Waiting for transaction confirmation... |
| 117 | + ✅ Swap completed successfully! |
| 118 | + 📝 Transaction hash: 0x0e8af450cecefb33515f8e15c7d4e56fdff25ea5f744d9722d3a4650c6536c28 |
| 119 | + |
| 120 | + Assistant: |
| 121 | + [Calling tool executeTrade with args {"token":"BNB","amount":10}] |
| 122 | + ✅ Your trade was executed successfully! You swapped **10 USDT** for BNB. |
| 123 | + |
| 124 | + ### Transaction Details: |
| 125 | + - **Transaction Hash**: [0x0e8af450cecefb33515f8e15c7d4e56fdff25ea5f744d9722d3a4650c6536c28](https://bscscan.com/tx/0x0e8af450cecefb33515f8e15c7d4e56fdff25ea5f744d9722d3a4650c6536c28) |
| 126 | + - **New USDT Balance**: **0.00 USDT** |
| 127 | + |
| 128 | + You have now entered a position in BNB. Keep an eye on the price to reach your target of $704.21, and remember to monitor the stop loss at $674.79! If you need further assistance or want to perform another action, feel free to ask. |
| 129 | + ``` |
| 130 | + |
| 131 | +## Usage |
| 132 | + |
| 133 | +### Available Commands |
| 134 | + |
| 135 | +The AI trading assistant supports natural language queries for both analysis and real trading: |
| 136 | + |
| 137 | +- **Technical Analysis**: "Analyze BNB for me" |
| 138 | +- **Price Checking**: "What's the current price of BNB?" |
| 139 | +- **Balance Verification**: "What's my USDT balance?" |
| 140 | +- **Real Trading**: "Trade 10 USDT for BNB" |
| 141 | + |
| 142 | +### BNB Chain Ecosystem Features |
| 143 | + |
| 144 | +#### 🔗 MCP Server Integration |
| 145 | +- **BNBChain MCP Server**: Connects to the official `@bnb-chain/mcp` server for blockchain operations |
| 146 | +- **Real Blockchain Interactions**: Uses MCP tools for wallet operations and balance checks |
| 147 | +- **Secure Key Management**: Integrates with private key management for wallet operations |
| 148 | + |
| 149 | +#### 💱 Real Trading Capabilities |
| 150 | +- **PancakeSwap Integration**: Executes real USDT→BNB swaps using PancakeSwap Universal Router |
| 151 | +- **Permit2 Support**: Implements gasless approvals using Uniswap's Permit2 standard |
| 152 | +- **Smart Contract Interaction**: Direct interaction with BNB Chain smart contracts |
| 153 | +- **Balance Verification**: Real-time USDT balance checks before trade execution |
| 154 | + |
| 155 | +#### 🏗️ BNB Chain Infrastructure |
| 156 | +- **BSC Network**: Built specifically for BNB Smart Chain (BSC) |
| 157 | +- **Gas Optimization**: Efficient transaction handling with proper gas estimation |
| 158 | + |
| 159 | +### Smart Contract Addresses |
| 160 | + |
| 161 | +- **USDT Token**: `0x55d398326f99059fF775485246999027B3197955` |
| 162 | +- **Permit2**: `0x31c2F6fcFf4F8759b3Bd5Bf0e1084A055615c768` |
| 163 | +- **Universal Router**: `0x1A0A18AC4BECDDbd6389559687d1A73d8927E416` |
| 164 | +- **WBNB Token**: `0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c` |
| 165 | + |
| 166 | +### Technical Indicators |
| 167 | + |
| 168 | +- **Bollinger Bands**: Volatility and trend analysis |
| 169 | +- **RSI**: Overbought/oversold conditions |
| 170 | +- **MACD**: Trend momentum and crossovers |
| 171 | +- **Moving Averages**: Trend direction |
| 172 | + |
| 173 | +### Supported Tokens |
| 174 | + |
| 175 | +- **BNB** (Binance Coin) - Native BSC token with real trading |
| 176 | +- **BTC** (Bitcoin) - Analysis and price tracking |
| 177 | +- **ETH** (Ethereum) - Analysis and price tracking |
| 178 | +- **SOL** (Solana) - Analysis and price tracking |
| 179 | +- **XRP** (Ripple) - Analysis and price tracking |
| 180 | +- **ADA** (Cardano) - Analysis and price tracking |
| 181 | +- **DOGE** (Dogecoin) - Analysis and price tracking |
| 182 | +- **TRX** (Tron) - Analysis and price tracking |
| 183 | + |
| 184 | +### Data Flow |
| 185 | + |
| 186 | +#### Analysis Flow |
| 187 | +1. **User Query** → Natural language request for analysis |
| 188 | +2. **Tool Detection** → AI determines to use `performAnalysis` tool |
| 189 | +3. **Data Fetching** → CoinGecko API provides real market data |
| 190 | +4. **Technical Analysis** → Calculate indicators (RSI, MACD, Bollinger Bands) |
| 191 | +5. **Text Generation** → OpenAI generates analysis |
| 192 | +6. **Response** → Formatted analysis returned to user |
| 193 | + |
| 194 | +#### Trading Flow |
| 195 | +1. **User Query** → Natural language trade request |
| 196 | +2. **Balance Check** → MCP verifies USDT balance on BNB Chain |
| 197 | +3. **Price Fetch** → Get current BNB price from CoinGecko |
| 198 | +4. **Smart Contract Interaction** → Execute swap via PancakeSwap |
| 199 | +5. **Transaction Confirmation** → Return transaction hash and results |
| 200 | + |
| 201 | +### Security Features |
| 202 | + |
| 203 | +- **Private Key Management**: Secure environment variable handling |
| 204 | +- **Balance Verification**: Pre-trade balance checks |
| 205 | +- **Transaction Validation**: Proper error handling and confirmation |
| 206 | +- **Gas Estimation**: Automatic gas calculation for transactions |
| 207 | + |
| 208 | +### Project Structure |
| 209 | + |
| 210 | +``` |
| 211 | +src/ |
| 212 | +├── client.ts # Main MCP client with OpenAI integration |
| 213 | +├── index.ts # Entry point |
| 214 | +├── types/ |
| 215 | +│ └── index.ts # TypeScript type definitions |
| 216 | +├── utils/ |
| 217 | +│ ├── index.ts # Utility exports |
| 218 | +│ └── technicalAnalysis.ts # Technical analysis calculations |
| 219 | +└── services/ |
| 220 | + ├── index.ts # Service exports |
| 221 | + ├── coinGeckoService.ts # CoinGecko API integration |
| 222 | + ├── analysisService.ts # OpenAI analysis text generation |
| 223 | + ├── tradingAnalysisService.ts # Main analysis orchestration |
| 224 | + ├── currentPriceService.ts # Real-time price fetching |
| 225 | + ├── tradingService.ts # BNB Chain trading operations |
| 226 | + └── swapService.ts # PancakeSwap integration |
| 227 | +``` |
| 228 | + |
| 229 | +## Contact |
| 230 | + |
| 231 | + |
0 commit comments