Skip to content

Commit

Permalink
add priority fees; move processing to module
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelvanderwaal committed Mar 23, 2024
1 parent 89a2cff commit 498548d
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 144 deletions.
9 changes: 7 additions & 2 deletions src/airdrop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
pub mod process;
pub mod sol;
pub mod spl;
pub use process::*;
pub use sol::*;
pub use spl::*;

pub use std::{collections::HashMap, fs::File, path::PathBuf, str::FromStr};

pub use anyhow::Result;
pub use jib::{Jib, Network};
use indicatif::ProgressBar;
pub use jib::{Jib, JibFailedTransaction, Network};
pub use log::debug;
pub use serde::{Deserialize, Serialize};
pub use solana_client::rpc_client::RpcClient;
pub use solana_sdk::{pubkey::Pubkey, signer::Signer};
use structopt::StructOpt;

pub use crate::update::{parse_keypair, parse_solana_config};

Expand All @@ -27,4 +31,5 @@ struct Recipient {
amount: u64,
}

pub const PRIORITY_FEE: u64 = 25_000;
// Test transactions take 3_150, but we pad it a bit.
pub const AIRDROP_SOL_CU: u32 = 5_000;
134 changes: 134 additions & 0 deletions src/airdrop/process.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use metaboss_lib::data::Priority;

use super::*;

#[derive(Debug, StructOpt)]
pub enum AirdropSubcommands {
/// Airdrop SOL (experimental)
#[structopt(name = "sol")]
Sol {
/// Path to the owner keypair file
#[structopt(short, long)]
keypair: Option<String>,

/// Path to the mint list file
#[structopt(short = "L", long)]
recipient_list: Option<String>,

/// Cache file
#[structopt(short, long)]
cache_file: Option<String>,

/// Rate limit in requests per second; defaults to 10
#[structopt(short = "R", long)]
rate_limit: Option<u64>,

/// Priority of the transaction: higher priority costs more.
#[structopt(short = "P", long, default_value = "none")]
priority: Priority,
},
/// Airdrop SPL tokens (experimental)
#[structopt(name = "spl")]
Spl {
/// Path to the owner keypair file
#[structopt(short, long)]
keypair: Option<String>,

/// Path to the mint list file
#[structopt(short = "L", long)]
recipient_list: Option<String>,

/// Cache file
#[structopt(short, long)]
cache_file: Option<String>,

/// Mint from the SPL token mint
#[structopt(short, long)]
mint: Pubkey,

#[structopt(long)]
mint_tokens: bool,

/// Rate limit in requests per second; defaults to 10
#[structopt(short = "R", long)]
rate_limit: Option<u64>,

/// Priority of the transaction: higher priority costs more.
#[structopt(short = "P", long, default_value = "none")]
priority: Priority,
},
/// Convert the bin cache file to json for readability
ReadCache {
/// Path to the cache file
cache_file: String,

/// Print errors to std out in addition to converting the cache file to json
#[structopt(long)]
errors: bool,
},
}

pub async fn process_airdrop(client: RpcClient, commands: AirdropSubcommands) -> Result<()> {
match commands {
AirdropSubcommands::Sol {
keypair,
recipient_list,
cache_file,
priority,
rate_limit,
} => {
airdrop_sol(AirdropSolArgs {
client,
keypair,
recipient_list,
cache_file,
priority,
rate_limit,
})
.await
}
AirdropSubcommands::Spl {
keypair,
recipient_list,
cache_file,
mint,
mint_tokens,
priority,
rate_limit,
} => {
airdrop_spl(AirdropSplArgs {
client,
keypair,
recipient_list,
cache_file,
mint,
mint_tokens,
priority,
rate_limit,
})
.await
}
AirdropSubcommands::ReadCache { cache_file, errors } => {
let path = std::path::Path::new(&cache_file);
let file = File::open(path)?;
let cache: Vec<JibFailedTransaction> = bincode::deserialize_from(file)?;

// Convert to JSON
let json_filename = path.with_extension("json");
let pb = ProgressBar::new_spinner();
pb.set_message("Writing cache file...");
pb.enable_steady_tick(100);

let cache_file = std::fs::File::create(json_filename)?;
serde_json::to_writer(cache_file, &cache)?;
pb.finish_and_clear();

if errors {
for tx in cache {
println!("{:?}", tx.error);
}
}
Ok(())
}
}
}
18 changes: 14 additions & 4 deletions src/airdrop/sol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use indicatif::ProgressBar;
use jib::JibFailedTransaction;
use metaboss_lib::data::Priority;

use super::*;

Expand All @@ -8,10 +9,12 @@ pub struct AirdropSolArgs {
pub keypair: Option<String>,
pub recipient_list: Option<String>,
pub cache_file: Option<String>,
pub boost: bool,
pub priority: Priority,
pub rate_limit: Option<u64>,
}



pub async fn airdrop_sol(args: AirdropSolArgs) -> Result<()> {
let solana_opts = parse_solana_config();
let keypair = parse_keypair(args.keypair, solana_opts);
Expand All @@ -31,9 +34,16 @@ pub async fn airdrop_sol(args: AirdropSolArgs) -> Result<()> {
let mut cache_file_name = format!("mb-cache-airdrop-{timestamp}.bin");
let successful_tx_file_name = format!("mb-successful-airdrops-{timestamp}.json");

if args.boost {
jib.set_priority_fee(PRIORITY_FEE);
}
let priority_fee = match args.priority {
Priority::None => 200, // 1 lamport
Priority::Low => 200_000, // 1_000 lamports
Priority::Medium => 1_000_000, // 5_000 lamports
Priority::High => 5_000_000, // 25_000 lamports
Priority::Max => 20_000_000, // 100_000 lamports
};

jib.set_priority_fee(priority_fee);
jib.set_compute_budget(AIRDROP_SOL_CU);

if let Some(rate) = args.rate_limit {
jib.set_rate_limit(rate);
Expand Down
17 changes: 12 additions & 5 deletions src/airdrop/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::anyhow;
use borsh::{BorshDeserialize, BorshSerialize};
use indicatif::ProgressBar;
use jib::JibFailedTransaction;
use metaboss_lib::transaction::send_and_confirm_tx;
use metaboss_lib::{data::Priority, transaction::send_and_confirm_tx};
use solana_program::{
instruction::{AccountMeta, Instruction},
program_pack::Pack,
Expand All @@ -20,7 +20,7 @@ pub struct AirdropSplArgs {
pub cache_file: Option<String>,
pub mint: Pubkey,
pub mint_tokens: bool,
pub boost: bool,
pub priority: Priority,
pub rate_limit: Option<u64>,
}

Expand Down Expand Up @@ -61,9 +61,16 @@ pub async fn airdrop_spl(args: AirdropSplArgs) -> Result<()> {
let mut cache_file_name = format!("mb-cache-airdrop-{timestamp}.bin");
let successful_tx_file_name = format!("mb-successful-airdrops-{timestamp}.json");

if args.boost {
jib.set_priority_fee(PRIORITY_FEE);
}
let priority_fee = match args.priority {
Priority::None => 200, // 1 lamport
Priority::Low => 200_000, // 1_000 lamports
Priority::Medium => 1_000_000, // 5_000 lamports
Priority::High => 5_000_000, // 25_000 lamports
Priority::Max => 20_000_000, // 100_000 lamports
};

jib.set_priority_fee(priority_fee);
jib.set_compute_budget(AIRDROP_SOL_CU);

if let Some(rate) = args.rate_limit {
jib.set_rate_limit(rate);
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extern crate log;

use anyhow::Result;
use metaboss::airdrop::process_airdrop;
use metaboss::check::process_check;
use metaboss::constants::PUBLIC_RPC_URLS;
use metaboss::extend_program::process_extend_program;
Expand Down
67 changes: 1 addition & 66 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use solana_program::pubkey::Pubkey;
use structopt::StructOpt;

use crate::{
airdrop::AirdropSubcommands,
check::CheckSubcommands,
collections::GetCollectionItemsMethods,
constants::DEFAULT_RATE_LIMIT,
Expand Down Expand Up @@ -162,72 +163,6 @@ pub enum Command {
},
}

#[derive(Debug, StructOpt)]
pub enum AirdropSubcommands {
/// Airdrop SOL (experimental)
#[structopt(name = "sol")]
Sol {
/// Path to the owner keypair file
#[structopt(short, long)]
keypair: Option<String>,

/// Path to the mint list file
#[structopt(short = "L", long)]
recipient_list: Option<String>,

/// Cache file
#[structopt(short, long)]
cache_file: Option<String>,

/// Rate limit in requests per second; defaults to 10
#[structopt(short = "R", long)]
rate_limit: Option<u64>,

/// Boost the transactions w/ priority fees
#[structopt(long)]
boost: bool,
},
/// Airdrop SPL tokens (experimental)
#[structopt(name = "spl")]
Spl {
/// Path to the owner keypair file
#[structopt(short, long)]
keypair: Option<String>,

/// Path to the mint list file
#[structopt(short = "L", long)]
recipient_list: Option<String>,

/// Cache file
#[structopt(short, long)]
cache_file: Option<String>,

/// Mint from the SPL token mint
#[structopt(short, long)]
mint: Pubkey,

#[structopt(long)]
mint_tokens: bool,

/// Rate limit in requests per second; defaults to 10
#[structopt(short = "R", long)]
rate_limit: Option<u64>,

/// Boost the transactions w/ priority fees
#[structopt(long)]
boost: bool,
},
/// Convert the bin cache file to json for readability
ReadCache {
/// Path to the cache file
cache_file: String,

/// Print errors to std out in addition to converting the cache file to json
#[structopt(long)]
errors: bool,
},
}

#[derive(Debug, StructOpt)]
pub enum BurnSubcommands {
/// Burn an asset.
Expand Down
Loading

0 comments on commit 498548d

Please sign in to comment.