corepc_types/v28/raw_transactions/
into.rs

1// SPDX-License-Identifier: CC0-1.0
2
3use bitcoin::{Amount, Txid, Wtxid};
4
5// TODO: Use explicit imports?
6use super::*;
7
8impl SubmitPackage {
9    /// Converts version specific type to a version in-specific, more strongly typed type.
10    pub fn into_model(self) -> Result<model::SubmitPackage, SubmitPackageError> {
11        use SubmitPackageError as E;
12
13        let mut tx_results = BTreeMap::new();
14        for (k, v) in self.tx_results {
15            let wtxid = k.parse::<Wtxid>().map_err(E::TxResultKey)?;
16            let result = v.into_model().map_err(E::TxResultValue)?;
17            tx_results.insert(wtxid, result);
18        }
19
20        let replaced_transactions = self
21            .replaced_transactions
22            .iter()
23            .map(|tx| tx.parse::<Txid>().map_err(E::ReplaceTransactions))
24            .collect::<Result<Vec<_>, _>>()?;
25
26        Ok(model::SubmitPackage {
27            package_msg: self.package_msg,
28            tx_results,
29            replaced_transactions,
30        })
31    }
32}
33
34impl SubmitPackageTxResult {
35    /// Converts version specific type to a version in-specific, more strongly typed type.
36    pub fn into_model(self) -> Result<model::SubmitPackageTxResult, SubmitPackageTxResultError> {
37        use SubmitPackageTxResultError as E;
38
39        let txid = self.txid.parse::<Txid>().map_err(E::Txid)?;
40        let other_wtxid =
41            self.other_wtxid.map(|s| s.parse::<Wtxid>().map_err(E::OtherWtxid)).transpose()?;
42        let vsize = self.vsize.map(|vsize| crate::to_u32(vsize, "vsize")).transpose()?;
43        let fees = self.fees.map(|fees| fees.into_model().map_err(E::Fees)).transpose()?;
44
45        Ok(model::SubmitPackageTxResult { txid, other_wtxid, vsize, fees, error: self.error })
46    }
47}
48
49impl SubmitPackageTxResultFees {
50    /// Converts version specific type to a version in-specific, more strongly typed type.
51    pub fn into_model(
52        self,
53    ) -> Result<model::SubmitPackageTxResultFees, SubmitPackageTxResultFeesError> {
54        use SubmitPackageTxResultFeesError as E;
55
56        let base_fee = Amount::from_btc(self.base_fee).map_err(E::BaseFee)?;
57        let effective_fee_rate = self
58            .effective_fee_rate
59            .map(|f| crate::btc_per_kb(f).map_err(E::EffectiveFeeRate))
60            .transpose()?
61            .flatten();
62        let effective_includes = self
63            .effective_includes
64            .unwrap_or_default()
65            .into_iter()
66            .map(|s| s.parse::<Wtxid>().map_err(E::EffectiveIncludes))
67            .collect::<Result<Vec<_>, _>>()?;
68
69        Ok(model::SubmitPackageTxResultFees { base_fee, effective_fee_rate, effective_includes })
70    }
71}