From 417c07f3f4866918e1aaa6d42b7d5ec0ca59dd51 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 6 Jan 2023 13:55:08 +0100 Subject: wallet-core: insufficient balance details for p2p payments --- .../taler-wallet-core/src/operations/balance.ts | 73 +++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) (limited to 'packages/taler-wallet-core/src/operations/balance.ts') diff --git a/packages/taler-wallet-core/src/operations/balance.ts b/packages/taler-wallet-core/src/operations/balance.ts index f697679af..383aa5dc1 100644 --- a/packages/taler-wallet-core/src/operations/balance.ts +++ b/packages/taler-wallet-core/src/operations/balance.ts @@ -56,7 +56,12 @@ import { canonicalizeBaseUrl, parsePaytoUri, } from "@gnu-taler/taler-util"; -import { AllowedAuditorInfo, AllowedExchangeInfo, RefreshGroupRecord, WalletStoresV1 } from "../db.js"; +import { + AllowedAuditorInfo, + AllowedExchangeInfo, + RefreshGroupRecord, + WalletStoresV1, +} from "../db.js"; import { GetReadOnlyAccess } from "../util/query.js"; import { InternalWalletState } from "../internal-wallet-state.js"; import { getExchangeDetails } from "./exchanges.js"; @@ -362,7 +367,7 @@ export async function getMerchantPaymentBalanceDetails( balanceMerchantDepositable: Amounts.zeroOfCurrency(req.currency), }; - const wbal = await ws.db + await ws.db .mktx((x) => [ x.coins, x.coinAvailability, @@ -415,3 +420,67 @@ export async function getMerchantPaymentBalanceDetails( return d; } + +export interface PeerPaymentRestrictionsForBalance { + currency: string; + restrictExchangeTo?: string; +} + +export interface PeerPaymentBalanceDetails { + /** + * Balance of type "available" (see balance.ts for definition). + */ + balanceAvailable: AmountJson; + + /** + * Balance of type "material" (see balance.ts for definition). + */ + balanceMaterial: AmountJson; +} + +export async function getPeerPaymentBalanceDetailsInTx( + ws: InternalWalletState, + tx: GetReadOnlyAccess<{ + coinAvailability: typeof WalletStoresV1.coinAvailability; + refreshGroups: typeof WalletStoresV1.refreshGroups; + }>, + req: PeerPaymentRestrictionsForBalance, +): Promise { + let balanceAvailable = Amounts.zeroOfCurrency(req.currency); + let balanceMaterial = Amounts.zeroOfCurrency(req.currency); + + await tx.coinAvailability.iter().forEach((ca) => { + if (ca.currency != req.currency) { + return; + } + if ( + req.restrictExchangeTo && + req.restrictExchangeTo !== ca.exchangeBaseUrl + ) { + return; + } + const singleCoinAmount: AmountJson = { + currency: ca.currency, + fraction: ca.amountFrac, + value: ca.amountVal, + }; + const coinAmount: AmountJson = Amounts.mult( + singleCoinAmount, + ca.freshCoinCount, + ).amount; + balanceAvailable = Amounts.add(balanceAvailable, coinAmount).amount; + balanceMaterial = Amounts.add(balanceMaterial, coinAmount).amount; + }); + + await tx.refreshGroups.iter().forEach((r) => { + balanceAvailable = Amounts.add( + balanceAvailable, + computeRefreshGroupAvailableAmount(r), + ).amount; + }); + + return { + balanceAvailable, + balanceMaterial, + }; +} -- cgit v1.2.3