diff options
author | Florian Dold <florian@dold.me> | 2024-05-15 23:31:09 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-05-15 23:31:09 +0200 |
commit | 6529421a5c09dec082f33ea80b1df2861e0bf5c9 (patch) | |
tree | 94188eb6f0889d48d790151496b26d934e260ed0 /packages | |
parent | 2b1e808d553d21beba3bafa09ef606035fcdb378 (diff) | |
download | wallet-core-6529421a5c09dec082f33ea80b1df2861e0bf5c9.tar.xz |
wallet-core: withdraw actual amount from reserve even if instructed amount differs
Diffstat (limited to 'packages')
3 files changed, 138 insertions, 9 deletions
diff --git a/packages/taler-harness/src/integrationtests/test-withdrawal-amount.ts b/packages/taler-harness/src/integrationtests/test-withdrawal-amount.ts new file mode 100644 index 000000000..cd6a1e325 --- /dev/null +++ b/packages/taler-harness/src/integrationtests/test-withdrawal-amount.ts @@ -0,0 +1,94 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + AmountString, + Logger, + WireGatewayApiClient, + j2s, +} from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { GlobalTestState } from "../harness/harness.js"; +import { createSimpleTestkudosEnvironmentV3 } from "../harness/helpers.js"; + +const logger = new Logger("test-withdrawal-manual.ts"); + +/** + * Check what happens when the withdrawal amount unexpectedly changes. + */ +export async function runWithdrawalAmountTest(t: GlobalTestState) { + // Set up test environment + + const { walletClient, bankClient, exchange, exchangeBankAccount } = + await createSimpleTestkudosEnvironmentV3(t); + + const wireGatewayApiClient = new WireGatewayApiClient( + exchangeBankAccount.wireGatewayApiBaseUrl, + { + auth: { + username: "admin", + password: "adminpw", + }, + }, + ); + + // Create a withdrawal operation + + const user = await bankClient.createRandomBankUser(); + + await walletClient.call(WalletApiOperation.AddExchange, { + exchangeBaseUrl: exchange.baseUrl, + }); + + logger.info("starting AcceptManualWithdrawal request"); + + const wres = await walletClient.call( + WalletApiOperation.AcceptManualWithdrawal, + { + exchangeBaseUrl: exchange.baseUrl, + amount: "TESTKUDOS:10" as AmountString, + }, + ); + + logger.info("AcceptManualWithdrawal finished"); + logger.info(`result: ${j2s(wres)}`); + + const reservePub: string = wres.reservePub; + + await wireGatewayApiClient.adminAddIncoming({ + amount: "TESTKUDOS:5", + debitAccountPayto: user.accountPaytoUri, + reservePub: reservePub, + }); + + await exchange.runWirewatchOnce(); + + await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + + // Check balance + + const balResp = await walletClient.call(WalletApiOperation.GetBalances, {}); + + // We managed to withdraw the actually transferred amount! + t.assertAmountEquals(balResp.balances[0].available, "TESTKUDOS:4.85"); + + await t.shutdown(); +} + +runWithdrawalAmountTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/testrunner.ts b/packages/taler-harness/src/integrationtests/testrunner.ts index 4b23d7762..eb2ae7fa6 100644 --- a/packages/taler-harness/src/integrationtests/testrunner.ts +++ b/packages/taler-harness/src/integrationtests/testrunner.ts @@ -120,6 +120,7 @@ import { runWithdrawalFeesTest } from "./test-withdrawal-fees.js"; import { runWithdrawalHandoverTest } from "./test-withdrawal-handover.js"; import { runWithdrawalHugeTest } from "./test-withdrawal-huge.js"; import { runWithdrawalManualTest } from "./test-withdrawal-manual.js"; +import { runWithdrawalAmountTest } from "./test-withdrawal-amount.js"; /** * Test runner. @@ -230,6 +231,7 @@ const allTests: TestMainFunction[] = [ runPeerPullLargeTest, runPeerPushLargeTest, runWithdrawalHandoverTest, + runWithdrawalAmountTest, ]; export interface TestRunSpec { diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts index 814201809..43256d3fe 100644 --- a/packages/taler-wallet-core/src/withdraw.ts +++ b/packages/taler-wallet-core/src/withdraw.ts @@ -1518,15 +1518,48 @@ async function processQueryReserve( logger.trace(`got reserve status ${j2s(result.response)}`); - const transitionResult = await ctx.transition({}, async (wg) => { - if (!wg) { - logger.warn(`withdrawal group ${withdrawalGroupId} not found`); - return TransitionResult.stay(); - } - wg.status = WithdrawalGroupStatus.PendingReady; - wg.reserveBalanceAmount = Amounts.stringify(result.response.balance); - return TransitionResult.transition(wg); - }); + let amountChanged = false; + if ( + Amounts.cmp( + result.response.balance, + withdrawalGroup.denomsSel.totalWithdrawCost, + ) != 0 + ) { + amountChanged = true; + } + + const exchangeBaseUrl = withdrawalGroup.exchangeBaseUrl; + const currency = Amounts.currencyOf(withdrawalGroup.instructedAmount); + + const transitionResult = await ctx.transition( + { + extraStores: ["denominations"], + }, + async (wg, tx) => { + if (!wg) { + logger.warn(`withdrawal group ${withdrawalGroupId} not found`); + return TransitionResult.stay(); + } + if (wg.status !== WithdrawalGroupStatus.PendingQueryingStatus) { + return TransitionResult.stay(); + } + if (amountChanged) { + const candidates = await getCandidateWithdrawalDenomsTx( + wex, + tx, + exchangeBaseUrl, + currency, + ); + wg.denomsSel = selectWithdrawalDenominations( + Amounts.parseOrThrow(result.response.balance), + candidates, + ); + } + wg.status = WithdrawalGroupStatus.PendingReady; + wg.reserveBalanceAmount = Amounts.stringify(result.response.balance); + return TransitionResult.transition(wg); + }, + ); if (transitionResult) { return TaskRunResult.progress(); |