feat: optimize SQL balance calculation (#1152)

* feat: optimize SQL balance calculation

replace proof-fetching approach with SUM aggregation

- add get_balance() method to Database trait
- implement SQL SUM aggregation in cdk-sql-common
- update total_balance() to use get_balance() instead of get_unspent_proofs()
- redb impl maintains existing behavior

---------

Co-authored-by: thesimplekid <tsk@thesimplekid.com>
Co-authored-by: Cesar Rodas <cesar@rodasm.com.py>
This commit is contained in:
vnprc
2025-10-06 04:29:57 -04:00
committed by GitHub
parent a8c35dbef0
commit 1a493d61f8
8 changed files with 150 additions and 2 deletions

View File

@@ -721,6 +721,18 @@ impl WalletDatabase for WalletRedbDatabase {
Ok(proofs)
}
async fn get_balance(
&self,
mint_url: Option<MintUrl>,
unit: Option<CurrencyUnit>,
state: Option<Vec<State>>,
) -> Result<u64, database::Error> {
// For redb, we still need to fetch all proofs and sum them
// since redb doesn't have SQL aggregation
let proofs = self.get_proofs(mint_url, unit, state, None).await?;
Ok(proofs.iter().map(|p| u64::from(p.proof.amount)).sum())
}
async fn update_proofs_state(
&self,
ys: Vec<PublicKey>,