feat: Add direction-specific quality thresholds and dynamic collateral display
- Split QUALITY_LEVERAGE_THRESHOLD into separate LONG and SHORT variants - Added /api/drift/account-health endpoint for real-time collateral data - Updated settings UI to show separate controls for LONG/SHORT thresholds - Position size calculations now use dynamic collateral from Drift account - Updated .env and docker-compose.yml with new environment variables - LONG threshold: 95, SHORT threshold: 90 (configurable independently) Files changed: - app/api/drift/account-health/route.ts (NEW) - Account health API endpoint - app/settings/page.tsx - Added collateral state, separate threshold inputs - app/api/settings/route.ts - GET/POST handlers for LONG/SHORT thresholds - .env - Added QUALITY_LEVERAGE_THRESHOLD_LONG/SHORT variables - docker-compose.yml - Added new env vars with fallback defaults Impact: - Users can now configure quality thresholds independently for LONG vs SHORT signals - Position size display dynamically updates based on actual Drift account collateral - More flexible risk management with direction-specific leverage tiers
This commit is contained in:
36
app/api/drift/account-health/route.ts
Normal file
36
app/api/drift/account-health/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { initializeDriftService } from '@/lib/drift/client'
|
||||
|
||||
/**
|
||||
* GET /api/drift/account-health
|
||||
*
|
||||
* Returns current account health metrics from Drift Protocol
|
||||
* Used by settings UI to dynamically display collateral and calculate position sizes
|
||||
*/
|
||||
export async function GET() {
|
||||
try {
|
||||
const driftService = await initializeDriftService()
|
||||
|
||||
if (!driftService) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Drift service not initialized' },
|
||||
{ status: 503 }
|
||||
)
|
||||
}
|
||||
|
||||
const health = await driftService.getAccountHealth()
|
||||
|
||||
return NextResponse.json({
|
||||
totalCollateral: health.totalCollateral,
|
||||
freeCollateral: health.freeCollateral,
|
||||
totalLiability: health.totalLiability,
|
||||
marginRatio: health.marginRatio,
|
||||
})
|
||||
} catch (error: any) {
|
||||
console.error('❌ Error fetching account health:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch account health', details: error.message },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,8 @@ export async function GET() {
|
||||
USE_ADAPTIVE_LEVERAGE: env.USE_ADAPTIVE_LEVERAGE === 'true',
|
||||
HIGH_QUALITY_LEVERAGE: parseFloat(env.HIGH_QUALITY_LEVERAGE || '5'),
|
||||
LOW_QUALITY_LEVERAGE: parseFloat(env.LOW_QUALITY_LEVERAGE || '1'),
|
||||
QUALITY_LEVERAGE_THRESHOLD: parseInt(env.QUALITY_LEVERAGE_THRESHOLD || '95'),
|
||||
QUALITY_LEVERAGE_THRESHOLD_LONG: parseInt(env.QUALITY_LEVERAGE_THRESHOLD_LONG || env.QUALITY_LEVERAGE_THRESHOLD || '95'),
|
||||
QUALITY_LEVERAGE_THRESHOLD_SHORT: parseInt(env.QUALITY_LEVERAGE_THRESHOLD_SHORT || '90'),
|
||||
}
|
||||
|
||||
return NextResponse.json(settings)
|
||||
@@ -207,7 +208,8 @@ export async function POST(request: NextRequest) {
|
||||
USE_ADAPTIVE_LEVERAGE: settings.USE_ADAPTIVE_LEVERAGE.toString(),
|
||||
HIGH_QUALITY_LEVERAGE: settings.HIGH_QUALITY_LEVERAGE.toString(),
|
||||
LOW_QUALITY_LEVERAGE: settings.LOW_QUALITY_LEVERAGE.toString(),
|
||||
QUALITY_LEVERAGE_THRESHOLD: settings.QUALITY_LEVERAGE_THRESHOLD.toString(),
|
||||
QUALITY_LEVERAGE_THRESHOLD_LONG: settings.QUALITY_LEVERAGE_THRESHOLD_LONG.toString(),
|
||||
QUALITY_LEVERAGE_THRESHOLD_SHORT: settings.QUALITY_LEVERAGE_THRESHOLD_SHORT.toString(),
|
||||
}
|
||||
|
||||
const success = updateEnvFile(updates)
|
||||
|
||||
Reference in New Issue
Block a user