From f947c8e54919343ac4f5f951d2f701651c06dd52 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 31 Mar 2023 19:09:41 -0300 Subject: calculate using server --- packages/demobank-ui/src/hooks/circuit.ts | 105 ++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'packages/demobank-ui/src/hooks') diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts index 137a7aee2..f90469e24 100644 --- a/packages/demobank-ui/src/hooks/circuit.ts +++ b/packages/demobank-ui/src/hooks/circuit.ts @@ -32,6 +32,7 @@ import { // FIX default import https://github.com/microsoft/TypeScript/issues/49189 import _useSWR, { SWRHook } from "swr"; +import { AmountJson, Amounts } from "@gnu-taler/taler-util"; const useSWR = _useSWR as unknown as SWRHook; export function useAdminAccountAPI(): AdminAccountAPI { @@ -215,6 +216,23 @@ export interface CircuitAccountAPI { async function getBusinessStatus( request: ReturnType["request"], basicAuth: { username: string; password: string }, +): Promise { + try { + const url = getInitialBackendBaseURL(); + const result = await request( + url, + `circuit-api/accounts/${basicAuth.username}`, + { basicAuth }, + ); + return result.ok; + } catch (error) { + return false; + } +} + +async function getEstimationByCredit( + request: ReturnType["request"], + basicAuth: { username: string; password: string }, ): Promise { try { const url = getInitialBackendBaseURL(); @@ -227,6 +245,93 @@ async function getBusinessStatus( } } +export type TransferCalculation = { + debit: AmountJson; + credit: AmountJson; + beforeFee: AmountJson; +}; +type EstimatorFunction = ( + amount: AmountJson, + sellFee: AmountJson, + sellRate: number, +) => Promise; + +type CashoutEstimators = { + estimateByCredit: EstimatorFunction; + estimateByDebit: EstimatorFunction; +}; + +export function useEstimator(): CashoutEstimators { + const { state } = useBackendContext(); + const { request } = useApiContext(); + const basicAuth = + state.status === "loggedOut" + ? undefined + : { username: state.username, password: state.password }; + return { + estimateByCredit: 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 url = getInitialBackendBaseURL(); + const result = await request( + url, + `circuit-api/cashouts/estimates`, + { + basicAuth, + params: { + amount_credit: Amounts.stringify(amount), + }, + }, + ); + // const credit = Amounts.parseOrThrow(result.data.data.amount_credit); + const credit = amount; + const _credit = { ...credit, currency: fee.currency }; + const beforeFee = Amounts.sub(_credit, fee).amount; + + const debit = Amounts.parseOrThrow(result.data.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 url = getInitialBackendBaseURL(); + const result = await request( + url, + `circuit-api/cashouts/estimates`, + { + basicAuth, + params: { + amount_debit: Amounts.stringify(amount), + }, + }, + ); + const credit = Amounts.parseOrThrow(result.data.amount_credit); + const _credit = { ...credit, currency: fee.currency }; + const debit = amount; + const beforeFee = Amounts.sub(_credit, fee).amount; + return { + debit, + beforeFee, + credit, + }; + }, + }; +} + export function useBusinessAccountFlag(): boolean | undefined { const [isBusiness, setIsBusiness] = useState(); const { state } = useBackendContext(); -- cgit v1.2.3