Skip to content

Commit

Permalink
resign tx after 5 failed attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
HardhatChad committed Aug 8, 2024
1 parent 3f92bda commit a2d137c
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/send_and_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Miner {
}
}

// Set compute units
// Set compute budget
let mut final_ixs = vec![];
match compute_budget {
ComputeBudget::Dynamic => {
Expand All @@ -70,15 +70,12 @@ impl Miner {
}
}

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);

// Set compute unit price
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(
priority_fee,
self.priority_fee.unwrap_or(0),
));

// Add in user instructions
final_ixs.extend_from_slice(ixs);

// Build tx
Expand All @@ -91,24 +88,35 @@ impl Miner {
};
let mut tx = Transaction::new_with_payer(&final_ixs, Some(&fee_payer.pubkey()));

// Sign tx
let (hash, _slot) = client
.get_latest_blockhash_with_commitment(self.rpc_client.commitment())
.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 {
progress_bar.set_message(format!("Submitting transaction... (attempt {})", attempts,));

// Sign tx with a new blockhash
if attempts % 5 == 0 {
// Reset the compute unit price
if self.dynamic_fee_strategy.is_some() {
let fee = self.dynamic_fee().await;
final_ixs.remove(1);
final_ixs.insert(1, ComputeBudgetInstruction::set_compute_unit_price(fee));
progress_bar.println(format!(" Priority fee: {} microlamports", fee));
}

// Resign the tx
let (hash, _slot) = client
.get_latest_blockhash_with_commitment(self.rpc_client.commitment())
.await
.unwrap();
if signer.pubkey() == fee_payer.pubkey() {
tx.sign(&[&signer], hash);
} else {
tx.sign(&[&signer, &fee_payer], hash);
}
}

// Send transaction
match client.send_transaction_with_config(&tx, send_cfg).await {
Ok(sig) => {
// Skip confirmation
Expand All @@ -117,7 +125,7 @@ impl Miner {
return Ok(sig);
}

// Confirm the tx landed
// Confirm transaction
for _ in 0..CONFIRM_RETRIES {
std::thread::sleep(Duration::from_millis(CONFIRM_DELAY));
match client.get_signature_statuses(&[sig]).await {
Expand Down

0 comments on commit a2d137c

Please sign in to comment.