Skip to content
Snippets Groups Projects
Commit fa9e1550 authored by Éloïs's avatar Éloïs
Browse files

[feat] gva: balance: return null if the pubkey has never transited money

parent c58446ef
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,7 @@ impl AccountBalanceQuery {
&self,
ctx: &async_graphql::Context<'_>,
#[graphql(desc = "Account script or public key")] script: String,
) -> async_graphql::Result<AmountWithBase> {
) -> async_graphql::Result<Option<AmountWithBase>> {
let account_script = if let Ok(pubkey) = PublicKey::from_base58(&script) {
WalletScriptV10::single_sig(pubkey)
} else {
......@@ -34,17 +34,14 @@ impl AccountBalanceQuery {
let data = ctx.data::<GvaSchemaData>()?;
let dbs_reader = data.dbs_reader();
let balance = data
Ok(data
.dbs_pool
.execute(move |_| dbs_reader.get_account_balance(&account_script))
.await??
.unwrap_or_default()
.0;
Ok(AmountWithBase {
amount: balance.amount() as i32,
base: balance.base() as i32,
})
.map(|balance| AmountWithBase {
amount: balance.0.amount() as i32,
base: balance.0.base() as i32,
}))
}
}
......
......@@ -281,14 +281,10 @@ fn decrease_account_balance<B: Backend>(
balances.get(WalletConditionsV2::from_ref(&account_script))?
{
let new_balance = balance - decrease_amount;
if new_balance > SourceAmount::ZERO {
balances.upsert(
WalletConditionsV2(account_script),
SourceAmountValV2(new_balance),
);
} else {
balances.remove(WalletConditionsV2(account_script));
}
}
Ok(())
}
......@@ -454,7 +450,7 @@ mod tests {
gva_db
.balances()
.get(WalletConditionsV2::from_ref(&script2))?,
None
Some(SourceAmountValV2(SourceAmount::ZERO))
);
assert_eq!(
gva_db
......@@ -494,7 +490,7 @@ mod tests {
gva_db
.balances()
.get(WalletConditionsV2::from_ref(&script2))?,
None
Some(SourceAmountValV2(SourceAmount::ZERO))
);
assert_eq!(
gva_db
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment