diff options
author | Sebastian <sebasjm@gmail.com> | 2024-03-05 14:31:53 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-03-05 14:31:53 -0300 |
commit | c08822cb82907f72f83d5af0c768e2a3785d8e8d (patch) | |
tree | cde163deedf2c8629db8d6464e7ecdf0f2b32889 /packages | |
parent | 0c73adb640c174c8ce7a8c0e9fd9de4c60b49696 (diff) | |
download | wallet-core-c08822cb82907f72f83d5af0c768e2a3785d8e8d.tar.xz |
fix min calculation when isDebit = false
Diffstat (limited to 'packages')
-rw-r--r-- | packages/demobank-ui/src/hooks/circuit.ts | 17 | ||||
-rw-r--r-- | packages/demobank-ui/src/pages/business/CreateCashout.tsx | 19 |
2 files changed, 24 insertions, 12 deletions
diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts index 2c0a58a5e..384e48259 100644 --- a/packages/demobank-ui/src/hooks/circuit.ts +++ b/packages/demobank-ui/src/hooks/circuit.ts @@ -21,6 +21,7 @@ import { AccessToken, AmountJson, Amounts, + HttpStatusCode, OperationOk, TalerBankConversionResultByMethod, TalerCoreBankErrorsByMethod, @@ -41,7 +42,7 @@ export type TransferCalculation = { debit: AmountJson; credit: AmountJson; beforeFee: AmountJson; -}; +} | "amount-is-too-small"; type EstimatorFunction = ( amount: AmountJson, fee: AmountJson, @@ -138,10 +139,16 @@ export function useCashoutEstimator(): ConversionEstimators { credit: fiatAmount, }); if (resp.type === "fail") { - // can't happen - // not-supported: it should not be able to call this function - // wrong-calculation: we are using just one parameter - throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + switch (resp.case) { + case HttpStatusCode.Conflict: { + return "amount-is-too-small" + } + // this below can't happen + case HttpStatusCode.NotImplemented: //it should not be able to call this function + case HttpStatusCode.BadRequest: //we are using just one parameter + throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + } + } const credit = Amounts.parseOrThrow(resp.body.amount_credit); const debit = Amounts.parseOrThrow(resp.body.amount_debit); diff --git a/packages/demobank-ui/src/pages/business/CreateCashout.tsx b/packages/demobank-ui/src/pages/business/CreateCashout.tsx index d4c39a88d..84d4a5863 100644 --- a/packages/demobank-ui/src/pages/business/CreateCashout.tsx +++ b/packages/demobank-ui/src/pages/business/CreateCashout.tsx @@ -41,7 +41,7 @@ import { VersionHint, useBankCoreApiContext } from "../../context/config.js"; import { useAccountDetails } from "../../hooks/access.js"; import { useBackendState } from "../../hooks/backend.js"; import { useBankState } from "../../hooks/bank-state.js"; -import { useConversionInfo, useEstimator } from "../../hooks/circuit.js"; +import { TransferCalculation, useCashoutEstimator, useConversionInfo, useEstimator } from "../../hooks/circuit.js"; import { RouteDefinition } from "../../route.js"; import { TanChannel, undefinedIfEmpty } from "../../utils.js"; import { LoginForm } from "../LoginForm.js"; @@ -81,7 +81,7 @@ export function CreateCashout({ const { estimateByCredit: calculateFromCredit, estimateByDebit: calculateFromDebit, - } = useEstimator(); + } = useCashoutEstimator(); const { state: credentials } = useBackendState(); const creds = credentials.status !== "loggedIn" ? undefined : credentials; const [, updateBankState] = useBankState(); @@ -185,7 +185,7 @@ export function CreateCashout({ credit: fiatZero, beforeFee: fiatZero, }; - const [calc, setCalc] = useState(zeroCalc); + const [calculationResult, setCalculation] = useState<TransferCalculation>(zeroCalc); const sellFee = Amounts.parseOrThrow(conversionInfo.cashout_fee); const sellRate = conversionInfo.cashout_ratio; /** @@ -200,21 +200,24 @@ export function CreateCashout({ useEffect(() => { async function doAsync() { await handleError(async () => { - const higerThanMin = Amounts.cmp(inputAmount, conversionInfo.cashout_min_amount) === 1 + const higerThanMin = form.isDebit ? + Amounts.cmp(inputAmount, conversionInfo.cashout_min_amount) === 1 : true; const notZero = Amounts.isNonZero(inputAmount) if (notZero && higerThanMin) { const resp = await (form.isDebit ? calculateFromDebit(inputAmount, sellFee) : calculateFromCredit(inputAmount, sellFee)); - setCalc(resp); + setCalculation(resp); } else { - setCalc(zeroCalc) + setCalculation(zeroCalc) } }); } doAsync(); }, [form.amount, form.isDebit]); + const calc = calculationResult === "amount-is-too-small" ? zeroCalc : calculationResult + const balanceAfter = Amounts.sub(account.balance, calc.debit).amount; function updateForm(newForm: typeof form): void { @@ -228,8 +231,10 @@ export function CreateCashout({ ? i18n.str`Invalid` : Amounts.cmp(limit, calc.debit) === -1 ? i18n.str`Balance is not enough` - : Amounts.cmp(inputAmount, conversionInfo.cashout_min_amount) < 1 + : form.isDebit && Amounts.cmp(inputAmount, conversionInfo.cashout_min_amount) < 1 ? i18n.str`Needs to be higher than ${Amounts.stringifyValueWithSpec(Amounts.parseOrThrow(conversionInfo.cashout_min_amount), regional_currency_specification).normal}` + : calculationResult === "amount-is-too-small" + ? i18n.str`Amount needs to be higher` : Amounts.isZero(calc.credit) ? i18n.str`The total transfer at destination will be zero` : undefined, |