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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::{str::FromStr, time::Duration};
use colored::*;
use indicatif::ProgressBar;
use ore_api::error::OreError;
use rand::seq::SliceRandom;
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},
pubkey::Pubkey,
system_instruction::transfer,
};
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::utils::{get_latest_blockhash_with_retries, ComputeBudget};
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 = 8;
const CONFIRM_DELAY: u64 = 500;
const GATEWAY_DELAY: u64 = 0;
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();
let fee_payer = self.fee_payer();
let mut send_client = self.rpc_client.clone();
// Return error, if balance is zero
self.check_balance().await;
// Set compute budget
let mut final_ixs = vec![];
match compute_budget {
ComputeBudget::Dynamic => {
todo!("simulate tx")
}
ComputeBudget::Fixed(cus) => {
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(cus))
}
}
// Set compute unit price
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(
self.priority_fee.unwrap_or(0),
));
// Add in user instructions
final_ixs.extend_from_slice(ixs);
// Add jito tip
let jito_tip = *self.tip.read().unwrap();
if jito_tip > 0 {
send_client = self.jito_client.clone();
}
if jito_tip > 0 {
let tip_accounts = [
"96gYZGLnJYVFmbjzopPSU6QiEV5fGqZNyN9nmNhvrZU5",
"HFqU5x63VTqvQss8hp11i4wVV8bD44PvwucfZ2bU7gRe",
"Cw8CFyM9FkoMi7K7Crf6HNQqf4uEMzpKw6QNghXLvLkY",
"ADaUMid9yfUytqMBgopwjb2DTLSokTSzL1zt6iGPaS49",
"DfXygSm4jCyNCybVYYK6DwvWqjKee8pbDmJGcLWNDXjh",
"ADuUkR4vqLUMWXxW9gh6D6L8pMSawimctcNZ5pGwDcEt",
"DttWaMuVvTiduZRnguLF7jNxTgiMBZ1hyAumKUiL2KRL",
"3AVi9Tg9Uo68tJfuvoKvqKNWKkC5wPdSSdeBnizKZ6jT",
];
final_ixs.push(transfer(
&signer.pubkey(),
&Pubkey::from_str(
&tip_accounts
.choose(&mut rand::thread_rng())
.unwrap()
.to_string(),
)
.unwrap(),
jito_tip,
));
progress_bar.println(format!(" Jito tip: {} SOL", lamports_to_sol(jito_tip)));
}
// 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(&fee_payer.pubkey()));
// Submit tx
let mut attempts = 0;
loop {
progress_bar.set_message(format!("Submitting transaction... (attempt {})", attempts,));
// Sign tx with a new blockhash (after approximately ~45 sec)
if attempts % 10 == 0 {
// Reset the compute unit price
if self.dynamic_fee {
let fee = match self.get_dynamic_priority_fee().await {
Ok(fee) => {
progress_bar.println(format!(" Priority fee: {} microlamports", fee));
fee
}
Err(err) => {
let fee = self.priority_fee.unwrap_or(0);
log_warning(
&progress_bar,
&format!(
"{} Falling back to static value: {} microlamports",
err, fee
),
);
fee
}
};
final_ixs.remove(1);
final_ixs.insert(1, ComputeBudgetInstruction::set_compute_unit_price(fee));
tx = Transaction::new_with_payer(&final_ixs, Some(&fee_payer.pubkey()));
}
// Resign the tx
let (hash, _slot) = get_latest_blockhash_with_retries(&client).await?;
if signer.pubkey() == fee_payer.pubkey() {
tx.sign(&[&signer], hash);
} else {
tx.sign(&[&signer, &fee_payer], hash);
}
}
// Send transaction
attempts += 1;
match send_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 transaction
'confirm: for _ in 0..CONFIRM_RETRIES {
tokio::time::sleep(Duration::from_millis(CONFIRM_DELAY)).await;
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 {
match err {
// Instruction error
solana_sdk::transaction::TransactionError::InstructionError(_, err) => {
match err {
// Custom instruction error, parse into OreError
solana_program::instruction::InstructionError::Custom(err_code) => {
match err_code {
e if e == OreError::NeedsReset as u32 => {
attempts = 0;
log_error(&progress_bar, "Needs reset. Retrying...", false);
break 'confirm;
},
_ => {
log_error(&progress_bar, &err.to_string(), true);
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
}
}
},
// Non custom instruction error, return
_ => {
log_error(&progress_bar, &err.to_string(), true);
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
}
}
},
// Non instruction error, return
_ => {
log_error(&progress_bar, &err.to_string(), true);
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
}
}
} else 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) => {
log_error(&progress_bar, &err.kind().to_string(), false);
}
}
}
}
// Handle submit errors
Err(err) => {
log_error(&progress_bar, &err.kind().to_string(), false);
}
}
// Retry
tokio::time::sleep(Duration::from_millis(GATEWAY_DELAY)).await;
if attempts > GATEWAY_RETRIES {
log_error(&progress_bar, "Max retries", true);
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom("Max retries".into()),
});
}
}
}
pub async fn check_balance(&self) {
// Throw error if balance is less than min
if let Ok(balance) = self
.rpc_client
.get_balance(&self.fee_payer().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
);
}
}
}
// 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()),
// });
// }
// }
}
}
fn log_error(progress_bar: &ProgressBar, err: &str, finish: bool) {
if finish {
progress_bar.finish_with_message(format!("{} {}", "ERROR".bold().red(), err));
} else {
progress_bar.println(format!(" {} {}", "ERROR".bold().red(), err));
}
}
fn log_warning(progress_bar: &ProgressBar, msg: &str) {
progress_bar.println(format!(" {} {}", "WARNING".bold().yellow(), msg));
}