Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: wallet balance api #101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
410 changes: 259 additions & 151 deletions src/wallet/api.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/wallet/direct_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
//TODO this search within a search could get very slow
// Filter out fidelity bonds. Use `wallet.redeem_fidelity()` function to spend fidelity bond coins.
let list_unspent_result = self
.list_unspent_from_wallet(true, true)?
.list_unspent_from_wallet(None)?

Check warning on line 123 in src/wallet/direct_send.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet/direct_send.rs#L123

Added line #L123 was not covered by tests
.into_iter()
.filter(|(_, info)| !matches!(info, UTXOSpendInfo::FidelityBondCoin { .. }))
.collect::<Vec<_>>();
Expand Down
9 changes: 7 additions & 2 deletions src/wallet/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,14 @@ impl Wallet {
) -> Result<u32, WalletError> {
let (index, fidelity_addr, fidelity_pubkey) = self.get_next_fidelity_address(locktime)?;

let all_utxos = self.get_all_utxo()?;

let mut seed_coin_utxo = self.list_descriptor_utxo_unspent_from_wallet(Some(&all_utxos))?;
let mut swap_coin_utxo = self.list_swap_coin_unspent_from_wallet(Some(&all_utxos))?;
seed_coin_utxo.append(&mut swap_coin_utxo);

// Fetch utxos, filter out existing fidelity coins
let mut unspents = self
.list_unspent_from_wallet(false, false)?
let mut unspents = seed_coin_utxo
.into_iter()
.filter(|(_, spend_info)| !matches!(spend_info, UTXOSpendInfo::FidelityBondCoin { .. }))
.collect::<Vec<_>>();
Expand Down
21 changes: 13 additions & 8 deletions src/wallet/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
) -> Result<Vec<f32>, WalletError> {
for _ in 0..100000 {
let mut knives = (1..count)
.map(|_| OsRng.next_u32() as f32 / u32::MAX as f32)
.map(|_| (OsRng.next_u32() as f32) / (u32::MAX as f32))
.collect::<Vec<f32>>();
knives.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));

Expand All @@ -84,7 +84,7 @@

if fractions
.iter()
.all(|f| *f * (total_amount as f32) > lower_limit as f32)
.all(|f| *f * (total_amount as f32) > (lower_limit as f32))
{
return Ok(fractions);
}
Expand All @@ -102,7 +102,7 @@
//there should always be enough to pay miner fees
)?
.iter()
.map(|f| (*f * total_amount as f32) as u64)
.map(|f| (*f * (total_amount as f32)) as u64)
.collect::<Vec<u64>>();

//rounding errors mean usually 1 or 2 satoshis are lost, add them back
Expand Down Expand Up @@ -203,9 +203,8 @@
destinations: &[Address],
fee_rate: u64,
change_address: &Address,
utxos: &mut dyn Iterator<Item = (Txid, u32, u64)>,
//utxos item is (txid, vout, value)
//utxos should be sorted by size, largest first
utxos: &mut dyn Iterator<Item = (Txid, u32, u64)>, //utxos item is (txid, vout, value)
//utxos should be sorted by size, largest first
) -> Result<Option<CreateFundingTxesResult>, WalletError> {
let mut funding_txes = Vec::<Transaction>::new();
let mut payment_output_positions = Vec::<u32>::new();
Expand All @@ -215,7 +214,7 @@
let mut destinations_iter = destinations.iter();
let first_tx_input = utxos.next().unwrap();

for _ in 0..(destinations.len() - 2) {
for _ in 0..destinations.len() - 2 {

Check warning on line 217 in src/wallet/funding.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet/funding.rs#L217

Added line #L217 was not covered by tests
let (txid, vout, value) = utxos.next().unwrap();

let mut outputs = HashMap::<String, Amount>::new();
Expand Down Expand Up @@ -418,7 +417,13 @@
//this function will pick the top most valuable UTXOs and use them
//to create funding transactions

let mut list_unspent_result = self.list_unspent_from_wallet(false, false)?;
let all_utxos = self.get_all_utxo()?;

Check warning on line 420 in src/wallet/funding.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet/funding.rs#L420

Added line #L420 was not covered by tests

let mut seed_coin_utxo = self.list_descriptor_utxo_unspent_from_wallet(Some(&all_utxos))?;
let mut swap_coin_utxo = self.list_swap_coin_unspent_from_wallet(Some(&all_utxos))?;
seed_coin_utxo.append(&mut swap_coin_utxo);

Check warning on line 424 in src/wallet/funding.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet/funding.rs#L422-L424

Added lines #L422 - L424 were not covered by tests

let mut list_unspent_result = seed_coin_utxo;

Check warning on line 426 in src/wallet/funding.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet/funding.rs#L426

Added line #L426 was not covered by tests
if list_unspent_result.len() < destinations.len() {
return Err(WalletError::Protocol(
"Not enough UTXOs to create this many funding txes".to_string(),
Expand Down
1 change: 1 addition & 0 deletions src/wallet/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl WalletStore {
#[cfg(test)]
mod tests {
use super::*;

use bitcoind::tempfile::tempdir;

#[test]
Expand Down
147 changes: 136 additions & 11 deletions tests/abort1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use coinswap::{

mod test_framework;
use log::{info, warn};
use std::{sync::Arc, thread, time::Duration};
use std::{assert_eq, sync::Arc, thread, time::Duration};
use test_framework::*;

/// Abort 1: TAKER Drops After Full Setup.
Expand Down Expand Up @@ -84,13 +84,34 @@ async fn test_stop_taker_after_setup() {
// confirm balances
test_framework.generate_1_block();

let mut all_utxos = taker.read().unwrap().get_wallet().get_all_utxo().unwrap();

// Get the original balances
let org_taker_balance = taker
let org_taker_balance_fidelity = taker
.read()
.unwrap()
.get_wallet()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let org_taker_balance_descriptor_utxo = taker
.read()
.unwrap()
.get_wallet()
.balance(false, false)
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let org_taker_balance_swap_coins = taker
.read()
.unwrap()
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let org_taker_balance_live_contract = taker
.read()
.unwrap()
.get_wallet()
.balance_live_contract(Some(&all_utxos))
.unwrap();
let org_taker_balance = org_taker_balance_descriptor_utxo + org_taker_balance_swap_coins;

// ---- Start Servers and attempt Swap ----

Expand Down Expand Up @@ -123,12 +144,39 @@ async fn test_stop_taker_after_setup() {
let org_maker_balances = makers
.iter()
.map(|maker| {
maker
all_utxos = maker.get_wallet().read().unwrap().get_all_utxo().unwrap();
let maker_balance_fidelity = maker
.get_wallet()
.read()
.unwrap()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let maker_balance_descriptor_utxo = maker
.get_wallet()
.read()
.unwrap()
.balance(false, false)
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let maker_balance_swap_coins = maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let maker_balance_live_contract = maker
.get_wallet()
.read()
.unwrap()
.balance_live_contract(Some(&all_utxos))
.unwrap();
assert_eq!(maker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
maker_balance_descriptor_utxo,
Amount::from_btc(0.14999).unwrap()
);
assert_eq!(maker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(maker_balance_live_contract, Amount::from_btc(0.0).unwrap());
maker_balance_descriptor_utxo + maker_balance_swap_coins
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -167,28 +215,105 @@ async fn test_stop_taker_after_setup() {
// All pending swapcoins are cleared now.
assert_eq!(taker.read().unwrap().get_wallet().get_swapcoins_count(), 0);

all_utxos = taker.read().unwrap().get_wallet().get_all_utxo().unwrap();

// Check everybody looses mining fees of contract txs.
let taker_balance = taker
let taker_balance_fidelity = taker
.read()
.unwrap()
.get_wallet()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let taker_balance_descriptor_utxo = taker
.read()
.unwrap()
.get_wallet()
.balance(false, false)
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let taker_balance_swap_coins = taker
.read()
.unwrap()
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let taker_balance_live_contract = taker
.read()
.unwrap()
.get_wallet()
.balance_live_contract(Some(&all_utxos))
.unwrap();
let taker_balance = taker_balance_descriptor_utxo + taker_balance_swap_coins;

assert_eq!(org_taker_balance - taker_balance, Amount::from_sat(4227));
assert_eq!(org_taker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
org_taker_balance_descriptor_utxo,
Amount::from_btc(0.15).unwrap()
);
assert_eq!(org_taker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(
org_taker_balance_live_contract,
Amount::from_btc(0.0).unwrap()
);
assert_eq!(taker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
taker_balance_descriptor_utxo,
Amount::from_btc(0.14995773).unwrap()
);
assert_eq!(taker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(taker_balance_live_contract, Amount::from_btc(0.0).unwrap());

makers
.iter()
.zip(org_maker_balances.iter())
.for_each(|(maker, org_balance)| {
let new_balance = maker
all_utxos = maker.get_wallet().read().unwrap().get_all_utxo().unwrap();
let maker_balance_fidelity = maker
.get_wallet()
.read()
.unwrap()
.balance(false, false)
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
log::info!("Org Balance: {}", *org_balance);
log::info!("New_balance: {}", new_balance);
let maker_balance_descriptor_utxo = maker
.get_wallet()
.read()
.unwrap()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let maker_balance_swap_coins = maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let maker_balance_live_contract = maker
.get_wallet()
.read()
.unwrap()
.balance_live_contract(Some(&all_utxos))
.unwrap();
let new_balance = maker
.get_wallet()
.read()
.unwrap()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap()
+ maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();

assert_eq!(*org_balance - new_balance, Amount::from_sat(4227));

assert_eq!(maker_balance_fidelity, Amount::from_btc(0.05).unwrap());
assert_eq!(
maker_balance_descriptor_utxo,
Amount::from_btc(0.14994773).unwrap()
);
assert_eq!(maker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(maker_balance_live_contract, Amount::from_btc(0.0).unwrap());
});

info!("All checks successful. Terminating integration test case");
Expand Down
Loading
Loading