Implement working Drift leverage trading

Key Features:
-  Drift SDK v2.126.0-beta.14 integration with Helius RPC
-  User account initialization and balance reading
-  Leverage trading API with real trades executed
-  Support for SOL, BTC, ETH, APT, AVAX, BNB, MATIC, ARB, DOGE, OP
-  Transaction confirmed: gNmaWVqcE4qNK31ksoUsK6pcHqdDTaUtJXY52ZoXRF

API Endpoints:
- POST /api/drift/trade - Main trading endpoint
- Actions: get_balance, place_order
- Successfully tested with 0.01 SOL buy order at 2x leverage

Technical Fixes:
- Fixed RPC endpoint blocking with Helius API key
- Resolved wallet signing compatibility issues
- Implemented proper BigNumber handling for amounts
- Added comprehensive error handling and logging

Trading Bot Status: 🚀 FULLY OPERATIONAL with leverage trading!
This commit is contained in:
mindesbunister
2025-07-22 12:23:51 +02:00
parent 491ff51ba9
commit fb194f1b12
28 changed files with 7285 additions and 198 deletions

62
test-trade-processing.js Normal file
View File

@@ -0,0 +1,62 @@
// Test the enhanced trade processing logic
const dummyTrade = {
id: 'test',
side: 'BUY',
amount: 2.04,
price: 100.0522427503856,
entryPrice: null,
exitPrice: null,
profit: null,
status: 'COMPLETED',
createdAt: new Date('2025-07-21T08:17:07Z'),
closedAt: null
};
console.log('Original trade data:');
console.log(JSON.stringify(dummyTrade, null, 2));
// Apply the enhanced logic
const currentPrice = 175.82;
const entryPrice = dummyTrade.entryPrice || dummyTrade.price;
let exitPrice = dummyTrade.exitPrice;
let calculatedProfit = dummyTrade.profit;
// If exit price is null but trade is completed, try to calculate from profit
if (dummyTrade.status === 'COMPLETED' && !exitPrice && calculatedProfit !== null && calculatedProfit !== undefined) {
if (dummyTrade.side === 'BUY') {
exitPrice = entryPrice + (calculatedProfit / dummyTrade.amount);
} else {
exitPrice = entryPrice - (calculatedProfit / dummyTrade.amount);
}
}
// If profit is null but we have both prices, calculate profit
if (dummyTrade.status === 'COMPLETED' && (calculatedProfit === null || calculatedProfit === undefined) && exitPrice && entryPrice) {
if (dummyTrade.side === 'BUY') {
calculatedProfit = (exitPrice - entryPrice) * dummyTrade.amount;
} else {
calculatedProfit = (entryPrice - exitPrice) * dummyTrade.amount;
}
}
// Determine result based on actual profit
let result = 'ACTIVE';
if (dummyTrade.status === 'COMPLETED') {
if (calculatedProfit === null || calculatedProfit === undefined) {
result = 'UNKNOWN'; // When we truly don't have enough data
} else if (Math.abs(calculatedProfit) < 0.01) { // Within 1 cent
result = 'BREAKEVEN';
} else if (calculatedProfit > 0) {
result = 'WIN';
} else {
result = 'LOSS';
}
}
console.log('\nProcessed trade data:');
console.log('Entry Price:', entryPrice);
console.log('Exit Price:', exitPrice);
console.log('Calculated Profit:', calculatedProfit);
console.log('Result:', result);
console.log('\nThis trade would be marked as UNKNOWN because it has no exit price or profit data.');