@@ -13,6 +13,7 @@ import {
1313 isOneOfVariant ,
1414 getVariant ,
1515 PerpMarkets ,
16+ BlockhashSubscriber ,
1617} from '@drift-labs/sdk' ;
1718import { Mutex } from 'async-mutex' ;
1819
@@ -51,6 +52,8 @@ const MAX_PRIORITY_FEE = process.env.MAX_PRIORITY_FEE
5152 ? parseInt ( process . env . MAX_PRIORITY_FEE ) || 500_000
5253 : 500_000 ;
5354
55+ const CACHED_BLOCKHASH_OFFSET = 5 ;
56+
5457const SLOT_STALENESS_THRESHOLD_RESTART = process . env
5558 . SLOT_STALENESS_THRESHOLD_RESTART
5659 ? parseInt ( process . env . SLOT_STALENESS_THRESHOLD_RESTART ) || 300
@@ -209,6 +212,7 @@ export class MakerBidAskTwapCrank implements Bot {
209212 private pythPullOracleClient : PythPullClient ;
210213 private pythHealthy : boolean = true ;
211214 private lookupTableAccounts : AddressLookupTableAccount [ ] ;
215+ private blockhashSubscriber : BlockhashSubscriber ;
212216
213217 private bundleSender ?: BundleSender ;
214218 private crankIntervalToMarketIndicies ?: { [ key : number ] : number [ ] } ;
@@ -220,6 +224,7 @@ export class MakerBidAskTwapCrank implements Bot {
220224 config : MakerBidAskTwapCrankConfig ,
221225 globalConfig : GlobalConfig ,
222226 runOnce : boolean ,
227+ blockhashSubscriber : BlockhashSubscriber ,
223228 pythPriceSubscriber ?: PythPriceFeedSubscriber ,
224229 lookupTableAccounts : AddressLookupTableAccount [ ] = [ ] ,
225230 bundleSender ?: BundleSender
@@ -236,6 +241,7 @@ export class MakerBidAskTwapCrank implements Bot {
236241 this . pythPullOracleClient = new PythPullClient ( this . driftClient . connection ) ;
237242 this . bundleSender = bundleSender ;
238243 this . crankIntervalToMarketIndicies = config . crankIntervalToMarketIndicies ;
244+ this . blockhashSubscriber = blockhashSubscriber ;
239245
240246 // Pyth lazer: remember to remove devnet guard
241247 if ( ! this . globalConfig . lazerEndpoints || ! this . globalConfig . lazerToken ) {
@@ -428,8 +434,7 @@ export class MakerBidAskTwapCrank implements Bot {
428434 ixs : TransactionInstruction [ ] ,
429435 doSimulation = true
430436 ) : Promise < VersionedTransaction | undefined > {
431- const recentBlockhash =
432- await this . driftClient . connection . getLatestBlockhash ( 'confirmed' ) ;
437+ const recentBlockhash = await this . getBlockhashForTx ( ) ;
433438
434439 let simResult : SimulateAndGetTxWithCUsResponse | undefined ;
435440 try {
@@ -441,7 +446,7 @@ export class MakerBidAskTwapCrank implements Bot {
441446 cuLimitMultiplier : CU_EST_MULTIPLIER ,
442447 minCuLimit : TWAP_CRANK_MIN_CU ,
443448 doSimulation,
444- recentBlockhash : recentBlockhash . blockhash ,
449+ recentBlockhash,
445450 } ) ;
446451 } catch ( error ) {
447452 logger . error ( `[${ this . name } ] simulating tx: ${ error } ` ) ;
@@ -469,6 +474,21 @@ export class MakerBidAskTwapCrank implements Bot {
469474 }
470475 }
471476
477+ private async getBlockhashForTx ( ) : Promise < string > {
478+ const cachedBlockhash = this . blockhashSubscriber . getLatestBlockhash (
479+ CACHED_BLOCKHASH_OFFSET
480+ ) ;
481+ if ( cachedBlockhash ) {
482+ return cachedBlockhash . blockhash as string ;
483+ }
484+
485+ const recentBlockhash =
486+ await this . driftClient . connection . getLatestBlockhash ( {
487+ commitment : 'confirmed' ,
488+ } ) ;
489+ return recentBlockhash . blockhash ;
490+ }
491+
472492 private async getPythIxsFromTwapCrankInfo (
473493 crankMarketIndex : number ,
474494 precedingIxs : TransactionInstruction [ ] = [ ]
0 commit comments