fix: resolve SL/TP order placement and orphaned cleanup issues

- Fixed Drift SDK initialization in place-order endpoint (NodeWallet vs Wallet)
- Added 2% minimum trigger distance validation in execute-drift endpoint
- Corrected orphaned order cleanup logic to cancel ALL orders when no position
- Resolved issue where SL/TP orders were immediately canceled due to insufficient distance
- Tested complete cycle: position entry → protective orders → orphaned cleanup

Orders now maintain proper 2% distance from market price and stay active.
Cleanup system correctly identifies and removes orphaned orders when positions close.
This commit is contained in:
mindesbunister
2025-07-28 17:51:21 +02:00
parent cf6fddc434
commit 16e0ed9e5f
4 changed files with 42 additions and 15 deletions

View File

@@ -99,13 +99,9 @@ export async function POST() {
// Check if this order is for a market where we have no position
const hasPosition = positionMarkets.has(order.marketIndex)
// CRITICAL FIX: Only cancel reduce-only orders if there's NO position
// Stop Loss and Take Profit orders are reduce-only but should EXIST when we have a position
const isReduceOnly = order.reduceOnly
// Only cancel orders that are truly orphaned (no position for that market)
// Do NOT cancel reduce-only orders when we have a position (these are SL/TP!)
return !hasPosition && !isReduceOnly
// CORRECTED LOGIC: Cancel ALL orders when no position exists
// If we have no position, then ALL orders (including SL/TP) are orphaned
return !hasPosition
})
// Additionally, find lingering SL/TP orders when position has changed significantly

View File

@@ -24,7 +24,7 @@ export async function POST(request) {
});
// Import Drift SDK
const { DriftClient, initialize, MarketType, PositionDirection, OrderType, OrderTriggerCondition, Wallet, BN } = await import('@drift-labs/sdk');
const { DriftClient, initialize, MarketType, PositionDirection, OrderType, OrderTriggerCondition, BN } = await import('@drift-labs/sdk');
const { Connection, Keypair } = await import('@solana/web3.js');
// Setup connection and wallet
@@ -40,7 +40,10 @@ export async function POST(request) {
const privateKeyArray = JSON.parse(process.env.SOLANA_PRIVATE_KEY);
const keypair = Keypair.fromSecretKey(new Uint8Array(privateKeyArray));
const wallet = new Wallet(keypair);
// Use the correct Wallet class like in cleanup endpoint
const { default: NodeWallet } = await import('@coral-xyz/anchor/dist/cjs/nodewallet.js');
const wallet = new NodeWallet(keypair);
// Initialize Drift client
const env = 'mainnet-beta';
@@ -49,12 +52,9 @@ export async function POST(request) {
connection,
wallet,
programID: sdkConfig.DRIFT_PROGRAM_ID,
accountSubscription: {
type: 'polling',
accountLoader: {
commitment: 'confirmed'
}
}
opts: {
commitment: 'confirmed',
},
});
await driftClient.subscribe();