diff options
author | Sebastian <sebasjm@gmail.com> | 2023-03-31 19:09:41 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-03-31 19:09:41 -0300 |
commit | f947c8e54919343ac4f5f951d2f701651c06dd52 (patch) | |
tree | 870c39a2214f1183113469b19b3111d79b69d68a /packages/demobank-ui/src/hooks/circuit.ts | |
parent | 8701ae100ec1e2733b8e9b7006a706d1c9fe32a8 (diff) | |
download | wallet-core-f947c8e54919343ac4f5f951d2f701651c06dd52.tar.xz |
calculate using server
Diffstat (limited to 'packages/demobank-ui/src/hooks/circuit.ts')
-rw-r--r-- | packages/demobank-ui/src/hooks/circuit.ts | 105 |
1 files changed, 105 insertions, 0 deletions
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 { @@ -218,6 +219,23 @@ async function getBusinessStatus( ): Promise<boolean> { try { const url = getInitialBackendBaseURL(); + const result = await request<SandboxBackend.Circuit.CircuitAccountData>( + url, + `circuit-api/accounts/${basicAuth.username}`, + { basicAuth }, + ); + return result.ok; + } catch (error) { + return false; + } +} + +async function getEstimationByCredit( + request: ReturnType<typeof useApiContext>["request"], + basicAuth: { username: string; password: string }, +): Promise<boolean> { + try { + const url = getInitialBackendBaseURL(); const result = await request< HttpResponseOk<SandboxBackend.Circuit.CircuitAccountData> >(url, `circuit-api/accounts/${basicAuth.username}`, { basicAuth }); @@ -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<TransferCalculation>; + +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<SandboxBackend.Circuit.CashoutEstimate>( + 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<SandboxBackend.Circuit.CashoutEstimate>( + 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<boolean | undefined>(); const { state } = useBackendContext(); |