Skip to content

Commit a2d137c

Browse files
committed
resign tx after 5 failed attempts
1 parent 3f92bda commit a2d137c

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/send_and_confirm.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Miner {
5858
}
5959
}
6060

61-
// Set compute units
61+
// Set compute budget
6262
let mut final_ixs = vec![];
6363
match compute_budget {
6464
ComputeBudget::Dynamic => {
@@ -70,15 +70,12 @@ impl Miner {
7070
}
7171
}
7272

73-
let priority_fee = match &self.dynamic_fee_strategy {
74-
Some(_) => self.dynamic_fee().await,
75-
None => self.priority_fee.unwrap_or(0),
76-
};
77-
println!(" Priority fee: {} microlamports", priority_fee);
78-
73+
// Set compute unit price
7974
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(
80-
priority_fee,
75+
self.priority_fee.unwrap_or(0),
8176
));
77+
78+
// Add in user instructions
8279
final_ixs.extend_from_slice(ixs);
8380

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

94-
// Sign tx
95-
let (hash, _slot) = client
96-
.get_latest_blockhash_with_commitment(self.rpc_client.commitment())
97-
.await
98-
.unwrap();
99-
100-
if signer.pubkey() == fee_payer.pubkey() {
101-
tx.sign(&[&signer], hash);
102-
} else {
103-
tx.sign(&[&signer, &fee_payer], hash);
104-
}
105-
10691
// Submit tx
10792
let progress_bar = spinner::new_progress_bar();
10893
let mut attempts = 0;
10994
loop {
11095
progress_bar.set_message(format!("Submitting transaction... (attempt {})", attempts,));
11196

97+
// Sign tx with a new blockhash
98+
if attempts % 5 == 0 {
99+
// Reset the compute unit price
100+
if self.dynamic_fee_strategy.is_some() {
101+
let fee = self.dynamic_fee().await;
102+
final_ixs.remove(1);
103+
final_ixs.insert(1, ComputeBudgetInstruction::set_compute_unit_price(fee));
104+
progress_bar.println(format!(" Priority fee: {} microlamports", fee));
105+
}
106+
107+
// Resign the tx
108+
let (hash, _slot) = client
109+
.get_latest_blockhash_with_commitment(self.rpc_client.commitment())
110+
.await
111+
.unwrap();
112+
if signer.pubkey() == fee_payer.pubkey() {
113+
tx.sign(&[&signer], hash);
114+
} else {
115+
tx.sign(&[&signer, &fee_payer], hash);
116+
}
117+
}
118+
119+
// Send transaction
112120
match client.send_transaction_with_config(&tx, send_cfg).await {
113121
Ok(sig) => {
114122
// Skip confirmation
@@ -117,7 +125,7 @@ impl Miner {
117125
return Ok(sig);
118126
}
119127

120-
// Confirm the tx landed
128+
// Confirm transaction
121129
for _ in 0..CONFIRM_RETRIES {
122130
std::thread::sleep(Duration::from_millis(CONFIRM_DELAY));
123131
match client.get_signature_statuses(&[sig]).await {

0 commit comments

Comments
 (0)