From 928b80ce82932861ac58764c4ef8de67b159cf47 Mon Sep 17 00:00:00 2001 From: umi <9@umi.cat> Date: Tue, 16 Apr 2024 10:12:55 +0800 Subject: [PATCH 1/2] Add init_claim function to Miner struct --- src/claim.rs | 7 ++++++ src/init_claim.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 +++- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/init_claim.rs diff --git a/src/claim.rs b/src/claim.rs index a0a93e2..98fd798 100644 --- a/src/claim.rs +++ b/src/claim.rs @@ -47,7 +47,14 @@ impl Miner { let jito_tip = self.priority_fee.expect("jito tip is required"); let beneficiary_ata = utils::get_ore_ata(args.beneficiary); + info!(ata = %beneficiary_ata, recipient = %args.beneficiary); + if let Ok(Some(_ata)) = client.get_token_account(&beneficiary_ata).await { + info!("Token account already exists: {:?}, continue to claim", beneficiary_ata); + } else { + error!("Token account does not exist: {:?}", beneficiary_ata); + return; + } let owner_proof_pdas = accounts .iter() diff --git a/src/init_claim.rs b/src/init_claim.rs new file mode 100644 index 0000000..d4cca51 --- /dev/null +++ b/src/init_claim.rs @@ -0,0 +1,58 @@ +use clap::Parser; +use crate::Miner; +use solana_sdk::{ + signature::{Keypair, Signer}, + signer::EncodableKey, + transaction::Transaction, +}; + +use tracing::{info, error}; + + +#[derive(Parser, Debug, Clone)] +pub struct InitClaimArgs { + #[arg(long, help = "The keypair to initalize the $ORE token account with.")] + pub keypair: String +} + +impl Miner { + pub async fn init_claim(&self, args: &InitClaimArgs) { + let client = Miner::get_client_confirmed(&self.rpc); + // initalize the token account + let keypair = Keypair::read_from_file(&args.keypair).unwrap(); + + // build instructions. + let token_account_pubkey = spl_associated_token_account::get_associated_token_address( + &keypair.pubkey(), + &ore::MINT_ADDRESS, + ); + + // Check if ata already exists + if let Ok(Some(_ata)) = client.get_token_account(&token_account_pubkey).await { + info!("Token account already exists: {:?}", token_account_pubkey); + } + + // Sign and send transaction. + let instruction = spl_associated_token_account::instruction::create_associated_token_account( + &keypair.pubkey(), + &keypair.pubkey(), + &ore::MINT_ADDRESS, + &spl_token::id(), + ); + + let recent_blockhash = client + .get_latest_blockhash() + .await + .expect("Failed to get recent blockhash"); + + let transaction = Transaction::new_signed_with_payer(&[instruction], + Some(&keypair.pubkey()), + &[&keypair], recent_blockhash); + + println!("Creating token account {} for {}...", token_account_pubkey, keypair.pubkey()); + match client.send_and_confirm_transaction(&transaction).await { + Ok(_sig) => info!("Created token account {:?} for {}", token_account_pubkey, keypair.pubkey()), + Err(e) => error!("Transaction failed: {:?}", e), + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 785dc18..bbf3dd4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,7 @@ mod generate_wallet; mod jito; mod register; mod utils; +mod init_claim; #[tokio::main(flavor = "multi_thread")] async fn main() { @@ -60,6 +61,7 @@ async fn main() { Command::JitoTipStream => miner.jito_tip_stream().await, Command::GenerateWallet(args) => miner.generate_wallet(args), Command::Collect(args) => miner.collect(args).await, + Command::InitClaim(args) => miner.init_claim(args).await, } } @@ -86,6 +88,7 @@ pub enum Command { GenerateWallet(crate::generate_wallet::GenerateWalletArgs), BatchTransfer(crate::batch_transfer::BatchTransferArgs), Collect(crate::collect::CollectArgs), + InitClaim(crate::init_claim::InitClaimArgs), } impl Miner { @@ -158,7 +161,7 @@ impl Miner { hash_and_pubkey: &[(Hash, Pubkey)], ) -> (Duration, Vec<(Hash, u64)>) { let mining_start = Instant::now(); - + println!("difficulty: {difficulty}", difficulty = difficulty); let mut child = tokio::process::Command::new(worker) .stdin(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped()) From 685398a991799f17020989689c83bdcd6b92d63e Mon Sep 17 00:00:00 2001 From: umi <9@umi.cat> Date: Tue, 16 Apr 2024 10:14:34 +0800 Subject: [PATCH 2/2] remove debug line --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index bbf3dd4..9351502 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,7 +161,7 @@ impl Miner { hash_and_pubkey: &[(Hash, Pubkey)], ) -> (Duration, Vec<(Hash, u64)>) { let mining_start = Instant::now(); - println!("difficulty: {difficulty}", difficulty = difficulty); + let mut child = tokio::process::Command::new(worker) .stdin(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped())