diff options
Diffstat (limited to 'packages/taler-wallet-core/src/operations/balance.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/balance.ts | 188 |
1 files changed, 87 insertions, 101 deletions
diff --git a/packages/taler-wallet-core/src/operations/balance.ts b/packages/taler-wallet-core/src/operations/balance.ts index a73476e9c..12f9795b2 100644 --- a/packages/taler-wallet-core/src/operations/balance.ts +++ b/packages/taler-wallet-core/src/operations/balance.ts @@ -71,14 +71,12 @@ import { OPERATION_STATUS_ACTIVE_LAST, RefreshGroupRecord, RefreshOperationStatus, - WalletDbReadOnlyTransactionArr, - WalletStoresV1, + WalletDbReadOnlyTransaction, WithdrawalGroupStatus, } from "../db.js"; import { InternalWalletState } from "../internal-wallet-state.js"; import { assertUnreachable } from "../util/assertUnreachable.js"; import { checkLogicInvariant } from "../util/invariants.js"; -import { GetReadOnlyAccess } from "../util/query.js"; import { getExchangeScopeInfo, getExchangeWireDetailsInTx, @@ -134,7 +132,7 @@ class BalancesStore { constructor( private ws: InternalWalletState, - private tx: WalletDbReadOnlyTransactionArr< + private tx: WalletDbReadOnlyTransaction< [ "globalCurrencyAuditors", "globalCurrencyExchanges", @@ -275,7 +273,7 @@ class BalancesStore { */ export async function getBalancesInsideTransaction( ws: InternalWalletState, - tx: WalletDbReadOnlyTransactionArr< + tx: WalletDbReadOnlyTransaction< [ "exchanges", "exchangeDetails", @@ -465,81 +463,79 @@ export async function getAcceptableExchangeBaseUrls( ): Promise<AcceptableExchanges> { const acceptableExchangeUrls = new Set<string>(); const depositableExchangeUrls = new Set<string>(); - await ws.db - .mktx((x) => [x.exchanges, x.exchangeDetails]) - .runReadOnly(async (tx) => { - // FIXME: We should have a DB index to look up all exchanges - // for a particular auditor ... - - const canonExchanges = new Set<string>(); - const canonAuditors = new Set<string>(); - - for (const exchangeHandle of req.acceptedExchanges) { - const normUrl = canonicalizeBaseUrl(exchangeHandle.exchangeBaseUrl); - canonExchanges.add(normUrl); - } + await ws.db.runReadOnlyTx(["exchanges", "exchangeDetails"], async (tx) => { + // FIXME: We should have a DB index to look up all exchanges + // for a particular auditor ... - for (const auditorHandle of req.acceptedAuditors) { - const normUrl = canonicalizeBaseUrl(auditorHandle.auditorBaseUrl); - canonAuditors.add(normUrl); - } + const canonExchanges = new Set<string>(); + const canonAuditors = new Set<string>(); - await tx.exchanges.iter().forEachAsync(async (exchange) => { - const dp = exchange.detailsPointer; - if (!dp) { - return; - } - const { currency, masterPublicKey } = dp; - const exchangeDetails = await tx.exchangeDetails.indexes.byPointer.get([ - exchange.baseUrl, - currency, - masterPublicKey, - ]); - if (!exchangeDetails) { - return; - } + for (const exchangeHandle of req.acceptedExchanges) { + const normUrl = canonicalizeBaseUrl(exchangeHandle.exchangeBaseUrl); + canonExchanges.add(normUrl); + } - let acceptable = false; + for (const auditorHandle of req.acceptedAuditors) { + const normUrl = canonicalizeBaseUrl(auditorHandle.auditorBaseUrl); + canonAuditors.add(normUrl); + } - if (canonExchanges.has(exchange.baseUrl)) { + await tx.exchanges.iter().forEachAsync(async (exchange) => { + const dp = exchange.detailsPointer; + if (!dp) { + return; + } + const { currency, masterPublicKey } = dp; + const exchangeDetails = await tx.exchangeDetails.indexes.byPointer.get([ + exchange.baseUrl, + currency, + masterPublicKey, + ]); + if (!exchangeDetails) { + return; + } + + let acceptable = false; + + if (canonExchanges.has(exchange.baseUrl)) { + acceptableExchangeUrls.add(exchange.baseUrl); + acceptable = true; + } + for (const exchangeAuditor of exchangeDetails.auditors) { + if (canonAuditors.has(exchangeAuditor.auditor_url)) { acceptableExchangeUrls.add(exchange.baseUrl); acceptable = true; + break; } - for (const exchangeAuditor of exchangeDetails.auditors) { - if (canonAuditors.has(exchangeAuditor.auditor_url)) { - acceptableExchangeUrls.add(exchange.baseUrl); - acceptable = true; + } + + if (!acceptable) { + return; + } + // FIXME: Also consider exchange and auditor public key + // instead of just base URLs? + + let wireMethodSupported = false; + for (const acc of exchangeDetails.wireInfo.accounts) { + const pp = parsePaytoUri(acc.payto_uri); + checkLogicInvariant(!!pp); + for (const wm of req.acceptedWireMethods) { + if (pp.targetType === wm) { + wireMethodSupported = true; break; } - } - - if (!acceptable) { - return; - } - // FIXME: Also consider exchange and auditor public key - // instead of just base URLs? - - let wireMethodSupported = false; - for (const acc of exchangeDetails.wireInfo.accounts) { - const pp = parsePaytoUri(acc.payto_uri); - checkLogicInvariant(!!pp); - for (const wm of req.acceptedWireMethods) { - if (pp.targetType === wm) { - wireMethodSupported = true; - break; - } - if (wireMethodSupported) { - break; - } + if (wireMethodSupported) { + break; } } + } - acceptableExchangeUrls.add(exchange.baseUrl); - if (wireMethodSupported) { - depositableExchangeUrls.add(exchange.baseUrl); - } - }); + acceptableExchangeUrls.add(exchange.baseUrl); + if (wireMethodSupported) { + depositableExchangeUrls.add(exchange.baseUrl); + } }); + }); return { acceptableExchanges: [...acceptableExchangeUrls], depositableExchanges: [...depositableExchangeUrls], @@ -587,15 +583,9 @@ export async function getMerchantPaymentBalanceDetails( balanceMerchantDepositable: Amounts.zeroOfCurrency(req.currency), }; - await ws.db - .mktx((x) => [ - x.coins, - x.coinAvailability, - x.refreshGroups, - x.purchases, - x.withdrawalGroups, - ]) - .runReadOnly(async (tx) => { + await ws.db.runReadOnlyTx( + ["coinAvailability", "refreshGroups"], + async (tx) => { await tx.coinAvailability.iter().forEach((ca) => { if (ca.currency != req.currency) { return; @@ -638,7 +628,8 @@ export async function getMerchantPaymentBalanceDetails( computeRefreshGroupAvailableAmount(r), ).amount; }); - }); + }, + ); return d; } @@ -649,27 +640,25 @@ export async function getBalanceDetail( ): Promise<MerchantPaymentBalanceDetails> { const exchanges: { exchangeBaseUrl: string; exchangePub: string }[] = []; const wires = new Array<string>(); - await ws.db - .mktx((x) => [x.exchanges, x.exchangeDetails]) - .runReadOnly(async (tx) => { - const allExchanges = await tx.exchanges.iter().toArray(); - for (const e of allExchanges) { - const details = await getExchangeWireDetailsInTx(tx, e.baseUrl); - if (!details || req.currency !== details.currency) { - continue; - } - details.wireInfo.accounts.forEach((a) => { - const payto = parsePaytoUri(a.payto_uri); - if (payto && !wires.includes(payto.targetType)) { - wires.push(payto.targetType); - } - }); - exchanges.push({ - exchangePub: details.masterPublicKey, - exchangeBaseUrl: e.baseUrl, - }); + await ws.db.runReadOnlyTx(["exchanges", "exchangeDetails"], async (tx) => { + const allExchanges = await tx.exchanges.iter().toArray(); + for (const e of allExchanges) { + const details = await getExchangeWireDetailsInTx(tx, e.baseUrl); + if (!details || req.currency !== details.currency) { + continue; } - }); + details.wireInfo.accounts.forEach((a) => { + const payto = parsePaytoUri(a.payto_uri); + if (payto && !wires.includes(payto.targetType)) { + wires.push(payto.targetType); + } + }); + exchanges.push({ + exchangePub: details.masterPublicKey, + exchangeBaseUrl: e.baseUrl, + }); + } + }); return await getMerchantPaymentBalanceDetails(ws, { currency: req.currency, @@ -699,10 +688,7 @@ export interface PeerPaymentBalanceDetails { export async function getPeerPaymentBalanceDetailsInTx( ws: InternalWalletState, - tx: GetReadOnlyAccess<{ - coinAvailability: typeof WalletStoresV1.coinAvailability; - refreshGroups: typeof WalletStoresV1.refreshGroups; - }>, + tx: WalletDbReadOnlyTransaction<["coinAvailability", "refreshGroups"]>, req: PeerPaymentRestrictionsForBalance, ): Promise<PeerPaymentBalanceDetails> { let balanceAvailable = Amounts.zeroOfCurrency(req.currency); |