Skip to content

Commit

Permalink
Merge pull request #4 from nifty-oss/feat/priority-fees
Browse files Browse the repository at this point in the history
add priority fees to all commands
  • Loading branch information
samuelvanderwaal authored Apr 20, 2024
2 parents 8302817 + b38c64c commit db840e5
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 28 deletions.
33 changes: 32 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::path::PathBuf;
use clap::{Parser, Subcommand};
use solana_program::pubkey::Pubkey;

use crate::transaction::Priority;

#[derive(Parser)]
#[clap(author, version, about)]
pub struct Args {
Expand All @@ -27,16 +29,27 @@ pub enum Commands {

/// The recipient to receive reclaimed rent. Defaults to the signer.
recipient: Option<Pubkey>,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Create an asset with extension data.
Mint { asset_file_path: PathBuf },
Mint {
asset_file_path: PathBuf,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Create a batch of assets with extension data.
MintBatch {
asset_files_dir: PathBuf,

/// Delay in ms between transactions.
#[arg(long, default_value = "100")]
delay: u64,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Create a basic asset with no extensions.
Create {
Expand All @@ -55,6 +68,9 @@ pub enum Commands {
/// Owner of the created asset, defaults to authority pubkey.
#[arg(short, long)]
owner: Option<Pubkey>,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Get an asset account's data and decode it.
Decode {
Expand Down Expand Up @@ -82,6 +98,9 @@ pub enum Commands {
/// Specify each one separately: --role burn --role lock --role transfer
#[arg(short = 'R', long)]
role: Vec<String>,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Lock an asset, preventing any actions to be performed on it.
Lock {
Expand All @@ -90,6 +109,9 @@ pub enum Commands {

/// Path to the signer keypair file. Defaults to the config keypair.
signer_keypair_path: Option<PathBuf>,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Revoke a delegate from an asset.
Revoke {
Expand All @@ -104,6 +126,9 @@ pub enum Commands {
/// Revoke all roles from the delegate and clear it.
#[arg(long)]
all: bool,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Transfer an asset to a new owner.
Transfer {
Expand All @@ -112,6 +137,9 @@ pub enum Commands {

/// The recipient of the asset.
recipient: Pubkey,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
/// Unlock an asset, allowing actions to be performed on it.
Unlock {
Expand All @@ -120,5 +148,8 @@ pub enum Commands {

/// Path to the signer keypair file. Defaults to the config keypair.
signer_keypair_path: Option<PathBuf>,

#[arg(short = 'P', long, default_value = "low")]
priority: Priority,
},
}
19 changes: 16 additions & 3 deletions src/commands/approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct ApproveArgs {
pub asset: Pubkey,
pub delegate: Pubkey,
pub role: Vec<String>,
pub priority: Priority,
}

pub fn handle_approve(args: ApproveArgs) -> Result<()> {
Expand All @@ -33,7 +34,7 @@ pub fn handle_approve(args: ApproveArgs) -> Result<()> {
})
.collect();

let args = ApproveInstructionArgs {
let ix_args = ApproveInstructionArgs {
delegate_input: DelegateInput::Some { roles },
};

Expand All @@ -42,9 +43,21 @@ pub fn handle_approve(args: ApproveArgs) -> Result<()> {
owner,
delegate,
}
.instruction(args);
.instruction(ix_args);

let sig = send_and_confirm_tx(&config.client, &[&owner_sk], &[ix])?;
let signers = vec![&owner_sk];

let micro_lamports = get_priority_fee(&args.priority);
let compute_units =
get_compute_units(&config.client, &[ix.clone()], &signers)?.unwrap_or(200_000);

let instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(compute_units as u32),
ComputeBudgetInstruction::set_compute_unit_price(micro_lamports),
ix,
];

let sig = send_and_confirm_tx_with_spinner(&config.client, &signers, &instructions)?;

println!("Setting {delegate} as a delegate on asset {asset} in tx: {sig}");

Expand Down
15 changes: 14 additions & 1 deletion src/commands/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct BurnArgs {
pub rpc_url: Option<String>,
pub asset: Pubkey,
pub recipient: Option<Pubkey>,
pub priority: Priority,
}

pub fn handle_burn(args: BurnArgs) -> Result<()> {
Expand All @@ -26,7 +27,19 @@ pub fn handle_burn(args: BurnArgs) -> Result<()> {
}
.instruction();

let sig = send_and_confirm_tx(&config.client, &[&signer_sk], &[ix])?;
let signers = vec![&signer_sk];

let micro_lamports = get_priority_fee(&args.priority);
let compute_units =
get_compute_units(&config.client, &[ix.clone()], &signers)?.unwrap_or(200_000);

let instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(compute_units as u32),
ComputeBudgetInstruction::set_compute_unit_price(micro_lamports),
ix,
];

let sig = send_and_confirm_tx_with_spinner(&config.client, &signers, &instructions)?;

println!("Burned asset {asset} in tx: {sig}");

Expand Down
15 changes: 14 additions & 1 deletion src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct CreateArgs {
pub asset_keypair_path: Option<PathBuf>,
pub immutable: bool,
pub owner: Option<Pubkey>,
pub priority: Priority,
}

pub fn handle_create(args: CreateArgs) -> Result<()> {
Expand Down Expand Up @@ -41,7 +42,19 @@ pub fn handle_create(args: CreateArgs) -> Result<()> {
}
.instruction(ix_args);

let sig = send_and_confirm_tx(&config.client, &[&authority_sk, &asset_sk], &[ix])?;
let signers = vec![&authority_sk, &asset_sk];

let micro_lamports = get_priority_fee(&args.priority);
let compute_units =
get_compute_units(&config.client, &[ix.clone()], &signers)?.unwrap_or(200_000);

let instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(compute_units as u32),
ComputeBudgetInstruction::set_compute_unit_price(micro_lamports),
ix,
];

let sig = send_and_confirm_tx_with_spinner(&config.client, &signers, &instructions)?;

println!("Asset {asset} created in tx: {sig}");

Expand Down
15 changes: 14 additions & 1 deletion src/commands/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct LockArgs {
pub rpc_url: Option<String>,
pub asset: Pubkey,
pub signer_keypair_path: Option<PathBuf>,
pub priority: Priority,
}

pub fn handle_lock(args: LockArgs) -> Result<()> {
Expand All @@ -27,7 +28,19 @@ pub fn handle_lock(args: LockArgs) -> Result<()> {

let ix = Lock { asset, signer }.instruction();

let sig = send_and_confirm_tx(&config.client, &[&payer_sk, &signer_sk], &[ix])?;
let signers = vec![&payer_sk, &signer_sk];

let micro_lamports = get_priority_fee(&args.priority);
let compute_units =
get_compute_units(&config.client, &[ix.clone()], &signers)?.unwrap_or(200_000);

let instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(compute_units as u32),
ComputeBudgetInstruction::set_compute_unit_price(micro_lamports),
ix,
];

let sig = send_and_confirm_tx_with_spinner(&config.client, &signers, &instructions)?;

println!("Locking asset {asset} in tx: {sig}");

Expand Down
20 changes: 18 additions & 2 deletions src/commands/mint.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::transaction::pack_instructions;
use solana_sdk::compute_budget::ComputeBudgetInstruction;

use crate::transaction::{get_compute_units, get_priority_fee, pack_instructions, Priority};

use super::*;

pub struct MintArgs {
pub keypair_path: Option<PathBuf>,
pub rpc_url: Option<String>,
pub asset_file_path: PathBuf,
pub priority: Priority,
}

pub async fn handle_mint(args: MintArgs) -> Result<()> {
Expand Down Expand Up @@ -43,6 +46,8 @@ pub async fn handle_mint(args: MintArgs) -> Result<()> {
})
.collect::<Vec<ExtensionArgs>>();

let micro_lamports = get_priority_fee(&args.priority);

let instructions = mint(MintIxArgs {
accounts,
asset_args,
Expand All @@ -51,9 +56,20 @@ pub async fn handle_mint(args: MintArgs) -> Result<()> {

let packed_instructions = pack_instructions(2, &authority_sk.pubkey(), &instructions);

let signers = vec![&authority_sk, &asset_sk];

// Instructions are packed to max data length sizes, so we only put one in each tx.
for instructions in packed_instructions {
let sig = send_and_confirm_tx(&config.client, &[&authority_sk, &asset_sk], &instructions)?;
let compute_units =
get_compute_units(&config.client, &instructions, &signers)?.unwrap_or(200_000);

let mut final_instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(compute_units as u32),
ComputeBudgetInstruction::set_compute_unit_price(micro_lamports),
];
final_instructions.extend(instructions);

let sig = send_and_confirm_tx_with_spinner(&config.client, &signers, &final_instructions)?;
println!("sig: {}", sig);
}

Expand Down
24 changes: 20 additions & 4 deletions src/commands/mint_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct MintBatchArgs {
pub rpc_url: Option<String>,
pub asset_files_dir: PathBuf,
pub delay: u64,
pub priority: Priority,
}

pub struct AssetStruct {
Expand Down Expand Up @@ -133,6 +134,8 @@ pub async fn handle_mint_batch(args: MintBatchArgs) -> Result<()> {
.unwrap()
.progress_chars("=>-");

let micro_lamports = get_priority_fee(&args.priority);

for (i, asset_instructions) in instructions.into_iter().enumerate() {
let client = client.clone();
let authority_sk = authority_sk.clone();
Expand All @@ -146,19 +149,32 @@ pub async fn handle_mint_batch(args: MintBatchArgs) -> Result<()> {
// to create the asset and set its extension data.
futures.push(tokio::spawn(async move {
// Pack all the instructions for minting this asset into as few transactions as possible.
let packed_transactions =
let packed_instructions =
pack_instructions(2, &authority_sk.pubkey(), &asset_instructions);

// Create a progress bar for each asset w/ the number of transactions to send.
let pb = mp_clone.add(ProgressBar::new(packed_transactions.len() as u64));
let pb = mp_clone.add(ProgressBar::new(packed_instructions.len() as u64));
pb.set_style(sty_clone.clone());

let asset_sk = &asset_keys.lock().await[i];
let asset_address = &asset_sk.pubkey();
pb.set_message(format!("sending transactions for asset {asset_address}"));

for transaction in packed_transactions {
let res = send_and_confirm_tx(&client, &[&authority_sk, &asset_sk], &transaction);
let signers = vec![&authority_sk, asset_sk];

for instructions in packed_instructions {
let compute_units = get_compute_units(&client, &instructions, &signers)
.unwrap_or(Some(200_000))
.unwrap_or(200_000);

let mut final_instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(compute_units as u32),
ComputeBudgetInstruction::set_compute_unit_price(micro_lamports),
];
final_instructions.extend(instructions);

let res = send_and_confirm_tx(&client, &signers, &final_instructions);

pb.inc(1);

match res {
Expand Down
9 changes: 8 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ pub use transfer::*;
pub use unlock::*;

// Internal lib
pub use crate::{setup::CliConfig, transaction::send_and_confirm_tx};
pub use crate::{
setup::CliConfig,
transaction::{
get_compute_units, get_priority_fee, send_and_confirm_tx, send_and_confirm_tx_with_spinner,
Priority,
},
};

// Standard lib
pub use std::{fs::File, path::PathBuf};
Expand All @@ -40,6 +46,7 @@ pub use {
serde::{Deserialize, Serialize},
solana_program::system_program,
solana_sdk::{
compute_budget::ComputeBudgetInstruction,
pubkey::Pubkey,
signature::{read_keypair_file, Keypair},
signer::Signer,
Expand Down
19 changes: 16 additions & 3 deletions src/commands/revoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct RevokeArgs {
pub asset: Pubkey,
pub role: Vec<String>,
pub all: bool,
pub priority: Priority,
}

pub fn handle_revoke(args: RevokeArgs) -> Result<()> {
Expand All @@ -31,17 +32,29 @@ pub fn handle_revoke(args: RevokeArgs) -> Result<()> {
})
.collect();

let args = RevokeInstructionArgs {
let ix_args = RevokeInstructionArgs {
delegate_input: if args.all {
DelegateInput::All
} else {
DelegateInput::Some { roles }
},
};

let ix = Revoke { asset, signer }.instruction(args);
let ix = Revoke { asset, signer }.instruction(ix_args);

let sig = send_and_confirm_tx(&config.client, &[&signer_sk], &[ix])?;
let signers = vec![&signer_sk];

let micro_lamports = get_priority_fee(&args.priority);
let compute_units =
get_compute_units(&config.client, &[ix.clone()], &signers)?.unwrap_or(200_000);

let instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(compute_units as u32),
ComputeBudgetInstruction::set_compute_unit_price(micro_lamports),
ix,
];

let sig = send_and_confirm_tx_with_spinner(&config.client, &signers, &instructions)?;

println!("Revoking the delegate on asset {asset} in tx: {sig}");

Expand Down
Loading

0 comments on commit db840e5

Please sign in to comment.