Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate boost features #155

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
743 changes: 372 additions & 371 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ core_affinity = "0.8.1"
drillx = "2.1.0"
futures = "0.3.30"
indicatif = "0.17.8"
mpl-token-metadata = "4.1.2"
num_cpus = "1.16.0"
ore-api = "2.1.8"
ore-boost-api = "0.1.0"
ore-utils = "2.1.8"
ore-pool-api = { path = "../ore-pool/api" }
ore-pool-types = { path = "../ore-pool/types", package = "types" }
Expand All @@ -54,10 +56,10 @@ tokio-tungstenite = "0.16"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0.63"

# [patch.crates-io]
# drillx = { path = "../drillx/drillx" }
# ore-api = { path = "../ore/api" }
# ore-utils = { path = "../ore/utils" }
[patch.crates-io]
ore-api = { path = "../ore/api" }
ore-boost-api = { path = "../ore-boost/api" }
ore-utils = { path = "../ore/utils" }

[profile.release]
opt-level = 3 # Optimize for binary size. You can use "3" for full optimizations if binary size isn't an issue.
Expand Down
50 changes: 48 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ pub struct MineArgs {
)]
pub buffer_time: u64,

#[arg(
long,
short,
value_name = "MINT_ADDRESS",
help = "The token to apply as boost #1"
)]
pub boost_1: Option<String>,

#[arg(
long,
short,
value_name = "MINT_ADDRESS",
help = "The token to apply as boost #2"
)]
pub boost_2: Option<String>,

#[arg(
long,
short,
value_name = "MINT_ADDRESS",
help = "The token to apply as boost #3"
)]
pub boost_3: Option<String>,

#[arg(
long,
short,
Expand All @@ -100,14 +124,17 @@ pub struct RewardsArgs {}
pub struct StakeArgs {
#[arg(
value_name = "AMOUNT",
help = "The amount of ORE to stake. Defaults to max."
help = "The amount of the token to stake. Defaults to max."
)]
pub amount: Option<f64>,

#[arg(value_name = "MINT_ADDRESS", help = "The mint to stake.")]
pub mint: String,

#[arg(
long,
value_name = "TOKEN_ACCOUNT_ADDRESS",
help = "Token account to send ORE from. Defaults to the associated token account."
help = "Token account to send from. Defaults to the associated token account."
)]
pub token_account: Option<String>,
}
Expand All @@ -124,6 +151,25 @@ pub struct TransferArgs {
pub to: String,
}

#[derive(Parser, Debug)]
pub struct UnstakeArgs {
#[arg(
value_name = "AMOUNT",
help = "The amount of the token to unstake. Defaults to max."
)]
pub amount: Option<f64>,

#[arg(value_name = "MINT_ADDRESS", help = "The mint to unstake.")]
pub mint: String,

#[arg(
long,
value_name = "TOKEN_ACCOUNT_ADDRESS",
help = "Token account to receive unstaked funds. Defaults to the associated token account."
)]
pub token_account: Option<String>,
}

#[derive(Parser, Debug)]
pub struct UpgradeArgs {
#[arg(
Expand Down
2 changes: 1 addition & 1 deletion src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Miner {
}

// Send and confirm
ixs.push(ore_api::instruction::claim(pubkey, beneficiary, amount));
ixs.push(ore_api::sdk::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 @@ -37,7 +37,7 @@ impl Miner {
}

// Submit close transaction
let ix = ore_api::instruction::close(signer.pubkey());
let ix = ore_api::sdk::close(signer.pubkey());
self.send_and_confirm(&[ix], ComputeBudget::Fixed(500_000), false)
.await
.ok();
Expand Down
2 changes: 1 addition & 1 deletion src/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Miner {

// Submit initialize tx
let blockhash = self.rpc_client.get_latest_blockhash().await.unwrap();
let ix = ore_api::instruction::initialize(self.signer().pubkey());
let ix = ore_api::sdk::initialize(self.signer().pubkey());
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&self.signer().pubkey()),
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod rewards;
mod send_and_confirm;
mod stake;
mod transfer;
mod unstake;
mod upgrade;
mod utils;

Expand Down Expand Up @@ -76,12 +77,15 @@ enum Commands {
#[command(about = "Fetch the current reward rate for each difficulty level")]
Rewards(RewardsArgs),

#[command(about = "Stake to earn a rewards multiplier")]
#[command(about = "Stake tokens to earn a mining multiplier")]
Stake(StakeArgs),

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

#[command(about = "Unstake tokens")]
Unstake(UnstakeArgs),

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

Expand Down Expand Up @@ -257,6 +261,9 @@ async fn main() {
Commands::Transfer(args) => {
miner.transfer(args).await;
}
Commands::Unstake(args) => {
miner.unstake(args).await;
}
Commands::Upgrade(args) => {
miner.upgrade(args).await;
}
Expand Down
121 changes: 107 additions & 14 deletions src/mine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
str::FromStr,
sync::{Arc, RwLock},
time::Instant,
usize,
Expand All @@ -9,23 +10,31 @@ use drillx::{
equix::{self},
Hash, Solution,
};
use mpl_token_metadata::accounts::Metadata;
use ore_api::{
consts::{BUS_ADDRESSES, BUS_COUNT, EPOCH_DURATION},
state::{Bus, Config},
};
use ore_boost_api::state::{boost_pda, stake_pda};
use ore_utils::AccountDeserialize;
use rand::Rng;
use solana_program::pubkey::Pubkey;
use solana_rpc_client::spinner;
use solana_program::{
instruction::{AccountMeta, Instruction},
program_pack::Pack,
pubkey::Pubkey,
};
use solana_rpc_client::{nonblocking::rpc_client::RpcClient, spinner};
use solana_sdk::signer::Signer;
use spl_token::state::Mint;

use crate::{
args::MineArgs,
error::Error,
pool::Pool,
send_and_confirm::ComputeBudget,
utils::{
amount_u64_to_string, get_clock, get_config, get_updated_proof_with_authority, proof_pubkey,
amount_u64_to_string, get_boost, get_clock, get_config, get_stake,
get_updated_proof_with_authority, proof_pubkey,
},
Miner,
};
Expand Down Expand Up @@ -55,6 +64,14 @@ impl Miner {
// Check num threads
self.check_num_cores(args.cores);

// Fetch boost data
let boost_data_1 =
fetch_boost_data(self.rpc_client.clone(), signer.pubkey(), &args.boost_1).await;
let boost_data_2 =
fetch_boost_data(self.rpc_client.clone(), signer.pubkey(), &args.boost_2).await;
let boost_data_3 =
fetch_boost_data(self.rpc_client.clone(), signer.pubkey(), &args.boost_3).await;

// Start mining loop
let mut last_hash_at = 0;
let mut last_balance = 0;
Expand All @@ -64,19 +81,25 @@ impl Miner {
let proof =
get_updated_proof_with_authority(&self.rpc_client, signer.pubkey(), last_hash_at)
.await;

// Print unclaimed balance
println!(
"\n\nStake: {} ORE\n{} Multiplier: {:12}x",
"\n\nBalance: {} ORE{}",
amount_u64_to_string(proof.balance),
if last_hash_at.gt(&0) {
format!(
" Change: {} ORE\n",
"\n Change: {} ORE",
amount_u64_to_string(proof.balance.saturating_sub(last_balance))
)
} else {
"".to_string()
},
calculate_multiplier(proof.balance, config.top_balance)
);

// Print boosts
log_boost_data(self.rpc_client.clone(), &boost_data_1, 1).await;
log_boost_data(self.rpc_client.clone(), &boost_data_2, 2).await;
log_boost_data(self.rpc_client.clone(), &boost_data_3, 3).await;
last_hash_at = proof.last_hash_at;
last_balance = proof.balance;

Expand All @@ -101,20 +124,25 @@ impl Miner {
.await;

// Build instruction set
let mut ixs = vec![ore_api::instruction::auth(proof_pubkey(signer.pubkey()))];
let mut compute_budget = 500_000;
if self.should_reset(config).await && rand::thread_rng().gen_range(0..100).eq(&0) {
let mut ixs = vec![ore_api::sdk::auth(proof_pubkey(signer.pubkey()))];
let mut compute_budget = 600_000;
// if self.should_reset(config).await && rand::thread_rng().gen_range(0..100).eq(&0) {
if self.should_reset(config).await {
compute_budget += 100_000;
ixs.push(ore_api::instruction::reset(signer.pubkey()));
ixs.push(ore_api::sdk::reset(signer.pubkey()));
}

// Build mine ix
ixs.push(ore_api::instruction::mine(
let mut ix = ore_api::sdk::mine(
signer.pubkey(),
signer.pubkey(),
self.find_bus().await,
solution,
));
);
apply_boost(&mut ix, &boost_data_1);
apply_boost(&mut ix, &boost_data_2);
apply_boost(&mut ix, &boost_data_3);
ixs.push(ix);

// Submit transaction
self.send_and_confirm(&ixs, ComputeBudget::Fixed(compute_budget), false)
Expand Down Expand Up @@ -353,8 +381,73 @@ impl Miner {
}
}

fn calculate_multiplier(balance: u64, top_balance: u64) -> f64 {
1.0 + (balance as f64 / top_balance as f64).min(1.0f64)
#[derive(Clone)]
struct BoostData {
boost_address: Pubkey,
stake_address: Pubkey,
mint: Mint,
metadata: Option<Metadata>,
}

async fn fetch_boost_data(
rpc: Arc<RpcClient>,
authority: Pubkey,
mint_address: &Option<String>,
) -> Option<BoostData> {
let Some(mint_address) = mint_address else {
return None;
};
let mint_address = Pubkey::from_str(&mint_address).unwrap();
let boost_address = boost_pda(mint_address).0;
let stake_address = stake_pda(authority, boost_address).0;
let mint = rpc
.get_account_data(&mint_address)
.await
.map(|data| Mint::unpack(&data).unwrap())
.unwrap();
let metadata = rpc
.get_account_data(&Metadata::find_pda(&mint_address).0)
.await
.ok()
.map(|data| Metadata::from_bytes(&data).unwrap());
Some(BoostData {
boost_address,
stake_address,
mint,
metadata,
})
}

async fn log_boost_data(rpc: Arc<RpcClient>, boost_data: &Option<BoostData>, id: u64) {
if let Some(boost_data) = boost_data {
let boost = get_boost(&rpc, boost_data.boost_address).await;
let stake = get_stake(&rpc, boost_data.stake_address).await;
let multiplier =
(boost.multiplier as f64) * (stake.balance as f64) / (boost.total_stake as f64);
println!(
" Boost {}: {:12}x ({})",
id,
multiplier,
format!(
"{} of {}{}",
stake.balance as f64 / 10f64.powf(boost_data.mint.decimals as f64),
boost.total_stake as f64 / 10f64.powf(boost_data.mint.decimals as f64),
boost_data
.clone()
.metadata
.map_or("".to_string(), |m| format!(" {}", m.symbol))
)
);
}
}

fn apply_boost(ix: &mut Instruction, boost_data: &Option<BoostData>) {
if let Some(boost_data) = boost_data {
ix.accounts
.push(AccountMeta::new_readonly(boost_data.boost_address, false));
ix.accounts
.push(AccountMeta::new_readonly(boost_data.stake_address, false));
}
}

fn format_duration(seconds: u32) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Miner {

// Sign and send transaction.
println!("Generating challenge...");
let ix = ore_api::instruction::open(signer.pubkey(), signer.pubkey(), fee_payer.pubkey());
let ix = ore_api::sdk::open(signer.pubkey(), signer.pubkey(), fee_payer.pubkey());
self.send_and_confirm(&[ix], ComputeBudget::Fixed(400_000), false)
.await
.ok();
Expand Down
Loading