Skip to content

Commit 5589a32

Browse files
committed
listtransactions
1 parent 3107be9 commit 5589a32

File tree

4 files changed

+96
-9
lines changed

4 files changed

+96
-9
lines changed

node/src/bin/space-cli.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ enum Commands {
207207
#[arg(long, short)]
208208
fee_rate: Option<u64>,
209209
},
210+
/// List last transactions
211+
#[command(name = "listtransactions")]
212+
ListTransactions {
213+
#[arg(default_value = "1")]
214+
count: usize,
215+
#[arg(default_value = "0")]
216+
skip: usize,
217+
},
210218
/// List won spaces including ones
211219
/// still in auction with a winning bid
212220
#[command(name = "listspaces")]
@@ -574,6 +582,13 @@ async fn handle_commands(
574582
let spaces = cli.client.wallet_list_auction_outputs(&cli.wallet).await?;
575583
println!("{}", serde_json::to_string_pretty(&spaces)?);
576584
}
585+
Commands::ListTransactions { count, skip } => {
586+
let txs = cli
587+
.client
588+
.wallet_list_transactions(&cli.wallet, count, skip)
589+
.await?;
590+
println!("{}", serde_json::to_string_pretty(&txs)?);
591+
}
577592
Commands::ListSpaces => {
578593
let spaces = cli.client.wallet_list_spaces(&cli.wallet).await?;
579594
println!("{}", serde_json::to_string_pretty(&spaces)?);

node/src/rpc.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ use crate::{
4444
source::BitcoinRpc,
4545
store::{ChainState, LiveSnapshot},
4646
wallets::{
47-
AddressKind, Balance, RpcWallet, TxResponse, WalletCommand, WalletOutput, WalletResponse,
47+
AddressKind, Balance, RpcWallet, TxInfo, TxResponse, WalletCommand, WalletOutput,
48+
WalletResponse,
4849
},
4950
};
5051

@@ -163,6 +164,14 @@ pub trait Rpc {
163164
fee_rate: FeeRate,
164165
) -> Result<Vec<TxResponse>, ErrorObjectOwned>;
165166

167+
#[method(name = "walletlisttransactions")]
168+
async fn wallet_list_transactions(
169+
&self,
170+
wallet: &str,
171+
count: usize,
172+
skip: usize,
173+
) -> Result<Vec<TxInfo>, ErrorObjectOwned>;
174+
166175
#[method(name = "walletlistspaces")]
167176
async fn wallet_list_spaces(&self, wallet: &str)
168177
-> Result<Vec<WalletOutput>, ErrorObjectOwned>;
@@ -715,6 +724,19 @@ impl RpcServer for RpcServerImpl {
715724
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
716725
}
717726

727+
async fn wallet_list_transactions(
728+
&self,
729+
wallet: &str,
730+
count: usize,
731+
skip: usize,
732+
) -> Result<Vec<TxInfo>, ErrorObjectOwned> {
733+
self.wallet(&wallet)
734+
.await?
735+
.send_list_transactions(count, skip)
736+
.await
737+
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
738+
}
739+
718740
async fn wallet_list_spaces(
719741
&self,
720742
wallet: &str,

node/src/wallets.rs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ use tokio::{
2020
};
2121
use wallet::{
2222
address::SpaceAddress,
23-
bdk_wallet,
2423
bdk_wallet::{
24+
self,
2525
chain::{local_chain::CheckPoint, BlockId},
2626
KeychainKind, LocalOutput,
2727
},
28-
bitcoin,
29-
bitcoin::{Address, Amount, FeeRate},
28+
bitcoin::{self, Address, Amount, FeeRate},
3029
builder::{
3130
CoinTransfer, SpaceTransfer, SpacesAwareCoinSelection, TransactionTag, TransferRequest,
3231
},
@@ -51,6 +50,12 @@ pub struct TxResponse {
5150
pub error: Option<BTreeMap<String, String>>,
5251
}
5352

53+
#[derive(Debug, Clone, Serialize, Deserialize)]
54+
pub struct TxInfo {
55+
pub txid: Txid,
56+
pub confirmed: bool,
57+
}
58+
5459
#[derive(Debug, Clone, Serialize, Deserialize)]
5560
pub struct WalletResponse {
5661
pub sent: Vec<TxResponse>,
@@ -83,6 +88,11 @@ pub enum WalletCommand {
8388
fee_rate: FeeRate,
8489
resp: crate::rpc::Responder<anyhow::Result<Vec<TxResponse>>>,
8590
},
91+
ListTransactions {
92+
count: usize,
93+
skip: usize,
94+
resp: crate::rpc::Responder<anyhow::Result<Vec<TxInfo>>>,
95+
},
8696
ListSpaces {
8797
resp: crate::rpc::Responder<anyhow::Result<Vec<WalletOutput>>>,
8898
},
@@ -154,8 +164,10 @@ impl RpcWallet {
154164
balance,
155165
dust: unspent
156166
.into_iter()
157-
.filter(|output| output.is_spaceout ||
158-
output.output.txout.value <= SpacesAwareCoinSelection::DUST_THRESHOLD)
167+
.filter(|output| {
168+
output.is_spaceout
169+
|| output.output.txout.value <= SpacesAwareCoinSelection::DUST_THRESHOLD
170+
})
159171
.map(|output| output.output.txout.value)
160172
.sum(),
161173
};
@@ -224,6 +236,10 @@ impl RpcWallet {
224236
WalletCommand::ListUnspent { resp } => {
225237
_ = resp.send(Self::list_unspent(wallet, state));
226238
}
239+
WalletCommand::ListTransactions { count, skip, resp } => {
240+
let transactions = Self::list_transactions(wallet, count, skip);
241+
_ = resp.send(transactions);
242+
}
227243
WalletCommand::ListSpaces { resp } => {
228244
let result = Self::list_unspent(wallet, state);
229245
match result {
@@ -375,6 +391,26 @@ impl RpcWallet {
375391
Ok(SpacesAwareCoinSelection::new(excluded))
376392
}
377393

394+
fn list_transactions(
395+
wallet: &mut SpacesWallet,
396+
count: usize,
397+
skip: usize,
398+
) -> anyhow::Result<Vec<TxInfo>> {
399+
let transactions = wallet
400+
.spaces
401+
.transactions()
402+
.into_iter()
403+
.skip(skip)
404+
.take(count)
405+
.map(|tx| {
406+
let txid = tx.tx_node.txid.clone();
407+
let confirmed = tx.chain_position.is_confirmed();
408+
TxInfo { txid, confirmed }
409+
})
410+
.collect();
411+
Ok(transactions)
412+
}
413+
378414
fn list_unspent(
379415
wallet: &mut SpacesWallet,
380416
store: &mut LiveSnapshot,
@@ -446,8 +482,10 @@ impl RpcWallet {
446482
if dust > SpacesAwareCoinSelection::DUST_THRESHOLD {
447483
// Allowing higher dust may space outs to be accidentally
448484
// spent during coin selection
449-
return Err(anyhow!("dust cannot be higher than {}",
450-
SpacesAwareCoinSelection::DUST_THRESHOLD));
485+
return Err(anyhow!(
486+
"dust cannot be higher than {}",
487+
SpacesAwareCoinSelection::DUST_THRESHOLD
488+
));
451489
}
452490
}
453491

@@ -789,6 +827,18 @@ impl RpcWallet {
789827
resp_rx.await?
790828
}
791829

830+
pub async fn send_list_transactions(
831+
&self,
832+
count: usize,
833+
skip: usize,
834+
) -> anyhow::Result<Vec<TxInfo>> {
835+
let (resp, resp_rx) = oneshot::channel();
836+
self.sender
837+
.send(WalletCommand::ListTransactions { count, skip, resp })
838+
.await?;
839+
resp_rx.await?
840+
}
841+
792842
pub async fn send_list_spaces(&self) -> anyhow::Result<Vec<WalletOutput>> {
793843
let (resp, resp_rx) = oneshot::channel();
794844
self.sender.send(WalletCommand::ListSpaces { resp }).await?;

wallet/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl SpacesWallet {
153153
}
154154

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

158158
descriptors.push(DescriptorInfo {
159159
descriptor: self

0 commit comments

Comments
 (0)