diff --git a/src/dynamic_fee.rs b/src/dynamic_fee.rs index 665f89cd..960720b3 100644 --- a/src/dynamic_fee.rs +++ b/src/dynamic_fee.rs @@ -13,7 +13,7 @@ enum FeeStrategy { } impl Miner { - pub async fn dynamic_fee(&self) -> Option { + pub async fn dynamic_fee(&self) -> Result { // Get url let rpc_url = self .dynamic_fee_url @@ -35,7 +35,7 @@ impl Miner { } else if host.contains("rpcpool.com") { FeeStrategy::Triton } else { - return None; + return Err("Dynamic fees not supported by this RPC.".to_string()); }; // Build fee estimate request @@ -109,45 +109,43 @@ impl Miner { FeeStrategy::Helius => response["result"]["priorityFeeEstimate"] .as_f64() .map(|fee| fee as u64) - .ok_or_else(|| format!("Failed to parse priority fee. Response: {:?}", response)) - .unwrap(), + .ok_or_else(|| format!("Failed to parse priority fee response: {:?}", response)), FeeStrategy::Quiknode => response["result"]["per_compute_unit"]["medium"] - .as_f64() - .map(|fee| fee as u64) - .ok_or_else(|| { - format!("Failed to parse priority fee. Response: {:?}", response) - }) - .unwrap(), + .as_f64() + .map(|fee| fee as u64) + .ok_or_else(|| format!("Please enable the Solana Priority Fee API add-on in your QuickNode account.")), FeeStrategy::Alchemy => response["result"] - .as_array() + .as_array() .and_then(|arr| { - Some( - arr.into_iter() - .map(|v| v["prioritizationFee"].as_u64().unwrap()) - .collect::>(), - ) - }) - .and_then(|fees| { - Some(((fees.iter().sum::() as f32 / fees.len() as f32).ceil() * 1.2) - as u64) - }) - .ok_or_else(|| { - format!("Failed to parse priority fee. Response: {:?}", response) - }) - .unwrap(), + Some( + arr.into_iter() + .map(|v| v["prioritizationFee"].as_u64().unwrap()) + .collect::>(), + ) + }) + .and_then(|fees| { + Some( + ((fees.iter().sum::() as f32 / fees.len() as f32).ceil() * 1.2) as u64, + ) + }) + .ok_or_else(|| format!("Failed to parse priority fee response: {:?}", response)), FeeStrategy::Triton => response["result"] .as_array() .and_then(|arr| arr.last()) .and_then(|last| last["prioritizationFee"].as_u64()) - .ok_or_else(|| format!("Failed to parse priority fee. Response: {:?}", response)) - .unwrap(), + .ok_or_else(|| format!("Failed to parse priority fee response: {:?}", response)), }; // Check if the calculated fee is higher than max - if let Some(max_fee) = self.priority_fee { - Some(calculated_fee.min(max_fee)) - } else { - Some(calculated_fee) + match calculated_fee { + Err(err) => Err(err), + Ok(fee) => { + if let Some(max_fee) = self.priority_fee { + Ok(fee.min(max_fee)) + } else { + Ok(fee) + } + } } } } diff --git a/src/mine.rs b/src/mine.rs index 1714a339..c549c53c 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -45,10 +45,13 @@ impl Miner { println!( "\nStake: {} ORE\n{} Multiplier: {:12}x", amount_u64_to_string(proof.balance), - if last_hash_at.gt(&0) { - format!(" Change: {} ORE\n", amount_u64_to_string(proof.balance.saturating_sub(last_balance))) + if last_hash_at.gt(&0) { + format!( + " Change: {} ORE\n", + amount_u64_to_string(proof.balance.saturating_sub(last_balance)) + ) } else { - "" + "".to_string() }, calculate_multiplier(proof.balance, config.top_balance) ); @@ -133,7 +136,8 @@ impl Miner { best_difficulty = difficulty; best_hash = hx; // {{ edit_1 }} - if best_difficulty.gt(&*global_best_difficulty.read().unwrap()) { + if best_difficulty.gt(&*global_best_difficulty.read().unwrap()) + { *global_best_difficulty.write().unwrap() = best_difficulty; } // {{ edit_1 }} @@ -142,7 +146,8 @@ impl Miner { // Exit if time has elapsed if nonce % 100 == 0 { - let global_best_difficulty = *global_best_difficulty.read().unwrap(); + let global_best_difficulty = + *global_best_difficulty.read().unwrap(); if timer.elapsed().as_secs().ge(&cutoff_time) { if i.id == 0 { progress_bar.set_message(format!( @@ -162,7 +167,7 @@ impl Miner { )); } } - + // Increment nonce nonce += 1; } diff --git a/src/send_and_confirm.rs b/src/send_and_confirm.rs index b5caa0ac..1d69aa3d 100644 --- a/src/send_and_confirm.rs +++ b/src/send_and_confirm.rs @@ -89,13 +89,21 @@ impl Miner { if attempts % 10 == 0 { // Reset the compute unit price if self.dynamic_fee { - let fee = if let Some(fee) = self.dynamic_fee().await { - progress_bar.println(format!(" Priority fee: {} microlamports", fee)); - fee - } else { - let fee = self.priority_fee.unwrap_or(0); - progress_bar.println(format!(" {} Dynamic fees not supported by this RPC. Falling back to static value: {} microlamports", "WARNING".bold().yellow(), fee)); - fee + let fee = match self.dynamic_fee().await { + Ok(fee) => { + progress_bar.println(format!(" Priority fee: {} microlamports", fee)); + fee + } + Err(err) => { + let fee = self.priority_fee.unwrap_or(0); + progress_bar.println(format!( + " {} {} Falling back to static value: {} microlamports", + "WARNING".bold().yellow(), + err, + fee + )); + fee + } }; final_ixs.remove(1); final_ixs.insert(1, ComputeBudgetInstruction::set_compute_unit_price(fee));