ref(example_cli): add typedefs to reduce type complexity

- Add typedefs to model the result of functions `planned_utxos`
and `init`

- Add new struct `CreateTxChange` to hold any change info
resulting from `create_tx`

These changes help resolve clippy::type_complexity
This commit is contained in:
vmammal 2023-10-28 22:53:37 -04:00
parent 64a90192d9
commit 4679ca1df7
No known key found for this signature in database

View File

@ -188,7 +188,12 @@ impl core::fmt::Display for Keychain {
} }
} }
#[allow(clippy::type_complexity)] pub struct CreateTxChange {
pub index_changeset: keychain::ChangeSet<Keychain>,
pub change_keychain: Keychain,
pub index: u32,
}
pub fn create_tx<A: Anchor, O: ChainOracle>( pub fn create_tx<A: Anchor, O: ChainOracle>(
graph: &mut KeychainTxGraph<A>, graph: &mut KeychainTxGraph<A>,
chain: &O, chain: &O,
@ -196,10 +201,7 @@ pub fn create_tx<A: Anchor, O: ChainOracle>(
cs_algorithm: CoinSelectionAlgo, cs_algorithm: CoinSelectionAlgo,
address: Address, address: Address,
value: u64, value: u64,
) -> anyhow::Result<( ) -> anyhow::Result<(Transaction, Option<CreateTxChange>)>
Transaction,
Option<(keychain::ChangeSet<Keychain>, (Keychain, u32))>,
)>
where where
O::Error: std::error::Error + Send + Sync + 'static, O::Error: std::error::Error + Send + Sync + 'static,
{ {
@ -391,7 +393,11 @@ where
} }
let change_info = if selection_meta.drain_value.is_some() { let change_info = if selection_meta.drain_value.is_some() {
Some((changeset, (internal_keychain, change_index))) Some(CreateTxChange {
index_changeset: changeset,
change_keychain: internal_keychain,
index: change_index,
})
} else { } else {
None None
}; };
@ -399,35 +405,34 @@ where
Ok((transaction, change_info)) Ok((transaction, change_info))
} }
#[allow(clippy::type_complexity)] // Alias the elements of `Result` of `planned_utxos`
pub type PlannedUtxo<K, A> = (bdk_tmp_plan::Plan<K>, FullTxOut<A>);
pub fn planned_utxos<A: Anchor, O: ChainOracle, K: Clone + bdk_tmp_plan::CanDerive>( pub fn planned_utxos<A: Anchor, O: ChainOracle, K: Clone + bdk_tmp_plan::CanDerive>(
graph: &KeychainTxGraph<A>, graph: &KeychainTxGraph<A>,
chain: &O, chain: &O,
assets: &bdk_tmp_plan::Assets<K>, assets: &bdk_tmp_plan::Assets<K>,
) -> Result<Vec<(bdk_tmp_plan::Plan<K>, FullTxOut<A>)>, O::Error> { ) -> Result<Vec<PlannedUtxo<K, A>>, O::Error> {
let chain_tip = chain.get_chain_tip()?; let chain_tip = chain.get_chain_tip()?;
let outpoints = graph.index.outpoints().iter().cloned(); let outpoints = graph.index.outpoints().iter().cloned();
graph graph
.graph() .graph()
.try_filter_chain_unspents(chain, chain_tip, outpoints) .try_filter_chain_unspents(chain, chain_tip, outpoints)
.filter_map( .filter_map(|r| -> Option<Result<PlannedUtxo<K, A>, _>> {
#[allow(clippy::type_complexity)] let (k, i, full_txo) = match r {
|r| -> Option<Result<(bdk_tmp_plan::Plan<K>, FullTxOut<A>), _>> { Err(err) => return Some(Err(err)),
let (k, i, full_txo) = match r { Ok(((k, i), full_txo)) => (k, i, full_txo),
Err(err) => return Some(Err(err)), };
Ok(((k, i), full_txo)) => (k, i, full_txo), let desc = graph
}; .index
let desc = graph .keychains()
.index .get(&k)
.keychains() .expect("keychain must exist")
.get(&k) .at_derivation_index(i)
.expect("keychain must exist") .expect("i can't be hardened");
.at_derivation_index(i) let plan = bdk_tmp_plan::plan_satisfaction(&desc, assets)?;
.expect("i can't be hardened"); Some(Ok((plan, full_txo)))
let plan = bdk_tmp_plan::plan_satisfaction(&desc, assets)?; })
Some(Ok((plan, full_txo)))
},
)
.collect() .collect()
} }
@ -597,7 +602,12 @@ where
let (tx, change_info) = let (tx, change_info) =
create_tx(graph, chain, keymap, coin_select, address, value)?; create_tx(graph, chain, keymap, coin_select, address, value)?;
if let Some((index_changeset, (change_keychain, index))) = change_info { if let Some(CreateTxChange {
index_changeset,
change_keychain,
index,
}) = change_info
{
// We must first persist to disk the fact that we've got a new address from the // We must first persist to disk the fact that we've got a new address from the
// change keychain so future scans will find the tx we're about to broadcast. // change keychain so future scans will find the tx we're about to broadcast.
// If we're unable to persist this, then we don't want to broadcast. // If we're unable to persist this, then we don't want to broadcast.
@ -646,17 +656,19 @@ where
} }
} }
#[allow(clippy::type_complexity)] // Alias the `Result` of `init`
pub fn init<CS: clap::Subcommand, S: clap::Args, C>( pub type InitialState<CS, S, C> = (
db_magic: &[u8],
db_default_path: &str,
) -> anyhow::Result<(
Args<CS, S>, Args<CS, S>,
KeyMap, KeyMap,
KeychainTxOutIndex<Keychain>, KeychainTxOutIndex<Keychain>,
Mutex<Database<C>>, Mutex<Database<C>>,
C, C,
)> );
pub fn init<CS: clap::Subcommand, S: clap::Args, C>(
db_magic: &[u8],
db_default_path: &str,
) -> anyhow::Result<InitialState<CS, S, C>>
where where
C: Default + Append + Serialize + DeserializeOwned, C: Default + Append + Serialize + DeserializeOwned,
{ {