gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] branch master updated: fix min calculation when isDe


From: gnunet
Subject: [taler-wallet-core] branch master updated: fix min calculation when isDebit = false
Date: Tue, 05 Mar 2024 18:31:59 +0100

This is an automated email from the git hooks/post-receive script.

sebasjm pushed a commit to branch master
in repository wallet-core.

The following commit(s) were added to refs/heads/master by this push:
     new c08822cb8 fix min calculation when isDebit = false
c08822cb8 is described below

commit c08822cb82907f72f83d5af0c768e2a3785d8e8d
Author: Sebastian <sebasjm@gmail.com>
AuthorDate: Tue Mar 5 14:31:53 2024 -0300

    fix min calculation when isDebit = false
---
 packages/demobank-ui/src/hooks/circuit.ts             | 17 ++++++++++++-----
 .../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,

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]