diff options
author | Sebastian <sebasjm@gmail.com> | 2024-05-20 12:48:44 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-05-20 12:48:44 -0300 |
commit | abafae8a1bf5b8b22b09438eac1d2292b6f836f2 (patch) | |
tree | 9c41e2f19cb40dd112521087ddfc0d78d799b913 /packages/taler-wallet-core/src/transactions.ts | |
parent | 184c3bcd2d7aabbc033b035fda34e86b3df2f98a (diff) | |
download | wallet-core-abafae8a1bf5b8b22b09438eac1d2292b6f836f2.tar.xz |
fix #8856 #8840
Diffstat (limited to 'packages/taler-wallet-core/src/transactions.ts')
-rw-r--r-- | packages/taler-wallet-core/src/transactions.ts | 86 |
1 files changed, 49 insertions, 37 deletions
diff --git a/packages/taler-wallet-core/src/transactions.ts b/packages/taler-wallet-core/src/transactions.ts index f36380033..b4809bfed 100644 --- a/packages/taler-wallet-core/src/transactions.ts +++ b/packages/taler-wallet-core/src/transactions.ts @@ -93,6 +93,7 @@ import { computeDenomLossTransactionStatus, DenomLossTransactionContext, ExchangeWireDetails, + fetchFreshExchange, getExchangeWireDetailsInTx, } from "./exchanges.js"; import { @@ -243,24 +244,22 @@ export async function getTransactionById( const opId = TaskIdentifiers.forWithdrawal(withdrawalGroupRecord); const ort = await tx.operationRetries.get(opId); + const exchangeDetails = await getExchangeWireDetailsInTx( + tx, + withdrawalGroupRecord.exchangeBaseUrl, + ); + if (!exchangeDetails) throw Error("not exchange details"); + if ( withdrawalGroupRecord.wgInfo.withdrawalType === WithdrawalRecordType.BankIntegrated ) { return buildTransactionForBankIntegratedWithdraw( withdrawalGroupRecord, + exchangeDetails, ort, ); } - checkDbInvariant( - withdrawalGroupRecord.exchangeBaseUrl !== undefined, - "manual withdraw should have exchange url", - ); - const exchangeDetails = await getExchangeWireDetailsInTx( - tx, - withdrawalGroupRecord.exchangeBaseUrl, - ); - if (!exchangeDetails) throw Error("not exchange details"); return buildTransactionForManualWithdraw( withdrawalGroupRecord, @@ -595,7 +594,6 @@ function buildTransactionForPeerPullCredit( const txState = computePeerPullCreditTransactionState(pullCredit); checkDbInvariant(wsr.instructedAmount !== undefined, "wg unitialized"); checkDbInvariant(wsr.denomsSel !== undefined, "wg unitialized"); - checkDbInvariant(wsr.exchangeBaseUrl !== undefined, "wg unitialized"); return { type: TransactionType.PeerPullCredit, txState, @@ -670,7 +668,6 @@ function buildTransactionForPeerPushCredit( } checkDbInvariant(wg.instructedAmount !== undefined, "wg unitialized"); checkDbInvariant(wg.denomsSel !== undefined, "wg unitialized"); - checkDbInvariant(wg.exchangeBaseUrl !== undefined, "wg unitialized"); const txState = computePeerPushCreditTransactionState(pushInc); return { @@ -723,23 +720,28 @@ function buildTransactionForPeerPushCredit( function buildTransactionForBankIntegratedWithdraw( wg: WithdrawalGroupRecord, + exchangeDetails: ExchangeWireDetails, ort?: OperationRetryRecord, ): TransactionWithdrawal { - if (wg.wgInfo.withdrawalType !== WithdrawalRecordType.BankIntegrated) + if (wg.wgInfo.withdrawalType !== WithdrawalRecordType.BankIntegrated) { throw Error(""); - + } const txState = computeWithdrawalTransactionStatus(wg); - checkDbInvariant(wg.instructedAmount !== undefined, "wg unitialized"); - checkDbInvariant(wg.denomsSel !== undefined, "wg unitialized"); - checkDbInvariant(wg.exchangeBaseUrl !== undefined, "wg unitialized"); + const zero = Amounts.stringify( + Amounts.zeroOfCurrency(exchangeDetails.currency), + ); return { type: TransactionType.Withdrawal, txState, txActions: computeWithdrawalTransactionActions(wg), - amountEffective: isUnsuccessfulTransaction(txState) - ? Amounts.stringify(Amounts.zeroOfAmount(wg.instructedAmount)) - : Amounts.stringify(wg.denomsSel.totalCoinValue), - amountRaw: Amounts.stringify(wg.instructedAmount), + exchangeBaseUrl: wg.exchangeBaseUrl, + amountEffective: + isUnsuccessfulTransaction(txState) || !wg.denomsSel + ? zero + : Amounts.stringify(wg.denomsSel.totalCoinValue), + amountRaw: !wg.instructedAmount + ? zero + : Amounts.stringify(wg.instructedAmount), withdrawalDetails: { type: WithdrawalType.TalerBankIntegrationApi, confirmed: wg.wgInfo.bankInfo.timestampBankConfirmed ? true : false, @@ -751,7 +753,6 @@ function buildTransactionForBankIntegratedWithdraw( wg.status === WithdrawalGroupStatus.PendingReady, }, kycUrl: wg.kycUrl, - exchangeBaseUrl: wg.exchangeBaseUrl, timestamp: timestampPreciseFromDb(wg.timestampStart), transactionId: constructTransactionIdentifier({ tag: TransactionType.Withdrawal, @@ -784,7 +785,6 @@ function buildTransactionForManualWithdraw( checkDbInvariant(wg.instructedAmount !== undefined, "wg unitialized"); checkDbInvariant(wg.denomsSel !== undefined, "wg unitialized"); - checkDbInvariant(wg.exchangeBaseUrl !== undefined, "wg unitialized"); const exchangePaytoUris = augmentPaytoUrisForWithdrawal( plainPaytoUris, wg.reservePub, @@ -996,12 +996,12 @@ async function lookupMaybeContractData( return contractData; } -async function buildTransactionForPurchase( +function buildTransactionForPurchase( purchaseRecord: PurchaseRecord, contractData: WalletContractData, refundsInfo: RefundGroupRecord[], ort?: OperationRetryRecord, -): Promise<Transaction> { +): Transaction { const zero = Amounts.zeroOfAmount(contractData.amount); const info: OrderShortInfo = { @@ -1094,24 +1094,22 @@ export async function getWithdrawalTransactionByUri( const opId = TaskIdentifiers.forWithdrawal(withdrawalGroupRecord); const ort = await tx.operationRetries.get(opId); + const exchangeDetails = await getExchangeWireDetailsInTx( + tx, + withdrawalGroupRecord.exchangeBaseUrl, + ); + if (!exchangeDetails) throw Error("not exchange details"); + if ( withdrawalGroupRecord.wgInfo.withdrawalType === WithdrawalRecordType.BankIntegrated ) { return buildTransactionForBankIntegratedWithdraw( withdrawalGroupRecord, + exchangeDetails, ort, ); } - checkDbInvariant( - withdrawalGroupRecord.exchangeBaseUrl !== undefined, - "manual withdraw should have exchange url", - ); - const exchangeDetails = await getExchangeWireDetailsInTx( - tx, - withdrawalGroupRecord.exchangeBaseUrl, - ); - if (!exchangeDetails) throw Error("not exchange details"); return buildTransactionForManualWithdraw( withdrawalGroupRecord, @@ -1390,11 +1388,26 @@ export async function getTransactions( // FIXME: If this is an orphan withdrawal, still report it as a withdrawal! // FIXME: Still report if requested with verbose option? return; - case WithdrawalRecordType.BankIntegrated: + case WithdrawalRecordType.BankIntegrated: { + const exchangeDetails = await getExchangeWireDetailsInTx( + tx, + wsr.exchangeBaseUrl, + ); + if (!exchangeDetails) { + // FIXME: report somehow + return; + } + transactions.push( - buildTransactionForBankIntegratedWithdraw(wsr, ort), + buildTransactionForBankIntegratedWithdraw( + wsr, + exchangeDetails, + ort, + ), ); return; + } + case WithdrawalRecordType.BankManual: { const exchangeDetails = await getExchangeWireDetailsInTx( tx, @@ -1404,7 +1417,6 @@ export async function getTransactions( // FIXME: report somehow return; } - transactions.push( buildTransactionForManualWithdraw(wsr, exchangeDetails, ort), ); @@ -1505,7 +1517,7 @@ export async function getTransactions( ); transactions.push( - await buildTransactionForPurchase( + buildTransactionForPurchase( purchase, contractData, refunds, |