Skip to content

Commit

Permalink
handle 0x0 more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
HardhatChad committed Aug 11, 2024
1 parent 8db4b70 commit 100d83d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Miner {
&ore_api::consts::MINT_ADDRESS,
&spl_token::id(),
);
self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false)
self.send_and_confirm(&[ix], ComputeBudget::Fixed(400_000), false)
.await
.ok();

Expand Down
2 changes: 1 addition & 1 deletion src/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Miner {

// Submit close transaction
let ix = ore_api::instruction::close(signer.pubkey());
self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false)
self.send_and_confirm(&[ix], ComputeBudget::Fixed(500_000), false)
.await
.ok();
}
Expand Down
2 changes: 1 addition & 1 deletion src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Miner {
// Sign and send transaction.
println!("Generating challenge...");
let ix = ore_api::instruction::open(signer.pubkey(), signer.pubkey(), fee_payer.pubkey());
self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false)
self.send_and_confirm(&[ix], ComputeBudget::Fixed(400_000), false)
.await
.ok();
}
Expand Down
105 changes: 78 additions & 27 deletions src/send_and_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{str::FromStr, time::Duration};

use chrono::Local;
use colored::*;
use ore_api::error::OreError;
use rand::seq::SliceRandom;
use solana_client::{
client_error::{ClientError, ClientErrorKind, Result as ClientResult},
Expand Down Expand Up @@ -33,9 +34,10 @@ const GATEWAY_RETRIES: usize = 150;
const CONFIRM_RETRIES: usize = 8;

const CONFIRM_DELAY: u64 = 500;
const GATEWAY_DELAY: u64 = 0; //300;
const GATEWAY_DELAY: u64 = 0;

pub enum ComputeBudget {
#[allow(dead_code)]
Dynamic,
Fixed(u32),
}
Expand All @@ -52,21 +54,14 @@ impl Miner {
let fee_payer = self.fee_payer();
let mut send_client = self.rpc_client.clone();

let current_tip = *self.tip.read().unwrap();

if current_tip > 0 {
send_client = self.jito_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
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(1_400_000))
todo!("simulate tx")
}
ComputeBudget::Fixed(cus) => {
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(cus))
Expand All @@ -81,8 +76,13 @@ impl Miner {
// Add in user instructions
final_ixs.extend_from_slice(ixs);

if current_tip > 0 {
let tips = [
// 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",
Expand All @@ -92,12 +92,16 @@ impl Miner {
"DttWaMuVvTiduZRnguLF7jNxTgiMBZ1hyAumKUiL2KRL",
"3AVi9Tg9Uo68tJfuvoKvqKNWKkC5wPdSSdeBnizKZ6jT",
];

final_ixs.push(transfer(
&signer.pubkey(),
&Pubkey::from_str(&tips.choose(&mut rand::thread_rng()).unwrap().to_string())
.unwrap(),
current_tip,
&Pubkey::from_str(
&tip_accounts
.choose(&mut rand::thread_rng())
.unwrap()
.to_string(),
)
.unwrap(),
jito_tip,
));
}

Expand Down Expand Up @@ -172,15 +176,62 @@ impl Miner {
for status in signature_statuses.value {
if let Some(status) = status {
if let Some(err) = status.err {
progress_bar.finish_with_message(format!(
"{}: {}",
"ERROR".bold().red(),
err
));
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
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;
progress_bar.println(format!(
" {} Needs reset. Retrying...",
"ERROR".bold().red(),
));
},
_ => {
progress_bar.finish_with_message(format!(
"{} {}",
"ERROR".bold().red(),
err
));
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
}
}
},

// Non custom instruction error, return
_ => {
progress_bar.finish_with_message(format!(
"{} {}",
"ERROR".bold().red(),
err
));
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
}
}
},

// Non instruction error, return
_ => {
progress_bar.finish_with_message(format!(
"{} {}",
"ERROR".bold().red(),
err
));
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom(err.to_string()),
});
}
}
}
if let Some(confirmation) = status.confirmation_status {
match confirmation {
Expand Down Expand Up @@ -210,7 +261,7 @@ impl Miner {
// Handle confirmation errors
Err(err) => {
progress_bar.set_message(format!(
"{}: {}",
"{} {}",
"ERROR".bold().red(),
err.kind().to_string()
));
Expand All @@ -222,7 +273,7 @@ impl Miner {
// Handle submit errors
Err(err) => {
progress_bar.set_message(format!(
"{}: {}",
"{} {}",
"ERROR".bold().red(),
err.kind().to_string()
));
Expand All @@ -233,7 +284,7 @@ impl Miner {
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()));
progress_bar.finish_with_message(format!("{} Max retries", "ERROR".bold().red()));
return Err(ClientError {
request: None,
kind: ClientErrorKind::Custom("Max retries".into()),
Expand Down
2 changes: 1 addition & 1 deletion src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Miner {
&ore_api::consts::MINT_ADDRESS,
&spl_token::id(),
);
self.send_and_confirm(&[ix], ComputeBudget::Dynamic, false)
self.send_and_confirm(&[ix], ComputeBudget::Fixed(500_000), false)
.await
.ok();
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{io::Read, time::Duration};

use cached::proc_macro::cached;
use colored::*;
use ore_api::{
consts::{
CONFIG_ADDRESS, MINT_ADDRESS, PROOF, TOKEN_DECIMALS, TOKEN_DECIMALS_V1, TREASURY_ADDRESS,
Expand All @@ -13,7 +12,6 @@ use serde::Deserialize;
use solana_client::client_error::{ClientError, ClientErrorKind};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_program::{pubkey::Pubkey, sysvar};
use solana_rpc_client::spinner;
use solana_sdk::{clock::Clock, hash::Hash};
use spl_associated_token_account::get_associated_token_address;
use tokio::time::sleep;
Expand Down

0 comments on commit 100d83d

Please sign in to comment.