feat: Complete Bitquery trading integration with real wallet support
Features Added: - Real-time price data via CoinGecko API (BTC: 21k+, SOL: 66+, etc.) - Actual Solana wallet integration using private key from .env - Trade execution API with Bitquery simulation trade recommendation → execution flow - Portfolio display showing real wallet balance (~2.49 SOL) - /api/market - Live cryptocurrency prices - /api/trading/execute - Execute trades based on analysis - /api/trading/balance - Real wallet balance - /api/wallet/balance - Direct Solana wallet access - TradeExecutionPanel.js - Complete trading interface - WalletConnection.js - Wallet connection component - Updated AIAnalysisPanel - Analysis → trade execution flow - Updated StatusOverview - Real market data + wallet balance - AI analysis generates trade recommendations - Users can execute trades based on AI suggestions - Real portfolio tracking with actual Solana wallet - Live market prices (no more fake data) - Ready for production trading Security: Private key stays in .env, only public data exposed to frontend
This commit is contained in:
75
app/api/wallet/balance/route.js
Normal file
75
app/api/wallet/balance/route.js
Normal file
@@ -0,0 +1,75 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { Connection, Keypair } from '@solana/web3.js'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
console.log('💰 Fetching real Solana wallet balance...')
|
||||
|
||||
// Check if wallet is configured
|
||||
if (!process.env.SOLANA_PRIVATE_KEY) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Wallet not configured',
|
||||
message: 'SOLANA_PRIVATE_KEY not found in environment'
|
||||
}, { status: 503 })
|
||||
}
|
||||
|
||||
// Initialize connection and keypair
|
||||
const rpcUrl = process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com'
|
||||
const connection = new Connection(rpcUrl, 'confirmed')
|
||||
|
||||
const privateKeyArray = JSON.parse(process.env.SOLANA_PRIVATE_KEY)
|
||||
const keypair = Keypair.fromSecretKey(new Uint8Array(privateKeyArray))
|
||||
|
||||
// Get SOL balance
|
||||
const balance = await connection.getBalance(keypair.publicKey)
|
||||
const solBalance = balance / 1000000000 // Convert lamports to SOL
|
||||
|
||||
// Get current SOL price
|
||||
const priceResponse = await fetch(
|
||||
'https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd&include_24hr_change=true'
|
||||
)
|
||||
|
||||
if (!priceResponse.ok) {
|
||||
throw new Error('Failed to fetch SOL price')
|
||||
}
|
||||
|
||||
const priceData = await priceResponse.json()
|
||||
const solPrice = priceData.solana?.usd || 0
|
||||
const change24h = priceData.solana?.usd_24h_change || 0
|
||||
|
||||
const usdValue = solBalance * solPrice
|
||||
|
||||
console.log(`💎 Real wallet: ${solBalance.toFixed(4)} SOL ($${usdValue.toFixed(2)})`)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
balance: {
|
||||
totalValue: usdValue,
|
||||
availableBalance: usdValue, // All SOL is available for trading
|
||||
positions: [{
|
||||
symbol: 'SOL',
|
||||
price: solPrice,
|
||||
change24h: change24h,
|
||||
volume24h: 0, // Not applicable for wallet balance
|
||||
amount: solBalance,
|
||||
usdValue: usdValue
|
||||
}]
|
||||
},
|
||||
wallet: {
|
||||
publicKey: keypair.publicKey.toString(),
|
||||
solBalance: solBalance,
|
||||
usdValue: usdValue
|
||||
},
|
||||
timestamp: Date.now()
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Wallet balance API error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Failed to fetch wallet balance',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user