Skip to content

Commit

Permalink
Fix fee calculation for batch inscribe on same sat (#2785)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Nov 30, 2023
1 parent 6e07786 commit b4746b8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/subcommand/wallet/inscribe/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,12 @@ impl Batch {

let commit_tx_address = Address::p2tr_tweaked(taproot_spend_info.output_key(), chain.network());

let total_postage = self.postage * u64::try_from(self.inscriptions.len()).unwrap();
let total_postage = match self.mode {
Mode::SameSat => self.postage,
Mode::SharedOutput | Mode::SeparateOutputs => {
self.postage * u64::try_from(self.inscriptions.len()).unwrap()
}
};

let mut reveal_inputs = vec![OutPoint::null()];
let mut reveal_outputs = self
Expand All @@ -291,8 +296,8 @@ impl Batch {
.map(|destination| TxOut {
script_pubkey: destination.script_pubkey(),
value: match self.mode {
Mode::SeparateOutputs | Mode::SameSat => self.postage.to_sat(),
Mode::SharedOutput => total_postage.to_sat(),
Mode::SeparateOutputs => self.postage.to_sat(),
Mode::SharedOutput | Mode::SameSat => total_postage.to_sat(),
},
})
.collect::<Vec<TxOut>>();
Expand Down
53 changes: 53 additions & 0 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1686,3 +1686,56 @@ fn batch_inscribe_with_sat_arg_fails_if_wrong_mode() {
.expected_stderr("error: `sat` can only be set in `same-sat` mode\n")
.run_and_extract_stdout();
}

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

let set_fee_rate = 1.0;

let output = CommandBuilder::new(format!("--index-sats wallet inscribe --fee-rate {set_fee_rate} --batch batch.yaml"))
.write("inscription.txt", "Hello World")
.write("tulip.png", [0; 555])
.write("meow.wav", [0; 2048])
.write(
"batch.yaml",
"mode: same-sat\nsat: 5000111111\ninscriptions:\n- file: inscription.txt\n- file: tulip.png\n- file: meow.wav\n"
)
.rpc_server(&rpc_server)
.run_and_deserialize_output::<Inscribe>();

let commit_tx = &rpc_server.mempool()[0];
let mut fee = 0;
for input in &commit_tx.input {
fee += rpc_server
.get_utxo_amount(&input.previous_output)
.unwrap()
.to_sat();
}
for output in &commit_tx.output {
fee -= output.value;
}
let fee_rate = fee as f64 / commit_tx.vsize() as f64;
pretty_assert_eq!(fee_rate, set_fee_rate);

let reveal_tx = &rpc_server.mempool()[1];
let mut fee = 0;
for input in &reveal_tx.input {
fee += &commit_tx.output[input.previous_output.vout as usize].value;
}
for output in &reveal_tx.output {
fee -= output.value;
}
let fee_rate = fee as f64 / reveal_tx.vsize() as f64;
pretty_assert_eq!(fee_rate, set_fee_rate);

assert_eq!(
ord::FeeRate::try_from(set_fee_rate)
.unwrap()
.fee(commit_tx.vsize() + reveal_tx.vsize())
.to_sat(),
output.total_fees
);
}

0 comments on commit b4746b8

Please sign in to comment.