1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::{
io::{stdout, Write},
time::Duration,
};
use solana_client::{
client_error::{ClientError, ClientErrorKind, Result as ClientResult},
rpc_config::{RpcSendTransactionConfig, RpcSimulateTransactionConfig},
};
use solana_program::instruction::Instruction;
use solana_sdk::{
commitment_config::CommitmentLevel,
compute_budget::ComputeBudgetInstruction,
signature::{Signature, Signer},
transaction::Transaction,
};
use solana_transaction_status::{TransactionConfirmationStatus, UiTransactionEncoding};
use crate::Miner;
const RPC_RETRIES: usize = 0;
const SIMULATION_RETRIES: usize = 4;
const GATEWAY_RETRIES: usize = 150;
const CONFIRM_RETRIES: usize = 1;
const CONFIRM_DELAY: u64 = 0;
const GATEWAY_DELAY: u64 = 900;
impl Miner {
pub async fn send_and_confirm(
&self,
ixs: &[Instruction],
dynamic_cus: bool,
skip_confirm: bool,
) -> ClientResult<Signature> {
let mut stdout = stdout();
let signer = self.signer();
let client = self.rpc_client.clone();
// Return error if balance is zero
let balance = client.get_balance(&signer.pubkey()).await.unwrap();
if balance <= 0 {
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom("Insufficient SOL balance".into()),
});
}
// Build tx
let (_hash, slot) = client
.get_latest_blockhash_with_commitment(self.rpc_client.commitment())
.await
.unwrap();
let send_cfg = RpcSendTransactionConfig {
skip_preflight: true,
preflight_commitment: Some(CommitmentLevel::Confirmed),
encoding: Some(UiTransactionEncoding::Base64),
max_retries: Some(RPC_RETRIES),
min_context_slot: None,
};
let mut tx = Transaction::new_with_payer(ixs, Some(&signer.pubkey()));
// Simulate tx
let mut sim_attempts = 0;
'simulate: loop {
let sim_res = client
.simulate_transaction_with_config(
&tx,
RpcSimulateTransactionConfig {
sig_verify: false,
replace_recent_blockhash: true,
commitment: Some(self.rpc_client.commitment()),
encoding: Some(UiTransactionEncoding::Base64),
accounts: None,
min_context_slot: Some(slot),
inner_instructions: false,
},
)
.await;
match sim_res {
Ok(sim_res) => {
if let Some(err) = sim_res.value.err {
println!("Simulaton error: {:?}", err);
sim_attempts += 1;
} else if let Some(units_consumed) = sim_res.value.units_consumed {
if dynamic_cus {
println!("Dynamic CUs: {:?}", units_consumed);
let cu_budget_ix = ComputeBudgetInstruction::set_compute_unit_limit(
units_consumed as u32 + 1000,
);
let cu_price_ix =
ComputeBudgetInstruction::set_compute_unit_price(self.priority_fee);
let mut final_ixs = vec![];
final_ixs.extend_from_slice(&[cu_budget_ix, cu_price_ix]);
final_ixs.extend_from_slice(ixs);
tx = Transaction::new_with_payer(&final_ixs, Some(&signer.pubkey()));
}
break 'simulate;
}
}
Err(err) => {
println!("Simulaton error: {:?}", err);
sim_attempts += 1;
}
}
// Abort if sim fails
if sim_attempts.gt(&SIMULATION_RETRIES) {
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom("Simulation failed".into()),
});
}
}
// Update hash before sending transactions
let (hash, _slot) = client
.get_latest_blockhash_with_commitment(self.rpc_client.commitment())
.await
.unwrap();
// Submit tx
tx.sign(&[&signer], hash);
// let mut sigs = vec![];
let mut attempts = 0;
loop {
println!("Attempt: {:?}", attempts);
match client.send_transaction_with_config(&tx, send_cfg).await {
Ok(sig) => {
println!("{:?}", sig);
// sigs.push(sig);
// Confirm tx
if skip_confirm {
return Ok(sig);
}
for _ in 0..CONFIRM_RETRIES {
std::thread::sleep(Duration::from_millis(CONFIRM_DELAY));
match client.get_signature_statuses(&[sig]).await {
Ok(signature_statuses) => {
println!("Confirmation: {:?}", signature_statuses.value[0]);
for signature_status in signature_statuses.value {
if let Some(signature_status) = signature_status.as_ref() {
if signature_status.confirmation_status.is_some() {
let current_commitment = signature_status
.confirmation_status
.as_ref()
.unwrap();
match current_commitment {
TransactionConfirmationStatus::Processed => {}
TransactionConfirmationStatus::Confirmed
| TransactionConfirmationStatus::Finalized => {
println!("Transaction landed!");
std::thread::sleep(Duration::from_millis(
GATEWAY_DELAY,
));
// MI
// return Ok(sig);
if signature_status.status.is_ok() && signature_status.err.is_none() {
return Ok(sig);
} else {
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom("Transaction landed with error".into()),
});
}
}
}
} else {
println!("No status");
}
}
}
}
// Handle confirmation errors
Err(err) => {
println!("{:?}", err.kind().to_string());
}
}
}
println!("Transaction did not land");
}
// Handle submit errors
Err(err) => {
println!("{:?}", err.kind().to_string());
}
}
// Retry
stdout.flush().ok();
std::thread::sleep(Duration::from_millis(GATEWAY_DELAY));
attempts += 1;
if attempts > GATEWAY_RETRIES {
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom("Max retries".into()),
});
}
}
}
}