From 60b438f7deef20af49227aafd0243a7a9caedc1e Mon Sep 17 00:00:00 2001 From: poocart <7067483+poocart@users.noreply.github.com> Date: Thu, 22 Feb 2024 12:18:00 +0200 Subject: [PATCH] added additional exception handler --- src/hooks/useWalletAddress.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/hooks/useWalletAddress.ts b/src/hooks/useWalletAddress.ts index 9c9c884..02ece24 100644 --- a/src/hooks/useWalletAddress.ts +++ b/src/hooks/useWalletAddress.ts @@ -27,19 +27,31 @@ const useWalletAddress = (walletType: IWalletType = 'etherspot-prime', chainId?: const updateAccountAddress = async () => { const etherspotPrimeSdk = await getSdk(walletAddressChainId); + let newAccountAddress; + try { /** * Currently `etherspotWallet` is marked as private on SDK, let's ignore until SDK team fixes it * Reference – https://github.com/etherspot/etherspot-prime-sdk/blob/master/src/sdk/sdk.ts#L31 */ - // @ts-ignore - const storedAddress = etherspotPrimeSdk?.etherspotWallet?.accountAddress; - const newAccountAddress = storedAddress ?? await etherspotPrimeSdk.getCounterFactualAddress(); - if (!shouldUpdate) return; - setAccountAddress(newAccountAddress); + // @ts-ignore + newAccountAddress = etherspotPrimeSdk?.etherspotWallet?.accountAddress; } catch (e) { - console.warn(`Unable to get wallet address for etherspot-prime type for chainId ID ${walletAddressChainId}.`, e); + console.warn(`Unable to get wallet address from SDK state for etherspot-prime type for chainId ID ${walletAddressChainId}.`, e); + } + + // if were unable to get wallet address from SDK state, try to get using getCounterFactualAddress + if (!newAccountAddress) { + try { + newAccountAddress = await etherspotPrimeSdk.getCounterFactualAddress(); + } catch (e) { + console.warn(`Unable to get wallet address for etherspot-prime type for chainId ID ${walletAddressChainId}.`, e); + } } + + if (!newAccountAddress || !shouldUpdate) return; + + setAccountAddress(newAccountAddress); } updateAccountAddress();