diff options
Diffstat (limited to 'packages/demobank-ui/src/hooks/circuit.ts')
-rw-r--r-- | packages/demobank-ui/src/hooks/circuit.ts | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts index 7d8884797..2c0a58a5e 100644 --- a/packages/demobank-ui/src/hooks/circuit.ts +++ b/packages/demobank-ui/src/hooks/circuit.ts @@ -47,7 +47,7 @@ type EstimatorFunction = ( fee: AmountJson, ) => Promise<TransferCalculation>; -type CashoutEstimators = { +type ConversionEstimators = { estimateByCredit: EstimatorFunction; estimateByDebit: EstimatorFunction; }; @@ -84,7 +84,53 @@ export function useConversionInfo() { return undefined; } -export function useEstimator(): CashoutEstimators { +export function useCashinEstimator(): ConversionEstimators { + const { api } = useBankCoreApiContext(); + return { + estimateByCredit: async (fiatAmount, fee) => { + const resp = await api.getConversionInfoAPI().getCashinRate({ + 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); + } + const credit = Amounts.parseOrThrow(resp.body.amount_credit); + const debit = Amounts.parseOrThrow(resp.body.amount_debit); + const beforeFee = Amounts.sub(credit, fee).amount; + + return { + debit, + beforeFee, + credit, + }; + }, + estimateByDebit: async (regionalAmount, fee) => { + const resp = await api.getConversionInfoAPI().getCashinRate({ + debit: regionalAmount, + }); + 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 = Amounts.parseOrThrow(resp.body.amount_debit); + const beforeFee = Amounts.add(credit, fee).amount; + + return { + debit, + beforeFee, + credit, + }; + }, + }; +} + +export function useCashoutEstimator(): ConversionEstimators { const { api } = useBankCoreApiContext(); return { estimateByCredit: async (fiatAmount, fee) => { @@ -130,6 +176,13 @@ export function useEstimator(): CashoutEstimators { }; } +/** + * @deprecated use useCashoutEstimator + */ +export function useEstimator(): ConversionEstimators { + return useCashoutEstimator() +} + export function revalidateBusinessAccounts() { return mutate((key) => Array.isArray(key) && key[key.length - 1] === "getAccounts", undefined, { revalidate: true }); } @@ -147,7 +200,7 @@ export function useBusinessAccounts() { token, {}, { - limit: PAGE_SIZE+1, + limit: PAGE_SIZE + 1, offset: String(offset), order: "asc", }, @@ -174,7 +227,7 @@ export function useBusinessAccounts() { const isFirstPage = !offset; const result = data && data.type == "ok" ? structuredClone(data.body.accounts) : [] - if (result.length == PAGE_SIZE+1) { + if (result.length == PAGE_SIZE + 1) { result.pop() } const pagination = { |