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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::time::Duration;
use colored::*;
use solana_client::{
client_error::{ClientError, ClientErrorKind, Result as ClientResult},
rpc_config::RpcSendTransactionConfig,
};
use solana_program::{
instruction::Instruction,
native_token::{lamports_to_sol, sol_to_lamports},
};
use solana_rpc_client::spinner;
use solana_sdk::{
commitment_config::CommitmentLevel,
compute_budget::ComputeBudgetInstruction,
signature::{Signature, Signer},
transaction::Transaction,
};
use solana_transaction_status::{TransactionConfirmationStatus, UiTransactionEncoding};
use crate::Miner;
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_DELAY: u64 = 0;
const GATEWAY_DELAY: u64 = 300;
pub enum ComputeBudget {
Dynamic,
Fixed(u32),
}
impl Miner {
pub async fn send_and_confirm(
&self,
ixs: &[Instruction],
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();
// Return error, if balance is zero
if let Ok(balance) = client.get_balance(&signer.pubkey()).await {
if balance <= sol_to_lamports(MIN_SOL_BALANCE) {
panic!(
"{} Insufficient balance: {} SOL\nPlease top up with at least {} SOL",
"ERROR".bold().red(),
lamports_to_sol(balance),
MIN_SOL_BALANCE
);
}
}
// Set compute units
let mut final_ixs = vec![];
match compute_budget {
ComputeBudget::Dynamic => {
// TODO simulate
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(1_400_000))
}
ComputeBudget::Fixed(cus) => {
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(cus))
}
}
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(
self.priority_fee,
));
final_ixs.extend_from_slice(ixs);
// Build tx
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(&final_ixs, Some(&signer.pubkey()));
// Sign tx
let (hash, _slot) = client
.get_latest_blockhash_with_commitment(self.rpc_client.commitment())
.await
.unwrap();
tx.sign(&[&signer], hash);
// Submit tx
let mut attempts = 0;
loop {
progress_bar.set_message(format!("Submitting transaction... (attempt {})", attempts));
match client.send_transaction_with_config(&tx, send_cfg).await {
Ok(sig) => {
// Skip confirmation
if skip_confirm {
progress_bar.finish_with_message(format!("Sent: {}", sig));
return Ok(sig);
}
// Confirm the tx landed
for _ in 0..CONFIRM_RETRIES {
std::thread::sleep(Duration::from_millis(CONFIRM_DELAY));
match client.get_signature_statuses(&[sig]).await {
Ok(signature_statuses) => {
for status in signature_statuses.value {
if let Some(status) = status {
if let Some(err) = status.err {
progress_bar.set_message(format!("Error: {}", err));
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
}
if let Some(confirmation) = status.confirmation_status {
match confirmation {
TransactionConfirmationStatus::Processed => {}
TransactionConfirmationStatus::Confirmed
| TransactionConfirmationStatus::Finalized => {
progress_bar.finish_with_message(format!(
"{} {}",
"OK".bold().green(),
sig
));
return Ok(sig);
}
}
}
}
}
}
// Handle confirmation errors
Err(err) => {
progress_bar.set_message(format!(
"{}: {}",
"ERROR".bold().red(),
err.kind().to_string()
));
}
}
}
}
// Handle submit errors
Err(err) => {
progress_bar.set_message(format!(
"{}: {}",
"ERROR".bold().red(),
err.kind().to_string()
));
}
}
// Retry
std::thread::sleep(Duration::from_millis(GATEWAY_DELAY));
attempts += 1;
if attempts > GATEWAY_RETRIES {
progress_bar.finish_with_message(format!("{}: Max retries", "ERROR".bold().red()));
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom("Max retries".into()),
});
}
}
}
// TODO
fn _simulate(&self) {
// 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()),
// });
// }
// }
}
}