Skip to content

change(rpc): Update getaddressbalance RPC to return received field #9295

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

Merged
merged 4 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 26 additions & 1 deletion zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,9 @@ where
let response = state.oneshot(request).await.map_misc_error()?;

match response {
zebra_state::ReadResponse::AddressBalance { balance, .. } => Ok(AddressBalance {
zebra_state::ReadResponse::AddressBalance { balance, received } => Ok(AddressBalance {
balance: u64::from(balance),
received,
}),
_ => unreachable!("Unexpected response from state service: {response:?}"),
}
Expand Down Expand Up @@ -3008,11 +3009,33 @@ impl GetBlockChainInfo {
/// This is used for the input parameter of [`RpcServer::get_address_balance`],
/// [`RpcServer::get_address_tx_ids`] and [`RpcServer::get_address_utxos`].
#[derive(Clone, Debug, Eq, PartialEq, Hash, serde::Deserialize, serde::Serialize)]
#[serde(from = "DAddressStrings")]
pub struct AddressStrings {
/// A list of transparent address strings.
addresses: Vec<String>,
}

impl From<DAddressStrings> for AddressStrings {
fn from(address_strings: DAddressStrings) -> Self {
match address_strings {
DAddressStrings::Addresses { addresses } => AddressStrings { addresses },
DAddressStrings::Address(address) => AddressStrings {
addresses: vec![address],
},
}
}
}

/// An intermediate type used to deserialize [`AddressStrings`].
#[derive(Clone, Debug, Eq, PartialEq, Hash, serde::Deserialize)]
#[serde(untagged)]
enum DAddressStrings {
/// A list of address strings.
Addresses { addresses: Vec<String> },
/// A single address string.
Address(String),
}

impl AddressStrings {
/// Creates a new `AddressStrings` given a vector.
#[cfg(test)]
Expand Down Expand Up @@ -3062,6 +3085,8 @@ impl AddressStrings {
pub struct AddressBalance {
/// The total transparent balance.
pub balance: u64,
/// The total received balance, including change.
pub received: u64,
}

/// A hex-encoded [`ConsensusBranchId`] string.
Expand Down
4 changes: 2 additions & 2 deletions zebra-rpc/src/methods/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ proptest! {
let state_query = state
.expect_request(zebra_state::ReadRequest::AddressBalance(addresses))
.map_ok(|responder| {
responder.respond(zebra_state::ReadResponse::AddressBalance { balance, received: Default::default() })
responder.respond(zebra_state::ReadResponse::AddressBalance { balance, received: balance.into() })
});

// Await the RPC call and the state query
Expand All @@ -658,7 +658,7 @@ proptest! {
// Check that response contains the expected balance
let received_balance = response?;

prop_assert_eq!(received_balance, AddressBalance { balance: balance.into() });
prop_assert_eq!(received_balance, AddressBalance { balance: balance.into(), received: balance.into() });

// Check no further requests were made during this test
mempool.expect_no_requests().await?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
assertion_line: 192
expression: address_balance
---
{
"balance": 687500
"balance": 687500,
"received": 687500
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
source: zebra-rpc/src/methods/tests/snapshot.rs
assertion_line: 192
expression: address_balance
---
{
"balance": 687500
"balance": 687500,
"received": 687500
}
4 changes: 3 additions & 1 deletion zebra-rpc/tests/serialization_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ fn test_get_address_balance() -> Result<(), Box<dyn std::error::Error>> {
// Test response
let json = r#"
{
"balance": 11290259389
"balance": 11290259389,
"received": 11290259390
}
"#;
let obj: AddressBalance = serde_json::from_str(json)?;
let new_obj = AddressBalance {
balance: obj.balance,
received: obj.received,
};

assert_eq!(obj, new_obj);
Expand Down
Loading