|
10 | 10 | // licenses.
|
11 | 11 |
|
12 | 12 | use bitcoin::blockdata::script::Script;
|
| 13 | +use bitcoin::psbt::PartiallySignedTransaction; |
13 | 14 | use bitcoin::secp256k1::{All, Secp256k1};
|
14 | 15 |
|
| 16 | +use crate::FeeRate; |
15 | 17 | use miniscript::{MiniscriptKey, Satisfier, ToPublicKey};
|
16 | 18 |
|
17 | 19 | // MSB of the nSequence. If set there's no consensus-constraint, so it must be disabled when
|
@@ -137,6 +139,38 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for Older {
|
137 | 139 |
|
138 | 140 | pub(crate) type SecpCtx = Secp256k1<All>;
|
139 | 141 |
|
| 142 | +/// Trait for extracting fee amount and rate information from a `PartiallySignedTransaction`. |
| 143 | +pub trait FeeInfo { |
| 144 | + /// The total transaction fee amount, sum of input amounts minus sum of output amounts, in Sats. |
| 145 | + fn fee_amount(&self) -> u64; |
| 146 | + |
| 147 | + /// The transaction's fee rate. This value will only be accurate if calculated AFTER the |
| 148 | + /// `PartiallySignedTransaction` is finalized and all witness/signature data is added to the |
| 149 | + /// transaction. |
| 150 | + fn fee_rate(&self) -> FeeRate; |
| 151 | +} |
| 152 | + |
| 153 | +impl FeeInfo for PartiallySignedTransaction { |
| 154 | + fn fee_amount(&self) -> u64 { |
| 155 | + let input_amount = &self |
| 156 | + .inputs |
| 157 | + .iter() |
| 158 | + .fold(0, |acc, i| acc + i.witness_utxo.as_ref().unwrap().value); |
| 159 | + let output_amount = &self |
| 160 | + .unsigned_tx |
| 161 | + .output |
| 162 | + .iter() |
| 163 | + .fold(0, |acc, o| acc + o.value); |
| 164 | + input_amount - output_amount |
| 165 | + } |
| 166 | + |
| 167 | + fn fee_rate(&self) -> FeeRate { |
| 168 | + let fee_amount = self.fee_amount(); |
| 169 | + let weight = self.clone().extract_tx().weight(); |
| 170 | + FeeRate::from_wu(fee_amount, weight) |
| 171 | + } |
| 172 | +} |
| 173 | + |
140 | 174 | #[cfg(test)]
|
141 | 175 | mod test {
|
142 | 176 | use super::{
|
|
0 commit comments