From 1852f05a1faebc66da5111fa89e1199f8f836dc2 Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Wed, 22 Jan 2025 17:58:53 +0530 Subject: [PATCH 1/9] add dedicated struct for utxo balances --- src/bin/maker-cli.rs | 17 +------------- src/maker/rpc/messages.rs | 47 +++++++++++++++++++++++---------------- src/maker/rpc/server.rs | 32 +++++++++++++------------- 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/bin/maker-cli.rs b/src/bin/maker-cli.rs index e40333bc..dc70128b 100644 --- a/src/bin/maker-cli.rs +++ b/src/bin/maker-cli.rs @@ -37,14 +37,8 @@ enum Commands { ListUtxoContract, /// Lists fidelity bond utxos. ListUtxoFidelity, - /// Get total wallet balance, excluding Fidelity bonds. + /// Get total wallet balance. GetBalance, - /// Get total balance received via incoming swaps. - GetBalanceSwap, - /// Get total balances of HTLC contract utxos. - GetBalanceContract, - /// Get total amount locked in fidelity bonds. - GetBalanceFidelity, /// Gets a new bitcoin receiving address GetNewAddress, /// Send Bitcoin to an external address and returns the txid. @@ -89,12 +83,6 @@ fn main() -> Result<(), MakerError> { Commands::ListUtxoContract => { send_rpc_req(stream, RpcMsgReq::ContractUtxo)?; } - Commands::GetBalanceContract => { - send_rpc_req(stream, RpcMsgReq::ContractBalance)?; - } - Commands::GetBalanceFidelity => { - send_rpc_req(stream, RpcMsgReq::FidelityBalance)?; - } Commands::ListUtxoFidelity => { send_rpc_req(stream, RpcMsgReq::FidelityUtxo)?; } @@ -104,9 +92,6 @@ fn main() -> Result<(), MakerError> { Commands::ListUtxo => { send_rpc_req(stream, RpcMsgReq::Utxo)?; } - Commands::GetBalanceSwap => { - send_rpc_req(stream, RpcMsgReq::SwapBalance)?; - } Commands::ListUtxoSwap => { send_rpc_req(stream, RpcMsgReq::SwapUtxo)?; } diff --git a/src/maker/rpc/messages.rs b/src/maker/rpc/messages.rs index c1baf2e6..7e3008f4 100644 --- a/src/maker/rpc/messages.rs +++ b/src/maker/rpc/messages.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, fmt::Display}; -use bitcoin::Txid; +use bitcoin::{Amount, Txid}; use bitcoind::bitcoincore_rpc::json::ListUnspentResultEntry; use serde::{Deserialize, Serialize}; use std::path::PathBuf; @@ -25,12 +25,6 @@ pub enum RpcMsgReq { FidelityUtxo, /// Request to retreive the total spenable balance in wallet. Balance, - /// Request to retrieve the total swap balance in wallet. - SwapBalance, - /// Request to retrieve the total balance in the contract pool. - ContractBalance, - /// Request to retrieve the total balance in the fidelity pool. - FidelityBalance, /// Request for generating a new wallet address. NewAddress, /// Request to send funds to a specific address. @@ -56,6 +50,30 @@ pub enum RpcMsgReq { SyncWallet, } +/// Represents balance of each utxo type. +pub struct Balance { + /// Seed balance. + pub regular: Amount, + /// Incoming swap balance. + pub swap: Amount, + /// Unfinished timelock contract balance. + pub contract: Amount, + /// Amount locked in Fidelity bonds. + pub fidelity: Amount, + /// Spendable amount in wallet (seed + swap balance). + pub spendable: Amount, +} + +impl Display for Balance { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Seed balance: {}, Swap balance: {}, Contract balance: {}, Fidelity balance: {}, Spendable balance: {}", + self.regular.to_sat(), self.swap.to_sat(), self.contract.to_sat(), self.fidelity.to_sat(), self.regular.to_sat() + self.swap.to_sat() + ) + } +} + /// Enum representing RPC message responses. /// /// These messages are sent in response to RPC requests and carry the results @@ -84,14 +102,8 @@ pub enum RpcMsgResp { /// List of UTXOs in the contract pool. utxos: Vec, }, - /// Response containing the total balance in the seed pool. - SeedBalanceResp(u64), - /// Response containing the total balance in the swap pool. - SwapBalanceResp(u64), - /// Response containing the total balance in the contract pool. - ContractBalanceResp(u64), - /// Response containing the total balance in the fidelity pool. - FidelityBalanceResp(u64), + /// Response containing the total wallet balance. + TotalBalanceResp(Balance), /// Response containing a newly generated wallet address. NewAddressResp(String), /// Response to a send-to-address request. @@ -115,10 +127,7 @@ impl Display for RpcMsgResp { match self { Self::Pong => write!(f, "Pong"), Self::NewAddressResp(addr) => write!(f, "{}", addr), - Self::SeedBalanceResp(bal) => write!(f, "{} sats", bal), - Self::ContractBalanceResp(bal) => write!(f, "{} sats", bal), - Self::SwapBalanceResp(bal) => write!(f, "{} sats", bal), - Self::FidelityBalanceResp(bal) => write!(f, "{} sats", bal), + Self::TotalBalanceResp(balance) => write!(f, "{}", balance), Self::UtxoResp { utxos } => write!(f, "{:#?}", utxos), Self::SwapUtxoResp { utxos } => write!(f, "{:#?}", utxos), Self::FidelityUtxoResp { utxos } => write!(f, "{:#?}", utxos), diff --git a/src/maker/rpc/server.rs b/src/maker/rpc/server.rs index c336a5e0..0e990e5c 100644 --- a/src/maker/rpc/server.rs +++ b/src/maker/rpc/server.rs @@ -10,7 +10,11 @@ use bitcoin::{Address, Amount}; use super::messages::RpcMsgReq; use crate::{ - maker::{error::MakerError, rpc::messages::RpcMsgResp, Maker}, + maker::{ + error::MakerError, + rpc::messages::{Balance, RpcMsgResp}, + Maker, + }, utill::{get_tor_hostname, read_message, send_message, ConnectionType, HEART_BEAT_INTERVAL}, wallet::{Destination, SendAmount}, }; @@ -63,24 +67,22 @@ fn handle_request(maker: &Arc, socket: &mut TcpStream) -> Result<(), Make .collect::>(); RpcMsgResp::SwapUtxoResp { utxos } } - RpcMsgReq::ContractBalance => { - let balance = maker.get_wallet().read()?.balance_live_contract(None)?; - RpcMsgResp::ContractBalanceResp(balance.to_sat()) - } - RpcMsgReq::FidelityBalance => { - let balance = maker.get_wallet().read()?.balance_fidelity_bonds(None)?; - RpcMsgResp::FidelityBalanceResp(balance.to_sat()) - } RpcMsgReq::Balance => { - let balance = maker.get_wallet().read()?.spendable_balance(None)?; - RpcMsgResp::SeedBalanceResp(balance.to_sat()) - } - RpcMsgReq::SwapBalance => { - let balance = maker + let regular = maker.get_wallet().read()?.spendable_balance(None)?; + let contract = maker.get_wallet().read()?.balance_live_contract(None)?; + let fidelity = maker.get_wallet().read()?.balance_fidelity_bonds(None)?; + let swap = maker .get_wallet() .read()? .balance_incoming_swap_coins(None)?; - RpcMsgResp::SwapBalanceResp(balance.to_sat()) + let spendable = Amount::from_sat(regular.to_sat() + swap.to_sat()); + RpcMsgResp::TotalBalanceResp(Balance { + regular, + swap, + contract, + fidelity, + spendable, + }) } RpcMsgReq::NewAddress => { let new_address = maker.get_wallet().write()?.get_next_external_address()?; From aa7770f865abf38fad9c06cd5d703f11092f0acf Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Sat, 25 Jan 2025 00:26:40 +0530 Subject: [PATCH 2/9] address review comments --- src/bin/maker-cli.rs | 7 ++++++- src/bin/taker.rs | 2 +- src/maker/rpc/messages.rs | 36 ++++++++++++++++++++---------------- src/maker/rpc/server.rs | 5 ++++- src/wallet/api.rs | 2 +- tests/abort1.rs | 4 +++- tests/abort2_case1.rs | 4 +++- tests/abort2_case2.rs | 4 +++- tests/abort2_case3.rs | 4 +++- tests/abort3_case1.rs | 4 +++- tests/abort3_case2.rs | 4 +++- tests/abort3_case3.rs | 4 +++- tests/malice1.rs | 4 +++- tests/malice2.rs | 4 +++- tests/standard_swap.rs | 4 +++- tests/test_framework/mod.rs | 16 ++++++++++++---- 16 files changed, 74 insertions(+), 34 deletions(-) diff --git a/src/bin/maker-cli.rs b/src/bin/maker-cli.rs index dc70128b..0084d01d 100644 --- a/src/bin/maker-cli.rs +++ b/src/bin/maker-cli.rs @@ -37,7 +37,12 @@ enum Commands { ListUtxoContract, /// Lists fidelity bond utxos. ListUtxoFidelity, - /// Get total wallet balance. + /// Get total wallet balances of different categories. + /// regular: All single signature regular wallet coins. + /// swap: All 2of2 multisig coins received in swaps. + /// contract: All live contract transaction balance locked in timelocks. If you see value in this field, you have unfinished or malfinished swaps. You can claim them back with recover command. + /// fidelity: All coins locked in fidelity bonds. + /// spendable: Spendable amount in wallet (seed + swap balance). GetBalance, /// Gets a new bitcoin receiving address GetNewAddress, diff --git a/src/bin/taker.rs b/src/bin/taker.rs index 8201d147..a90bf93c 100644 --- a/src/bin/taker.rs +++ b/src/bin/taker.rs @@ -174,7 +174,7 @@ fn main() -> Result<(), TakerError> { println!("{:#?}", utxos); } Commands::GetBalanceContract => { - let balance = taker.get_wallet().balance_live_contract(None)?; + let balance = taker.get_wallet().balance_live_timelock_contract(None)?; println!("{:?}", balance); } Commands::GetBalanceSwap => { diff --git a/src/maker/rpc/messages.rs b/src/maker/rpc/messages.rs index 7e3008f4..2ab05057 100644 --- a/src/maker/rpc/messages.rs +++ b/src/maker/rpc/messages.rs @@ -3,6 +3,7 @@ use std::{collections::HashMap, fmt::Display}; use bitcoin::{Amount, Txid}; use bitcoind::bitcoincore_rpc::json::ListUnspentResultEntry; use serde::{Deserialize, Serialize}; +use serde_json::{json, to_string_pretty}; use std::path::PathBuf; use crate::wallet::FidelityBond; @@ -50,30 +51,20 @@ pub enum RpcMsgReq { SyncWallet, } -/// Represents balance of each utxo type. +/// Represents total wallet balances of different categories. pub struct Balance { - /// Seed balance. + /// All single signature regular wallet coins (seed balance). pub regular: Amount, - /// Incoming swap balance. + /// All 2of2 multisig coins received in swaps. pub swap: Amount, - /// Unfinished timelock contract balance. + /// All live contract transaction balance locked in timelocks. pub contract: Amount, - /// Amount locked in Fidelity bonds. + /// All coins locked in fidelity bonds. pub fidelity: Amount, /// Spendable amount in wallet (seed + swap balance). pub spendable: Amount, } -impl Display for Balance { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "Seed balance: {}, Swap balance: {}, Contract balance: {}, Fidelity balance: {}, Spendable balance: {}", - self.regular.to_sat(), self.swap.to_sat(), self.contract.to_sat(), self.fidelity.to_sat(), self.regular.to_sat() + self.swap.to_sat() - ) - } -} - /// Enum representing RPC message responses. /// /// These messages are sent in response to RPC requests and carry the results @@ -127,7 +118,20 @@ impl Display for RpcMsgResp { match self { Self::Pong => write!(f, "Pong"), Self::NewAddressResp(addr) => write!(f, "{}", addr), - Self::TotalBalanceResp(balance) => write!(f, "{}", balance), + Self::TotalBalanceResp(balance) => { + write!( + f, + "{}", + to_string_pretty(&json!({ + "regular": balance.regular.to_sat(), + "swap": balance.swap.to_sat(), + "contract": balance.contract.to_sat(), + "fidelity": balance.fidelity.to_sat(), + "spendable": balance.regular.to_sat() + balance.swap.to_sat(), + })) + .unwrap() + ) + } Self::UtxoResp { utxos } => write!(f, "{:#?}", utxos), Self::SwapUtxoResp { utxos } => write!(f, "{:#?}", utxos), Self::FidelityUtxoResp { utxos } => write!(f, "{:#?}", utxos), diff --git a/src/maker/rpc/server.rs b/src/maker/rpc/server.rs index 0e990e5c..4a4cba6b 100644 --- a/src/maker/rpc/server.rs +++ b/src/maker/rpc/server.rs @@ -69,7 +69,10 @@ fn handle_request(maker: &Arc, socket: &mut TcpStream) -> Result<(), Make } RpcMsgReq::Balance => { let regular = maker.get_wallet().read()?.spendable_balance(None)?; - let contract = maker.get_wallet().read()?.balance_live_contract(None)?; + let contract = maker + .get_wallet() + .read()? + .balance_live_timelock_contract(None)?; let fidelity = maker.get_wallet().read()?.balance_fidelity_bonds(None)?; let swap = maker .get_wallet() diff --git a/src/wallet/api.rs b/src/wallet/api.rs index 1868bdb4..1567f599 100644 --- a/src/wallet/api.rs +++ b/src/wallet/api.rs @@ -312,7 +312,7 @@ impl Wallet { /// Calculates live contract balance of the wallet. /// Optionally takes in a list of UTXOs to reduce rpc call. If None is provided, the full list is fetched from core rpc. - pub fn balance_live_contract( + pub fn balance_live_timelock_contract( &self, all_utxos: Option<&Vec>, ) -> Result { diff --git a/tests/abort1.rs b/tests/abort1.rs index d05a6abc..efe2b192 100644 --- a/tests/abort1.rs +++ b/tests/abort1.rs @@ -97,7 +97,9 @@ fn test_stop_taker_after_setup() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/abort2_case1.rs b/tests/abort2_case1.rs index d69d2e42..5d16fc9f 100644 --- a/tests/abort2_case1.rs +++ b/tests/abort2_case1.rs @@ -94,7 +94,9 @@ fn test_abort_case_2_move_on_with_other_makers() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/abort2_case2.rs b/tests/abort2_case2.rs index 62609e4f..2d3e4ce1 100644 --- a/tests/abort2_case2.rs +++ b/tests/abort2_case2.rs @@ -100,7 +100,9 @@ fn test_abort_case_2_recover_if_no_makers_found() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/abort2_case3.rs b/tests/abort2_case3.rs index c42872f7..f872ce18 100644 --- a/tests/abort2_case3.rs +++ b/tests/abort2_case3.rs @@ -97,7 +97,9 @@ fn maker_drops_after_sending_senders_sigs() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/abort3_case1.rs b/tests/abort3_case1.rs index 00dcf2d9..01c28af1 100644 --- a/tests/abort3_case1.rs +++ b/tests/abort3_case1.rs @@ -101,7 +101,9 @@ fn abort3_case1_close_at_contract_sigs_for_recvr_and_sender() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/abort3_case2.rs b/tests/abort3_case2.rs index ea8883d2..1509ca0f 100644 --- a/tests/abort3_case2.rs +++ b/tests/abort3_case2.rs @@ -95,7 +95,9 @@ fn abort3_case2_close_at_contract_sigs_for_recvr() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/abort3_case3.rs b/tests/abort3_case3.rs index 83b4bf25..fa883664 100644 --- a/tests/abort3_case3.rs +++ b/tests/abort3_case3.rs @@ -93,7 +93,9 @@ fn abort3_case3_close_at_hash_preimage_handover() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/malice1.rs b/tests/malice1.rs index 4bb7ae8a..bb6cea03 100644 --- a/tests/malice1.rs +++ b/tests/malice1.rs @@ -90,7 +90,9 @@ fn malice1_taker_broadcast_contract_prematurely() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/malice2.rs b/tests/malice2.rs index 8af68ce0..2242b1f7 100644 --- a/tests/malice2.rs +++ b/tests/malice2.rs @@ -89,7 +89,9 @@ fn malice2_maker_broadcast_contract_prematurely() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/standard_swap.rs b/tests/standard_swap.rs index 5a5c8bac..e1cab971 100644 --- a/tests/standard_swap.rs +++ b/tests/standard_swap.rs @@ -85,7 +85,9 @@ fn test_standard_coinswap() { .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); assert_eq!(seed_balance, Amount::from_btc(0.14999).unwrap()); assert_eq!(fidelity_balance, Amount::from_btc(0.05).unwrap()); diff --git a/tests/test_framework/mod.rs b/tests/test_framework/mod.rs index a32bab47..6e4c2b4a 100644 --- a/tests/test_framework/mod.rs +++ b/tests/test_framework/mod.rs @@ -210,7 +210,9 @@ pub fn fund_and_verify_taker( .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); // TODO: Think about this: utxo_count*utxo_amt. assert_eq!(seed_balance, Amount::from_btc(0.15).unwrap()); @@ -261,7 +263,9 @@ pub fn fund_and_verify_maker( .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); // TODO: Think about this: utxo_count*utxo_amt. assert_eq!(seed_balance, Amount::from_btc(0.20).unwrap()); @@ -288,7 +292,9 @@ pub fn verify_swap_results( let swapcoin_balance = wallet .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); let spendable_balance = seed_balance + swapcoin_balance; @@ -333,7 +339,9 @@ pub fn verify_swap_results( let swapcoin_balance = wallet .balance_incoming_swap_coins(Some(&all_utxos)) .unwrap(); - let live_contract_balance = wallet.balance_live_contract(Some(&all_utxos)).unwrap(); + let live_contract_balance = wallet + .balance_live_timelock_contract(Some(&all_utxos)) + .unwrap(); let spendable_balance = seed_balance + swapcoin_balance; From 9c5f7e4345a2d4878c90647ad475782c6a48e036 Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Sun, 26 Jan 2025 03:03:40 +0530 Subject: [PATCH 3/9] enhance(taker.rs): pretty print total wallet balances by category --- src/bin/taker.rs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/bin/taker.rs b/src/bin/taker.rs index a90bf93c..e7a90b26 100644 --- a/src/bin/taker.rs +++ b/src/bin/taker.rs @@ -7,6 +7,7 @@ use coinswap::{ wallet::{Destination, RPCConfig, SendAmount}, }; use log::LevelFilter; +use serde_json::{json, to_string_pretty}; use std::{path::PathBuf, str::FromStr}; /// A simple command line app to operate as coinswap client. @@ -63,14 +64,12 @@ enum Commands { ListUtxoSwap, /// Lists all utxos that we need to claim via timelock. If you see entries in this list, do a `taker recover` to claim them. ListUtxoContract, - /// Get the total spendable wallet balance in sats (regular + swap utxos) + /// Get total wallet balances of different categories. + /// regular: All single signature regular wallet coins. + /// swap: All 2of2 multisig coins received in swaps. + /// contract: All live contract transaction balance locked in timelocks. If you see value in this field, you have unfinished or malfinished swaps. You can claim them back with recover command. + /// spendable: Spendable amount in wallet (regular + swap utxos). GetBalance, - /// Get Balance of all single sig regular wallet utxos. - GetBalanceRegular, - /// Get the total balance received in incoming swaps (sats) - GetBalanceSwap, - /// Get the total amount stuck in timelock contracts (sats) - GetBalanceContract, /// Returns a new address GetNewAddress, /// Send to an external wallet address. @@ -173,21 +172,21 @@ fn main() -> Result<(), TakerError> { .collect::>(); println!("{:#?}", utxos); } - Commands::GetBalanceContract => { - let balance = taker.get_wallet().balance_live_timelock_contract(None)?; - println!("{:?}", balance); - } - Commands::GetBalanceSwap => { - let balance = taker.get_wallet().balance_incoming_swap_coins(None)?; - println!("{:?}", balance); - } Commands::GetBalance => { - let balance = taker.get_wallet().spendable_balance(None)?; - println!("{:?}", balance); - } - Commands::GetBalanceRegular => { - let balance = taker.get_wallet().balance_descriptor_utxo(None)?; - println!("{:?}", balance); + let regular = taker.get_wallet().balance_descriptor_utxo(None)?; + let contract = taker.get_wallet().balance_live_timelock_contract(None)?; + let swap = taker.get_wallet().balance_incoming_swap_coins(None)?; + let spendable = taker.get_wallet().spendable_balance(None)?; + println!( + "{}", + to_string_pretty(&json!({ + "regular": regular.to_sat(), + "contract": contract.to_sat(), + "swap": swap.to_sat(), + "spendable": spendable.to_sat(), + })) + .unwrap() + ); } Commands::GetNewAddress => { let address = taker.get_wallet_mut().get_next_external_address()?; From fe296e2dfe8dd3e4b1cf83ed52fb08cd0858fd03 Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Tue, 28 Jan 2025 11:20:37 +0530 Subject: [PATCH 4/9] add Balances struct to wallet::api --- src/bin/maker-cli.rs | 4 ++-- src/bin/taker.rs | 17 +++++++---------- src/maker/rpc/messages.rs | 36 +++++++++++------------------------- src/maker/rpc/server.rs | 26 +++----------------------- src/wallet/api.rs | 31 +++++++++++++++++++++++++++++++ src/wallet/mod.rs | 2 +- 6 files changed, 55 insertions(+), 61 deletions(-) diff --git a/src/bin/maker-cli.rs b/src/bin/maker-cli.rs index 0084d01d..d353f8ef 100644 --- a/src/bin/maker-cli.rs +++ b/src/bin/maker-cli.rs @@ -38,11 +38,11 @@ enum Commands { /// Lists fidelity bond utxos. ListUtxoFidelity, /// Get total wallet balances of different categories. - /// regular: All single signature regular wallet coins. + /// regular: All single signature regular wallet coins (seed balance). /// swap: All 2of2 multisig coins received in swaps. /// contract: All live contract transaction balance locked in timelocks. If you see value in this field, you have unfinished or malfinished swaps. You can claim them back with recover command. /// fidelity: All coins locked in fidelity bonds. - /// spendable: Spendable amount in wallet (seed + swap balance). + /// spendable: Spendable amount in wallet (regular + swap balance). GetBalance, /// Gets a new bitcoin receiving address GetNewAddress, diff --git a/src/bin/taker.rs b/src/bin/taker.rs index e7a90b26..6592f18c 100644 --- a/src/bin/taker.rs +++ b/src/bin/taker.rs @@ -65,10 +65,10 @@ enum Commands { /// Lists all utxos that we need to claim via timelock. If you see entries in this list, do a `taker recover` to claim them. ListUtxoContract, /// Get total wallet balances of different categories. - /// regular: All single signature regular wallet coins. + /// regular: All single signature regular wallet coins (seed balance). /// swap: All 2of2 multisig coins received in swaps. /// contract: All live contract transaction balance locked in timelocks. If you see value in this field, you have unfinished or malfinished swaps. You can claim them back with recover command. - /// spendable: Spendable amount in wallet (regular + swap utxos). + /// spendable: Spendable amount in wallet (regular + swap balance). GetBalance, /// Returns a new address GetNewAddress, @@ -173,17 +173,14 @@ fn main() -> Result<(), TakerError> { println!("{:#?}", utxos); } Commands::GetBalance => { - let regular = taker.get_wallet().balance_descriptor_utxo(None)?; - let contract = taker.get_wallet().balance_live_timelock_contract(None)?; - let swap = taker.get_wallet().balance_incoming_swap_coins(None)?; - let spendable = taker.get_wallet().spendable_balance(None)?; + let balances = taker.get_wallet().get_balances()?; println!( "{}", to_string_pretty(&json!({ - "regular": regular.to_sat(), - "contract": contract.to_sat(), - "swap": swap.to_sat(), - "spendable": spendable.to_sat(), + "regular": balances.regular.to_sat(), + "contract": balances.contract.to_sat(), + "swap": balances.swap.to_sat(), + "spendable": balances.spendable.to_sat(), })) .unwrap() ); diff --git a/src/maker/rpc/messages.rs b/src/maker/rpc/messages.rs index 2ab05057..d36e0033 100644 --- a/src/maker/rpc/messages.rs +++ b/src/maker/rpc/messages.rs @@ -1,12 +1,12 @@ use std::{collections::HashMap, fmt::Display}; -use bitcoin::{Amount, Txid}; +use bitcoin::Txid; use bitcoind::bitcoincore_rpc::json::ListUnspentResultEntry; use serde::{Deserialize, Serialize}; use serde_json::{json, to_string_pretty}; use std::path::PathBuf; -use crate::wallet::FidelityBond; +use crate::wallet::{Balances, FidelityBond}; /// Enum representing RPC message requests. /// @@ -24,7 +24,7 @@ pub enum RpcMsgReq { ContractUtxo, /// Request to fetch UTXOs in the fidelity pool. FidelityUtxo, - /// Request to retreive the total spenable balance in wallet. + /// Request to retreive the total wallet balances of different categories. Balance, /// Request for generating a new wallet address. NewAddress, @@ -51,20 +51,6 @@ pub enum RpcMsgReq { SyncWallet, } -/// Represents total wallet balances of different categories. -pub struct Balance { - /// All single signature regular wallet coins (seed balance). - pub regular: Amount, - /// All 2of2 multisig coins received in swaps. - pub swap: Amount, - /// All live contract transaction balance locked in timelocks. - pub contract: Amount, - /// All coins locked in fidelity bonds. - pub fidelity: Amount, - /// Spendable amount in wallet (seed + swap balance). - pub spendable: Amount, -} - /// Enum representing RPC message responses. /// /// These messages are sent in response to RPC requests and carry the results @@ -93,8 +79,8 @@ pub enum RpcMsgResp { /// List of UTXOs in the contract pool. utxos: Vec, }, - /// Response containing the total wallet balance. - TotalBalanceResp(Balance), + /// Response containing the total wallet balances of different categories. + TotalBalanceResp(Balances), /// Response containing a newly generated wallet address. NewAddressResp(String), /// Response to a send-to-address request. @@ -118,16 +104,16 @@ impl Display for RpcMsgResp { match self { Self::Pong => write!(f, "Pong"), Self::NewAddressResp(addr) => write!(f, "{}", addr), - Self::TotalBalanceResp(balance) => { + Self::TotalBalanceResp(balances) => { write!( f, "{}", to_string_pretty(&json!({ - "regular": balance.regular.to_sat(), - "swap": balance.swap.to_sat(), - "contract": balance.contract.to_sat(), - "fidelity": balance.fidelity.to_sat(), - "spendable": balance.regular.to_sat() + balance.swap.to_sat(), + "regular": balances.regular.to_sat(), + "swap": balances.swap.to_sat(), + "contract": balances.contract.to_sat(), + "fidelity": balances.fidelity.to_sat(), + "spendable": balances.spendable.to_sat(), })) .unwrap() ) diff --git a/src/maker/rpc/server.rs b/src/maker/rpc/server.rs index 4a4cba6b..27b2b309 100644 --- a/src/maker/rpc/server.rs +++ b/src/maker/rpc/server.rs @@ -10,11 +10,7 @@ use bitcoin::{Address, Amount}; use super::messages::RpcMsgReq; use crate::{ - maker::{ - error::MakerError, - rpc::messages::{Balance, RpcMsgResp}, - Maker, - }, + maker::{error::MakerError, rpc::messages::RpcMsgResp, Maker}, utill::{get_tor_hostname, read_message, send_message, ConnectionType, HEART_BEAT_INTERVAL}, wallet::{Destination, SendAmount}, }; @@ -68,24 +64,8 @@ fn handle_request(maker: &Arc, socket: &mut TcpStream) -> Result<(), Make RpcMsgResp::SwapUtxoResp { utxos } } RpcMsgReq::Balance => { - let regular = maker.get_wallet().read()?.spendable_balance(None)?; - let contract = maker - .get_wallet() - .read()? - .balance_live_timelock_contract(None)?; - let fidelity = maker.get_wallet().read()?.balance_fidelity_bonds(None)?; - let swap = maker - .get_wallet() - .read()? - .balance_incoming_swap_coins(None)?; - let spendable = Amount::from_sat(regular.to_sat() + swap.to_sat()); - RpcMsgResp::TotalBalanceResp(Balance { - regular, - swap, - contract, - fidelity, - spendable, - }) + let balances = maker.get_wallet().read()?.get_balances()?; + RpcMsgResp::TotalBalanceResp(balances) } RpcMsgReq::NewAddress => { let new_address = maker.get_wallet().write()?.get_next_external_address()?; diff --git a/src/wallet/api.rs b/src/wallet/api.rs index 1567f599..781a5028 100644 --- a/src/wallet/api.rs +++ b/src/wallet/api.rs @@ -17,6 +17,7 @@ use bitcoin::{ Address, Amount, OutPoint, PublicKey, Script, ScriptBuf, Transaction, Txid, }; use bitcoind::bitcoincore_rpc::{bitcoincore_rpc_json::ListUnspentResultEntry, Client, RpcApi}; +use serde::{Deserialize, Serialize}; use std::path::Path; use crate::{ @@ -136,6 +137,21 @@ pub enum UTXOSpendInfo { FidelityBondCoin { index: u32, input_value: Amount }, } +/// Represents total wallet balances of different categories. +#[derive(Serialize, Deserialize, Debug)] +pub struct Balances { + /// All single signature regular wallet coins (seed balance). + pub regular: Amount, + /// All 2of2 multisig coins received in swaps. + pub swap: Amount, + /// All live contract transaction balance locked in timelocks. + pub contract: Amount, + /// All coins locked in fidelity bonds. + pub fidelity: Amount, + /// Spendable amount in wallet (regular + swap balance). + pub spendable: Amount, +} + impl Wallet { /// Initialize the wallet at a given path. /// @@ -290,6 +306,21 @@ impl Wallet { self.store.incoming_swapcoins.len() + self.store.outgoing_swapcoins.len() } + pub fn get_balances(&self) -> Result { + let regular = self.balance_descriptor_utxo(None)?; + let contract = self.balance_live_timelock_contract(None)?; + let swap = self.balance_incoming_swap_coins(None)?; + let fidelity = self.balance_fidelity_bonds(None)?; + let spendable = self.spendable_balance(None)?; + Ok(Balances { + regular, + swap, + contract, + fidelity, + spendable, + }) + } + /// Calculates the total spendable balance of the wallet. Includes all utxos except the fidelity bond. pub fn spendable_balance( &self, diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 7c3d5bea..a5f7a0da 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -9,7 +9,7 @@ mod rpc; mod storage; mod swapcoin; -pub(crate) use api::{UTXOSpendInfo, Wallet}; +pub(crate) use api::{Balances, UTXOSpendInfo, Wallet}; pub use direct_send::{Destination, SendAmount}; pub use error::WalletError; pub(crate) use fidelity::{fidelity_redeemscript, FidelityBond, FidelityError}; From aff133d36cdbb9b358d81344d28b009709cc75bc Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Fri, 31 Jan 2025 21:49:33 +0530 Subject: [PATCH 5/9] fix failing tests --- src/bin/maker-cli.rs | 4 ++-- src/bin/taker.rs | 4 ++-- tests/maker_cli.rs | 45 ++++++++++++++++++++++---------------------- tests/taker_cli.rs | 11 +++++++---- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/bin/maker-cli.rs b/src/bin/maker-cli.rs index d353f8ef..6420f5f5 100644 --- a/src/bin/maker-cli.rs +++ b/src/bin/maker-cli.rs @@ -43,7 +43,7 @@ enum Commands { /// contract: All live contract transaction balance locked in timelocks. If you see value in this field, you have unfinished or malfinished swaps. You can claim them back with recover command. /// fidelity: All coins locked in fidelity bonds. /// spendable: Spendable amount in wallet (regular + swap balance). - GetBalance, + GetBalances, /// Gets a new bitcoin receiving address GetNewAddress, /// Send Bitcoin to an external address and returns the txid. @@ -91,7 +91,7 @@ fn main() -> Result<(), MakerError> { Commands::ListUtxoFidelity => { send_rpc_req(stream, RpcMsgReq::FidelityUtxo)?; } - Commands::GetBalance => { + Commands::GetBalances => { send_rpc_req(stream, RpcMsgReq::Balance)?; } Commands::ListUtxo => { diff --git a/src/bin/taker.rs b/src/bin/taker.rs index 6592f18c..bd7a859e 100644 --- a/src/bin/taker.rs +++ b/src/bin/taker.rs @@ -69,7 +69,7 @@ enum Commands { /// swap: All 2of2 multisig coins received in swaps. /// contract: All live contract transaction balance locked in timelocks. If you see value in this field, you have unfinished or malfinished swaps. You can claim them back with recover command. /// spendable: Spendable amount in wallet (regular + swap balance). - GetBalance, + GetBalances, /// Returns a new address GetNewAddress, /// Send to an external wallet address. @@ -172,7 +172,7 @@ fn main() -> Result<(), TakerError> { .collect::>(); println!("{:#?}", utxos); } - Commands::GetBalance => { + Commands::GetBalances => { let balances = taker.get_wallet().get_balances()?; println!( "{}", diff --git a/tests/maker_cli.rs b/tests/maker_cli.rs index eb419f48..3abddf2e 100644 --- a/tests/maker_cli.rs +++ b/tests/maker_cli.rs @@ -3,6 +3,7 @@ use bitcoin::{Address, Amount}; use bitcoind::BitcoinD; use coinswap::utill::setup_logger; +use serde_json::{json, Value}; use std::{ fs, io::{BufRead, BufReader}, @@ -163,22 +164,19 @@ fn test_maker_cli() { // assert!(tor_addr.contains("onion:6102")); // Initial Balance checks - let seed_balance = maker_cli.execute_maker_cli(&["get-balance"]); - await_message(&rx, "RPC request received: Balance"); + let balances = maker_cli.execute_maker_cli(&["get-balances"]); + await_message(&rx, "RPC request received: Balances"); - let contract_balance = maker_cli.execute_maker_cli(&["get-balance-contract"]); - await_message(&rx, "RPC request received: ContractBalance"); - - let fidelity_balance = maker_cli.execute_maker_cli(&["get-balance-fidelity"]); - await_message(&rx, "RPC request received: FidelityBalance"); - - let swap_balance = maker_cli.execute_maker_cli(&["get-balance-swap"]); - await_message(&rx, "RPC request received: SwapBalance"); - - assert_eq!(seed_balance, "1000000 sats"); - assert_eq!(swap_balance, "0 sats"); - assert_eq!(fidelity_balance, "5000000 sats"); - assert_eq!(contract_balance, "0 sats"); + assert_eq!( + serde_json::from_str::(&balances).unwrap(), + json!({ + "regular": 1000000, + "swap": 0, + "contract": 0, + "fidelity": 5000000, + "spendable": 1000000 + }) + ); // Initial UTXO checks let all_utxos = maker_cli.execute_maker_cli(&["list-utxo"]); @@ -218,16 +216,17 @@ fn test_maker_cli() { generate_blocks(&maker_cli.bitcoind, 1); // Check balances - assert_eq!(maker_cli.execute_maker_cli(&["get-balance"]), "999000 sats"); - assert_eq!( - maker_cli.execute_maker_cli(&["get-balance-contract"]), - "0 sats" - ); + let balances = maker_cli.execute_maker_cli(&["get-balances"]); assert_eq!( - maker_cli.execute_maker_cli(&["get-balance-fidelity"]), - "5000000 sats" + serde_json::from_str::(&balances).unwrap(), + json!({ + "regular": 999000, + "swap": 0, + "contract": 0, + "fidelity": 5000000, + "spendable": 999000 + }) ); - assert_eq!(maker_cli.execute_maker_cli(&["get-balance-swap"]), "0 sats"); // Verify the seed UTXO count; other balance types remain unaffected when sending funds to an address. let seed_utxo = maker_cli.execute_maker_cli(&["list-utxo"]); diff --git a/tests/taker_cli.rs b/tests/taker_cli.rs index a0f91543..8c3dc5d9 100644 --- a/tests/taker_cli.rs +++ b/tests/taker_cli.rs @@ -2,6 +2,7 @@ use bitcoin::{address::NetworkChecked, Address, Amount}; use bitcoind::{bitcoincore_rpc::RpcApi, tempfile::env::temp_dir, BitcoinD}; +use serde_json::Value; use std::{fs, path::PathBuf, process::Command, str::FromStr}; mod test_framework; use test_framework::{generate_blocks, init_bitcoind, send_to_address}; @@ -94,9 +95,10 @@ fn test_taker_cli() { generate_blocks(bitcoind, 10); // Assert that total_balance & seed_balance must be 3 BTC - let spendable_balance = taker_cli.execute(&["get-balance"]); + let balances = taker_cli.execute(&["get-balances"]); + let balances = serde_json::from_str::(&balances).unwrap(); - assert_eq!("300000000 SAT", spendable_balance); + assert_eq!("300000000", balances["spendable"].to_string()); // Assert that total no of seed-utxos are 3. let all_utxos = taker_cli.execute(&["list-utxo"]); @@ -122,10 +124,11 @@ fn test_taker_cli() { generate_blocks(bitcoind, 10); // Assert the total_amount & seed_amount must be initial (balance -fee) - let spendable_balance = taker_cli.execute(&["get-balance"]); + let balances = taker_cli.execute(&["get-balances"]); + let balances = serde_json::from_str::(&balances).unwrap(); // Since the amount is sent back to our wallet, the transaction fee is deducted from the balance. - assert_eq!("299999000 SAT", spendable_balance); + assert_eq!("299999000", balances["spendable"].to_string()); // Assert that no of seed utxos are 2 let all_utxos = taker_cli.execute(&["list-utxo"]); From bb9523a5f9ac79f5f71b73889911b12b1e8308a3 Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Sat, 1 Feb 2025 15:26:48 +0530 Subject: [PATCH 6/9] rename Balance to Balances --- src/bin/maker-cli.rs | 2 +- src/maker/rpc/messages.rs | 2 +- src/maker/rpc/server.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bin/maker-cli.rs b/src/bin/maker-cli.rs index 6420f5f5..20d74dec 100644 --- a/src/bin/maker-cli.rs +++ b/src/bin/maker-cli.rs @@ -92,7 +92,7 @@ fn main() -> Result<(), MakerError> { send_rpc_req(stream, RpcMsgReq::FidelityUtxo)?; } Commands::GetBalances => { - send_rpc_req(stream, RpcMsgReq::Balance)?; + send_rpc_req(stream, RpcMsgReq::Balances)?; } Commands::ListUtxo => { send_rpc_req(stream, RpcMsgReq::Utxo)?; diff --git a/src/maker/rpc/messages.rs b/src/maker/rpc/messages.rs index d36e0033..5b3d13e9 100644 --- a/src/maker/rpc/messages.rs +++ b/src/maker/rpc/messages.rs @@ -25,7 +25,7 @@ pub enum RpcMsgReq { /// Request to fetch UTXOs in the fidelity pool. FidelityUtxo, /// Request to retreive the total wallet balances of different categories. - Balance, + Balances, /// Request for generating a new wallet address. NewAddress, /// Request to send funds to a specific address. diff --git a/src/maker/rpc/server.rs b/src/maker/rpc/server.rs index 27b2b309..eae59033 100644 --- a/src/maker/rpc/server.rs +++ b/src/maker/rpc/server.rs @@ -63,7 +63,7 @@ fn handle_request(maker: &Arc, socket: &mut TcpStream) -> Result<(), Make .collect::>(); RpcMsgResp::SwapUtxoResp { utxos } } - RpcMsgReq::Balance => { + RpcMsgReq::Balances => { let balances = maker.get_wallet().read()?.get_balances()?; RpcMsgResp::TotalBalanceResp(balances) } From 6414699a336db0301a770a504bdbd9a2559d51d9 Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Mon, 3 Feb 2025 01:09:32 +0530 Subject: [PATCH 7/9] update maker cli and taker tutorials --- docs/app demos/maker-cli.md | 88 ++++++++++++++++++++++++++++--------- docs/app demos/taker.md | 16 ++++--- 2 files changed, 77 insertions(+), 27 deletions(-) diff --git a/docs/app demos/maker-cli.md b/docs/app demos/maker-cli.md index 9f095ce7..e30d4d5d 100644 --- a/docs/app demos/maker-cli.md +++ b/docs/app demos/maker-cli.md @@ -58,10 +58,7 @@ OPTIONS: Print version information SUBCOMMANDS: - get-balance Retrieve the total wallet balance, excluding fidelity bonds - get-balance-contract Retrieve the balance of HTLC contract UTXOs - get-balance-fidelity Check the amount locked in fidelity bonds - get-balance-swap Get the balance received from incoming swaps + get-balances Retrieve the total wallet balances of different categories (sats) get-new-address Generate a new Bitcoin receiving address list-utxo List all UTXOs in the wallet, including fidelity bonds list-utxo-contract List HTLC contract UTXOs @@ -216,12 +213,18 @@ Since only one live fidelity bond is allowed at a time, this shows a single UTXO To check the balance of our fidelity UTXOs, use: ```bash -$ ./maker-cli get-balance-fidelity +$ ./maker-cli get-balances ``` **Output:** ```bash -50000 sats +{ + "regular": 1000000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 1000000 +} ``` This confirms the balance of our fidelity UTXOs matches the amount we set when creating the bond. @@ -247,9 +250,15 @@ Next, we’ll explore other UTXOs and balances in Coinswap. Since we have not done any coinswap yet, so we have no swap utxos yet and thus we would have no swap balances as can be verified by running the command: ```bash - $ ./maker-cli get-balance-swap +$ ./maker-cli get-balances - 0 sats +{ + "regular": 1000000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 1000000 +} ``` @@ -264,8 +273,15 @@ $ ./maker-cli list-utxo-contract As mentioned above -> we have not paritcipated in any coinswap till now, thus have no unsuccessfull coinswap currently -> thus we have no `contract utxos` and have no balance in this category as shown: ```bash -$ ./maker-cli get-balance-contract -0 sats +$ ./maker-cli get-balances + +{ + "regular": 1000000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 1000000 +} ``` @@ -335,11 +351,17 @@ The remaining balance after these transactions is: **949,000 sats** = **1,000,000 sats** (total funding) - **50,000 sats** (for the fidelity bond) - **1,000 sats** (mining fees). -We can verify this balance by running the `get-balance` command, which shows the total spendable balance (excluding the fidelity bond balance): +We can verify this balance by running the `get-balances` command, which shows the total wallet balances of different categories: ```bash -$ ./maker-cli get-balance -949,000 sats + $ ./maker-cli get-balances + { + "regular": 949000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 949000 + } ``` --- @@ -445,9 +467,15 @@ $ ./maker-cli list-utxo-fidelity }, ] -$ ./maker-cli get-balance-fidelity +$ ./maker-cli get-balances -50000 sats +{ + "regular": 949000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 949000 +} ``` > **NOTE**: Fidelity UTXOs are not used for spending purposes. We can only spend these UTXOs by using the `redeem_fidelity` command after the fidelity bond expires. This is why the UTXO list and balance remain unchanged. @@ -459,8 +487,14 @@ $ ./maker-cli get-balance-fidelity $ ./maker-cli list-utxo-swap [] -$ ./maker-cli get-balance-swap -0 sats +$ ./maker-cli get-balances +{ + "regular": 949000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 949000 +} ``` --- @@ -470,8 +504,14 @@ $ ./maker-cli get-balance-swap $ ./maker-cli list-utxo-contract [] -$ ./maker-cli get-balance-contract -0 sats +$ ./maker-cli get-balances +{ + "regular": 949000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 949000 +} ``` --- @@ -521,8 +561,14 @@ $ ./maker-cli list-utxo }, ] -$ ./maker-cli get-balance -938000 sats +$ ./maker-cli get-balances +{ + "regular": 938000, + "swap": 0, + "contract": 0, + "fidelity": 50000, + "spendable": 938000 +} ``` --- diff --git a/docs/app demos/taker.md b/docs/app demos/taker.md index 3b86f3a2..d1b2cd6c 100644 --- a/docs/app demos/taker.md +++ b/docs/app demos/taker.md @@ -68,9 +68,7 @@ OPTIONS: SUBCOMMANDS: do-coinswap Initiate the coinswap process fetch-offers Update the offerbook with current market offers and display them - get-balance Get the total spendable wallet balance (sats) - get-balance-contract Get the total amount stuck in HTLC contracts (sats) - get-balance-swap Get the total balance received from swaps (sats) + get-balances Retrieve the total wallet balances of different categories (sats) get-new-address Returns a new address help Print this message or the help of the given subcommand(s) list-utxo Lists all currently spendable utxos @@ -92,9 +90,15 @@ Now we can use a testnet4 faucet to send some coins to this address. You can fin Once you have some coins in your wallet, you can check your balance by running the following command: ```sh -$ taker -r 127.0.0.1:38332 -a user:pass get-balance - -10000000 SAT +$ taker -r 127.0.0.1:38332 -a user:pass get-balances + +{ + "regular": 10000000, + "swap": 0, + "contract": 0, + "fidelity": 0, + "spendable": 10000000 +} ``` Now we are ready to initate a coinswap. We are first going to sync the offer book to get a list of available makers. From 463721689ecb21841cc3cfca3d10035a58101d1d Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Mon, 3 Feb 2025 16:06:59 +0530 Subject: [PATCH 8/9] address review comments --- docs/app demos/maker-cli.md | 5 ++--- docs/app demos/taker.md | 1 - tests/taker_cli.rs | 8 +++++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/app demos/maker-cli.md b/docs/app demos/maker-cli.md index e30d4d5d..edb75f85 100644 --- a/docs/app demos/maker-cli.md +++ b/docs/app demos/maker-cli.md @@ -286,10 +286,9 @@ $ ./maker-cli get-balances >[!IMPORTANT] -> Currently `maker-cli` does not provide any method to see maker's normal wallet utxos and their correspinding balance seperately as we do have for other utxos types. -> we have to manually figure these utxos and their balances by using `list-utxo` and `get-balance` command respectively. +> we have to manually figure utxos and their balances by using `list-utxo` and `get-balances` command respectively. > where `list-utxo` returns all the utxos present in the maker wallet including the `fidleity utxos` also. -> and `get-balance` returns the total spendable balance which includes balance of normal utxos , swap utxos, contract utxos but excludes `fidelitly utxos`. +> and `get-balances` returns the total wallet balances of different categories which includes balance of normal utxos, swap utxos, contract utxos, fidelitly utxos and spendable utxos (normal + swap utxos). Let's find them out: diff --git a/docs/app demos/taker.md b/docs/app demos/taker.md index d1b2cd6c..82e84a74 100644 --- a/docs/app demos/taker.md +++ b/docs/app demos/taker.md @@ -96,7 +96,6 @@ $ taker -r 127.0.0.1:38332 -a user:pass get-balances "regular": 10000000, "swap": 0, "contract": 0, - "fidelity": 0, "spendable": 10000000 } ``` diff --git a/tests/taker_cli.rs b/tests/taker_cli.rs index 8c3dc5d9..c1664424 100644 --- a/tests/taker_cli.rs +++ b/tests/taker_cli.rs @@ -3,7 +3,7 @@ use bitcoin::{address::NetworkChecked, Address, Amount}; use bitcoind::{bitcoincore_rpc::RpcApi, tempfile::env::temp_dir, BitcoinD}; use serde_json::Value; -use std::{fs, path::PathBuf, process::Command, str::FromStr}; +use std::{fs, path::PathBuf, println, process::Command, str::FromStr}; mod test_framework; use test_framework::{generate_blocks, init_bitcoind, send_to_address}; /// The taker-cli command struct @@ -98,6 +98,9 @@ fn test_taker_cli() { let balances = taker_cli.execute(&["get-balances"]); let balances = serde_json::from_str::(&balances).unwrap(); + assert_eq!("300000000", balances["regular"].to_string()); + assert_eq!("0", balances["swap"].to_string()); + assert_eq!("0", balances["contract"].to_string()); assert_eq!("300000000", balances["spendable"].to_string()); // Assert that total no of seed-utxos are 3. @@ -128,6 +131,9 @@ fn test_taker_cli() { let balances = serde_json::from_str::(&balances).unwrap(); // Since the amount is sent back to our wallet, the transaction fee is deducted from the balance. + assert_eq!("299999000", balances["regular"].to_string()); + assert_eq!("0", balances["swap"].to_string()); + assert_eq!("0", balances["contract"].to_string()); assert_eq!("299999000", balances["spendable"].to_string()); // Assert that no of seed utxos are 2 From 77a9dbdddc9e726a14a1651b32d747a32323ba54 Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Mon, 3 Feb 2025 16:16:49 +0530 Subject: [PATCH 9/9] remove println import --- tests/taker_cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/taker_cli.rs b/tests/taker_cli.rs index c1664424..6a6b241a 100644 --- a/tests/taker_cli.rs +++ b/tests/taker_cli.rs @@ -3,7 +3,7 @@ use bitcoin::{address::NetworkChecked, Address, Amount}; use bitcoind::{bitcoincore_rpc::RpcApi, tempfile::env::temp_dir, BitcoinD}; use serde_json::Value; -use std::{fs, path::PathBuf, println, process::Command, str::FromStr}; +use std::{fs, path::PathBuf, process::Command, str::FromStr}; mod test_framework; use test_framework::{generate_blocks, init_bitcoind, send_to_address}; /// The taker-cli command struct