From 46602143fe5df0ebd29a598d3a62a7b58a19ad8f Mon Sep 17 00:00:00 2001 From: veeso Date: Fri, 26 Jul 2024 20:02:51 +0200 Subject: [PATCH] fix: transaction fee calcs --- src/utils/fees.rs | 5 +++++ src/wallet/builder/rune.rs | 13 +++---------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/utils/fees.rs b/src/utils/fees.rs index fdf93ef..616a73c 100644 --- a/src/utils/fees.rs +++ b/src/utils/fees.rs @@ -91,6 +91,11 @@ pub fn estimate_transaction_fees( current_fee_rate.fee_vb(vbytes as u64).unwrap() } +pub fn calculate_transaction_fees(transaction: &Transaction, current_fee_rate: FeeRate) -> Amount { + let vbytes = transaction.vsize(); + current_fee_rate.fee_vb(vbytes as u64).unwrap() +} + fn estimate_vbytes( inputs: usize, script_type: ScriptType, diff --git a/src/wallet/builder/rune.rs b/src/wallet/builder/rune.rs index 62bf2c2..541513f 100644 --- a/src/wallet/builder/rune.rs +++ b/src/wallet/builder/rune.rs @@ -3,9 +3,8 @@ use bitcoin::transaction::Version; use bitcoin::{Address, Amount, FeeRate, ScriptBuf, Transaction, TxIn, TxOut}; use ordinals::{Edict, Etching, RuneId, Runestone as OrdRunestone}; -use crate::fees::estimate_transaction_fees; +use crate::fees::calculate_transaction_fees; use crate::wallet::builder::TxInputInfo; -use crate::wallet::ScriptType; use crate::{OrdError, OrdTransactionBuilder}; /// Postage amount for rune transaction. @@ -129,13 +128,7 @@ impl OrdTransactionBuilder { output: outputs, }; - let fee_amount = estimate_transaction_fees( - ScriptType::P2TR, - unsigned_tx.input.len(), - args.fee_rate, - &None, - unsigned_tx.output.clone(), - ); + let fee_amount = calculate_transaction_fees(&unsigned_tx, args.fee_rate); let change_amount = args .input_amount() .checked_sub(fee_amount + RUNE_POSTAGE * 2) @@ -161,7 +154,7 @@ mod tests { use bitcoin::{Network, OutPoint, PrivateKey, PublicKey, Txid}; use super::*; - use crate::wallet::LocalSigner; + use crate::wallet::{LocalSigner, ScriptType}; use crate::Wallet; #[tokio::test]