fix: Correct Drift account balance display
- Remove incorrect fallback to SOL wallet balance - Show actual Drift account balance (0) instead of wallet balance (2.67) - Drift account balance should be separate from SOL wallet balance - Fix balance calculation to properly handle zero balances
This commit is contained in:
@@ -309,26 +309,32 @@ export class DriftTradingService {
|
||||
console.log(` Net USD Value: $${finalNetUsdValue.toFixed(2)}`)
|
||||
console.log(` Leverage: ${leverage.toFixed(2)}x`)
|
||||
|
||||
// If we have real collateral data, use it
|
||||
if (enhancedTotalCollateral > 0) {
|
||||
return {
|
||||
totalCollateral: enhancedTotalCollateral,
|
||||
freeCollateral: enhancedFreeCollateral,
|
||||
marginRequirement,
|
||||
accountValue: enhancedTotalCollateral,
|
||||
leverage,
|
||||
availableBalance: enhancedFreeCollateral,
|
||||
netUsdValue: finalNetUsdValue,
|
||||
unrealizedPnl: totalUnrealizedPnl
|
||||
}
|
||||
} else {
|
||||
// Fall through to fallback if no real data
|
||||
console.log('⚠️ No collateral data found, falling back to SOL balance conversion')
|
||||
// If we have real collateral data, use it - even if it's zero
|
||||
// This ensures we show the actual Drift account balance, not wallet balance
|
||||
return {
|
||||
totalCollateral: enhancedTotalCollateral,
|
||||
freeCollateral: enhancedFreeCollateral,
|
||||
marginRequirement,
|
||||
accountValue: enhancedTotalCollateral,
|
||||
leverage,
|
||||
availableBalance: enhancedFreeCollateral,
|
||||
netUsdValue: finalNetUsdValue,
|
||||
unrealizedPnl: totalUnrealizedPnl
|
||||
}
|
||||
|
||||
} catch (sdkError: any) {
|
||||
console.log('⚠️ SDK balance method failed, using fallback:', sdkError.message)
|
||||
// Fall through to fallback method
|
||||
console.log('⚠️ SDK balance method failed, returning zero balance for empty Drift account:', sdkError.message)
|
||||
// Return zero balance instead of using SOL wallet balance
|
||||
return {
|
||||
totalCollateral: 0,
|
||||
freeCollateral: 0,
|
||||
marginRequirement: 0,
|
||||
accountValue: 0,
|
||||
leverage: 0,
|
||||
availableBalance: 0,
|
||||
netUsdValue: 0,
|
||||
unrealizedPnl: 0
|
||||
}
|
||||
} finally {
|
||||
// Always unsubscribe to clean up
|
||||
if (this.driftClient) {
|
||||
@@ -341,53 +347,19 @@ export class DriftTradingService {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: Use SOL balance and estimate USD value
|
||||
console.log('📊 Using fallback balance method - converting SOL to estimated USD value')
|
||||
const solBalance = await this.connection.getBalance(this.publicKey)
|
||||
const solInTokens = solBalance / 1e9 // Convert lamports to SOL
|
||||
// DISABLED: Fallback SOL balance conversion - this should not be used for Drift account balance
|
||||
// The Drift account balance should be separate from the SOL wallet balance
|
||||
console.log('📊 Drift account has no balance - returning zero instead of using SOL wallet balance')
|
||||
|
||||
console.log(`🔍 Debug: Raw SOL balance in lamports: ${solBalance}`)
|
||||
console.log(`🔍 Debug: SOL balance in tokens: ${solInTokens}`)
|
||||
|
||||
// For your account, manually set the correct balance if the calculation seems wrong
|
||||
// This is a temporary fix until we can properly read the Drift account balance
|
||||
let correctedBalance = solInTokens
|
||||
if (solInTokens > 100) { // If showing unreasonably high SOL amount
|
||||
console.log('⚠️ SOL balance seems too high, using corrected value')
|
||||
correctedBalance = 1.6 // Approximately $256 worth at $160/SOL
|
||||
}
|
||||
|
||||
// Estimate SOL price (you might want to get this from an oracle or API)
|
||||
const estimatedSolPrice = 160 // Approximate SOL price in USD
|
||||
const estimatedUsdValue = correctedBalance * estimatedSolPrice
|
||||
|
||||
console.log(`💰 Fallback calculation: ${correctedBalance.toFixed(4)} SOL × $${estimatedSolPrice} = $${estimatedUsdValue.toFixed(2)}`)
|
||||
|
||||
// If the user has some SOL, provide reasonable trading limits
|
||||
if (estimatedUsdValue > 10) { // At least $10 worth
|
||||
const availableForTrading = estimatedUsdValue * 0.8 // Use 80% for safety
|
||||
|
||||
return {
|
||||
totalCollateral: estimatedUsdValue,
|
||||
freeCollateral: availableForTrading,
|
||||
marginRequirement: 0,
|
||||
accountValue: estimatedUsdValue,
|
||||
leverage: 1,
|
||||
availableBalance: availableForTrading,
|
||||
netUsdValue: estimatedUsdValue,
|
||||
unrealizedPnl: 0
|
||||
}
|
||||
}
|
||||
|
||||
// Very minimal balance
|
||||
// Return zero balance for empty Drift account
|
||||
return {
|
||||
totalCollateral: 0,
|
||||
freeCollateral: 0,
|
||||
marginRequirement: 0,
|
||||
accountValue: solInTokens,
|
||||
accountValue: 0,
|
||||
leverage: 0,
|
||||
availableBalance: 0,
|
||||
netUsdValue: solInTokens,
|
||||
netUsdValue: 0,
|
||||
unrealizedPnl: 0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user