Skip to content

Commit

Permalink
fixup: Return concrete types from find_incomplete_coinswaps
Browse files Browse the repository at this point in the history
  • Loading branch information
GeneFerneau committed Aug 19, 2021
1 parent 58c0f8b commit ee48eae
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 37 deletions.
50 changes: 36 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod wallet_sync;
use wallet_sync::Wallet;

mod contracts;
use contracts::read_locktime_from_contract;
use contracts::{read_locktime_from_contract, SwapCoin};

mod error;
mod maker_protocol;
Expand Down Expand Up @@ -171,29 +171,51 @@ fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option<bool>) {
let incomplete_coinswaps = wallet.find_incomplete_coinswaps(&rpc).unwrap();
if incomplete_coinswaps.len() > 0 {
println!("= incomplete coinswaps =");
for (hashvalue, utxo_swapcoins) in incomplete_coinswaps {
let balance: Amount = utxo_swapcoins
for (hashvalue, (utxo_incoming_swapcoins, utxo_outgoing_swapcoins)) in incomplete_coinswaps
{
let balance: Amount = utxo_incoming_swapcoins
.iter()
.fold(Amount::ZERO, |acc, us| acc + us.0.amount);
.zip(utxo_outgoing_swapcoins.iter())
.fold(Amount::ZERO, |acc, (in_us, out_us)| {
acc + in_us.0.amount + out_us.0.amount
});
println!(
"{:16} {:8} {:8} {:<15} {:<7} value",
"outpoint", "type", "preimage", "locktime/blocks", "conf",
);
for (utxo, swapcoin) in utxo_swapcoins {
let txid = utxo.txid.to_hex();
for ((in_utxo, in_swapcoin), (out_utxo, out_swapcoin)) in utxo_incoming_swapcoins
.iter()
.zip(utxo_outgoing_swapcoins.iter())
{
let in_txid = in_utxo.txid.to_hex();
let out_txid = out_utxo.txid.to_hex();

#[rustfmt::skip]
println!("{}{}{}:{} {:8} {:8} {:^15} {:<7} {}",
if long_form { &in_txid } else {&in_txid[0..6] },
if long_form { "" } else { ".." },
if long_form { &"" } else { &in_txid[58..64] },
in_utxo.vout,
in_swapcoin.contract_type(),
if in_swapcoin.is_known() { "known" } else { "unknown" },
read_locktime_from_contract(&in_swapcoin.get_contract_redeemscript())
.expect("unable to read locktime from contract"),
in_utxo.confirmations,
in_utxo.amount
);

#[rustfmt::skip]
println!("{}{}{}:{} {:8} {:8} {:^15} {:<7} {}",
if long_form { &txid } else {&txid[0..6] },
if long_form { &out_txid } else {&out_txid[0..6] },
if long_form { "" } else { ".." },
if long_form { &"" } else { &txid[58..64] },
utxo.vout,
swapcoin.contract_type(),
if swapcoin.is_known() { "known" } else { "unknown" },
read_locktime_from_contract(&swapcoin.get_contract_redeemscript())
if long_form { &"" } else { &out_txid[58..64] },
out_utxo.vout,
out_swapcoin.contract_type(),
if out_swapcoin.is_known() { "known" } else { "unknown" },
read_locktime_from_contract(&out_swapcoin.get_contract_redeemscript())
.expect("unable to read locktime from contract"),
utxo.confirmations,
utxo.amount
out_utxo.confirmations,
out_utxo.amount
);
}
println!(
Expand Down
75 changes: 52 additions & 23 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,16 @@ impl Wallet {
pub fn find_incomplete_coinswaps(
&self,
rpc: &Client,
) -> Result<HashMap<Hash160, Vec<(ListUnspentResultEntry, &dyn SwapCoin)>>, Error> {
) -> Result<
HashMap<
Hash160,
(
Vec<(ListUnspentResultEntry, &IncomingSwapCoin)>,
Vec<(ListUnspentResultEntry, &OutgoingSwapCoin)>,
),
>,
Error,
> {
rpc.call::<Value>("lockunspent", &[Value::Bool(true)])
.map_err(|e| Error::Rpc(e))?;

Expand All @@ -806,8 +815,24 @@ impl Wallet {
);
//TODO make this read_hashvalue_from_contract() a struct function of WalletCoinSwap

let mut incomplete_swapcoin_groups =
HashMap::<Hash160, Vec<(ListUnspentResultEntry, &dyn SwapCoin)>>::new();
let mut incomplete_swapcoin_groups = HashMap::<
Hash160,
(
Vec<(ListUnspentResultEntry, &IncomingSwapCoin)>,
Vec<(ListUnspentResultEntry, &OutgoingSwapCoin)>,
),
>::new();
let get_hashvalue = |s: &dyn SwapCoin| {
if s.is_known() {
return None;
}
let swapcoin_hashvalue = read_hashvalue_from_contract(&s.get_contract_redeemscript())
.expect("unable to read hashvalue from contract_redeemscript");
if completed_coinswap_hashvalues.contains(&swapcoin_hashvalue) {
return None;
}
Some(swapcoin_hashvalue)
};
for utxo in rpc.list_unspent(None, None, None, None, None)? {
if utxo.descriptor.is_none() {
continue;
Expand All @@ -817,27 +842,31 @@ impl Wallet {
} else {
continue;
};
let swapcoin: &dyn SwapCoin =
if let Some(s) = self.find_incoming_swapcoin(multisig_redeemscript) {
s
} else if let Some(s) = self.find_outgoing_swapcoin(multisig_redeemscript) {
s
} else {
continue;
};
if swapcoin.is_known() {
continue;
}
let swapcoin_hashvalue =
read_hashvalue_from_contract(&swapcoin.get_contract_redeemscript())
.expect("unable to read hashvalue from contract_redeemscript");
if completed_coinswap_hashvalues.contains(&swapcoin_hashvalue) {
if let Some(s) = self.find_incoming_swapcoin(multisig_redeemscript) {
if let Some(swapcoin_hashvalue) = get_hashvalue(s) {
incomplete_swapcoin_groups
.entry(swapcoin_hashvalue)
.or_insert((
Vec::<(ListUnspentResultEntry, &IncomingSwapCoin)>::new(),
Vec::<(ListUnspentResultEntry, &OutgoingSwapCoin)>::new(),
))
.0
.push((utxo, s));
}
} else if let Some(s) = self.find_outgoing_swapcoin(multisig_redeemscript) {
if let Some(swapcoin_hashvalue) = get_hashvalue(s) {
incomplete_swapcoin_groups
.entry(swapcoin_hashvalue)
.or_insert((
Vec::<(ListUnspentResultEntry, &IncomingSwapCoin)>::new(),
Vec::<(ListUnspentResultEntry, &OutgoingSwapCoin)>::new(),
))
.1
.push((utxo, s));
}
} else {
continue;
}
incomplete_swapcoin_groups
.entry(swapcoin_hashvalue)
.or_insert(Vec::<(ListUnspentResultEntry, &dyn SwapCoin)>::new())
.push((utxo, swapcoin));
};
}
Ok(incomplete_swapcoin_groups)
}
Expand Down

0 comments on commit ee48eae

Please sign in to comment.