From: Alekos Filini Date: Thu, 6 Aug 2020 11:09:39 +0000 (+0200) Subject: [wallet] Add a `TxBuilder` struct to simplify `create_tx()`'s interface X-Git-Tag: 0.1.0-beta.1~19 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/struct.SegwitCodeLengthError.html?a=commitdiff_plain;h=d514091c782215d533862ebe8adbdb5930f1310a;p=bdk-cli [wallet] Add a `TxBuilder` struct to simplify `create_tx()`'s interface --- diff --git a/src/cli.rs b/src/cli.rs index 788aa69..c946f22 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -13,7 +13,7 @@ use bitcoin::{Address, OutPoint}; use crate::error::Error; use crate::types::ScriptType; -use crate::Wallet; +use crate::{TxBuilder, Wallet}; fn parse_addressee(s: &str) -> Result<(Address, u64), String> { let parts: Vec<_> = s.split(":").collect(); @@ -326,29 +326,37 @@ where .map(|s| parse_addressee(s)) .collect::, _>>() .map_err(|s| Error::Generic(s))?; - let send_all = sub_matches.is_present("send_all"); - let fee_rate = sub_matches - .value_of("fee_rate") - .map(|s| f32::from_str(s).unwrap()) - .unwrap_or(1.0); - let utxos = sub_matches - .values_of("utxos") - .map(|s| s.map(|i| parse_outpoint(i).unwrap()).collect()); - let unspendable = sub_matches - .values_of("unspendable") - .map(|s| s.map(|i| parse_outpoint(i).unwrap()).collect()); - let policy: Option<_> = sub_matches - .value_of("policy") - .map(|s| serde_json::from_str::>>(&s).unwrap()); - - let result = wallet.create_tx( - addressees, - send_all, - fee_rate * 1e-5, - policy, - utxos, - unspendable, - )?; + let mut tx_builder = TxBuilder::from_addressees(addressees); + + if sub_matches.is_present("send_all") { + tx_builder.send_all(); + } + if let Some(fee_rate) = sub_matches.value_of("fee_rate") { + let fee_rate = f32::from_str(fee_rate).map_err(|s| Error::Generic(s.to_string()))?; + tx_builder.fee_rate(fee_rate); + } + if let Some(utxos) = sub_matches.values_of("utxos") { + let utxos = utxos + .map(|i| parse_outpoint(i)) + .collect::, _>>() + .map_err(|s| Error::Generic(s.to_string()))?; + tx_builder.utxos(utxos); + } + + if let Some(unspendable) = sub_matches.values_of("unspendable") { + let unspendable = unspendable + .map(|i| parse_outpoint(i)) + .collect::, _>>() + .map_err(|s| Error::Generic(s.to_string()))?; + tx_builder.unspendable(unspendable); + } + if let Some(policy) = sub_matches.value_of("policy") { + let policy = serde_json::from_str::>>(&policy) + .map_err(|s| Error::Generic(s.to_string()))?; + tx_builder.policy_path(policy); + } + + let result = wallet.create_tx(&tx_builder)?; Ok(Some(format!( "{:#?}\nPSBT: {}", result.1,