|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use colored::*; |
| 4 | +use ore_api::consts::MINT_ADDRESS; |
| 5 | +use solana_program::pubkey::Pubkey; |
| 6 | +use solana_sdk::signature::Signer; |
| 7 | +use spl_token::amount_to_ui_amount; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + args::TransferArgs, |
| 11 | + cu_limits::CU_LIMIT_CLAIM, |
| 12 | + send_and_confirm::ComputeBudget, |
| 13 | + utils::{amount_f64_to_u64, ask_confirm}, |
| 14 | + Miner, |
| 15 | +}; |
| 16 | + |
| 17 | +impl Miner { |
| 18 | + pub async fn transfer(&self, args: TransferArgs) { |
| 19 | + let signer = self.signer(); |
| 20 | + let pubkey = signer.pubkey(); |
| 21 | + let sender_tokens = |
| 22 | + spl_associated_token_account::get_associated_token_address(&pubkey, &MINT_ADDRESS); |
| 23 | + let mut ixs = vec![]; |
| 24 | + |
| 25 | + // Initialize recipient, if needed |
| 26 | + let to = Pubkey::from_str(&args.to).expect("Failed to parse recipient wallet address"); |
| 27 | + let recipient_tokens = |
| 28 | + spl_associated_token_account::get_associated_token_address(&to, &MINT_ADDRESS); |
| 29 | + if self |
| 30 | + .rpc_client |
| 31 | + .get_token_account(&recipient_tokens) |
| 32 | + .await |
| 33 | + .is_err() |
| 34 | + { |
| 35 | + ixs.push( |
| 36 | + spl_associated_token_account::instruction::create_associated_token_account( |
| 37 | + &signer.pubkey(), |
| 38 | + &to, |
| 39 | + &ore_api::consts::MINT_ADDRESS, |
| 40 | + &spl_token::id(), |
| 41 | + ), |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + // Parse amount to claim |
| 46 | + let amount = amount_f64_to_u64(args.amount); |
| 47 | + |
| 48 | + // Confirm user wants to claim |
| 49 | + if !ask_confirm( |
| 50 | + format!( |
| 51 | + "\nYou are about to transfer {}.\n\nAre you sure you want to continue? [Y/n]", |
| 52 | + format!( |
| 53 | + "{} ORE", |
| 54 | + amount_to_ui_amount(amount, ore_api::consts::TOKEN_DECIMALS) |
| 55 | + ) |
| 56 | + .bold(), |
| 57 | + ) |
| 58 | + .as_str(), |
| 59 | + ) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Send and confirm |
| 64 | + ixs.push( |
| 65 | + spl_token::instruction::transfer( |
| 66 | + &spl_token::id(), |
| 67 | + &sender_tokens, |
| 68 | + &recipient_tokens, |
| 69 | + &pubkey, |
| 70 | + &[&pubkey], |
| 71 | + amount, |
| 72 | + ) |
| 73 | + .unwrap(), |
| 74 | + ); |
| 75 | + self.send_and_confirm(&ixs, ComputeBudget::Fixed(CU_LIMIT_CLAIM), false) |
| 76 | + .await |
| 77 | + .ok(); |
| 78 | + } |
| 79 | +} |
0 commit comments