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 /packages/taler-wallet-webextension/src/cta | |
parent | ce3c3d78392c25300d0a96e140c6092a6b8a898d (diff) | |
download | wallet-core-28dce57f92d5f1fc276098e262aa37139c614e26.tar.xz |
fix: 7740 check max on p2p push
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta')
-rw-r--r-- | packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts | 57 |
1 files changed, 46 insertions, 11 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)); + } +} |