Skip to content

Commit

Permalink
Merge pull request #29 from Ximik/listtransactions
Browse files Browse the repository at this point in the history
CLI listtransactions command
  • Loading branch information
buffrr authored Nov 18, 2024
2 parents f664298 + b8dada2 commit 5198a46
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
15 changes: 15 additions & 0 deletions node/src/bin/space-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ enum Commands {
#[arg(long, short)]
fee_rate: Option<u64>,
},
/// List last transactions
#[command(name = "listtransactions")]
ListTransactions {
#[arg(default_value = "10")]
count: usize,
#[arg(default_value = "0")]
skip: usize,
},
/// List won spaces including ones
/// still in auction with a winning bid
#[command(name = "listspaces")]
Expand Down Expand Up @@ -535,6 +543,13 @@ async fn handle_commands(
let spaces = cli.client.wallet_list_bidouts(&cli.wallet).await?;
println!("{}", serde_json::to_string_pretty(&spaces)?);
}
Commands::ListTransactions { count, skip } => {
let txs = cli
.client
.wallet_list_transactions(&cli.wallet, count, skip)
.await?;
println!("{}", serde_json::to_string_pretty(&txs)?);
}
Commands::ListSpaces => {
let spaces = cli.client.wallet_list_spaces(&cli.wallet).await?;
println!("{}", serde_json::to_string_pretty(&spaces)?);
Expand Down
24 changes: 23 additions & 1 deletion node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ use crate::{
source::BitcoinRpc,
store::{ChainState, LiveSnapshot, RolloutEntry, Sha256},
wallets::{
AddressKind, Balance, RpcWallet, TxResponse, WalletCommand, WalletOutput, WalletResponse,
AddressKind, Balance, RpcWallet, TxInfo, TxResponse, WalletCommand, WalletOutput,
WalletResponse,
},
};

Expand Down Expand Up @@ -168,6 +169,14 @@ pub trait Rpc {
fee_rate: FeeRate,
) -> Result<Vec<TxResponse>, ErrorObjectOwned>;

#[method(name = "walletlisttransactions")]
async fn wallet_list_transactions(
&self,
wallet: &str,
count: usize,
skip: usize,
) -> Result<Vec<TxInfo>, ErrorObjectOwned>;

#[method(name = "walletforcespend")]
async fn wallet_force_spend(
&self,
Expand Down Expand Up @@ -729,6 +738,19 @@ impl RpcServer for RpcServerImpl {
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
}

async fn wallet_list_transactions(
&self,
wallet: &str,
count: usize,
skip: usize,
) -> Result<Vec<TxInfo>, ErrorObjectOwned> {
self.wallet(&wallet)
.await?
.send_list_transactions(count, skip)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
}

async fn wallet_force_spend(
&self,
wallet: &str,
Expand Down
60 changes: 60 additions & 0 deletions node/src/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ pub struct TxResponse {
pub raw: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxInfo {
pub txid: Txid,
pub confirmed: bool,
pub sent: Amount,
pub received: Amount,
pub fee: Option<Amount>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WalletResponse {
pub result: Vec<TxResponse>,
Expand Down Expand Up @@ -86,6 +95,11 @@ pub enum WalletCommand {
fee_rate: FeeRate,
resp: crate::rpc::Responder<anyhow::Result<Vec<TxResponse>>>,
},
ListTransactions {
count: usize,
skip: usize,
resp: crate::rpc::Responder<anyhow::Result<Vec<TxInfo>>>,
},
ListSpaces {
resp: crate::rpc::Responder<anyhow::Result<Vec<WalletOutput>>>,
},
Expand Down Expand Up @@ -291,6 +305,10 @@ impl RpcWallet {
WalletCommand::ListUnspent { resp } => {
_ = resp.send(Self::list_unspent(wallet, state));
}
WalletCommand::ListTransactions { count, skip, resp } => {
let transactions = Self::list_transactions(wallet, count, skip);
_ = resp.send(transactions);
}
WalletCommand::ListSpaces { resp } => {
let result = Self::list_unspent(wallet, state);
match result {
Expand Down Expand Up @@ -448,6 +466,36 @@ impl RpcWallet {
Ok(SpacesAwareCoinSelection::new(excluded, confirmed_only))
}

fn list_transactions(
wallet: &mut SpacesWallet,
count: usize,
skip: usize,
) -> anyhow::Result<Vec<TxInfo>> {
let mut transactions: Vec<_> = wallet.spaces.transactions().collect();
transactions.sort();

Ok(transactions
.iter()
.rev()
.skip(skip)
.take(count)
.map(|ctx| {
let tx = ctx.tx_node.tx.clone();
let txid = ctx.tx_node.txid.clone();
let confirmed = ctx.chain_position.is_confirmed();
let (sent, received) = wallet.spaces.sent_and_received(&tx);
let fee = wallet.spaces.calculate_fee(&tx).ok();
TxInfo {
txid,
confirmed,
sent,
received,
fee,
}
})
.collect())
}

fn list_unspent(
wallet: &mut SpacesWallet,
store: &mut LiveSnapshot,
Expand Down Expand Up @@ -884,6 +932,18 @@ impl RpcWallet {
resp_rx.await?
}

pub async fn send_list_transactions(
&self,
count: usize,
skip: usize,
) -> anyhow::Result<Vec<TxInfo>> {
let (resp, resp_rx) = oneshot::channel();
self.sender
.send(WalletCommand::ListTransactions { count, skip, resp })
.await?;
resp_rx.await?
}

pub async fn send_force_spend(
&self,
outpoint: OutPoint,
Expand Down
2 changes: 1 addition & 1 deletion wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl SpacesWallet {
}

pub fn get_info(&self) -> WalletInfo {
let mut descriptors = Vec::with_capacity(4);
let mut descriptors = Vec::with_capacity(2);

descriptors.push(DescriptorInfo {
descriptor: self
Expand Down

0 comments on commit 5198a46

Please sign in to comment.