Skip to content

Commit

Permalink
implement transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
HardhatChad committed Aug 9, 2024
1 parent 12c9f5d commit 89f59e7
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ pub struct StakeArgs {
pub sender: Option<String>,
}

#[derive(Parser, Debug)]
pub struct TransferArgs {
#[arg(
index = 0,
value_name = "AMOUNT",
help = "The amount of ORE to transfer.",
required = true
)]
pub amount: f64,

#[arg(
index = 1,
value_name = "WALLET_ADDRESS",
help = "The wallet address of the receipient."
)]
pub to: String,
}

#[derive(Parser, Debug)]
pub struct UpgradeArgs {
#[arg(
Expand Down
8 changes: 4 additions & 4 deletions src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Miner {
let proof = get_proof_with_authority(&self.rpc_client, pubkey).await;
let mut ixs = vec![];
let beneficiary = match args.to {
None => self.initialize_ata(pubkey).await,
Some(to) => {
// Create beneficiary token account, if needed
let wallet = Pubkey::from_str(&to).expect("Failed to parse wallet address");
Expand All @@ -36,7 +37,7 @@ impl Miner {
{
ixs.push(
spl_associated_token_account::instruction::create_associated_token_account(
&signer.pubkey(),
&pubkey,
&wallet,
&ore_api::consts::MINT_ADDRESS,
&spl_token::id(),
Expand All @@ -45,7 +46,6 @@ impl Miner {
}
benefiary_tokens
}
None => self.initialize_ata().await,
};

// Parse amount to claim
Expand Down Expand Up @@ -77,14 +77,14 @@ impl Miner {
.ok();
}

async fn initialize_ata(&self) -> Pubkey {
async fn initialize_ata(&self, wallet: Pubkey) -> Pubkey {
// Initialize client.
let signer = self.signer();
let client = self.rpc_client.clone();

// Build instructions.
let token_account_pubkey = spl_associated_token_account::get_associated_token_address(
&signer.pubkey(),
&wallet,
&ore_api::consts::MINT_ADDRESS,
);

Expand Down
4 changes: 2 additions & 2 deletions src/dynamic_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ impl Miner {
.map(|prioritization_fees| {
estimate_prioritization_fee_micro_lamports(prioritization_fees)
})
.ok_or_else(|error: serde_json::Error| {
.or_else(|error: serde_json::Error| {
Err(format!(
"Failed to parse priority fee. Response: {response:?}, error: {error}"
"Failed to parse priority fee response: {response:?}, error: {error}"
))
})
}
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod proof;
mod rewards;
mod send_and_confirm;
mod stake;
mod transfer;
mod upgrade;
mod utils;

Expand Down Expand Up @@ -69,6 +70,9 @@ enum Commands {
#[command(about = "Stake to earn a rewards multiplier")]
Stake(StakeArgs),

#[command(about = "Send ORE to anyone, anywhere in the world.")]
Transfer(TransferArgs),

#[command(about = "Upgrade your ORE tokens from v1 to v2")]
Upgrade(UpgradeArgs),

Expand Down Expand Up @@ -200,6 +204,9 @@ async fn main() {
Commands::Stake(args) => {
miner.stake(args).await;
}
Commands::Transfer(args) => {
miner.transfer(args).await;
}
Commands::Upgrade(args) => {
miner.upgrade(args).await;
}
Expand Down
79 changes: 79 additions & 0 deletions src/transfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::str::FromStr;

use colored::*;
use ore_api::consts::MINT_ADDRESS;
use solana_program::pubkey::Pubkey;
use solana_sdk::signature::Signer;
use spl_token::amount_to_ui_amount;

use crate::{
args::TransferArgs,
cu_limits::CU_LIMIT_CLAIM,
send_and_confirm::ComputeBudget,
utils::{amount_f64_to_u64, ask_confirm},
Miner,
};

impl Miner {
pub async fn transfer(&self, args: TransferArgs) {
let signer = self.signer();
let pubkey = signer.pubkey();
let sender_tokens =
spl_associated_token_account::get_associated_token_address(&pubkey, &MINT_ADDRESS);
let mut ixs = vec![];

// Initialize recipient, if needed
let to = Pubkey::from_str(&args.to).expect("Failed to parse recipient wallet address");
let recipient_tokens =
spl_associated_token_account::get_associated_token_address(&to, &MINT_ADDRESS);
if self
.rpc_client
.get_token_account(&recipient_tokens)
.await
.is_err()
{
ixs.push(
spl_associated_token_account::instruction::create_associated_token_account(
&signer.pubkey(),
&to,
&ore_api::consts::MINT_ADDRESS,
&spl_token::id(),
),
);
}

// Parse amount to claim
let amount = amount_f64_to_u64(args.amount);

// Confirm user wants to claim
if !ask_confirm(
format!(
"\nYou are about to transfer {}.\n\nAre you sure you want to continue? [Y/n]",
format!(
"{} ORE",
amount_to_ui_amount(amount, ore_api::consts::TOKEN_DECIMALS)
)
.bold(),
)
.as_str(),
) {
return;
}

// Send and confirm
ixs.push(
spl_token::instruction::transfer(
&spl_token::id(),
&sender_tokens,
&recipient_tokens,
&pubkey,
&[&pubkey],
amount,
)
.unwrap(),
);
self.send_and_confirm(&ixs, ComputeBudget::Fixed(CU_LIMIT_CLAIM), false)
.await
.ok();
}
}

0 comments on commit 89f59e7

Please sign in to comment.