@@ -332,6 +332,13 @@ impl TokenMemo {
332332 }
333333}
334334
335+ #[ derive( Debug , Clone ) ]
336+ pub enum ComputeUnitLimit {
337+ Default ,
338+ Simulated ,
339+ Static ( u32 ) ,
340+ }
341+
335342pub struct Token < T > {
336343 client : Arc < dyn ProgramClient < T > > ,
337344 pubkey : Pubkey , /* token mint */
@@ -344,7 +351,7 @@ pub struct Token<T> {
344351 memo : Arc < RwLock < Option < TokenMemo > > > ,
345352 transfer_hook_accounts : Option < Vec < AccountMeta > > ,
346353 compute_unit_price : Option < u64 > ,
347- compute_unit_limit : Option < u32 > ,
354+ compute_unit_limit : ComputeUnitLimit ,
348355}
349356
350357impl < T > fmt:: Debug for Token < T > {
@@ -411,7 +418,7 @@ where
411418 memo : Arc :: new ( RwLock :: new ( None ) ) ,
412419 transfer_hook_accounts : None ,
413420 compute_unit_price : None ,
414- compute_unit_limit : None ,
421+ compute_unit_limit : ComputeUnitLimit :: Default ,
415422 }
416423 }
417424
@@ -466,8 +473,8 @@ where
466473 self
467474 }
468475
469- pub fn with_compute_unit_limit ( mut self , compute_unit_limit : u32 ) -> Self {
470- self . compute_unit_limit = Some ( compute_unit_limit) ;
476+ pub fn with_compute_unit_limit ( mut self , compute_unit_limit : ComputeUnitLimit ) -> Self {
477+ self . compute_unit_limit = compute_unit_limit;
471478 self
472479 }
473480
@@ -548,19 +555,16 @@ where
548555 . simulate_transaction ( & transaction)
549556 . await
550557 . map_err ( TokenError :: Client ) ?;
551- if let Ok ( units_consumed) = simulation_result. get_compute_units_consumed ( ) {
552- // Overwrite the compute unit limit instruction with the actual units consumed
553- let compute_unit_limit =
554- u32:: try_from ( units_consumed) . map_err ( |x| TokenError :: Client ( x. into ( ) ) ) ?;
555- instructions
556- . last_mut ( )
557- . expect ( "Compute budget instruction was added earlier" )
558- . data = ComputeBudgetInstruction :: set_compute_unit_limit ( compute_unit_limit) . data ;
559- } else {
560- // `get_compute_units_consumed()` fails for offline signing, so we
561- // catch that error and remove the instruction that was added
562- instructions. pop ( ) ;
563- }
558+ let units_consumed = simulation_result
559+ . get_compute_units_consumed ( )
560+ . map_err ( TokenError :: Client ) ?;
561+ // Overwrite the compute unit limit instruction with the actual units consumed
562+ let compute_unit_limit =
563+ u32:: try_from ( units_consumed) . map_err ( |x| TokenError :: Client ( x. into ( ) ) ) ?;
564+ instructions
565+ . last_mut ( )
566+ . expect ( "Compute budget instruction was added earlier" )
567+ . data = ComputeBudgetInstruction :: set_compute_unit_limit ( compute_unit_limit) . data ;
564568 Ok ( ( ) )
565569 }
566570
@@ -619,13 +623,17 @@ where
619623 // all instructions have been added to the transaction, so be sure to
620624 // keep this instruction as the last one before creating and sending the
621625 // transaction.
622- if let Some ( compute_unit_limit) = self . compute_unit_limit {
623- instructions. push ( ComputeBudgetInstruction :: set_compute_unit_limit (
624- compute_unit_limit,
625- ) ) ;
626- } else {
627- self . add_compute_unit_limit_from_simulation ( & mut instructions, & blockhash)
628- . await ?;
626+ match self . compute_unit_limit {
627+ ComputeUnitLimit :: Default => { }
628+ ComputeUnitLimit :: Simulated => {
629+ self . add_compute_unit_limit_from_simulation ( & mut instructions, & blockhash)
630+ . await ?;
631+ }
632+ ComputeUnitLimit :: Static ( compute_unit_limit) => {
633+ instructions. push ( ComputeBudgetInstruction :: set_compute_unit_limit (
634+ compute_unit_limit,
635+ ) ) ;
636+ }
629637 }
630638
631639 let message = Message :: new_with_blockhash ( & instructions, fee_payer, & blockhash) ;
0 commit comments