Skip to content

Commit

Permalink
Merge pull request #124 from JustPandaEver/master
Browse files Browse the repository at this point in the history
Fix many bugs
  • Loading branch information
HardhatChad authored Aug 9, 2024
2 parents 3820cb3 + 2eb7aa8 commit 723ca52
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
51 changes: 51 additions & 0 deletions src/dynamic_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use url::Url;
enum FeeStrategy {
Helius,
Triton,
Alchemy,
Quiknode,
}

impl Miner {
Expand All @@ -26,6 +28,10 @@ impl Miner {
.to_string();
let strategy = if host.contains("helius-rpc.com") {
FeeStrategy::Helius
} else if host.contains("alchemy.com") {
FeeStrategy::Alchemy
} else if host.contains("quiknode.pro") {
FeeStrategy::Quiknode
} else if host.contains("rpcpool.com") {
FeeStrategy::Triton
} else {
Expand All @@ -51,6 +57,27 @@ impl Miner {
}]
})
}
FeeStrategy::Alchemy => {
json!({
"jsonrpc": "2.0",
"id": "priority-fee-estimate",
"method": "getRecentPrioritizationFees",
"params": [
ore_addresses
]
})
}
FeeStrategy::Quiknode => {
json!({
"jsonrpc": "2.0",
"id": "1",
"method": "qn_estimatePriorityFees",
"params": {
"account": "oreV2ZymfyeXgNgBdqMkumTqqAprVqgBWQfoYkrtKWQ",
"last_n_blocks": 100
}
})
}
FeeStrategy::Triton => {
json!({
"jsonrpc": "2.0",
Expand Down Expand Up @@ -84,6 +111,30 @@ impl Miner {
.map(|fee| fee as u64)
.ok_or_else(|| format!("Failed to parse priority fee. Response: {:?}", response))
.unwrap(),
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(),
FeeStrategy::Alchemy => response["result"]
.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(),
FeeStrategy::Triton => response["result"]
.as_array()
.and_then(|arr| arr.last())
Expand Down
34 changes: 28 additions & 6 deletions src/mine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::Arc, time::Instant};
use std::{sync::Arc, sync::RwLock, time::Instant};

use colored::*;
use drillx::{
Expand Down Expand Up @@ -35,18 +35,25 @@ impl Miner {

// Start mining loop
let mut last_hash_at = 0;
let mut last_balance = 0;
loop {
// Fetch proof
let config = get_config(&self.rpc_client).await;
let proof =
get_updated_proof_with_authority(&self.rpc_client, signer.pubkey(), last_hash_at)
.await;
last_hash_at = proof.last_hash_at;
println!(
"\nStake: {} ORE\n Multiplier: {:12}x",
"\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)))
} else {
""
},
calculate_multiplier(proof.balance, config.top_balance)
);
last_hash_at = proof.last_hash_at;
last_balance = proof.balance;

// Calculate cutoff time
let cutoff_time = self.get_cutoff(proof, args.buffer_time).await;
Expand Down Expand Up @@ -87,11 +94,13 @@ impl Miner {
) -> Solution {
// Dispatch job to each thread
let progress_bar = Arc::new(spinner::new_progress_bar());
let global_best_difficulty = Arc::new(RwLock::new(0u32));
progress_bar.set_message("Mining...");
let core_ids = core_affinity::get_core_ids().unwrap();
let handles: Vec<_> = core_ids
.into_iter()
.map(|i| {
let global_best_difficulty = Arc::clone(&global_best_difficulty);
std::thread::spawn({
let proof = proof.clone();
let progress_bar = progress_bar.clone();
Expand Down Expand Up @@ -123,24 +132,37 @@ impl Miner {
best_nonce = nonce;
best_difficulty = difficulty;
best_hash = hx;
// {{ edit_1 }}
if best_difficulty.gt(&*global_best_difficulty.read().unwrap()) {
*global_best_difficulty.write().unwrap() = best_difficulty;
}
// {{ edit_1 }}
}
}

// Exit if time has elapsed
if nonce % 100 == 0 {
let global_best_difficulty = *global_best_difficulty.read().unwrap();
if timer.elapsed().as_secs().ge(&cutoff_time) {
if best_difficulty.ge(&min_difficulty) {
if i.id == 0 {
progress_bar.set_message(format!(
"Mining... ({} difficulty)",
global_best_difficulty,
));
}
if global_best_difficulty.ge(&min_difficulty) {
// Mine until min difficulty has been met
break;
}
} else if i.id == 0 {
progress_bar.set_message(format!(
"Mining... ({} sec remaining)",
"Mining... ({} difficulty, {} sec remaining)",
global_best_difficulty,
cutoff_time.saturating_sub(timer.elapsed().as_secs()),
));
}
}

// Increment nonce
nonce += 1;
}
Expand Down

0 comments on commit 723ca52

Please sign in to comment.