@@ -869,46 +869,52 @@ where
869869 . await
870870 . expect ( "Fail to estimate USD gas fees" ) ;
871871
872- let should_send_now = effective_gas_estimate <= target_usd_fee_threshold
873- || ( attempt == max_estimate_retries
874- && effective_gas_estimate < max_usd_fee_threshold) ;
875-
876872 last_estimates. push ( round_to_decimals ( effective_gas_estimate, 2 ) ) ;
877873
878- if should_send_now {
879- let receipt = self . submit_proof ( chain_id, tx) . await ?;
880- let wei = Unit :: ETHER . wei_const ( ) . to :: < u128 > ( ) as f64 ;
881- let effective_gas_price: f64 = receipt. effective_gas_price ( ) as f64 ;
882- let effective_gas_used =
883- effective_gas_price. mul ( receipt. gas_used ( ) as f64 ) . div ( wei) ;
884-
885- let eth_to_usd_rate = fetch_usd_rate ( ) . await ?;
886- let usd_fee = effective_gas_used. mul ( eth_to_usd_rate. from_asset . to_asset ) ;
887-
888- info ! (
889- message = "Transaction gas fee used" ,
890- gas_fee = effective_gas_used,
891- usd_fee = usd_fee,
892- tx_hash = %receipt. transaction_hash( ) ,
893- last_usd_gas_estimates = ?last_estimates
894- ) ;
895-
896- break receipt. transaction_hash ( ) ;
897- } else if !should_send_now {
898- error ! (
899- message = "Failed to match send proof condition" ,
900- gas_estimate = effective_gas_estimate,
901- attempts = attempt
902- ) ;
903- bail ! ( "Failed to match send proof condition" ) ;
874+ // 1. If the price is acceptable let's just send it, no questions asked.
875+ if effective_gas_estimate <= target_usd_fee_threshold {
876+ let tx_hash = self
877+ . submit_proof_with_info ( chain_id, & tx, last_estimates. last ( ) . cloned ( ) )
878+ . await ?;
879+ break tx_hash;
904880 }
905881
906882 info ! (
907883 message = "USD Gas fee too high!!" ,
908884 usd_estimate = round_to_decimals( effective_gas_estimate, 2 )
909885 ) ;
910- sleep ( Duration :: from_secs ( retry_sleep_interval) ) . await ;
911- attempt += 1 ;
886+
887+ error ! (
888+ message = "Failed to match send proof condition" ,
889+ gas_estimate = effective_gas_estimate,
890+ attempts = attempt
891+ ) ;
892+
893+ // 2. Otherwise let's wait and see if the price will go down later. Do this
894+ // for [`max_estimate_retries`] times
895+ if attempt < max_estimate_retries {
896+ error ! ( message = "Retrying..." , ) ;
897+ sleep ( Duration :: from_secs ( retry_sleep_interval) ) . await ;
898+ attempt += 1 ;
899+ continue ;
900+ }
901+
902+ error ! ( message = "Exhausted all retires. Checking one more time but this time with a higher threshold" , ) ;
903+
904+ // 3. If we reached the maximum number of retires we should check one more time,
905+ // but this time with a higher threshold. If the higher threshold doesn't help
906+ // then we need to bail out. Sorry
907+ if effective_gas_estimate <= max_usd_fee_threshold {
908+ let tx_hash = self
909+ . submit_proof_with_info ( chain_id, & tx, last_estimates. last ( ) . cloned ( ) )
910+ . await ?;
911+ break tx_hash;
912+ }
913+
914+ // If we are here it means that we have exhausted all the retires and everything.
915+ // There is nothing that we can do now. :(
916+ error ! ( message = "Exhausted all options. Failed to submit proof. Exiting loop" ) ;
917+ bail ! ( "Failed to match send proof condition" ) ;
912918 } ;
913919
914920 return Ok ( tx_hash) ;
@@ -918,6 +924,31 @@ where
918924 }
919925 }
920926
927+ async fn submit_proof_with_info (
928+ & self ,
929+ chain_id : u64 ,
930+ tx : & N :: TransactionRequest ,
931+ last_estimates : Option < f64 > ,
932+ ) -> Result < B256 > {
933+ let receipt = self . submit_proof ( chain_id, tx. clone ( ) ) . await ?;
934+ let wei = Unit :: ETHER . wei_const ( ) . to :: < u128 > ( ) as f64 ;
935+ let effective_gas_price: f64 = receipt. effective_gas_price ( ) as f64 ;
936+ let effective_gas_used = effective_gas_price. mul ( receipt. gas_used ( ) as f64 ) . div ( wei) ;
937+
938+ let eth_to_usd_rate = fetch_usd_rate ( ) . await ?;
939+ let usd_fee = effective_gas_used. mul ( eth_to_usd_rate. from_asset . to_asset ) ;
940+
941+ info ! (
942+ message = "Transaction gas fee used" ,
943+ gas_fee = effective_gas_used,
944+ usd_fee = usd_fee,
945+ tx_hash = %receipt. transaction_hash( ) ,
946+ last_usd_gas_estimates = ?last_estimates
947+ ) ;
948+
949+ Ok ( receipt. transaction_hash ( ) )
950+ }
951+
921952 /// Check the verifying key in the contract matches the
922953 /// verifying key in the prover for the given `chain_id`.
923954 async fn check_vkey ( & self , chain_id : u64 ) -> Result < ( ) > {
0 commit comments