Skip to content

Commit 89f59e7

Browse files
committed
implement transfer
1 parent 12c9f5d commit 89f59e7

File tree

5 files changed

+110
-6
lines changed

5 files changed

+110
-6
lines changed

src/args.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ pub struct StakeArgs {
103103
pub sender: Option<String>,
104104
}
105105

106+
#[derive(Parser, Debug)]
107+
pub struct TransferArgs {
108+
#[arg(
109+
index = 0,
110+
value_name = "AMOUNT",
111+
help = "The amount of ORE to transfer.",
112+
required = true
113+
)]
114+
pub amount: f64,
115+
116+
#[arg(
117+
index = 1,
118+
value_name = "WALLET_ADDRESS",
119+
help = "The wallet address of the receipient."
120+
)]
121+
pub to: String,
122+
}
123+
106124
#[derive(Parser, Debug)]
107125
pub struct UpgradeArgs {
108126
#[arg(

src/claim.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ impl Miner {
2121
let proof = get_proof_with_authority(&self.rpc_client, pubkey).await;
2222
let mut ixs = vec![];
2323
let beneficiary = match args.to {
24+
None => self.initialize_ata(pubkey).await,
2425
Some(to) => {
2526
// Create beneficiary token account, if needed
2627
let wallet = Pubkey::from_str(&to).expect("Failed to parse wallet address");
@@ -36,7 +37,7 @@ impl Miner {
3637
{
3738
ixs.push(
3839
spl_associated_token_account::instruction::create_associated_token_account(
39-
&signer.pubkey(),
40+
&pubkey,
4041
&wallet,
4142
&ore_api::consts::MINT_ADDRESS,
4243
&spl_token::id(),
@@ -45,7 +46,6 @@ impl Miner {
4546
}
4647
benefiary_tokens
4748
}
48-
None => self.initialize_ata().await,
4949
};
5050

5151
// Parse amount to claim
@@ -77,14 +77,14 @@ impl Miner {
7777
.ok();
7878
}
7979

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

8585
// Build instructions.
8686
let token_account_pubkey = spl_associated_token_account::get_associated_token_address(
87-
&signer.pubkey(),
87+
&wallet,
8888
&ore_api::consts::MINT_ADDRESS,
8989
);
9090

src/dynamic_fee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ impl Miner {
135135
.map(|prioritization_fees| {
136136
estimate_prioritization_fee_micro_lamports(prioritization_fees)
137137
})
138-
.ok_or_else(|error: serde_json::Error| {
138+
.or_else(|error: serde_json::Error| {
139139
Err(format!(
140-
"Failed to parse priority fee. Response: {response:?}, error: {error}"
140+
"Failed to parse priority fee response: {response:?}, error: {error}"
141141
))
142142
})
143143
}

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod proof;
1515
mod rewards;
1616
mod send_and_confirm;
1717
mod stake;
18+
mod transfer;
1819
mod upgrade;
1920
mod utils;
2021

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

73+
#[command(about = "Send ORE to anyone, anywhere in the world.")]
74+
Transfer(TransferArgs),
75+
7276
#[command(about = "Upgrade your ORE tokens from v1 to v2")]
7377
Upgrade(UpgradeArgs),
7478

@@ -200,6 +204,9 @@ async fn main() {
200204
Commands::Stake(args) => {
201205
miner.stake(args).await;
202206
}
207+
Commands::Transfer(args) => {
208+
miner.transfer(args).await;
209+
}
203210
Commands::Upgrade(args) => {
204211
miner.upgrade(args).await;
205212
}

src/transfer.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)