Skip to content

Commit

Permalink
Merge pull request #1701 from nuttycom/total_spent_and_received
Browse files Browse the repository at this point in the history
zcash_client_sqlite: Add v_transactions.{total_spent, total_received}
  • Loading branch information
nuttycom authored Feb 11, 2025
2 parents ca27ff5 + c38d930 commit f339981
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 4 deletions.
7 changes: 7 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Added
- `zcash_client_backend::data_api::testing::TransactionSummary` has added
accessor methods `total_spent` and `total_received`.

### Changed
- MSRV is now 1.81.0.
- Migrated to `bip32 =0.6.0-pre.1`, `nonempty 0.11`, `incrementalmerkletree 0.8`,
Expand All @@ -26,6 +30,9 @@ and this library adheres to Rust's notion of
- `zcash_client_backend::data_api::WalletRead::get_known_ephemeral_addresses`
now takes a `Range<zcash_transparent::keys::NonHardenedChildIndex>` as its
argument instead of a `Range<u32>`
- `zcash_client_backend::data_api::testing::TransactionSummary::from_parts`
has been modified; it now requires additional `total_spent` and `total_received`
arguments.

### Deprecated
- `zcash_client_backend::address` (use `zcash_keys::address` instead)
Expand Down
16 changes: 16 additions & 0 deletions zcash_client_backend/src/data_api/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct TransactionSummary<AccountId> {
expiry_height: Option<BlockHeight>,
mined_height: Option<BlockHeight>,
account_value_delta: ZatBalance,
total_spent: Zatoshis,
total_received: Zatoshis,
fee_paid: Option<Zatoshis>,
spent_note_count: usize,
has_change: bool,
Expand All @@ -121,6 +123,8 @@ impl<AccountId> TransactionSummary<AccountId> {
expiry_height: Option<BlockHeight>,
mined_height: Option<BlockHeight>,
account_value_delta: ZatBalance,
total_spent: Zatoshis,
total_received: Zatoshis,
fee_paid: Option<Zatoshis>,
spent_note_count: usize,
has_change: bool,
Expand All @@ -136,6 +140,8 @@ impl<AccountId> TransactionSummary<AccountId> {
expiry_height,
mined_height,
account_value_delta,
total_spent,
total_received,
fee_paid,
spent_note_count,
has_change,
Expand Down Expand Up @@ -180,6 +186,16 @@ impl<AccountId> TransactionSummary<AccountId> {
self.account_value_delta
}

/// Returns the total value of notes spent by the account in this transaction.
pub fn total_spent(&self) -> Zatoshis {
self.total_spent
}

/// Returns the total value of notes received by the account in this transaction.
pub fn total_received(&self) -> Zatoshis {
self.total_received
}

/// Returns the fee paid by this transaction, if known.
pub fn fee_paid(&self) -> Option<Zatoshis> {
self.fee_paid
Expand Down
23 changes: 23 additions & 0 deletions zcash_client_backend/src/data_api/testing/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ pub fn send_single_step_proposed_transfer<T: ShieldedPoolTester>(

let tx_history = st.wallet().get_tx_history().unwrap();
assert_eq!(tx_history.len(), 2);
{
let tx_0 = &tx_history[0];
assert_eq!(tx_0.total_spent(), Zatoshis::const_from_u64(0));
assert_eq!(tx_0.total_received(), Zatoshis::const_from_u64(60000));
}

{
let tx_1 = &tx_history[1];
assert_eq!(tx_1.total_spent(), Zatoshis::const_from_u64(60000));
assert_eq!(tx_1.total_received(), Zatoshis::const_from_u64(40000));
}

let network = *st.network();
assert_matches!(
Expand Down Expand Up @@ -462,6 +473,18 @@ pub fn send_with_multiple_change_outputs<T: ShieldedPoolTester>(

let tx_history = st.wallet().get_tx_history().unwrap();
assert_eq!(tx_history.len(), 2);
{
let tx_0 = &tx_history[0];
assert_eq!(tx_0.total_spent(), Zatoshis::const_from_u64(0));
assert_eq!(tx_0.total_received(), Zatoshis::const_from_u64(650_0000));
}

{
let tx_1 = &tx_history[1];
assert_eq!(tx_1.total_spent(), Zatoshis::const_from_u64(650_0000));
assert_eq!(tx_1.total_received(), Zatoshis::const_from_u64(548_5000));
assert_eq!(tx_1.fee_paid(), Some(Zatoshis::const_from_u64(15000)));
}

let network = *st.network();
assert_matches!(
Expand Down
1 change: 1 addition & 0 deletions zcash_client_sqlite/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this library adheres to Rust's notion of
- `zcash_client_sqlite::wallet::init::init_wallet_db` now has an additional
generic parameter, enabling it to be used with wallets constructed via
`WalletDb::from_connection`.
- The `v_transactions` view has added columns `total_spent` and `total_received`.

## [0.14.0] - 2024-12-16

Expand Down
2 changes: 2 additions & 0 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3664,6 +3664,8 @@ pub mod testing {
row.get::<_, Option<u32>>("mined_height")?
.map(BlockHeight::from),
ZatBalance::from_i64(row.get("account_balance_delta")?)?,
Zatoshis::from_nonnegative_i64(row.get("total_spent")?)?,
Zatoshis::from_nonnegative_i64(row.get("total_received")?)?,
row.get::<_, Option<i64>>("fee_paid")?
.map(Zatoshis::from_nonnegative_i64)
.transpose()?,
Expand Down
6 changes: 6 additions & 0 deletions zcash_client_sqlite/src/wallet/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ notes AS (
ro.pool AS pool,
id_within_pool_table,
ro.value AS value,
ro.value AS received_value,
0 AS spent_value,
0 AS spent_note_count,
CASE
WHEN ro.is_change THEN 1
Expand Down Expand Up @@ -788,6 +790,8 @@ notes AS (
ro.pool AS pool,
id_within_pool_table,
-ro.value AS value,
0 AS received_value,
ro.value AS spent_value,
1 AS spent_note_count,
0 AS change_note_count,
0 AS received_count,
Expand Down Expand Up @@ -836,6 +840,8 @@ SELECT accounts.uuid AS account_uuid,
transactions.expiry_height AS expiry_height,
transactions.raw AS raw,
SUM(notes.value) AS account_balance_delta,
SUM(notes.spent_value) AS total_spent,
SUM(notes.received_value) AS total_received,
transactions.fee AS fee_paid,
SUM(notes.change_note_count) > 0 AS has_change,
MAX(COALESCE(sent_note_counts.sent_notes, 0)) AS sent_note_count,
Expand Down
10 changes: 6 additions & 4 deletions zcash_client_sqlite/src/wallet/init/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod ufvk_support;
mod utxos_table;
mod utxos_to_txos;
mod v_sapling_shard_unscanned_ranges;
mod v_transactions_additional_totals;
mod v_transactions_net;
mod v_transactions_note_uniqueness;
mod v_transactions_shielding_balance;
Expand Down Expand Up @@ -82,10 +83,10 @@ pub(super) fn all_migrations<P: consensus::Parameters + 'static>(
// ------------------------------ tx_retrieval_queue ----------------------------
// |
// support_legacy_sqlite
// / \
// fix_broken_commitment_trees add_account_uuids
// |
// fix_bad_change_flagging
// / \
// fix_broken_commitment_trees add_account_uuids
// | |
// fix_bad_change_flagging v_transactions_additional_totals
vec![
Box::new(initial_setup::Migration {}),
Box::new(utxos_table::Migration {}),
Expand Down Expand Up @@ -149,6 +150,7 @@ pub(super) fn all_migrations<P: consensus::Parameters + 'static>(
}),
Box::new(fix_bad_change_flagging::Migration),
Box::new(add_account_uuids::Migration),
Box::new(v_transactions_additional_totals::Migration),
]
}

Expand Down
Loading

0 comments on commit f339981

Please sign in to comment.