From 28dce57f92d5f1fc276098e262aa37139c614e26 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 28 Jun 2023 11:38:01 -0300 Subject: fix: 7740 check max on p2p push --- .../src/cta/TransferCreate/state.ts | 57 +++++++++++++++++----- .../src/wallet/Transaction.tsx | 2 +- packages/taler-wallet-webextension/src/wxApi.ts | 14 ++++-- 3 files changed, 58 insertions(+), 15 deletions(-) (limited to 'packages/taler-wallet-webextension') diff --git a/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts b/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts index 80b8a01bd..dcd41bcc1 100644 --- a/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts +++ b/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts @@ -14,15 +14,21 @@ GNU Taler; see the file COPYING. If not, see */ -import { Amounts, TalerProtocolTimestamp } from "@gnu-taler/taler-util"; +import { + Amounts, + TalerError, + TalerErrorCode, + TalerProtocolTimestamp, +} from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { isFuture, parse } from "date-fns"; -import { useState } from "preact/hooks"; +import { useEffect, useState } from "preact/hooks"; import { alertFromError, useAlertContext } from "../../context/alert.js"; import { useBackendContext } from "../../context/backend.js"; import { useTranslationContext } from "@gnu-taler/web-util/browser"; import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js"; import { Props, State } from "./index.js"; +import { BackgroundError, WxApiType } from "../../wxApi.js"; export function useComponentState({ amount: amountStr, @@ -38,9 +44,7 @@ export function useComponentState({ const [timestamp, setTimestamp] = useState(); const hook = useAsyncAsHook(async () => { - const resp = await api.wallet.call(WalletApiOperation.CheckPeerPushDebit, { - amount: amountStr, - }); + const resp = await checkPeerPushDebitAndCheckMax(api, amountStr); return resp; }); @@ -59,12 +63,6 @@ export function useComponentState({ ), }; } - // if (hook.hasError) { - // return { - // status: "loading-uri", - // error: hook, - // }; - // } const { amountEffective, amountRaw } = hook.response; const debitAmount = Amounts.parseOrThrow(amountEffective); @@ -140,3 +138,40 @@ export function useComponentState({ error: undefined, }; } + +async function checkPeerPushDebitAndCheckMax( + api: WxApiType, + amountState: string, +) { + // FIXME : https://bugs.gnunet.org/view.php?id=7872 + try { + return await api.wallet.call(WalletApiOperation.CheckPeerPushDebit, { + amount: amountState, + }); + } catch (e) { + if (!(e instanceof BackgroundError)) { + throw e; + } + if ( + !e.hasErrorCode( + TalerErrorCode.WALLET_PEER_PUSH_PAYMENT_INSUFFICIENT_BALANCE, + ) + ) { + throw e; + } + const material = Amounts.parseOrThrow( + e.errorDetail.insufficientBalanceDetails.balanceMaterial, + ); + const gap = Amounts.parseOrThrow( + e.errorDetail.insufficientBalanceDetails.feeGapEstimate, + ); + const newAmount = Amounts.sub(material, gap).amount; + const amount = Amounts.parseOrThrow(amountState); + if (Amounts.cmp(newAmount, amount) === 0) { + //insufficient balance and the exception didn't give + //a good response that allow us to try again + throw e; + } + return checkPeerPushDebitAndCheckMax(api, Amounts.stringify(newAmount)); + } +} diff --git a/packages/taler-wallet-webextension/src/wallet/Transaction.tsx b/packages/taler-wallet-webextension/src/wallet/Transaction.tsx index 17a921b54..fbd6f6ea4 100644 --- a/packages/taler-wallet-webextension/src/wallet/Transaction.tsx +++ b/packages/taler-wallet-webextension/src/wallet/Transaction.tsx @@ -1741,7 +1741,7 @@ function DepositDetails({ amount }: { amount: AmountWithFee }): VNode { - Total transfer + Total diff --git a/packages/taler-wallet-webextension/src/wxApi.ts b/packages/taler-wallet-webextension/src/wxApi.ts index ce1dac14f..46c9f1b2d 100644 --- a/packages/taler-wallet-webextension/src/wxApi.ts +++ b/packages/taler-wallet-webextension/src/wxApi.ts @@ -24,9 +24,11 @@ import { AbsoluteTime, CoreApiResponse, + DetailsMap, Logger, LogLevel, NotificationType, + TalerError, TalerErrorCode, TalerErrorDetail, WalletDiagnostics, @@ -92,13 +94,19 @@ export interface BackgroundApiClient { ): Promise; } -export class BackgroundError extends Error { - public errorDetail: TalerErrorDetail; +export class BackgroundError extends Error { + public errorDetail: TalerErrorDetail & T; - constructor(title: string, e: TalerErrorDetail) { + constructor(title: string, e: TalerErrorDetail & T) { super(title); this.errorDetail = e; } + + hasErrorCode( + code: C, + ): this is BackgroundError { + return this.errorDetail.code === code; + } } /** -- cgit v1.2.3