From 0836b1023f8c7a70260a32be7a19984b29a2910a Mon Sep 17 00:00:00 2001 From: Eugene Panteleymonchuk Date: Wed, 31 Jan 2024 15:57:59 +0200 Subject: [PATCH] chore: move check alias output balance to useAddressBalance. Signed-off-by: Eugene Panteleymonchuk --- .../src/app/routes/stardust/AddressState.ts | 18 ++------ client/src/helpers/hooks/useAddressBalance.ts | 27 ++++++++++-- client/src/helpers/hooks/useAliasDetails.ts | 43 +++++++++++-------- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/client/src/app/routes/stardust/AddressState.ts b/client/src/app/routes/stardust/AddressState.ts index bd0692a49..5b1d46526 100644 --- a/client/src/app/routes/stardust/AddressState.ts +++ b/client/src/app/routes/stardust/AddressState.ts @@ -118,16 +118,17 @@ export const useAddressPageState = (): [IAddressState, React.Dispatch { - const consolidatedAvailableBalance = consolidateOutputBalance(availableBalance, aliasOutput); setState({ addressBasicOutputs, isBasicOutputsLoading, @@ -169,7 +169,7 @@ export const useAddressPageState = (): [IAddressState, React.Dispatch { - let total = currentBalance ? parseInt(currentBalance as string) : 0; - - if (additionalOutput?.type === OutputType.Alias) { - total += parseInt(additionalOutput.amount); - } - - return total; -}; diff --git a/client/src/helpers/hooks/useAddressBalance.ts b/client/src/helpers/hooks/useAddressBalance.ts index 7d647d72a..7188cabc2 100644 --- a/client/src/helpers/hooks/useAddressBalance.ts +++ b/client/src/helpers/hooks/useAddressBalance.ts @@ -1,16 +1,23 @@ import { useEffect, useState } from "react"; +import { AliasOutput, OutputType } from "@iota/sdk-wasm/web"; import { useIsMounted } from "./useIsMounted"; import { ServiceFactory } from "~factories/serviceFactory"; import { STARDUST } from "~models/config/protocolVersion"; import { StardustApiClient } from "~services/stardust/stardustApiClient"; +import { fetchAliasDetailsOutput } from "~helpers/hooks/useAliasDetails"; /** * Fetch the address balance * @param network The Network in context * @param address The bech32 address + * @param options * @returns The address balance, signature locked balance and a loading bool. */ -export function useAddressBalance(network: string, address: string | null): [number | null, number | null, boolean] { +export function useAddressBalance( + network: string, + address: string | null, + options?: { aliasId?: string | null }, +): [number | null, number | null, boolean] { const isMounted = useIsMounted(); const [apiClient] = useState(ServiceFactory.get(`api-client-${STARDUST}`)); const [balance, setBalance] = useState(null); @@ -23,10 +30,12 @@ export function useAddressBalance(network: string, address: string | null): [num // eslint-disable-next-line no-void void (async () => { const response = await apiClient.addressBalanceChronicle({ network, address }); + const output = options?.aliasId ? await fetchAliasDetailsOutput(network, options.aliasId) : null; if (response?.totalBalance !== undefined && isMounted) { setBalance(response.totalBalance); - setAvailableBalance(response.availableBalance ?? null); + const availableBalance = consolidateOutputBalance(response.totalBalance, output); + setAvailableBalance(availableBalance ?? null); } else if (isMounted) { // Fallback balance from iotajs (node) const addressDetailsWithBalance = await apiClient.addressBalance({ network, address }); @@ -40,7 +49,19 @@ export function useAddressBalance(network: string, address: string | null): [num } else { setIsLoading(false); } - }, [network, address]); + }, [network, address, options?.aliasId]); return [balance, availableBalance, isLoading]; } + +const consolidateOutputBalance = (currentBalance: number | string | null, additionalOutput?: AliasOutput | null) => { + if (!currentBalance) return null; + + let total = currentBalance ? parseInt(currentBalance as string) : 0; + + if (additionalOutput?.type === OutputType.Alias) { + total += parseInt(additionalOutput.amount); + } + + return total; +}; diff --git a/client/src/helpers/hooks/useAliasDetails.ts b/client/src/helpers/hooks/useAliasDetails.ts index 166132134..3ba3ffb04 100644 --- a/client/src/helpers/hooks/useAliasDetails.ts +++ b/client/src/helpers/hooks/useAliasDetails.ts @@ -14,30 +14,20 @@ import { HexHelper } from "../stardust/hexHelper"; */ export function useAliasDetails(network: string, aliasId: string | null): [AliasOutput | null, boolean] { const isMounted = useIsMounted(); - const [apiClient] = useState(ServiceFactory.get(`api-client-${STARDUST}`)); const [aliasOutput, setAliasOutput] = useState(null); const [isLoading, setIsLoading] = useState(true); useEffect(() => { setIsLoading(true); - if (aliasId) { + if (aliasId && isMounted) { // eslint-disable-next-line no-void void (async () => { - apiClient - .aliasDetails({ - network, - aliasId: HexHelper.addPrefix(aliasId), - }) - .then((response) => { - if (!response?.error && isMounted) { - const output = response.aliasDetails?.output as AliasOutput; - - setAliasOutput(output); - } - }) - .finally(() => { - setIsLoading(false); - }); + try { + const aliasOutput = await fetchAliasDetailsOutput(network, aliasId); + setAliasOutput(aliasOutput); + } finally { + setIsLoading(false); + } })(); } else { setIsLoading(false); @@ -46,3 +36,22 @@ export function useAliasDetails(network: string, aliasId: string | null): [Alias return [aliasOutput, isLoading]; } + +export const fetchAliasDetailsOutput = async (network: string, aliasId: string | null): Promise => { + if (!aliasId) { + return null; + } + + const apiClient = ServiceFactory.get(`api-client-${STARDUST}`); + + const response = await apiClient.aliasDetails({ + network, + aliasId: HexHelper.addPrefix(aliasId), + }); + + if (!response?.error) { + return response.aliasDetails?.output as AliasOutput; + } + + return null; +};