From 100d83db53bcdae720b336cb2bd1a2ef156d858a Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Sun, 11 Aug 2024 14:43:27 +0000 Subject: [PATCH] handle 0x0 more gracefully --- src/claim.rs | 2 +- src/close.rs | 2 +- src/open.rs | 2 +- src/send_and_confirm.rs | 105 +++++++++++++++++++++++++++++----------- src/upgrade.rs | 2 +- src/utils.rs | 2 - 6 files changed, 82 insertions(+), 33 deletions(-) diff --git a/src/claim.rs b/src/claim.rs index ea790d16..29f823ae 100644 --- a/src/claim.rs +++ b/src/claim.rs @@ -99,7 +99,7 @@ impl Miner { &ore_api::consts::MINT_ADDRESS, &spl_token::id(), ); - self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false) + self.send_and_confirm(&[ix], ComputeBudget::Fixed(400_000), false) .await .ok(); diff --git a/src/close.rs b/src/close.rs index 72695dfa..7afd59db 100644 --- a/src/close.rs +++ b/src/close.rs @@ -37,7 +37,7 @@ impl Miner { // Submit close transaction let ix = ore_api::instruction::close(signer.pubkey()); - self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false) + self.send_and_confirm(&[ix], ComputeBudget::Fixed(500_000), false) .await .ok(); } diff --git a/src/open.rs b/src/open.rs index 640b9c9c..e132847e 100644 --- a/src/open.rs +++ b/src/open.rs @@ -15,7 +15,7 @@ impl Miner { // Sign and send transaction. println!("Generating challenge..."); let ix = ore_api::instruction::open(signer.pubkey(), signer.pubkey(), fee_payer.pubkey()); - self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false) + self.send_and_confirm(&[ix], ComputeBudget::Fixed(400_000), false) .await .ok(); } diff --git a/src/send_and_confirm.rs b/src/send_and_confirm.rs index 3ecc8f5c..da9dc97d 100644 --- a/src/send_and_confirm.rs +++ b/src/send_and_confirm.rs @@ -2,6 +2,7 @@ use std::{str::FromStr, time::Duration}; use chrono::Local; use colored::*; +use ore_api::error::OreError; use rand::seq::SliceRandom; use solana_client::{ client_error::{ClientError, ClientErrorKind, Result as ClientResult}, @@ -33,9 +34,10 @@ const GATEWAY_RETRIES: usize = 150; const CONFIRM_RETRIES: usize = 8; const CONFIRM_DELAY: u64 = 500; -const GATEWAY_DELAY: u64 = 0; //300; +const GATEWAY_DELAY: u64 = 0; pub enum ComputeBudget { + #[allow(dead_code)] Dynamic, Fixed(u32), } @@ -52,12 +54,6 @@ impl Miner { let fee_payer = self.fee_payer(); let mut send_client = self.rpc_client.clone(); - let current_tip = *self.tip.read().unwrap(); - - if current_tip > 0 { - send_client = self.jito_client.clone(); - } - // Return error, if balance is zero self.check_balance().await; @@ -65,8 +61,7 @@ impl Miner { let mut final_ixs = vec![]; match compute_budget { ComputeBudget::Dynamic => { - // TODO simulate - final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(1_400_000)) + todo!("simulate tx") } ComputeBudget::Fixed(cus) => { final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(cus)) @@ -81,8 +76,13 @@ impl Miner { // Add in user instructions final_ixs.extend_from_slice(ixs); - if current_tip > 0 { - let tips = [ + // Add jito tip + let jito_tip = *self.tip.read().unwrap(); + if jito_tip > 0 { + send_client = self.jito_client.clone(); + } + if jito_tip > 0 { + let tip_accounts = [ "96gYZGLnJYVFmbjzopPSU6QiEV5fGqZNyN9nmNhvrZU5", "HFqU5x63VTqvQss8hp11i4wVV8bD44PvwucfZ2bU7gRe", "Cw8CFyM9FkoMi7K7Crf6HNQqf4uEMzpKw6QNghXLvLkY", @@ -92,12 +92,16 @@ impl Miner { "DttWaMuVvTiduZRnguLF7jNxTgiMBZ1hyAumKUiL2KRL", "3AVi9Tg9Uo68tJfuvoKvqKNWKkC5wPdSSdeBnizKZ6jT", ]; - final_ixs.push(transfer( &signer.pubkey(), - &Pubkey::from_str(&tips.choose(&mut rand::thread_rng()).unwrap().to_string()) - .unwrap(), - current_tip, + &Pubkey::from_str( + &tip_accounts + .choose(&mut rand::thread_rng()) + .unwrap() + .to_string(), + ) + .unwrap(), + jito_tip, )); } @@ -172,15 +176,62 @@ impl Miner { for status in signature_statuses.value { if let Some(status) = status { if let Some(err) = status.err { - progress_bar.finish_with_message(format!( - "{}: {}", - "ERROR".bold().red(), - err - )); - return Err(ClientError { - request: None, - kind: ClientErrorKind::Custom(err.to_string()), - }); + match err { + // Instruction error + solana_sdk::transaction::TransactionError::InstructionError(_, err) => { + match err { + // Custom instruction error, parse into OreError + solana_program::instruction::InstructionError::Custom(err_code) => { + match err_code { + e if e == OreError::NeedsReset as u32 => { + attempts = 0; + progress_bar.println(format!( + " {} Needs reset. Retrying...", + "ERROR".bold().red(), + )); + }, + _ => { + progress_bar.finish_with_message(format!( + "{} {}", + "ERROR".bold().red(), + err + )); + return Err(ClientError { + request: None, + kind: ClientErrorKind::Custom(err.to_string()), + }); + } + } + }, + + // Non custom instruction error, return + _ => { + progress_bar.finish_with_message(format!( + "{} {}", + "ERROR".bold().red(), + err + )); + return Err(ClientError { + request: None, + kind: ClientErrorKind::Custom(err.to_string()), + }); + } + } + }, + + // Non instruction error, return + _ => { + progress_bar.finish_with_message(format!( + "{} {}", + "ERROR".bold().red(), + err + )); + return Err(ClientError { + request: None, + kind: ClientErrorKind::Custom(err.to_string()), + }); + } + } } if let Some(confirmation) = status.confirmation_status { match confirmation { @@ -210,7 +261,7 @@ impl Miner { // Handle confirmation errors Err(err) => { progress_bar.set_message(format!( - "{}: {}", + "{} {}", "ERROR".bold().red(), err.kind().to_string() )); @@ -222,7 +273,7 @@ impl Miner { // Handle submit errors Err(err) => { progress_bar.set_message(format!( - "{}: {}", + "{} {}", "ERROR".bold().red(), err.kind().to_string() )); @@ -233,7 +284,7 @@ impl Miner { std::thread::sleep(Duration::from_millis(GATEWAY_DELAY)); attempts += 1; if attempts > GATEWAY_RETRIES { - progress_bar.finish_with_message(format!("{}: Max retries", "ERROR".bold().red())); + progress_bar.finish_with_message(format!("{} Max retries", "ERROR".bold().red())); return Err(ClientError { request: None, kind: ClientErrorKind::Custom("Max retries".into()), diff --git a/src/upgrade.rs b/src/upgrade.rs index ca4fef17..187682f8 100644 --- a/src/upgrade.rs +++ b/src/upgrade.rs @@ -105,7 +105,7 @@ impl Miner { &ore_api::consts::MINT_ADDRESS, &spl_token::id(), ); - self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false) + self.send_and_confirm(&[ix], ComputeBudget::Fixed(500_000), false) .await .ok(); } diff --git a/src/utils.rs b/src/utils.rs index d19d26cf..9326cc19 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,6 @@ use std::{io::Read, time::Duration}; use cached::proc_macro::cached; -use colored::*; use ore_api::{ consts::{ CONFIG_ADDRESS, MINT_ADDRESS, PROOF, TOKEN_DECIMALS, TOKEN_DECIMALS_V1, TREASURY_ADDRESS, @@ -13,7 +12,6 @@ use serde::Deserialize; use solana_client::client_error::{ClientError, ClientErrorKind}; use solana_client::nonblocking::rpc_client::RpcClient; use solana_program::{pubkey::Pubkey, sysvar}; -use solana_rpc_client::spinner; use solana_sdk::{clock::Clock, hash::Hash}; use spl_associated_token_account::get_associated_token_address; use tokio::time::sleep;