From 1edc144b3595ae1ab6b8af7a43d26b811b2c2623 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 23 Apr 2024 03:09:40 +0200 Subject: wallet-core: pass options object to all transactions --- .../taler-wallet-core/src/pay-peer-pull-credit.ts | 138 ++++++++++++--------- 1 file changed, 76 insertions(+), 62 deletions(-) (limited to 'packages/taler-wallet-core/src/pay-peer-pull-credit.ts') diff --git a/packages/taler-wallet-core/src/pay-peer-pull-credit.ts b/packages/taler-wallet-core/src/pay-peer-pull-credit.ts index 4155f83e6..840c244d0 100644 --- a/packages/taler-wallet-core/src/pay-peer-pull-credit.ts +++ b/packages/taler-wallet-core/src/pay-peer-pull-credit.ts @@ -110,7 +110,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { async deleteTransaction(): Promise { const { wex: ws, pursePub } = this; await ws.db.runReadWriteTx( - ["withdrawalGroups", "peerPullCredit", "tombstones"], + { storeNames: ["withdrawalGroups", "peerPullCredit", "tombstones"] }, async (tx) => { const pullIni = await tx.peerPullCredit.get(pursePub); if (!pullIni) { @@ -140,7 +140,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { async suspendTransaction(): Promise { const { wex, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { @@ -200,7 +200,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { async failTransaction(): Promise { const { wex, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { @@ -251,7 +251,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { async resumeTransaction(): Promise { const { wex, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { @@ -310,7 +310,7 @@ export class PeerPullCreditTransactionContext implements TransactionContext { async abortTransaction(): Promise { const { wex, pursePub, taskId: retryTag, transactionId } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { @@ -388,7 +388,7 @@ async function queryPurseForPeerPullCredit( case HttpStatusCode.Gone: { // Exchange says that purse doesn't exist anymore => expired! const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const finPi = await tx.peerPullCredit.get(pullIni.pursePub); if (!finPi) { @@ -426,9 +426,12 @@ async function queryPurseForPeerPullCredit( return TaskRunResult.backoff(); } - const reserve = await wex.db.runReadOnlyTx(["reserves"], async (tx) => { - return await tx.reserves.get(pullIni.mergeReserveRowId); - }); + const reserve = await wex.db.runReadOnlyTx( + { storeNames: ["reserves"] }, + async (tx) => { + return await tx.reserves.get(pullIni.mergeReserveRowId); + }, + ); if (!reserve) { throw Error("reserve for peer pull credit not found in wallet DB"); @@ -449,7 +452,7 @@ async function queryPurseForPeerPullCredit( }, }); const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const finPi = await tx.peerPullCredit.get(pullIni.pursePub); if (!finPi) { @@ -497,7 +500,7 @@ async function longpollKycStatus( kycStatusRes.status === HttpStatusCode.NoContent ) { const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const peerIni = await tx.peerPullCredit.get(pursePub); if (!peerIni) { @@ -548,13 +551,15 @@ async function processPeerPullCreditAbortingDeletePurse( logger.info(`deleted purse with response status ${resp.status}`); const transitionInfo = await wex.db.runReadWriteTx( - [ - "peerPullCredit", - "refreshGroups", - "denominations", - "coinAvailability", - "coins", - ], + { + storeNames: [ + "peerPullCredit", + "refreshGroups", + "denominations", + "coinAvailability", + "coins", + ], + }, async (tx) => { const ppiRec = await tx.peerPullCredit.get(pursePub); if (!ppiRec) { @@ -593,7 +598,7 @@ async function handlePeerPullCreditWithdrawing( const wgId = pullIni.withdrawalGroupId; let finished: boolean = false; const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit", "withdrawalGroups"], + { storeNames: ["peerPullCredit", "withdrawalGroups"] }, async (tx) => { const ppi = await tx.peerPullCredit.get(pullIni.pursePub); if (!ppi) { @@ -640,16 +645,19 @@ async function handlePeerPullCreditCreatePurse( ): Promise { const purseFee = Amounts.stringify(Amounts.zeroOfAmount(pullIni.amount)); const pursePub = pullIni.pursePub; - const mergeReserve = await wex.db.runReadOnlyTx(["reserves"], async (tx) => { - return tx.reserves.get(pullIni.mergeReserveRowId); - }); + const mergeReserve = await wex.db.runReadOnlyTx( + { storeNames: ["reserves"] }, + async (tx) => { + return tx.reserves.get(pullIni.mergeReserveRowId); + }, + ); if (!mergeReserve) { throw Error("merge reserve for peer pull payment not found in database"); } const contractTermsRecord = await wex.db.runReadOnlyTx( - ["contractTerms"], + { storeNames: ["contractTerms"] }, async (tx) => { return tx.contractTerms.get(pullIni.contractTermsHash); }, @@ -737,7 +745,7 @@ async function handlePeerPullCreditCreatePurse( }); const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const pi2 = await tx.peerPullCredit.get(pursePub); if (!pi2) { @@ -758,9 +766,12 @@ export async function processPeerPullCredit( wex: WalletExecutionContext, pursePub: string, ): Promise { - const pullIni = await wex.db.runReadOnlyTx(["peerPullCredit"], async (tx) => { - return tx.peerPullCredit.get(pursePub); - }); + const pullIni = await wex.db.runReadOnlyTx( + { storeNames: ["peerPullCredit"] }, + async (tx) => { + return tx.peerPullCredit.get(pursePub); + }, + ); if (!pullIni) { throw Error("peer pull payment initiation not found in database"); } @@ -847,7 +858,7 @@ async function processPeerPullCreditKycRequired( const kycStatus = await kycStatusRes.json(); logger.info(`kyc status: ${j2s(kycStatus)}`); const { transitionInfo, result } = await wex.db.runReadWriteTx( - ["peerPullCredit"], + { storeNames: ["peerPullCredit"] }, async (tx) => { const peerInc = await tx.peerPullCredit.get(pursePub); if (!peerInc) { @@ -947,42 +958,45 @@ async function getPreferredExchangeForCurrency( ): Promise { // Find an exchange with the matching currency. // Prefer exchanges with the most recent withdrawal. - const url = await wex.db.runReadOnlyTx(["exchanges"], async (tx) => { - const exchanges = await tx.exchanges.iter().toArray(); - let candidate = undefined; - for (const e of exchanges) { - if (e.detailsPointer?.currency !== currency) { - continue; - } - if (!candidate) { - candidate = e; - continue; - } - if (candidate.lastWithdrawal && !e.lastWithdrawal) { - continue; - } - const exchangeLastWithdrawal = timestampOptionalPreciseFromDb( - e.lastWithdrawal, - ); - const candidateLastWithdrawal = timestampOptionalPreciseFromDb( - candidate.lastWithdrawal, - ); - if (exchangeLastWithdrawal && candidateLastWithdrawal) { - if ( - AbsoluteTime.cmp( - AbsoluteTime.fromPreciseTimestamp(exchangeLastWithdrawal), - AbsoluteTime.fromPreciseTimestamp(candidateLastWithdrawal), - ) > 0 - ) { + const url = await wex.db.runReadOnlyTx( + { storeNames: ["exchanges"] }, + async (tx) => { + const exchanges = await tx.exchanges.iter().toArray(); + let candidate = undefined; + for (const e of exchanges) { + if (e.detailsPointer?.currency !== currency) { + continue; + } + if (!candidate) { candidate = e; + continue; + } + if (candidate.lastWithdrawal && !e.lastWithdrawal) { + continue; + } + const exchangeLastWithdrawal = timestampOptionalPreciseFromDb( + e.lastWithdrawal, + ); + const candidateLastWithdrawal = timestampOptionalPreciseFromDb( + candidate.lastWithdrawal, + ); + if (exchangeLastWithdrawal && candidateLastWithdrawal) { + if ( + AbsoluteTime.cmp( + AbsoluteTime.fromPreciseTimestamp(exchangeLastWithdrawal), + AbsoluteTime.fromPreciseTimestamp(candidateLastWithdrawal), + ) > 0 + ) { + candidate = e; + } } } - } - if (candidate) { - return candidate.baseUrl; - } - return undefined; - }); + if (candidate) { + return candidate.baseUrl; + } + return undefined; + }, + ); return url; } @@ -1039,7 +1053,7 @@ export async function initiatePeerPullPayment( const mergeTimestamp = TalerPreciseTimestamp.now(); const transitionInfo = await wex.db.runReadWriteTx( - ["peerPullCredit", "contractTerms"], + { storeNames: ["peerPullCredit", "contractTerms"] }, async (tx) => { const ppi: PeerPullCreditRecord = { amount: req.partialContractTerms.amount, -- cgit v1.2.3