Skip to content

Commit

Permalink
Refactor burn command (#3976)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Oct 3, 2024
1 parent 80f8cae commit 46611bd
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 137 deletions.
4 changes: 2 additions & 2 deletions crates/mockcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct TransactionTemplate<'a> {
pub output_values: &'a [u64],
pub outputs: usize,
pub p2tr: bool,
pub receiver: Option<Address>,
pub recipient: Option<Address>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'a> Default for TransactionTemplate<'a> {
output_values: &[],
outputs: 1,
p2tr: false,
receiver: None,
recipient: None,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/mockcore/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ impl State {
.get(i)
.cloned()
.unwrap_or(value_per_output),
script_pubkey: if template.receiver.is_some() {
template.receiver.as_ref().unwrap().script_pubkey()
script_pubkey: if let Some(recipient) = &template.recipient {
recipient.script_pubkey()
} else if template.p2tr {
let secp = Secp256k1::new();
let keypair = KeyPair::new(&secp, &mut rand::thread_rng());
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use {
teleburn, ParsedEnvelope,
},
into_usize::IntoUsize,
outgoing::Outgoing,
representation::Representation,
settings::Settings,
subcommand::{OutputFormat, Subcommand, SubcommandResult},
Expand Down Expand Up @@ -129,7 +130,7 @@ pub mod wallet;
type Result<T = (), E = Error> = std::result::Result<T, E>;
type SnafuResult<T = (), E = SnafuError> = std::result::Result<T, E>;

pub const TARGET_POSTAGE: Amount = Amount::from_sat(10_000);
const TARGET_POSTAGE: Amount = Amount::from_sat(10_000);

static SHUTTING_DOWN: AtomicBool = AtomicBool::new(false);
static LISTENERS: Mutex<Vec<axum_server::Handle>> = Mutex::new(Vec::new());
Expand Down
42 changes: 20 additions & 22 deletions src/subcommand/wallet/burn.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use {super::*, crate::outgoing::Outgoing, bitcoin::opcodes};

const MAX_BURN_SATS: u64 = 10000;
use {super::*, bitcoin::opcodes};

#[derive(Debug, Parser)]
pub struct Burn {
Expand All @@ -10,29 +8,33 @@ pub struct Burn {
fee_rate: FeeRate,
#[arg(
long,
help = "Target <AMOUNT> postage with sent inscriptions. [default: 10000 sat]"
help = "Target <AMOUNT> postage with sent inscriptions. [default: 10000 sat]",
value_name = "AMOUNT"
)]
postage: Option<Amount>,
inscription_id: InscriptionId,
inscription: InscriptionId,
}

impl Burn {
pub(crate) fn run(self, wallet: Wallet) -> SubcommandResult {
let inscription_info = wallet
.inscription_info()
.get(&self.inscription_id)
.ok_or_else(|| anyhow!("inscription {} not found", self.inscription_id))?
.get(&self.inscription)
.ok_or_else(|| anyhow!("inscription {} not found", self.inscription))?
.clone();

if inscription_info.value.unwrap() > MAX_BURN_SATS {
return Err(anyhow!(
"The amount of sats where the inscription is on exceeds {}",
MAX_BURN_SATS
));
let Some(value) = inscription_info.value else {
bail!("Cannot burn unbound inscription");
};

ensure! {
value <= TARGET_POSTAGE.to_sat(),
"Cannot burn inscription contained in UTXO exceeding {TARGET_POSTAGE}",
}

if self.postage.unwrap_or_default() > Amount::from_sat(MAX_BURN_SATS) {
return Err(anyhow!("Target postage exceeds {}", MAX_BURN_SATS));
ensure! {
self.postage.unwrap_or_default() <= TARGET_POSTAGE,
"Postage may not exceed {TARGET_POSTAGE}",
}

let unsigned_transaction = Self::create_unsigned_burn_transaction(
Expand All @@ -47,7 +49,7 @@ impl Burn {
Ok(Some(Box::new(send::Output {
txid,
psbt,
outgoing: Outgoing::InscriptionId(self.inscription_id),
outgoing: Outgoing::InscriptionId(self.inscription),
fee,
})))
}
Expand All @@ -67,13 +69,9 @@ impl Burn {

let change = [wallet.get_change_address()?, wallet.get_change_address()?];

let postage = if let Some(postage) = postage {
Target::ExactPostage(postage)
} else {
Target::Postage
};
let postage = postage.map(Target::ExactPostage).unwrap_or(Target::Postage);

let burn_script = script::Builder::new()
let script_pubkey = script::Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.into_script();

Expand All @@ -84,7 +82,7 @@ impl Burn {
wallet.utxos().clone(),
wallet.locked_utxos().clone().into_keys().collect(),
runic_outputs,
burn_script,
script_pubkey,
change,
fee_rate,
postage,
Expand Down
3 changes: 2 additions & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub(crate) struct Inscribe {
pub(crate) parent: Option<InscriptionId>,
#[arg(
long,
help = "Include <AMOUNT> postage with inscription. [default: 10000sat]"
help = "Include <AMOUNT> postage with inscription. [default: 10000sat]",
value_name = "AMOUNT"
)]
pub(crate) postage: Option<Amount>,
#[clap(long, help = "Allow reinscription.")]
Expand Down
5 changes: 3 additions & 2 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {super::*, crate::outgoing::Outgoing};
use super::*;

#[derive(Debug, Parser)]
pub(crate) struct Send {
Expand All @@ -8,7 +8,8 @@ pub(crate) struct Send {
fee_rate: FeeRate,
#[arg(
long,
help = "Target <AMOUNT> postage with sent inscriptions. [default: 10000 sat]"
help = "Target <AMOUNT> postage with sent inscriptions. [default: 10000 sat]",
value_name = "AMOUNT"
)]
pub(crate) postage: Option<Amount>,
address: Address<NetworkUnchecked>,
Expand Down
4 changes: 2 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub(crate) fn address() -> Address {
}

pub(crate) fn recipient() -> ScriptBuf {
recipient_as_address().script_pubkey()
recipient_address().script_pubkey()
}

pub(crate) fn recipient_as_address() -> Address {
pub(crate) fn recipient_address() -> Address {
"tb1q6en7qjxgw4ev8xwx94pzdry6a6ky7wlfeqzunz"
.parse::<Address<NetworkUnchecked>>()
.unwrap()
Expand Down
44 changes: 22 additions & 22 deletions src/wallet/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod tests {
let utxos = vec![(outpoint(1), tx_out(20000, address()))];
let inscription = inscription("text/plain", "ord");
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();
let reveal_change = [commit_address, change(1)];

let batch::Transactions {
Expand Down Expand Up @@ -123,7 +123,7 @@ mod tests {
let utxos = vec![(outpoint(1), tx_out(20000, address()))];
let inscription = inscription("text/plain", "ord");
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();
let reveal_change = [commit_address, change(1)];

let batch::Transactions {
Expand Down Expand Up @@ -173,7 +173,7 @@ mod tests {
let inscription = inscription("text/plain", "ord");
let satpoint = None;
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();

let error = batch::Plan {
satpoint,
Expand Down Expand Up @@ -225,7 +225,7 @@ mod tests {
let inscription = inscription("text/plain", "ord");
let satpoint = None;
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();

assert!(batch::Plan {
satpoint,
Expand Down Expand Up @@ -270,7 +270,7 @@ mod tests {
let inscription = inscription("text/plain", "ord");
let satpoint = None;
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();
let fee_rate = 3.3;

let batch::Transactions {
Expand Down Expand Up @@ -358,7 +358,7 @@ mod tests {
.into();

let commit_address = change(1);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();
let fee_rate = 4.0;

let batch::Transactions {
Expand Down Expand Up @@ -444,7 +444,7 @@ mod tests {
let inscription = inscription("text/plain", "ord");
let satpoint = None;
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();
let commit_fee_rate = 3.3;
let fee_rate = 1.0;

Expand Down Expand Up @@ -509,7 +509,7 @@ mod tests {
let inscription = inscription("text/plain", [0; MAX_STANDARD_TX_WEIGHT as usize]);
let satpoint = None;
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();

let error = batch::Plan {
satpoint,
Expand Down Expand Up @@ -550,7 +550,7 @@ mod tests {
let inscription = inscription("text/plain", [0; MAX_STANDARD_TX_WEIGHT as usize]);
let satpoint = None;
let commit_address = change(0);
let reveal_address = recipient_as_address();
let reveal_address = recipient_address();

let batch::Transactions { reveal_tx, .. } = batch::Plan {
satpoint,
Expand Down Expand Up @@ -605,7 +605,7 @@ mod tests {
wallet_inscriptions.insert(parent_info.location, vec![parent]);

let commit_address = change(1);
let reveal_addresses = vec![recipient_as_address()];
let reveal_addresses = vec![recipient_address()];

let inscriptions = vec![
InscriptionTemplate {
Expand Down Expand Up @@ -719,9 +719,9 @@ mod tests {

let commit_address = change(1);
let reveal_addresses = vec![
recipient_as_address(),
recipient_as_address(),
recipient_as_address(),
recipient_address(),
recipient_address(),
recipient_address(),
];

let inscriptions = vec![
Expand Down Expand Up @@ -864,7 +864,7 @@ mod tests {
];

let commit_address = change(1);
let reveal_addresses = vec![recipient_as_address()];
let reveal_addresses = vec![recipient_address()];

let error = batch::Plan {
satpoint: None,
Expand Down Expand Up @@ -941,7 +941,7 @@ mod tests {
];

let commit_address = change(1);
let reveal_addresses = vec![recipient_as_address(), recipient_as_address()];
let reveal_addresses = vec![recipient_address(), recipient_address()];

let _ = batch::Plan {
satpoint: None,
Expand Down Expand Up @@ -980,7 +980,7 @@ mod tests {
];

let commit_address = change(1);
let reveal_addresses = vec![recipient_as_address()];
let reveal_addresses = vec![recipient_address()];

let error = batch::Plan {
satpoint: None,
Expand Down Expand Up @@ -1025,9 +1025,9 @@ mod tests {

let commit_address = change(1);
let reveal_addresses = vec![
recipient_as_address(),
recipient_as_address(),
recipient_as_address(),
recipient_address(),
recipient_address(),
recipient_address(),
];

let inscriptions = vec![
Expand Down Expand Up @@ -1098,9 +1098,9 @@ mod tests {

let commit_address = change(1);
let reveal_addresses = vec![
recipient_as_address(),
recipient_as_address(),
recipient_as_address(),
recipient_address(),
recipient_address(),
recipient_address(),
];

let inscriptions = vec![
Expand Down
Loading

0 comments on commit 46611bd

Please sign in to comment.