aboutsummaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/hooks/circuit.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/demobank-ui/src/hooks/circuit.ts')
-rw-r--r--packages/demobank-ui/src/hooks/circuit.ts99
1 files changed, 62 insertions, 37 deletions
diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts
index 44edb4f8a..d0d180a53 100644
--- a/packages/demobank-ui/src/hooks/circuit.ts
+++ b/packages/demobank-ui/src/hooks/circuit.ts
@@ -18,7 +18,7 @@ import { useState } from "preact/hooks";
import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils.js";
import { useBackendState } from "./backend.js";
-import { AccessToken, AmountJson, AmountString, Amounts, OperationOk, TalerCoreBankErrorsByMethod, TalerCoreBankResultByMethod, TalerCorebankApi, TalerError, TalerHttpError } from "@gnu-taler/taler-util";
+import { AccessToken, AmountJson, AmountString, Amounts, OperationOk, TalerBankConversionResultByMethod, TalerCoreBankErrorsByMethod, TalerCoreBankResultByMethod, TalerCorebankApi, TalerError, TalerHttpError } from "@gnu-taler/taler-util";
import _useSWR, { SWRHook } from "swr";
import { useBankCoreApiContext } from "../context/config.js";
import { assertUnreachable } from "../pages/WithdrawalOperationPage.js";
@@ -34,6 +34,7 @@ export type TransferCalculation = {
};
type EstimatorFunction = (
amount: AmountJson,
+ currency: string,
sellFee: AmountJson,
sellRate: number,
) => Promise<TransferCalculation>;
@@ -43,50 +44,74 @@ type CashoutEstimators = {
estimateByDebit: EstimatorFunction;
};
+export function useConversionInfo() {
+ const { api, config } = useBankCoreApiContext()
+
+ async function fetcher() {
+ return await api.getConversionInfoAPI().getConfig()
+ }
+ const { data, error } = useSWR<TalerBankConversionResultByMethod<"getConfig">, TalerHttpError>(
+ !config.allow_conversion ? undefined : ["getConversionInfoAPI"], fetcher, {
+ refreshInterval: 0,
+ refreshWhenHidden: false,
+ revalidateOnFocus: false,
+ revalidateOnReconnect: false,
+ refreshWhenOffline: false,
+ errorRetryCount: 0,
+ errorRetryInterval: 1,
+ shouldRetryOnError: false,
+ keepPreviousData: true,
+ });
+
+ if (data) return data
+ if (error) return error;
+ return undefined;
+
+}
+
export function useEstimator(): CashoutEstimators {
const { state } = useBackendState();
const { api } = useBankCoreApiContext();
return {
- estimateByCredit: async (amount, fee, rate) => {
- const resp = await api.getCashoutRate({
- credit: amount
- });
- 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)
- }
- const credit = amount;
- const _credit = { ...credit, currency: fee.currency };
- const beforeFee = Amounts.sub(_credit, fee).amount;
+ estimateByCredit: async (fiatAmount, regionalCurrency, fee, rate) => {
+ // const resp = await api.getConversionInfoAPI().getCashoutRate({
+ // credit: amount
+ // });
+ // 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)
+ // }
+ const credit = fiatAmount;
+ const beforeFee = Amounts.sub(credit, fee).amount;
+
+ // const debit = Amounts.parseOrThrow(resp.body.amount_debit);
+ //FIXME: remove this when endpoint works
+ const debit = Amounts.add(
+ Amounts.zeroOfCurrency(regionalCurrency),
+ beforeFee
+ ).amount;
- const debit = Amounts.parseOrThrow(resp.body.amount_debit);
return {
debit,
beforeFee,
credit,
};
},
- estimateByDebit: async (amount, fee, rate) => {
- const zeroBalance = Amounts.zeroOfCurrency(fee.currency);
- const zeroFiat = Amounts.zeroOfCurrency(fee.currency);
- const zeroCalc = {
- debit: zeroBalance,
- credit: zeroFiat,
- beforeFee: zeroBalance,
- };
- const resp = await api.getCashoutRate({ debit: amount });
- 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)
- }
- const credit = Amounts.parseOrThrow(resp.body.amount_credit);
- const _credit = { ...credit, currency: fee.currency };
- const debit = amount;
- const beforeFee = Amounts.sub(_credit, fee).amount;
+ estimateByDebit: async (regionalAmount, fiatCurrency, fee, rate) => {
+ // const resp = await api.getConversionInfoAPI().getCashoutRate({ debit: amount });
+ // 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)
+ // }
+ // const credit = Amounts.parseOrThrow(resp.body.amount_credit);
+ const debit = regionalAmount;
+ const _credit = Amounts.parseOrThrow(regionalAmount);
+ const beforeFee = { ..._credit, currency: fiatCurrency };
+ const credit = Amounts.sub(beforeFee, fee).amount;
return {
debit,
beforeFee,
@@ -178,7 +203,7 @@ export function useCashouts(account: string) {
}
const { data, error } = useSWR<OperationOk<{ cashouts: CashoutWithId[] }> | TalerCoreBankErrorsByMethod<"getAccountCashouts">, TalerHttpError>(
- !config.have_cashout ? false : [account, token, "getAccountCashouts"], fetcher, {
+ !config.allow_conversion ? false : [account, token, "getAccountCashouts"], fetcher, {
refreshInterval: 0,
refreshWhenHidden: false,
revalidateOnFocus: false,
@@ -280,7 +305,7 @@ export function useLastMonitorInfo(time: Date, timeframe: TalerCorebankApi.Monit
}
}
- const previous: TalerCoreBankResultByMethod<"getMonitor"> = {
+ const previous: TalerCoreBankResultByMethod<"getMonitor"> = {
type: "ok" as const,
body: {
type: "with-conversions" as const,
@@ -304,7 +329,7 @@ export function useLastMonitorInfo(time: Date, timeframe: TalerCorebankApi.Monit
}
const { data, error } = useSWR<LastMonitor, TalerHttpError>(
- config.have_cashout || true ? ["useLastMonitorInfo"] : false, fetcher, {
+ config.allow_conversion || true ? ["useLastMonitorInfo"] : false, fetcher, {
refreshInterval: 0,
refreshWhenHidden: false,
revalidateOnFocus: false,