diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src')
3 files changed, 58 insertions, 15 deletions
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 <http://www.gnu.org/licenses/> */ -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<string | undefined>(); 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 { </tr> <tr> <td> - <i18n.Translate>Total transfer</i18n.Translate> + <i18n.Translate>Total</i18n.Translate> </td> <td> <Amount value={amount.total} maxFracSize={amount.maxFrac} /> 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<BackgroundOperations[Op]["response"]>; } -export class BackgroundError extends Error { - public errorDetail: TalerErrorDetail; +export class BackgroundError<T = any> extends Error { + public errorDetail: TalerErrorDetail & T; - constructor(title: string, e: TalerErrorDetail) { + constructor(title: string, e: TalerErrorDetail & T) { super(title); this.errorDetail = e; } + + hasErrorCode<C extends keyof DetailsMap>( + code: C, + ): this is BackgroundError<DetailsMap[C]> { + return this.errorDetail.code === code; + } } /** |