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

draft #328

Closed
wants to merge 1 commit into from
Closed

draft #328

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
20 changes: 17 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ borsh = "0.10.3"
bs58 = "0.4.0"
chrono = "0.4.31"
console = "0.15.7"
dialoguer = "0.11.0"
dirs = "5.0.1"
env_logger = "0.9.3"
futures = "0.3.29"
Expand All @@ -24,7 +25,7 @@ indicatif = { version = "0.16.2", features = ["rayon"] }
jib = "0.8.0"
lazy_static = "1.4.0"
log = "0.4.20"
metaboss_lib = "0.17.1"
metaboss_lib = "0.17.2"
mpl-token-metadata = { version = "3.2.3", features = ["serde"] }
num_cpus = "1.16.0"
once_cell = "1.19.0"
Expand Down
30 changes: 25 additions & 5 deletions src/create/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use mpl_token_metadata::{
instructions::{CreateBuilder, CreateMasterEditionV3Builder},
types::{CreateArgs, DataV2, TokenStandard},
};
use solana_sdk::signature::read_keypair_file;
use solana_sdk::{compute_budget::ComputeBudgetInstruction, signature::read_keypair_file};
use spl_associated_token_account::get_associated_token_address;
use spl_token::instruction::mint_to;

use crate::utils::create_token_if_missing_instruction;
use crate::utils::{calculate_priority_fees, create_token_if_missing_instruction};

use super::*;

Expand Down Expand Up @@ -68,7 +68,13 @@ pub fn create_metadata(args: CreateMetadataArgs) -> Result<()> {
.create_args(create_args)
.instruction();

let instructions = vec![ix];
let priority_fee = calculate_priority_fees(&args.client, vec![&keypair], ix.clone())?;

let instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.compute),
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.fee),
ix,
];

let sig = send_and_confirm_transaction(&args.client, keypair, &instructions)?;

Expand Down Expand Up @@ -150,7 +156,13 @@ pub fn create_fungible(args: CreateFungibleArgs) -> Result<()> {
.create_args(create_args)
.instruction();

let mut instructions = vec![ix];
let priority_fee = calculate_priority_fees(&args.client, vec![&keypair, &mint], ix.clone())?;

let mut instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.compute),
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.fee),
ix,
];

if let Some(initial_supply) = args.initial_supply {
// Convert float to native token units
Expand Down Expand Up @@ -233,9 +245,17 @@ pub fn create_master_edition(args: CreateMasterEditionArgs) -> Result<()> {
}
let ix = builder.instruction();

let priority_fee = calculate_priority_fees(&args.client, vec![&keypair], ix.clone())?;

let instructions = vec![
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.compute),
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.fee),
ix,
];

let recent_blockhash = args.client.get_latest_blockhash()?;
let tx = Transaction::new_signed_with_payer(
&[ix],
&instructions,
Some(&keypair.pubkey()),
&[&keypair, &mint_authority],
recent_blockhash,
Expand Down
79 changes: 79 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::{anyhow, Result};
use borsh::{BorshDeserialize, BorshSerialize};
use dialoguer::Confirm;
use metaboss_lib::data::{ComputeUnits, PriorityFee};
use retry::{delay::Exponential, retry};
use serde::Deserialize;
use serde_json::json;
Expand All @@ -10,6 +12,7 @@ use solana_program::program_pack::Pack;
use solana_program::system_program;
use solana_program::{pubkey, pubkey::Pubkey};
use solana_sdk::commitment_config::CommitmentConfig;
use solana_sdk::compute_budget::ComputeBudgetInstruction;
use solana_sdk::{
instruction::Instruction, signature::Keypair, signer::Signer, transaction::Transaction,
};
Expand All @@ -22,6 +25,82 @@ use crate::wtf_errors::{
ANCHOR_ERROR, AUCTIONEER_ERROR, AUCTION_HOUSE_ERROR, CANDY_CORE_ERROR, CANDY_ERROR,
CANDY_GUARD_ERROR, METADATA_ERROR,
};
pub fn calculate_priority_fees(
client: &RpcClient,
signers: Vec<&Keypair>,
instruction: Instruction,
) -> Result<PriorityFee> {
let compute_units = calculate_units_consumed(client, signers, vec![instruction.clone()])?;

let write_lock_accounts = instruction
.accounts
.into_iter()
.filter(|am| am.is_writable)
.map(|am| am.pubkey)
.collect::<Vec<Pubkey>>();

// Get recent prioritization fees.
let fees = client.get_recent_prioritization_fees(&write_lock_accounts)?;

let max_fee = fees.iter().map(|pf| pf.prioritization_fee).max();
let max_fee = max_fee.unwrap_or(0);

println!("Max fee: {}", max_fee);
println!("Compute units: {}", compute_units);

// At least 1 lamport priority fee.
let priority_fee_lamports = std::cmp::max(max_fee * compute_units as u64 / 1_000_000, 1);
let priority_fee_sol = priority_fee_lamports as f64 / 1_000_000_000.0;

let confirmation = Confirm::new()
.with_prompt(format!(
"The priority fee for this transaction is {} SOL. Continue?",
priority_fee_sol,
))
.interact()?;

if !confirmation {
return Err(anyhow!("Transaction cancelled"));
}

// Pad compute units a bit.
let compute = compute_units + 20_000;
// Ensure that fee * compute / 1_000_000 is at least 1 lamport.
let fee = std::cmp::max(max_fee, 1_400_000 / compute as u64);

Ok(PriorityFee { fee, compute })
}

pub fn calculate_units_consumed(
client: &RpcClient,
signers: Vec<&Keypair>,
instructions: Vec<Instruction>,
) -> Result<ComputeUnits> {
// Simulate the transaction and see how much compute it used
let mut ixs = vec![
ComputeBudgetInstruction::set_compute_unit_limit(1_000_000),
ComputeBudgetInstruction::set_compute_unit_price(1),
];
ixs.extend(instructions);

let blockhash = client.get_latest_blockhash()?;

let tx = Transaction::new_signed_with_payer(
&ixs,
Some(&signers[0].pubkey()),
signers.as_slice(),
blockhash,
);

let tx_simulation = client.simulate_transaction(&tx)?;

let fee = tx_simulation
.value
.units_consumed
.ok_or_else(|| anyhow!("No compute units in simulation response"))? as u32;

Ok(fee)
}

pub fn send_and_confirm_transaction(
client: &RpcClient,
Expand Down
Loading