Skip to content

Commit

Permalink
change claim to use wallet address
Browse files Browse the repository at this point in the history
  • Loading branch information
HardhatChad committed Jul 12, 2024
1 parent 835bb7f commit 98645b3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 28 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ colored = "2.0"
drillx = "2.0.0-beta"
futures = "0.3.30"
num_cpus = "1.16.0"
ore-api = "2.0.0-beta"
ore-utils = "2.0.0-beta"
ore-api = "2.0.0-beta.1"
ore-utils = "2.0.0-beta.1"
rand = "0.8.4"
solana-cli-config = "^1.18"
solana-client = "^1.18"
Expand Down
6 changes: 3 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ pub struct ClaimArgs {

#[arg(
long,
value_name = "TOKEN_ACCOUNT_ADDRESS",
help = "Token account to receive mining rewards."
value_name = "WALLET_ADDRESS",
help = "Wallet to receive claimed tokens."
)]
pub beneficiary: Option<String>,
pub to: Option<String>,
}

#[derive(Parser, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use solana_sdk::signature::Signer;

use crate::{
args::BalanceArgs,
utils::{amount_u64_to_string, get_proof},
utils::{amount_u64_to_string, get_proof_with_authority},
Miner,
};

Expand All @@ -22,7 +22,7 @@ impl Miner {
} else {
signer.pubkey()
};
let proof = get_proof(&self.rpc_client, address).await;
let proof = get_proof_with_authority(&self.rpc_client, address).await;
let token_account_address = spl_associated_token_account::get_associated_token_address(
&address,
&ore_api::consts::MINT_ADDRESS,
Expand Down
41 changes: 34 additions & 7 deletions src/claim.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand All @@ -9,27 +10,52 @@ use crate::{
args::ClaimArgs,
cu_limits::CU_LIMIT_CLAIM,
send_and_confirm::ComputeBudget,
utils::{amount_f64_to_u64, ask_confirm, get_proof},
utils::{amount_f64_to_u64, ask_confirm, get_proof_with_authority},
Miner,
};

impl Miner {
pub async fn claim(&self, args: ClaimArgs) {
let signer = self.signer();
let pubkey = signer.pubkey();
let proof = get_proof(&self.rpc_client, pubkey).await;
let beneficiary = match args.beneficiary {
Some(beneficiary) => {
Pubkey::from_str(&beneficiary).expect("Failed to parse beneficiary address")
let proof = get_proof_with_authority(&self.rpc_client, pubkey).await;
let mut ixs = vec![];
let beneficiary = match args.to {
Some(to) => {
// Create beneficiary token account, if needed
let wallet = Pubkey::from_str(&to).expect("Failed to parse wallet address");
let benefiary_tokens = spl_associated_token_account::get_associated_token_address(
&wallet,
&MINT_ADDRESS,
);
if self
.rpc_client
.get_token_account(&benefiary_tokens)
.await
.is_err()
{
ixs.push(
spl_associated_token_account::instruction::create_associated_token_account(
&signer.pubkey(),
&wallet,
&ore_api::consts::MINT_ADDRESS,
&spl_token::id(),
),
);
}
benefiary_tokens
}
None => self.initialize_ata().await,
};

// Parse amount to claim
let amount = if let Some(amount) = args.amount {
amount_f64_to_u64(amount)
} else {
proof.balance
};

// Confirm user wants to claim
if !ask_confirm(
format!(
"\nYou are about to claim {}.\n\nAre you sure you want to continue? [Y/n]",
Expand All @@ -44,8 +70,9 @@ impl Miner {
return;
}

let ix = ore_api::instruction::claim(pubkey, beneficiary, amount);
self.send_and_confirm(&[ix], ComputeBudget::Fixed(CU_LIMIT_CLAIM), false)
// Send and confirm
ixs.push(ore_api::instruction::claim(pubkey, beneficiary, amount));
self.send_and_confirm(&ixs, ComputeBudget::Fixed(CU_LIMIT_CLAIM), 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 @@ -30,7 +30,7 @@ impl Miner {
if proof.balance.gt(&0) {
self.claim(ClaimArgs {
amount: None,
beneficiary: None,
to: None,
})
.await;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cu_limits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub const CU_LIMIT_CLAIM: u32 = 20_000;
pub const CU_LIMIT_CLAIM: u32 = 32_000;
pub const _CU_LIMIT_RESET: u32 = 12_200;
pub const _CU_LIMIT_MINE: u32 = 3200;
19 changes: 13 additions & 6 deletions src/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use drillx::{
};
use ore_api::{
consts::{BUS_ADDRESSES, BUS_COUNT, EPOCH_DURATION},
state::Proof,
state::{Config, Proof},
};
use rand::Rng;
use solana_program::pubkey::Pubkey;
Expand All @@ -17,7 +17,7 @@ use solana_sdk::signer::Signer;
use crate::{
args::MineArgs,
send_and_confirm::ComputeBudget,
utils::{amount_u64_to_string, get_clock, get_config, get_proof},
utils::{amount_u64_to_string, get_clock, get_config, get_proof_with_authority},
Miner,
};

Expand All @@ -33,7 +33,7 @@ impl Miner {
// Start mining loop
loop {
// Fetch proof
let proof = get_proof(&self.rpc_client, signer.pubkey()).await;
let proof = get_proof_with_authority(&self.rpc_client, signer.pubkey()).await;
println!(
"\nStake balance: {} ORE",
amount_u64_to_string(proof.balance)
Expand All @@ -46,10 +46,14 @@ impl Miner {
let solution = Self::find_hash_par(proof, cutoff_time, args.threads).await;

// Submit most difficult hash
let config = get_config(&self.rpc_client).await;
let mut ixs = vec![];
if self.needs_reset().await {
if self.should_reset(config).await {
ixs.push(ore_api::instruction::reset(signer.pubkey()));
}
if self.should_crown(config, proof).await {
// TODO ixs.push(ore_api::instruction::crown())
}
ixs.push(ore_api::instruction::mine(
signer.pubkey(),
find_bus(),
Expand Down Expand Up @@ -155,9 +159,12 @@ impl Miner {
}
}

async fn needs_reset(&self) -> bool {
async fn should_crown(&self, config: Config, proof: Proof) -> bool {
proof.balance.gt(&config.max_stake)
}

async fn should_reset(&self, config: Config) -> bool {
let clock = get_clock(&self.rpc_client).await;
let config = get_config(&self.rpc_client).await;
config
.last_reset_at
.saturating_add(EPOCH_DURATION)
Expand Down
8 changes: 6 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ pub async fn get_config(client: &RpcClient) -> Config {
*Config::try_from_bytes(&data).expect("Failed to parse config account")
}

pub async fn get_proof(client: &RpcClient, authority: Pubkey) -> Proof {
pub async fn get_proof_with_authority(client: &RpcClient, authority: Pubkey) -> Proof {
let proof_address = proof_pubkey(authority);
get_proof(client, proof_address).await
}

pub async fn get_proof(client: &RpcClient, address: Pubkey) -> Proof {
let data = client
.get_account_data(&proof_address)
.get_account_data(&address)
.await
.expect("Failed to get miner account");
*Proof::try_from_bytes(&data).expect("Failed to parse miner account")
Expand Down

0 comments on commit 98645b3

Please sign in to comment.