Skip to content

Commit 3f03266

Browse files
committed
Test listreceivedbyaddress
The RPC was untested and missing a change in the return type in v18. `involves_watch_only` is optional, the v17-v23 help says "only returned...", from v24 it specifically says optional. Update the struct in v18, make the field an Option and add a test.
1 parent 42e808f commit 3f03266

File tree

18 files changed

+141
-44
lines changed

18 files changed

+141
-44
lines changed

integration_test/tests/wallet.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,23 @@ fn wallet__list_received_by_label__modelled() {
548548
assert!(model.0.iter().any(|item| item.label == label));
549549
}
550550

551+
#[test]
552+
fn wallet__list_received_by_address__modelled() {
553+
let node = Node::with_wallet(Wallet::Default, &[]);
554+
node.fund_wallet();
555+
let address = node.client.new_address().expect("failed to create new address");
556+
let amount = Amount::from_sat(10_000);
557+
let _ = node.client.send_to_address(&address, amount).expect("sendtoaddress");
558+
node.mine_a_block();
559+
560+
let json: ListReceivedByAddress = node.client.list_received_by_address().expect("listreceivedbyaddress");
561+
let model: Result<mtype::ListReceivedByAddress, _> = json.into_model();
562+
let model = model.unwrap();
563+
564+
let unchecked_addr = address.as_unchecked();
565+
assert!(model.0.iter().any(|item| &item.address == unchecked_addr));
566+
}
567+
551568
#[test]
552569
fn wallet__import_multi() {
553570
let node = match () {

types/src/model/wallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ pub struct ListReceivedByAddress(pub Vec<ListReceivedByAddressItem>);
524524
#[serde(deny_unknown_fields)]
525525
pub struct ListReceivedByAddressItem {
526526
/// Only returned if imported addresses were involved in transaction.
527-
pub involves_watch_only: bool,
527+
pub involves_watch_only: Option<bool>,
528528
/// The receiving address.
529529
pub address: Address<NetworkUnchecked>,
530530
/// The total amount received by the address.

types/src/v17/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
//! | listlabels | version | |
185185
//! | listlockunspent | version + model | UNTESTED |
186186
//! | listreceivedbyaccount | returns nothing | |
187-
//! | listreceivedbyaddress | version + model | UNTESTED |
187+
//! | listreceivedbyaddress | version + model | |
188188
//! | listsinceblock | version + model | UNTESTED |
189189
//! | listtransactions | version + model | UNTESTED |
190190
//! | listunspent | version + model | |

types/src/v17/wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ pub struct ListReceivedByAddress(pub Vec<ListReceivedByAddressItem>);
669669
pub struct ListReceivedByAddressItem {
670670
/// Only returned if imported addresses were involved in transaction.
671671
#[serde(rename = "involvesWatchonly")]
672-
pub involves_watch_only: bool,
672+
pub involves_watch_only: Option<bool>,
673673
/// The receiving address.
674674
pub address: String,
675675
/// DEPRECATED. Backwards compatible alias for label.

types/src/v18/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
//! | listaddressgroupings | version + model | UNTESTED |
187187
//! | listlabels | version | |
188188
//! | listlockunspent | version + model | UNTESTED |
189-
//! | listreceivedbyaddress | version + model | UNTESTED |
189+
//! | listreceivedbyaddress | version + model | |
190190
//! | listreceivedbylabel | version + model | |
191191
//! | listsinceblock | version + model | UNTESTED |
192192
//! | listtransactions | version + model | UNTESTED |
@@ -246,7 +246,8 @@ pub use self::{
246246
wallet::{
247247
GetAddressInfo, GetAddressInfoEmbedded, GetAddressInfoError, GetReceivedByLabel,
248248
ImportMulti, ImportMultiEntry, JsonRpcError, ListReceivedByLabel, ListReceivedByLabelError,
249-
ListUnspent, ListUnspentItem, ListWalletDir, ListWalletDirWallet,
249+
ListUnspent, ListUnspentItem, ListWalletDir, ListWalletDirWallet, ListReceivedByAddress,
250+
ListReceivedByAddressItem,
250251
},
251252
};
252253
#[doc(inline)]
@@ -272,7 +273,7 @@ pub use crate::v17::{
272273
GetTxOutSetInfoError, GetUnconfirmedBalance, GetWalletInfo, GetWalletInfoError,
273274
GetZmqNotifications, ListAddressGroupings, ListAddressGroupingsError, ListAddressGroupingsItem,
274275
ListBanned, ListLabels, ListLockUnspent, ListLockUnspentItem, ListLockUnspentItemError,
275-
ListReceivedByAddress, ListReceivedByAddressError, ListReceivedByAddressItem, ListSinceBlock,
276+
ListReceivedByAddressError, ListSinceBlock,
276277
ListSinceBlockError, ListSinceBlockTransaction, ListSinceBlockTransactionError,
277278
ListTransactions, ListTransactionsItem, ListTransactionsItemError, ListUnspentItemError,
278279
ListWallets, LoadWallet, LockUnspent, Locked, Logging, MapMempoolEntryError, MempoolAcceptance,

types/src/v18/wallet/into.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bitcoin::{
1111
use super::{
1212
GetAddressInfo, GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
1313
GetReceivedByLabel, ListReceivedByLabel, ListReceivedByLabelError, ListReceivedByLabelItem,
14-
ListUnspent, ListUnspentItem, ListUnspentItemError,
14+
ListUnspent, ListUnspentItem, ListUnspentItemError, ListReceivedByAddress, ListReceivedByAddressError, ListReceivedByAddressItem,
1515
};
1616
use crate::model;
1717

@@ -165,6 +165,47 @@ impl GetReceivedByLabel {
165165
}
166166
}
167167

168+
impl ListReceivedByAddress {
169+
/// Converts version specific type to a version nonspecific, more strongly typed type.
170+
pub fn into_model(self) -> Result<model::ListReceivedByAddress, ListReceivedByAddressError> {
171+
let balances = self
172+
.0
173+
.into_iter()
174+
.map(|balance| balance.into_model())
175+
.collect::<Result<Vec<_>, _>>()?;
176+
Ok(model::ListReceivedByAddress(balances))
177+
}
178+
}
179+
180+
impl ListReceivedByAddressItem {
181+
/// Converts version specific type to a version nonspecific, more strongly typed type.
182+
pub fn into_model(
183+
self,
184+
) -> Result<model::ListReceivedByAddressItem, ListReceivedByAddressError> {
185+
use ListReceivedByAddressError as E;
186+
187+
let address = self.address.parse::<Address<_>>().map_err(E::Address)?;
188+
let amount = Amount::from_btc(self.amount).map_err(E::Amount)?;
189+
let txids = self
190+
.txids
191+
.iter()
192+
.enumerate()
193+
.map(|(i, txid)| {
194+
txid.parse::<Txid>().map_err(|e| ListReceivedByAddressError::Txids(i, e))
195+
})
196+
.collect::<Result<Vec<_>, _>>()?;
197+
198+
Ok(model::ListReceivedByAddressItem {
199+
involves_watch_only: self.involves_watch_only,
200+
address,
201+
amount,
202+
confirmations: self.confirmations,
203+
label: self.label,
204+
txids,
205+
})
206+
}
207+
}
208+
168209
impl ListReceivedByLabel {
169210
/// Converts version specific type to a version nonspecific, more strongly typed type.
170211
pub fn into_model(self) -> Result<model::ListReceivedByLabel, ListReceivedByLabelError> {

types/src/v18/wallet/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use serde::{Deserialize, Serialize};
1111

1212
pub use self::error::{GetAddressInfoError, ListReceivedByLabelError};
1313
pub use super::{
14-
GetAddressInfoEmbeddedError, GetAddressInfoLabel, ListUnspentItemError, ScriptType,
14+
GetAddressInfoEmbeddedError, GetAddressInfoLabel, ListReceivedByAddressError,
15+
ListUnspentItemError, ScriptType,
1516
};
1617

1718
/// Result of the JSON-RPC method `getaddressinfo`.
@@ -190,6 +191,34 @@ pub struct JsonRpcError {
190191
pub data: Option<serde_json::Value>, // Can hold arbitrary extra information
191192
}
192193

194+
/// Result of the JSON-RPC method `listreceivedbyaddress`.
195+
///
196+
/// > listreceivedbyaddress ( minconf include_empty include_watchonly address_filter )
197+
/// >
198+
/// > List balances by receiving address.
199+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
200+
#[serde(deny_unknown_fields)]
201+
pub struct ListReceivedByAddress(pub Vec<ListReceivedByAddressItem>);
202+
203+
/// List item returned as part of of `listreceivedByaddress`.
204+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
205+
#[serde(deny_unknown_fields)]
206+
pub struct ListReceivedByAddressItem {
207+
/// Only returned if imported addresses were involved in transaction.
208+
#[serde(rename = "involvesWatchonly")]
209+
pub involves_watch_only: Option<bool>,
210+
/// The receiving address.
211+
pub address: String,
212+
/// The total amount in BTC received by the address.
213+
pub amount: f64,
214+
/// The number of confirmations of the most recent transaction included.
215+
pub confirmations: i64,
216+
/// The label of the receiving address. The default label is "".
217+
pub label: String,
218+
/// The ids of transactions received with the address.
219+
pub txids: Vec<String>,
220+
}
221+
193222
/// Result of the JSON-RPC method `listreceivedbylabel`.
194223
///
195224
/// > listreceivedbylabel ( minconf include_empty include_watchonly )

types/src/v19/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
//! | listaddressgroupings | version + model | UNTESTED |
188188
//! | listlabels | version | |
189189
//! | listlockunspent | version + model | UNTESTED |
190-
//! | listreceivedbyaddress | version + model | UNTESTED |
190+
//! | listreceivedbyaddress | version + model | |
191191
//! | listreceivedbylabel | version + model | |
192192
//! | listsinceblock | version + model | UNTESTED |
193193
//! | listtransactions | version + model | UNTESTED |
@@ -271,7 +271,7 @@ pub use crate::v17::{
271271
GetTxOutSetInfoError, GetUnconfirmedBalance, GetWalletInfo, GetWalletInfoError,
272272
GetZmqNotifications, ListAddressGroupings, ListAddressGroupingsError, ListAddressGroupingsItem,
273273
ListBanned, ListLabels, ListLockUnspent, ListLockUnspentItem, ListLockUnspentItemError,
274-
ListReceivedByAddress, ListReceivedByAddressError, ListReceivedByAddressItem, ListSinceBlock,
274+
ListReceivedByAddressError, ListSinceBlock,
275275
ListSinceBlockError, ListSinceBlockTransaction, ListSinceBlockTransactionError,
276276
ListTransactions, ListTransactionsItem, ListTransactionsItemError, ListUnspentItemError,
277277
ListWallets, LoadWallet, LockUnspent, Locked, Logging, PruneBlockchain, RawTransactionError,
@@ -289,4 +289,5 @@ pub use crate::v18::{
289289
GetAddressInfoError, GetNodeAddresses, GetReceivedByLabel, ImportMulti, ImportMultiEntry,
290290
JoinPsbts, JsonRpcError, ListReceivedByLabel, ListReceivedByLabelError, ListUnspent,
291291
ListUnspentItem, ListWalletDir, ListWalletDirWallet, NodeAddress, UtxoUpdatePsbt,
292+
ListReceivedByAddress, ListReceivedByAddressItem,
292293
};

types/src/v20/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
//! | listaddressgroupings | version + model | UNTESTED |
189189
//! | listlabels | version | |
190190
//! | listlockunspent | version + model | UNTESTED |
191-
//! | listreceivedbyaddress | version + model | UNTESTED |
191+
//! | listreceivedbyaddress | version + model | |
192192
//! | listreceivedbylabel | version + model | |
193193
//! | listsinceblock | version + model | UNTESTED |
194194
//! | listtransactions | version + model | UNTESTED |
@@ -265,8 +265,8 @@ pub use crate::{
265265
GetTxOutSetInfoError, GetUnconfirmedBalance, GetWalletInfo, GetWalletInfoError,
266266
GetZmqNotifications, ListAddressGroupings, ListAddressGroupingsError,
267267
ListAddressGroupingsItem, ListLabels, ListLockUnspent, ListLockUnspentItem,
268-
ListLockUnspentItemError, ListReceivedByAddress, ListReceivedByAddressError,
269-
ListReceivedByAddressItem, ListSinceBlock, ListSinceBlockError, ListSinceBlockTransaction,
268+
ListLockUnspentItemError, ListReceivedByAddressError,
269+
ListSinceBlock, ListSinceBlockError, ListSinceBlockTransaction,
270270
ListSinceBlockTransactionError, ListTransactions, ListTransactionsItem,
271271
ListTransactionsItemError, ListUnspentItemError, ListWallets, LoadWallet, LockUnspent,
272272
Locked, PruneBlockchain, RawTransactionError, RawTransactionInput, RawTransactionOutput,
@@ -282,7 +282,8 @@ pub use crate::{
282282
AnalyzePsbtInputMissingError, DeriveAddresses, GetAddressInfoError, GetNodeAddresses,
283283
GetReceivedByLabel, ImportMulti, ImportMultiEntry, JoinPsbts, JsonRpcError,
284284
ListReceivedByLabel, ListReceivedByLabelError, ListUnspent, ListUnspentItem, ListWalletDir,
285-
ListWalletDirWallet, NodeAddress, UtxoUpdatePsbt,
285+
ListWalletDirWallet, NodeAddress, UtxoUpdatePsbt, ListReceivedByAddress,
286+
ListReceivedByAddressItem,
286287
},
287288
v19::{
288289
Bip9SoftforkInfo, Bip9SoftforkStatistics, Bip9SoftforkStatus, GetBalances,

types/src/v21/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
//! | listlabels | version | |
193193
//! | listlockunspent | version + model | UNTESTED |
194194
//! | psbtbumpfee | version + model | |
195-
//! | listreceivedbyaddress | version + model | UNTESTED |
195+
//! | listreceivedbyaddress | version + model | |
196196
//! | listreceivedbylabel | version + model | |
197197
//! | listsinceblock | version + model | UNTESTED |
198198
//! | listtransactions | version + model | UNTESTED |
@@ -281,8 +281,8 @@ pub use crate::{
281281
GetTxOutSetInfoError, GetUnconfirmedBalance, GetWalletInfo, GetWalletInfoError,
282282
GetZmqNotifications, ListAddressGroupings, ListAddressGroupingsError,
283283
ListAddressGroupingsItem, ListLabels, ListLockUnspent, ListLockUnspentItem,
284-
ListLockUnspentItemError, ListReceivedByAddress, ListReceivedByAddressError,
285-
ListReceivedByAddressItem, ListSinceBlock, ListSinceBlockError, ListSinceBlockTransaction,
284+
ListLockUnspentItemError, ListReceivedByAddressError,
285+
ListSinceBlock, ListSinceBlockError, ListSinceBlockTransaction,
286286
ListSinceBlockTransactionError, ListTransactions, ListTransactionsItem,
287287
ListTransactionsItemError, ListUnspentItemError, ListWallets, LoadWallet, LockUnspent,
288288
Locked, PruneBlockchain, RawTransactionError, RawTransactionInput, RawTransactionOutput,
@@ -297,7 +297,8 @@ pub use crate::{
297297
AnalyzePsbtInputMissingError, DeriveAddresses, GetAddressInfoError, GetNodeAddresses,
298298
GetReceivedByLabel, ImportMulti, ImportMultiEntry, JoinPsbts, JsonRpcError,
299299
ListReceivedByLabel, ListReceivedByLabelError, ListUnspent, ListUnspentItem, ListWalletDir,
300-
ListWalletDirWallet, NodeAddress, UtxoUpdatePsbt,
300+
ListWalletDirWallet, NodeAddress, UtxoUpdatePsbt, ListReceivedByAddress,
301+
ListReceivedByAddressItem,
301302
},
302303
v19::{
303304
Bip9SoftforkStatistics, Bip9SoftforkStatus, GetBalances, GetBalancesError, GetBalancesMine,

0 commit comments

Comments
 (0)