Skip to content

Commit

Permalink
Add --commit-fee-rate for setting inscribe commit transaction fee r…
Browse files Browse the repository at this point in the history
…ate (ordinals#1490)
  • Loading branch information
andrewtoth committed Feb 6, 2023
1 parent 387bb79 commit f2d8d0d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Preview {
subcommand: Subcommand::Wallet(super::wallet::Wallet::Inscribe(
super::wallet::inscribe::Inscribe {
fee_rate: FeeRate::try_from(1.0).unwrap(),
commit_fee_rate: None,
file,
no_backup: true,
satpoint: None,
Expand Down
97 changes: 93 additions & 4 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub(crate) struct Inscribe {
help = "Use fee rate of <FEE_RATE> sats/vB"
)]
pub(crate) fee_rate: FeeRate,
#[clap(
long,
help = "Use <COMMIT_FEE_RATE> sats/vbyte for commit transaction.\nDefaults to <FEE_RATE> if unset."
)]
pub(crate) commit_fee_rate: Option<FeeRate>,
#[clap(help = "Inscribe sat with contents of <FILE>")]
pub(crate) file: PathBuf,
#[clap(long, help = "Do not back up recovery key.")]
Expand Down Expand Up @@ -69,6 +74,7 @@ impl Inscribe {
utxos.clone(),
commit_tx_change,
reveal_tx_destination,
self.commit_fee_rate.unwrap_or(self.fee_rate),
self.fee_rate,
)?;

Expand Down Expand Up @@ -131,7 +137,8 @@ impl Inscribe {
utxos: BTreeMap<OutPoint, Amount>,
change: [Address; 2],
destination: Address,
fee_rate: FeeRate,
commit_fee_rate: FeeRate,
reveal_fee_rate: FeeRate,
) -> Result<(Transaction, Transaction, TweakedKeyPair)> {
let satpoint = if let Some(satpoint) = satpoint {
satpoint
Expand Down Expand Up @@ -188,7 +195,7 @@ impl Inscribe {

let (_, reveal_fee) = Self::build_reveal_transaction(
&control_block,
fee_rate,
reveal_fee_rate,
OutPoint::null(),
TxOut {
script_pubkey: destination.script_pubkey(),
Expand All @@ -203,7 +210,7 @@ impl Inscribe {
utxos,
commit_tx_address.clone(),
change,
fee_rate,
commit_fee_rate,
reveal_fee + TransactionBuilder::TARGET_POSTAGE,
)?;

Expand All @@ -216,7 +223,7 @@ impl Inscribe {

let (mut reveal_tx, fee) = Self::build_reveal_transaction(
&control_block,
fee_rate,
reveal_fee_rate,
OutPoint {
txid: unsigned_commit_tx.txid(),
vout: vout.try_into().unwrap(),
Expand Down Expand Up @@ -368,6 +375,7 @@ mod tests {
[commit_address, change(1)],
reveal_address,
FeeRate::try_from(1.0).unwrap(),
FeeRate::try_from(1.0).unwrap(),
)
.unwrap();

Expand Down Expand Up @@ -397,6 +405,7 @@ mod tests {
[commit_address, change(1)],
reveal_address,
FeeRate::try_from(1.0).unwrap(),
FeeRate::try_from(1.0).unwrap(),
)
.unwrap();

Expand Down Expand Up @@ -430,6 +439,7 @@ mod tests {
[commit_address, change(1)],
reveal_address,
FeeRate::try_from(1.0).unwrap(),
FeeRate::try_from(1.0).unwrap(),
)
.unwrap_err()
.to_string();
Expand Down Expand Up @@ -470,6 +480,7 @@ mod tests {
[commit_address, change(1)],
reveal_address,
FeeRate::try_from(1.0).unwrap(),
FeeRate::try_from(1.0).unwrap(),
)
.is_ok())
}
Expand Down Expand Up @@ -504,9 +515,86 @@ mod tests {
[commit_address, change(1)],
reveal_address,
FeeRate::try_from(fee_rate).unwrap(),
FeeRate::try_from(fee_rate).unwrap(),
)
.unwrap();

let sig_vbytes = 17;
let fee = FeeRate::try_from(fee_rate)
.unwrap()
.fee(commit_tx.vsize() + sig_vbytes)
.to_sat();

let reveal_value = commit_tx
.output
.iter()
.map(|o| o.value)
.reduce(|acc, i| acc + i)
.unwrap();

assert_eq!(reveal_value, 20_000 - fee);

let fee = FeeRate::try_from(fee_rate)
.unwrap()
.fee(reveal_tx.vsize())
.to_sat();

assert_eq!(
reveal_tx.output[0].value,
20_000 - fee - (20_000 - commit_tx.output[0].value),
);
}

#[test]
fn inscribe_with_commit_fee_rate() {
let utxos = vec![
(outpoint(1), Amount::from_sat(10_000)),
(outpoint(2), Amount::from_sat(20_000)),
];
let mut inscriptions = BTreeMap::new();
inscriptions.insert(
SatPoint {
outpoint: outpoint(1),
offset: 0,
},
inscription_id(1),
);

let inscription = inscription("text/plain", "ord");
let satpoint = None;
let commit_address = change(0);
let reveal_address = recipient();
let commit_fee_rate = 3.3;
let fee_rate = 1.0;

let (commit_tx, reveal_tx, _private_key) = Inscribe::create_inscription_transactions(
satpoint,
inscription,
inscriptions,
bitcoin::Network::Signet,
utxos.into_iter().collect(),
[commit_address, change(1)],
reveal_address,
FeeRate::try_from(commit_fee_rate).unwrap(),
FeeRate::try_from(fee_rate).unwrap(),
)
.unwrap();

let sig_vbytes = 17;
let fee = FeeRate::try_from(commit_fee_rate)
.unwrap()
.fee(commit_tx.vsize() + sig_vbytes)
.to_sat();

let reveal_value = commit_tx
.output
.iter()
.map(|o| o.value)
.reduce(|acc, i| acc + i)
.unwrap();

assert_eq!(reveal_value, 20_000 - fee);

let fee = FeeRate::try_from(fee_rate)
.unwrap()
.fee(reveal_tx.vsize())
Expand Down Expand Up @@ -536,6 +624,7 @@ mod tests {
[commit_address, change(1)],
reveal_address,
FeeRate::try_from(1.0).unwrap(),
FeeRate::try_from(1.0).unwrap(),
)
.unwrap_err()
.to_string();
Expand Down
62 changes: 58 additions & 4 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,75 @@ fn inscribe_with_fee_rate() {
.rpc_server(&rpc_server)
.output::<Inscribe>();

let tx = &rpc_server.mempool()[0];
let tx1 = &rpc_server.mempool()[0];
let mut fee = 0;
for input in &tx.input {
for input in &tx1.input {
fee += rpc_server
.get_utxo_amount(&input.previous_output)
.unwrap()
.to_sat();
}
for output in &tx.output {
for output in &tx1.output {
fee -= output.value;
}

let fee_rate = fee as f64 / tx.vsize() as f64;
let fee_rate = fee as f64 / tx1.vsize() as f64;

pretty_assert_eq!(fee_rate, 2.0);

let tx2 = &rpc_server.mempool()[1];
let mut fee = 0;
for input in &tx2.input {
fee += &tx1.output[input.previous_output.vout as usize].value;
}
for output in &tx2.output {
fee -= output.value;
}

let fee_rate = fee as f64 / tx2.vsize() as f64;

pretty_assert_eq!(fee_rate, 2.0);
}

#[test]
fn inscribe_with_commit_fee_rate() {
let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);
rpc_server.mine_blocks(1);

CommandBuilder::new("--index-sats wallet inscribe degenerate.png --commit-fee-rate 2.0")
.write("degenerate.png", [1; 520])
.rpc_server(&rpc_server)
.output::<Inscribe>();

let tx1 = &rpc_server.mempool()[0];
let mut fee = 0;
for input in &tx1.input {
fee += rpc_server
.get_utxo_amount(&input.previous_output)
.unwrap()
.to_sat();
}
for output in &tx1.output {
fee -= output.value;
}

let fee_rate = fee as f64 / tx1.vsize() as f64;

pretty_assert_eq!(fee_rate, 2.0);

let tx2 = &rpc_server.mempool()[1];
let mut fee = 0;
for input in &tx2.input {
fee += &tx1.output[input.previous_output.vout as usize].value;
}
for output in &tx2.output {
fee -= output.value;
}

let fee_rate = fee as f64 / tx2.vsize() as f64;

pretty_assert_eq!(fee_rate, 1.0);
}

#[test]
Expand Down

0 comments on commit f2d8d0d

Please sign in to comment.