// Direct API test to place REAL orders via the trading endpoint const https = require('https'); const http = require('http'); async function testRealOrderViaAPI() { console.log('๐Ÿš€ REAL DRIFT ORDER TEST VIA API'); console.log('============================================================'); console.log('โš ๏ธ This will place a REAL $1 order using the trading API'); console.log('๐Ÿ“‹ Order should appear in Drift Protocol interface'); console.log('============================================================\n'); // Test different percentage levels const testCases = [ { stopLossPercent: 1.5, takeProfitPercent: 1.0, name: 'Conservative Scalping (1.5%/1.0%)' }, { stopLossPercent: 1.0, takeProfitPercent: 0.75, name: 'Moderate Scalping (1.0%/0.75%)' }, { stopLossPercent: 0.5, takeProfitPercent: 0.25, name: 'Tight Scalping (0.5%/0.25%)' }, { stopLossPercent: 0.25, takeProfitPercent: 0.1, name: 'Ultra-tight Scalping (0.25%/0.1%)' } ]; const successfulTests = []; for (const testCase of testCases) { console.log(`๐Ÿ”ฌ Testing: ${testCase.name}`); console.log(` Stop Loss: ${testCase.stopLossPercent}%`); console.log(` Take Profit: ${testCase.takeProfitPercent}%`); try { const orderData = { symbol: 'SOL-PERP', side: 'buy', amount: 1, // $1 USD orderType: 'market', stopLoss: true, takeProfit: true, stopLossPercent: testCase.stopLossPercent, takeProfitPercent: testCase.takeProfitPercent, leverage: 1 }; console.log(` ๐Ÿ“ค Placing real order...`); const response = await makeAPIRequest('POST', '/api/drift/trade', orderData); if (response.success) { console.log(` โœ… SUCCESS: Order placed successfully!`); console.log(` ๐Ÿ“‹ Response:`, JSON.stringify(response, null, 2)); successfulTests.push({ ...testCase, response: response }); // Wait 5 seconds before closing the position console.log(` โณ Waiting 5 seconds before closing position...`); await new Promise(resolve => setTimeout(resolve, 5000)); // Close the position const closeData = { symbol: 'SOL-PERP', side: 'sell', amount: 1, orderType: 'market' }; const closeResponse = await makeAPIRequest('POST', '/api/drift/trade', closeData); if (closeResponse.success) { console.log(` ๐Ÿงน Position closed successfully`); } else { console.log(` โš ๏ธ Warning: Could not close position automatically`); console.log(` ๐Ÿ“‹ Close response:`, JSON.stringify(closeResponse, null, 2)); } } else { console.log(` โŒ FAILED: ${response.error || 'Unknown error'}`); console.log(` ๐Ÿ“‹ Full response:`, JSON.stringify(response, null, 2)); } } catch (error) { console.log(` โŒ FAILED: ${error.message}`); } console.log(''); // Empty line for readability // Wait between tests to avoid rate limiting await new Promise(resolve => setTimeout(resolve, 3000)); } // Summary console.log('๐ŸŽฏ FINAL RESULTS:'); console.log('============================================================'); if (successfulTests.length > 0) { console.log(`โœ… Successfully placed ${successfulTests.length}/${testCases.length} orders`); const tightestTest = successfulTests.reduce((tightest, current) => { return current.stopLossPercent < tightest.stopLossPercent ? current : tightest; }); console.log(`\n๐Ÿ† TIGHTEST SUCCESSFUL PERCENTAGES:`); console.log(` Stop Loss: ${tightestTest.stopLossPercent}%`); console.log(` Take Profit: ${tightestTest.takeProfitPercent}%`); console.log(` Test: ${tightestTest.name}`); console.log(`\n๐Ÿ’ก RECOMMENDED API UPDATE:`); console.log(` stopLossPercentCalc = Math.max(stopLossPercent / 100, ${(tightestTest.stopLossPercent / 100).toFixed(4)}) // ${tightestTest.stopLossPercent}%`); console.log(` takeProfitPercentCalc = Math.max(takeProfitPercent / 100, ${(tightestTest.takeProfitPercent / 100).toFixed(4)}) // ${tightestTest.takeProfitPercent}%`); } else { console.log('โŒ No orders were successfully placed'); console.log(' Current minimum percentages appear to be necessary'); } } function makeAPIRequest(method, path, data) { return new Promise((resolve, reject) => { const postData = JSON.stringify(data); const options = { hostname: 'localhost', port: 3000, path: path, method: method, headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) } }; const req = http.request(options, (res) => { let responseData = ''; res.on('data', (chunk) => { responseData += chunk; }); res.on('end', () => { try { const parsedData = JSON.parse(responseData); resolve(parsedData); } catch (e) { resolve({ success: false, error: 'Invalid JSON response', raw: responseData }); } }); }); req.on('error', (error) => { reject(error); }); req.write(postData); req.end(); }); } // Safety check if (process.argv.includes('--confirm')) { testRealOrderViaAPI().catch(console.error); } else { console.log('โš ๏ธ SAFETY CONFIRMATION REQUIRED โš ๏ธ'); console.log('This test will place REAL $1 orders on Drift Protocol'); console.log('Orders will be automatically closed after testing'); console.log(''); console.log('To proceed, run: node test-api-real-orders.js --confirm'); }