Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
HardhatChad committed Aug 8, 2024
1 parent 0ac1be8 commit 5c41cf1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 50 deletions.
22 changes: 13 additions & 9 deletions src/dynamic_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use serde_json::{json, Value};

impl Miner {
pub async fn dynamic_fee(&self) -> u64 {
let ore_addresses: Vec<String> =
std::iter::once("oreV2ZymfyeXgNgBdqMkumTqqAprVqgBWQfoYkrtKWQ".to_string())
.chain(BUS_ADDRESSES.iter().map(|pubkey| pubkey.to_string()))
.collect();
let ore_addresses: Vec<String> = std::iter::once(ore_api::ID.to_string())
.chain(BUS_ADDRESSES.iter().map(|pubkey| pubkey.to_string()))
.collect();

match &self.dynamic_fee_strategy {
None => self.priority_fee.unwrap_or(0),
Some(strategy) => {
let client = Client::new();

let body = match strategy.as_str() {
"helius" => {
json!({
Expand Down Expand Up @@ -46,8 +44,13 @@ impl Miner {
_ => return self.priority_fee.unwrap_or(0),
};

// Send request
let url = self
.dynamic_fee_url
.clone()
.unwrap_or(self.rpc_client.url());
let response: Value = client
.post(self.dynamic_fee_url.as_ref().unwrap())
.post(url)
.json(&body)
.send()
.await
Expand All @@ -56,6 +59,7 @@ impl Miner {
.await
.unwrap();

// Parse fee
let calculated_fee = match strategy.as_str() {
"helius" => response["result"]["priorityFeeEstimate"]
.as_f64()
Expand All @@ -75,13 +79,13 @@ impl Miner {
_ => return self.priority_fee.unwrap_or(0),
};

// Check if the calculated fee is higher than self.dynamic_fee_max
if let Some(max_fee) = self.dynamic_fee_max {
// Check if the calculated fee is higher than max
if let Some(max_fee) = self.priority_fee {
calculated_fee.min(max_fee)
} else {
calculated_fee
}
}
}
}
}
}
28 changes: 7 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct Miner {
pub priority_fee: Option<u64>,
pub dynamic_fee_url: Option<String>,
pub dynamic_fee_strategy: Option<String>,
pub dynamic_fee_max: Option<u64>,
pub rpc_client: Arc<RpcClient>,
pub fee_payer_filepath: Option<String>,
}
Expand Down Expand Up @@ -61,7 +60,7 @@ enum Commands {
#[command(about = "Start mining")]
Mine(MineArgs),

#[command(about = "Proof")]
#[command(about = "Fetch a proof account by address")]
Proof(ProofArgs),

#[command(about = "Fetch the current reward rate for each difficulty level")]
Expand Down Expand Up @@ -109,24 +108,24 @@ struct Args {
#[arg(
long,
value_name = "FEE_PAYER_FILEPATH",
help = "Filepath to keypair to use for fee payer",
help = "Filepath to keypair to use as transaction fee payer",
global = true
)]
fee_payer_filepath: Option<String>,
fee_payer: Option<String>,

#[arg(
long,
value_name = "MICROLAMPORTS",
help = "Number of microlamports to pay as priority fee per transaction",
default_value = "0",
help = "Price to pay for compute unit. If dynamic fee url is also set, this value will be the max.",
default_value = "500000",
global = true
)]
priority_fee: Option<u64>,

#[arg(
long,
value_name = "DYNAMIC_FEE_URL",
help = "RPC URL to use for dynamic fee estimation. If set will enable dynamic fee pricing instead of static priority fee pricing.",
help = "RPC URL to use for dynamic fee estimation.",
global = true
)]
dynamic_fee_url: Option<String>,
Expand All @@ -139,14 +138,6 @@ struct Args {
global = true
)]
dynamic_fee_strategy: Option<String>,
#[arg(
long,
value_name = "DYNAMIC_FEE_MAX",
help = "Maximum priority fee to use for dynamic fee estimation.",
default_value = "500000",
global = true
)]
dynamic_fee_max: Option<u64>,

#[command(subcommand)]
command: Commands,
Expand All @@ -171,9 +162,7 @@ async fn main() {
// Initialize miner.
let cluster = args.rpc.unwrap_or(cli_config.json_rpc_url);
let default_keypair = args.keypair.unwrap_or(cli_config.keypair_path.clone());
let fee_payer_filepath = args
.fee_payer_filepath
.unwrap_or(cli_config.keypair_path.clone());
let fee_payer_filepath = args.fee_payer.unwrap_or(cli_config.keypair_path.clone());
let rpc_client = RpcClient::new_with_commitment(cluster, CommitmentConfig::confirmed());

let miner = Arc::new(Miner::new(
Expand All @@ -182,7 +171,6 @@ async fn main() {
Some(default_keypair),
args.dynamic_fee_url,
args.dynamic_fee_strategy,
args.dynamic_fee_max,
Some(fee_payer_filepath),
));

Expand Down Expand Up @@ -235,7 +223,6 @@ impl Miner {
keypair_filepath: Option<String>,
dynamic_fee_url: Option<String>,
dynamic_fee_strategy: Option<String>,
dynamic_fee_max: Option<u64>,
fee_payer_filepath: Option<String>,
) -> Self {
Self {
Expand All @@ -244,7 +231,6 @@ impl Miner {
priority_fee,
dynamic_fee_url,
dynamic_fee_strategy,
dynamic_fee_max,
fee_payer_filepath,
}
}
Expand Down
32 changes: 12 additions & 20 deletions src/send_and_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const MIN_SOL_BALANCE: f64 = 0.005;
const RPC_RETRIES: usize = 0;
const _SIMULATION_RETRIES: usize = 4;
const GATEWAY_RETRIES: usize = 150;
const CONFIRM_RETRIES: usize = 1;
const CONFIRM_RETRIES: usize = 8;

const CONFIRM_DELAY: u64 = 0;
const GATEWAY_DELAY: u64 = 300;
const CONFIRM_DELAY: u64 = 500;
const GATEWAY_DELAY: u64 = 0; //300;

pub enum ComputeBudget {
Dynamic,
Expand All @@ -42,7 +42,6 @@ impl Miner {
compute_budget: ComputeBudget,
skip_confirm: bool,
) -> ClientResult<Signature> {
let progress_bar = spinner::new_progress_bar();
let signer = self.signer();
let client = self.rpc_client.clone();
let fee_payer = self.fee_payer();
Expand Down Expand Up @@ -71,16 +70,15 @@ impl Miner {
}
}

let priority_fee = match &self.dynamic_fee_url {
Some(_) => {
self.dynamic_fee().await
}
None => {
self.priority_fee.unwrap_or(0)
}
let priority_fee = match &self.dynamic_fee_strategy {
Some(_) => self.dynamic_fee().await,
None => self.priority_fee.unwrap_or(0),
};
println!(" Priority fee: {} microlamports", priority_fee);

final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(priority_fee));
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(
priority_fee,
));
final_ixs.extend_from_slice(ixs);

// Build tx
Expand All @@ -99,23 +97,17 @@ impl Miner {
.await
.unwrap();


if signer.pubkey() == fee_payer.pubkey() {
tx.sign(&[&signer], hash);
} else {
tx.sign(&[&signer, &fee_payer], hash);
}

// Submit tx
let progress_bar = spinner::new_progress_bar();
let mut attempts = 0;
loop {

let message = match &self.dynamic_fee_url {
Some(_) => format!("Submitting transaction... (attempt {} with dynamic priority fee of {} via {})", attempts, priority_fee, self.dynamic_fee_strategy.as_ref().unwrap()),
None => format!("Submitting transaction... (attempt {} with static priority fee of {})", attempts, priority_fee),
};

progress_bar.set_message(message);
progress_bar.set_message(format!("Submitting transaction... (attempt {})", attempts,));

match client.send_transaction_with_config(&tx, send_cfg).await {
Ok(sig) => {
Expand Down

0 comments on commit 5c41cf1

Please sign in to comment.