Fix critical balance validation and add comprehensive trading features
- Fixed CoinGecko API rate limiting with fallback SOL price (68.11) - Corrected internal API calls to use proper Docker container ports - Fixed balance validation to prevent trades exceeding wallet funds - Blocked 0.5 SOL trades with only 0.073 SOL available (~2.24) - Added persistent storage for positions, trades, and pending orders - Implemented limit order system with auto-fill monitoring - Created pending orders panel and management API - Added trades history tracking and display panel - Enhanced position tracking with P&L calculations - Added wallet balance validation API endpoint - Positions stored in data/positions.json - Trade history stored in data/trades.json - Pending orders with auto-fill logic - Real-time balance validation before trades - All trades now validate against actual wallet balance - Insufficient balance trades are properly blocked - Added comprehensive error handling and logging - Fixed Docker networking for internal API calls - SPOT and leveraged trading modes - Limit orders with price monitoring - Stop loss and take profit support - DEX integration with Jupiter - Real-time position updates and P&L tracking Tested and verified all balance validation works correctly
This commit is contained in:
86
lib/wallet-balance-tracker.js
Normal file
86
lib/wallet-balance-tracker.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
// Persistent storage for wallet balance changes
|
||||
const WALLET_TRANSACTIONS_FILE = path.join(process.cwd(), 'data', 'wallet-transactions.json')
|
||||
|
||||
// Ensure data directory exists
|
||||
const dataDir = path.join(process.cwd(), 'data')
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true })
|
||||
}
|
||||
|
||||
// Helper functions for wallet transaction tracking
|
||||
function loadWalletTransactions() {
|
||||
try {
|
||||
if (fs.existsSync(WALLET_TRANSACTIONS_FILE)) {
|
||||
const data = fs.readFileSync(WALLET_TRANSACTIONS_FILE, 'utf8')
|
||||
return JSON.parse(data)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading wallet transactions:', error)
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
function saveWalletTransactions(transactions) {
|
||||
try {
|
||||
fs.writeFileSync(WALLET_TRANSACTIONS_FILE, JSON.stringify(transactions, null, 2))
|
||||
} catch (error) {
|
||||
console.error('Error saving wallet transactions:', error)
|
||||
}
|
||||
}
|
||||
|
||||
export function addWalletTransaction(transaction) {
|
||||
const transactions = loadWalletTransactions()
|
||||
|
||||
const newTransaction = {
|
||||
id: `wtx_${Date.now()}_${Math.random().toString(36).substr(2, 8)}`,
|
||||
type: transaction.type, // 'trade_open', 'trade_close', 'swap', etc.
|
||||
symbol: transaction.symbol,
|
||||
amount: transaction.amount, // Positive = received, Negative = sent
|
||||
usdValue: transaction.usdValue,
|
||||
timestamp: Date.now(),
|
||||
txId: transaction.txId,
|
||||
positionId: transaction.positionId || null,
|
||||
notes: transaction.notes || null
|
||||
}
|
||||
|
||||
transactions.push(newTransaction)
|
||||
|
||||
// Keep only last 500 transactions
|
||||
if (transactions.length > 500) {
|
||||
transactions.splice(0, transactions.length - 500)
|
||||
}
|
||||
|
||||
saveWalletTransactions(transactions)
|
||||
|
||||
console.log('💼 Wallet transaction recorded:', newTransaction)
|
||||
return newTransaction
|
||||
}
|
||||
|
||||
export function getWalletTransactions() {
|
||||
return loadWalletTransactions()
|
||||
}
|
||||
|
||||
export function getWalletBalanceAdjustment() {
|
||||
const transactions = loadWalletTransactions()
|
||||
|
||||
// Calculate total adjustments by symbol
|
||||
const adjustments = {}
|
||||
|
||||
transactions.forEach(tx => {
|
||||
if (!adjustments[tx.symbol]) {
|
||||
adjustments[tx.symbol] = { amount: 0, usdValue: 0 }
|
||||
}
|
||||
adjustments[tx.symbol].amount += tx.amount
|
||||
adjustments[tx.symbol].usdValue += tx.usdValue
|
||||
})
|
||||
|
||||
return adjustments
|
||||
}
|
||||
|
||||
export function clearWalletTransactions() {
|
||||
saveWalletTransactions([])
|
||||
console.log('💼 Wallet transactions cleared')
|
||||
}
|
||||
Reference in New Issue
Block a user