diff options
author | Sebastian <sebasjm@gmail.com> | 2023-06-28 11:38:01 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-06-28 11:38:01 -0300 |
commit | 28dce57f92d5f1fc276098e262aa37139c614e26 (patch) | |
tree | 95630884ef03897e1040abde5aa14a8387f27e1d | |
parent | ce3c3d78392c25300d0a96e140c6092a6b8a898d (diff) |
fix: 7740 check max on p2p push
4 files changed, 59 insertions, 16 deletions
diff --git a/packages/taler-util/src/taleruri.test.ts b/packages/taler-util/src/taleruri.test.ts index 3244bbbd9..eb74436cb 100644 --- a/packages/taler-util/src/taleruri.test.ts +++ b/packages/taler-util/src/taleruri.test.ts @@ -367,6 +367,6 @@ test("taler withdraw exchange URI with amount (stringify)", (t) => { }); t.deepEqual( url, - "taler://withdraw-exchange/exchange.demo.taler.net/GJKG23V4ZBHEH45YRK7TWQE8ZTY7JWTY5094TQJSRZN5DSDBX8E0?a=KUDOS%3A19", + "taler://withdraw-exchange/exchange.demo.taler.net/JFX1NE38C65A5XT8VSNQXX7R7BBG4GNZ63F5T7Y6859V4J8KBKF0?a=KUDOS%3A19", ); }); 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; + } } /** |