From 37400403e6ea7a4d307c633b6d3c6f7688fb237e Mon Sep 17 00:00:00 2001 From: Karolina Kosiorowska Date: Wed, 13 Nov 2024 12:37:24 +0100 Subject: [PATCH 1/2] Create a `useWalletAddress` hook --- dapp/src/hooks/store/index.ts | 1 + dapp/src/hooks/store/useWalletAddress.ts | 6 ++++++ dapp/src/hooks/useWallet.ts | 7 ++++--- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 dapp/src/hooks/store/useWalletAddress.ts diff --git a/dapp/src/hooks/store/index.ts b/dapp/src/hooks/store/index.ts index aafdf9918..63016f6d7 100644 --- a/dapp/src/hooks/store/index.ts +++ b/dapp/src/hooks/store/index.ts @@ -12,3 +12,4 @@ export * from "./useIsSignedMessage" export { default as useHasFetchedActivities } from "./useHasFetchedActivities" export { default as useActivities } from "./useActivities" export { default as useLatestCompletedActivities } from "./useLatestCompletedActivities" +export { default as useWalletAddress } from "./useWalletAddress" diff --git a/dapp/src/hooks/store/useWalletAddress.ts b/dapp/src/hooks/store/useWalletAddress.ts new file mode 100644 index 000000000..424d4cd8f --- /dev/null +++ b/dapp/src/hooks/store/useWalletAddress.ts @@ -0,0 +1,6 @@ +import { selectWalletAddress } from "#/store/wallet" +import { useAppSelector } from "./useAppSelector" + +export default function useWalletAddress() { + return useAppSelector(selectWalletAddress) +} diff --git a/dapp/src/hooks/useWallet.ts b/dapp/src/hooks/useWallet.ts index 2786a4472..1462cfa3e 100644 --- a/dapp/src/hooks/useWallet.ts +++ b/dapp/src/hooks/useWallet.ts @@ -15,11 +15,12 @@ import { Status, } from "#/types" import { useMutation, useQueryClient } from "@tanstack/react-query" -import { useDispatch, useSelector } from "react-redux" -import { selectWalletAddress, setAddress } from "#/store/wallet" +import { useDispatch } from "react-redux" +import { setAddress } from "#/store/wallet" import useBitcoinBalance from "./orangeKit/useBitcoinBalance" import useResetWalletState from "./useResetWalletState" import useLastUsedBtcAddress from "./useLastUsedBtcAddress" +import useWalletAddress from "./store/useWalletAddress" const { typeConversionToConnector, typeConversionToOrangeKitConnector } = orangeKit @@ -44,7 +45,7 @@ type UseWalletReturn = { export function useWallet(): UseWalletReturn { const queryClient = useQueryClient() const dispatch = useDispatch() - const btcAddress = useSelector(selectWalletAddress) + const btcAddress = useWalletAddress() const resetWalletState = useResetWalletState() const { setAddressInLocalStorage, removeAddressFromLocalStorage } = useLastUsedBtcAddress() From a69515a17fd1fe069a1cb480da4fbc53ec45c6fa Mon Sep 17 00:00:00 2001 From: Karolina Kosiorowska Date: Wed, 13 Nov 2024 12:45:08 +0100 Subject: [PATCH 2/2] Make sure the connector is updated Connector should update after each address change. --- dapp/src/hooks/orangeKit/useConnector.ts | 11 +++++------ dapp/src/hooks/sdk/useInitializeAcreSdk.ts | 4 +--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/dapp/src/hooks/orangeKit/useConnector.ts b/dapp/src/hooks/orangeKit/useConnector.ts index b533ed648..6b1c494a3 100644 --- a/dapp/src/hooks/orangeKit/useConnector.ts +++ b/dapp/src/hooks/orangeKit/useConnector.ts @@ -2,19 +2,18 @@ import { useMemo } from "react" import { useAccount } from "wagmi" import { orangeKit } from "#/utils" import { OrangeKitConnector } from "#/types" +import useWalletAddress from "../store/useWalletAddress" type UseConnectorReturn = OrangeKitConnector | undefined export function useConnector(): UseConnectorReturn { - const { connector, status } = useAccount() + const { connector } = useAccount() + const address = useWalletAddress() return useMemo(() => { - if ( - orangeKit.isOrangeKitConnector(connector) && - orangeKit.isConnectedStatus(status) - ) + if (address && orangeKit.isOrangeKitConnector(connector)) return orangeKit.typeConversionToOrangeKitConnector(connector) return undefined - }, [connector, status]) + }, [connector, address]) } diff --git a/dapp/src/hooks/sdk/useInitializeAcreSdk.ts b/dapp/src/hooks/sdk/useInitializeAcreSdk.ts index 05cd359b8..a6974bcd5 100644 --- a/dapp/src/hooks/sdk/useInitializeAcreSdk.ts +++ b/dapp/src/hooks/sdk/useInitializeAcreSdk.ts @@ -3,12 +3,10 @@ import { logPromiseFailure } from "#/utils" import { useAcreContext } from "#/acre-react/hooks" import { AcreBitcoinProvider } from "@acre-btc/sdk" import { useBitcoinProvider } from "../orangeKit/useBitcoinProvider" -import { useWallet } from "../useWallet" export function useInitializeAcreSdk() { const { init } = useAcreContext() const bitcoinProvider = useBitcoinProvider() - const { address } = useWallet() useEffect(() => { const initSDK = async (provider?: AcreBitcoinProvider) => { @@ -16,5 +14,5 @@ export function useInitializeAcreSdk() { } logPromiseFailure(initSDK(bitcoinProvider)) - }, [init, bitcoinProvider, address]) + }, [init, bitcoinProvider]) }