Phase 2: Market context capture at entry
- Added getFundingRate() method to DriftService - Capture expectedEntryPrice from oracle before order execution - Capture fundingRateAtEntry from Drift Protocol - Save market context fields to database (expectedEntryPrice, fundingRateAtEntry) - Calculate entry slippage percentage in createTrade() - Fixed template literal syntax errors in execute endpoint Database fields populated: - expectedEntryPrice: Oracle price before order - entrySlippagePct: Calculated from entrySlippage - fundingRateAtEntry: Current funding rate from Drift Next: Phase 3 (analytics API) or test market context on next trade
This commit is contained in:
@@ -233,6 +233,31 @@ export class DriftService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get funding rate for a perpetual market
|
||||
* Returns funding rate as percentage (e.g., 0.01 = 1% per 8 hours)
|
||||
*/
|
||||
async getFundingRate(marketIndex: number): Promise<number | null> {
|
||||
this.ensureInitialized()
|
||||
|
||||
try {
|
||||
const perpMarketAccount = this.driftClient!.getPerpMarketAccount(marketIndex)
|
||||
if (!perpMarketAccount) {
|
||||
console.warn(`⚠️ No perp market account found for index ${marketIndex}`)
|
||||
return null
|
||||
}
|
||||
|
||||
// Funding rate is stored as a number with 9 decimals (1e9)
|
||||
// Convert to percentage
|
||||
const fundingRate = Number(perpMarketAccount.amm.lastFundingRate) / 1e9
|
||||
|
||||
return fundingRate
|
||||
} catch (error) {
|
||||
console.error(`❌ Failed to get funding rate for market ${marketIndex}:`, error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get account health (margin ratio)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user