aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/operations/balance.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2023-01-06 13:55:08 +0100
committerFlorian Dold <florian@dold.me>2023-01-06 13:55:08 +0100
commit417c07f3f4866918e1aaa6d42b7d5ec0ca59dd51 (patch)
tree9966f647bb0779cf2de248b805f0ea13a24ddba6 /packages/taler-wallet-core/src/operations/balance.ts
parentc2c35925bb953bf07e32c005dbe312d220b45749 (diff)
downloadwallet-core-417c07f3f4866918e1aaa6d42b7d5ec0ca59dd51.tar.xz
wallet-core: insufficient balance details for p2p payments
Diffstat (limited to 'packages/taler-wallet-core/src/operations/balance.ts')
-rw-r--r--packages/taler-wallet-core/src/operations/balance.ts73
1 files changed, 71 insertions, 2 deletions
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<PeerPaymentBalanceDetails> {
+ 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,
+ };
+}