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(` Net USD Value: $${finalNetUsdValue.toFixed(2)}`)
|
||||||
console.log(` Leverage: ${leverage.toFixed(2)}x`)
|
console.log(` Leverage: ${leverage.toFixed(2)}x`)
|
||||||
|
|
||||||
// If we have real collateral data, use it
|
// If we have real collateral data, use it - even if it's zero
|
||||||
if (enhancedTotalCollateral > 0) {
|
// This ensures we show the actual Drift account balance, not wallet balance
|
||||||
return {
|
return {
|
||||||
totalCollateral: enhancedTotalCollateral,
|
totalCollateral: enhancedTotalCollateral,
|
||||||
freeCollateral: enhancedFreeCollateral,
|
freeCollateral: enhancedFreeCollateral,
|
||||||
marginRequirement,
|
marginRequirement,
|
||||||
accountValue: enhancedTotalCollateral,
|
accountValue: enhancedTotalCollateral,
|
||||||
leverage,
|
leverage,
|
||||||
availableBalance: enhancedFreeCollateral,
|
availableBalance: enhancedFreeCollateral,
|
||||||
netUsdValue: finalNetUsdValue,
|
netUsdValue: finalNetUsdValue,
|
||||||
unrealizedPnl: totalUnrealizedPnl
|
unrealizedPnl: totalUnrealizedPnl
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Fall through to fallback if no real data
|
|
||||||
console.log('⚠️ No collateral data found, falling back to SOL balance conversion')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (sdkError: any) {
|
} catch (sdkError: any) {
|
||||||
console.log('⚠️ SDK balance method failed, using fallback:', sdkError.message)
|
console.log('⚠️ SDK balance method failed, returning zero balance for empty Drift account:', sdkError.message)
|
||||||
// Fall through to fallback method
|
// 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 {
|
} finally {
|
||||||
// Always unsubscribe to clean up
|
// Always unsubscribe to clean up
|
||||||
if (this.driftClient) {
|
if (this.driftClient) {
|
||||||
@@ -341,53 +347,19 @@ export class DriftTradingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback: Use SOL balance and estimate USD value
|
// DISABLED: Fallback SOL balance conversion - this should not be used for Drift account balance
|
||||||
console.log('📊 Using fallback balance method - converting SOL to estimated USD value')
|
// The Drift account balance should be separate from the SOL wallet balance
|
||||||
const solBalance = await this.connection.getBalance(this.publicKey)
|
console.log('📊 Drift account has no balance - returning zero instead of using SOL wallet balance')
|
||||||
const solInTokens = solBalance / 1e9 // Convert lamports to SOL
|
|
||||||
|
|
||||||
console.log(`🔍 Debug: Raw SOL balance in lamports: ${solBalance}`)
|
// Return zero balance for empty Drift account
|
||||||
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 {
|
return {
|
||||||
totalCollateral: 0,
|
totalCollateral: 0,
|
||||||
freeCollateral: 0,
|
freeCollateral: 0,
|
||||||
marginRequirement: 0,
|
marginRequirement: 0,
|
||||||
accountValue: solInTokens,
|
accountValue: 0,
|
||||||
leverage: 0,
|
leverage: 0,
|
||||||
availableBalance: 0,
|
availableBalance: 0,
|
||||||
netUsdValue: solInTokens,
|
netUsdValue: 0,
|
||||||
unrealizedPnl: 0
|
unrealizedPnl: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user