Skip to content

Commit

Permalink
cleanup fees and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
HardhatChad committed Aug 9, 2024
1 parent 723ca52 commit 5e6ef7e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
60 changes: 29 additions & 31 deletions src/dynamic_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum FeeStrategy {
}

impl Miner {
pub async fn dynamic_fee(&self) -> Option<u64> {
pub async fn dynamic_fee(&self) -> Result<u64, String> {
// Get url
let rpc_url = self
.dynamic_fee_url
Expand All @@ -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
Expand Down Expand Up @@ -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::<Vec<u64>>(),
)
})
.and_then(|fees| {
Some(((fees.iter().sum::<u64>() 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::<Vec<u64>>(),
)
})
.and_then(|fees| {
Some(
((fees.iter().sum::<u64>() 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)
}
}
}
}
}
17 changes: 11 additions & 6 deletions src/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -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 }}
Expand All @@ -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!(
Expand All @@ -162,7 +167,7 @@ impl Miner {
));
}
}

// Increment nonce
nonce += 1;
}
Expand Down
22 changes: 15 additions & 7 deletions src/send_and_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 5e6ef7e

Please sign in to comment.