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

Use dedicated struct for wallet balance #385

Merged
merged 9 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
88 changes: 67 additions & 21 deletions docs/app demos/maker-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
```


Expand All @@ -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
}
```


Expand Down Expand Up @@ -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
}
```

---
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
```

---
Expand All @@ -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
}
```

---
Expand Down Expand Up @@ -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
}
```

---
Expand Down
16 changes: 10 additions & 6 deletions docs/app demos/taker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Levi0804 marked this conversation as resolved.
Show resolved Hide resolved
"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.
Expand Down
28 changes: 9 additions & 19 deletions src/bin/maker-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@
ListUtxoContract,
/// Lists fidelity bond utxos.
ListUtxoFidelity,
/// Get total wallet balance, excluding Fidelity bonds.
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,
/// Get total wallet balances of different categories.
/// 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 (regular + swap balance).
GetBalances,
/// Gets a new bitcoin receiving address
GetNewAddress,
/// Send Bitcoin to an external address and returns the txid.
Expand Down Expand Up @@ -89,24 +88,15 @@
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)?;
}
Commands::GetBalance => {
send_rpc_req(stream, RpcMsgReq::Balance)?;
Commands::GetBalances => {
send_rpc_req(stream, RpcMsgReq::Balances)?;

Check warning on line 95 in src/bin/maker-cli.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/maker-cli.rs#L95

Added line #L95 was not covered by tests
}
Commands::ListUtxo => {
send_rpc_req(stream, RpcMsgReq::Utxo)?;
}
Commands::GetBalanceSwap => {
send_rpc_req(stream, RpcMsgReq::SwapBalance)?;
}
Commands::ListUtxoSwap => {
send_rpc_req(stream, RpcMsgReq::SwapUtxo)?;
}
Expand Down
42 changes: 19 additions & 23 deletions src/bin/taker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.
Expand Down Expand Up @@ -63,14 +64,12 @@
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)
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,
/// Get total wallet balances of different categories.
/// 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 balance).
GetBalances,
/// Returns a new address
GetNewAddress,
/// Send to an external wallet address.
Expand Down Expand Up @@ -173,21 +172,18 @@
.collect::<Vec<_>>();
println!("{:#?}", utxos);
}
Commands::GetBalanceContract => {
let balance = taker.get_wallet().balance_live_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);
Commands::GetBalances => {
let balances = taker.get_wallet().get_balances()?;
println!(

Check warning on line 177 in src/bin/taker.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/taker.rs#L176-L177

Added lines #L176 - L177 were not covered by tests
"{}",
to_string_pretty(&json!({
"regular": balances.regular.to_sat(),
"contract": balances.contract.to_sat(),
"swap": balances.swap.to_sat(),
"spendable": balances.spendable.to_sat(),
}))
.unwrap()
);
}
Commands::GetNewAddress => {
let address = taker.get_wallet_mut().get_next_external_address()?;
Expand Down
41 changes: 20 additions & 21 deletions src/maker/rpc/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
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.
///
Expand All @@ -23,14 +24,8 @@
ContractUtxo,
/// Request to fetch UTXOs in the fidelity pool.
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 to retreive the total wallet balances of different categories.
Balances,
/// Request for generating a new wallet address.
NewAddress,
/// Request to send funds to a specific address.
Expand Down Expand Up @@ -84,14 +79,8 @@
/// List of UTXOs in the contract pool.
utxos: Vec<ListUnspentResultEntry>,
},
/// 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 balances of different categories.
TotalBalanceResp(Balances),
/// Response containing a newly generated wallet address.
NewAddressResp(String),
/// Response to a send-to-address request.
Expand All @@ -115,10 +104,20 @@
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(balances) => {
write!(

Check warning on line 108 in src/maker/rpc/messages.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/rpc/messages.rs#L107-L108

Added lines #L107 - L108 were not covered by tests
f,
"{}",
to_string_pretty(&json!({
"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(),

Check warning on line 116 in src/maker/rpc/messages.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/rpc/messages.rs#L111-L116

Added lines #L111 - L116 were not covered by tests
}))
.unwrap()
)
}
Self::UtxoResp { utxos } => write!(f, "{:#?}", utxos),
Self::SwapUtxoResp { utxos } => write!(f, "{:#?}", utxos),
Self::FidelityUtxoResp { utxos } => write!(f, "{:#?}", utxos),
Expand Down
21 changes: 3 additions & 18 deletions src/maker/rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,9 @@
.collect::<Vec<_>>();
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
.get_wallet()
.read()?
.balance_incoming_swap_coins(None)?;
RpcMsgResp::SwapBalanceResp(balance.to_sat())
RpcMsgReq::Balances => {
let balances = maker.get_wallet().read()?.get_balances()?;
RpcMsgResp::TotalBalanceResp(balances)

Check warning on line 68 in src/maker/rpc/server.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/rpc/server.rs#L67-L68

Added lines #L67 - L68 were not covered by tests
}
RpcMsgReq::NewAddress => {
let new_address = maker.get_wallet().write()?.get_next_external_address()?;
Expand Down
Loading
Loading