CRITICAL FIX: Add transaction confirmation to detect failed orders
- Added getConnection() method to DriftService - Added proper transaction confirmation in openPosition() - Check confirmation.value.err to detect on-chain failures - Return error if transaction fails instead of assuming success - Prevents phantom trades that never actually execute This fixes the issue where bot was recording trades with transaction signatures that don't exist on-chain (like 2gqrPxnvGzdRp56...).
This commit is contained in:
@@ -299,6 +299,13 @@ export class DriftService {
|
||||
return this.driftClient!
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Solana connection instance
|
||||
*/
|
||||
getConnection(): Connection {
|
||||
return this.connection
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user instance
|
||||
*/
|
||||
|
||||
@@ -141,13 +141,38 @@ export async function openPosition(
|
||||
console.log('🚀 Placing REAL market order...')
|
||||
const txSig = await driftClient.placePerpOrder(orderParams)
|
||||
|
||||
console.log(`✅ Order placed! Transaction: ${txSig}`)
|
||||
console.log(`📝 Transaction submitted: ${txSig}`)
|
||||
|
||||
// CRITICAL: Confirm transaction actually executed on-chain
|
||||
console.log('⏳ Confirming transaction on-chain...')
|
||||
const connection = driftService.getConnection()
|
||||
|
||||
try {
|
||||
const confirmation = await connection.confirmTransaction(txSig, 'confirmed')
|
||||
|
||||
if (confirmation.value.err) {
|
||||
console.error(`❌ Transaction failed on-chain:`, confirmation.value.err)
|
||||
return {
|
||||
success: false,
|
||||
error: `Transaction failed: ${JSON.stringify(confirmation.value.err)}`,
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Transaction confirmed on-chain: ${txSig}`)
|
||||
|
||||
} catch (confirmError) {
|
||||
console.error(`❌ Failed to confirm transaction:`, confirmError)
|
||||
return {
|
||||
success: false,
|
||||
error: `Transaction confirmation failed: ${confirmError instanceof Error ? confirmError.message : 'Unknown error'}`,
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a moment for position to update
|
||||
console.log('⏳ Waiting for position to update...')
|
||||
await new Promise(resolve => setTimeout(resolve, 2000))
|
||||
|
||||
// Get actual fill price from position (optional - may not be immediate in DRY_RUN)
|
||||
// Get actual fill price from position
|
||||
const position = await driftService.getPosition(marketConfig.driftMarketIndex)
|
||||
|
||||
if (position && position.side !== 'none') {
|
||||
|
||||
Reference in New Issue
Block a user