diff options
Diffstat (limited to 'packages/taler-wallet-core/src/util/coinSelection.ts')
-rw-r--r-- | packages/taler-wallet-core/src/util/coinSelection.ts | 79 |
1 files changed, 39 insertions, 40 deletions
diff --git a/packages/taler-wallet-core/src/util/coinSelection.ts b/packages/taler-wallet-core/src/util/coinSelection.ts index be868867d..0f6316bce 100644 --- a/packages/taler-wallet-core/src/util/coinSelection.ts +++ b/packages/taler-wallet-core/src/util/coinSelection.ts @@ -58,7 +58,7 @@ import { DenominationRecord } from "../db.js"; import { getExchangeWireDetailsInTx, isWithdrawableDenom, - WalletDbReadOnlyTransactionArr, + WalletDbReadOnlyTransaction, } from "../index.js"; import { InternalWalletState } from "../internal-wallet-state.js"; import { @@ -359,33 +359,31 @@ export async function selectPayCoinsNew( logger.trace(`coin selection request ${j2s(req)}`); logger.trace(`selected coins (via denoms) for payment: ${j2s(finalSel)}`); - await ws.db - .mktx((x) => [x.coins, x.denominations]) - .runReadOnly(async (tx) => { - for (const dph of Object.keys(finalSel)) { - const selInfo = finalSel[dph]; - const numRequested = selInfo.contributions.length; - const query = [ - selInfo.exchangeBaseUrl, - selInfo.denomPubHash, - selInfo.maxAge, - CoinStatus.Fresh, - ]; - logger.trace(`query: ${j2s(query)}`); - const coins = - await tx.coins.indexes.byExchangeDenomPubHashAndAgeAndStatus.getAll( - query, - numRequested, - ); - if (coins.length != numRequested) { - throw Error( - `coin selection failed (not available anymore, got only ${coins.length}/${numRequested})`, - ); - } - coinPubs.push(...coins.map((x) => x.coinPub)); - coinContributions.push(...selInfo.contributions); + await ws.db.runReadOnlyTx(["coins", "denominations"], async (tx) => { + for (const dph of Object.keys(finalSel)) { + const selInfo = finalSel[dph]; + const numRequested = selInfo.contributions.length; + const query = [ + selInfo.exchangeBaseUrl, + selInfo.denomPubHash, + selInfo.maxAge, + CoinStatus.Fresh, + ]; + logger.trace(`query: ${j2s(query)}`); + const coins = + await tx.coins.indexes.byExchangeDenomPubHashAndAgeAndStatus.getAll( + query, + numRequested, + ); + if (coins.length != numRequested) { + throw Error( + `coin selection failed (not available anymore, got only ${coins.length}/${numRequested})`, + ); } - }); + coinPubs.push(...coins.map((x) => x.coinPub)); + coinContributions.push(...selInfo.contributions); + } + }); return { type: "success", @@ -911,7 +909,7 @@ export interface PeerCoinSelectionRequest { */ async function selectPayPeerCandidatesForExchange( ws: InternalWalletState, - tx: WalletDbReadOnlyTransactionArr<["coinAvailability", "denominations"]>, + tx: WalletDbReadOnlyTransaction<["coinAvailability", "denominations"]>, exchangeBaseUrl: string, ): Promise<AvailableDenom[]> { const denoms: AvailableDenom[] = []; @@ -1048,17 +1046,17 @@ export async function selectPeerCoins( // one coin to spend. throw new Error("amount of zero not allowed"); } - return await ws.db - .mktx((x) => [ - x.exchanges, - x.contractTerms, - x.coins, - x.coinAvailability, - x.denominations, - x.refreshGroups, - x.peerPushDebit, - ]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + [ + "exchanges", + "contractTerms", + "coins", + "coinAvailability", + "denominations", + "refreshGroups", + "peerPushDebit", + ], + async (tx) => { const exchanges = await tx.exchanges.iter().toArray(); const exchangeFeeGap: { [url: string]: AmountJson } = {}; const currency = Amounts.currencyOf(instructedAmount); @@ -1232,5 +1230,6 @@ export async function selectPeerCoins( }; return { type: "failure", insufficientBalanceDetails: errDetails }; - }); + }, + ); } |