diff --git a/apps/demo_web/src/components/widgets/cosmos_offchain_sign_widget/cosmos_offchain_sign_widget.tsx b/apps/demo_web/src/components/widgets/cosmos_offchain_sign_widget/cosmos_offchain_sign_widget.tsx index 093797808..089e81e01 100644 --- a/apps/demo_web/src/components/widgets/cosmos_offchain_sign_widget/cosmos_offchain_sign_widget.tsx +++ b/apps/demo_web/src/components/widgets/cosmos_offchain_sign_widget/cosmos_offchain_sign_widget.tsx @@ -3,8 +3,7 @@ import { CosmosIcon } from "@oko-wallet/oko-common-ui/icons/cosmos_icon"; import { SignWidget } from "@oko-wallet-demo-web/components/widgets/sign_widget/sign_widget"; import { useSDKState } from "@oko-wallet-demo-web/state/sdk"; - -const COSMOS_CHAIN_ID = "cosmoshub-4"; +import { COSMOS_CHAIN_ID } from "@oko-wallet-demo-web/constants/cosmos"; export const CosmosOffChainSignWidget = () => { const okoCosmos = useSDKState((state) => state.oko_cosmos); diff --git a/apps/demo_web/src/components/widgets/cosmos_onchain_sign_widget/cosmos_onchain_sign_widget.tsx b/apps/demo_web/src/components/widgets/cosmos_onchain_sign_widget/cosmos_onchain_sign_widget.tsx index 0fd0463a2..321b90d89 100644 --- a/apps/demo_web/src/components/widgets/cosmos_onchain_sign_widget/cosmos_onchain_sign_widget.tsx +++ b/apps/demo_web/src/components/widgets/cosmos_onchain_sign_widget/cosmos_onchain_sign_widget.tsx @@ -15,8 +15,8 @@ import { Checkbox } from "@oko-wallet/oko-common-ui/checkbox"; import styles from "./cosmos_onchain_sign_widget.module.scss"; import { SignWidget } from "@oko-wallet-demo-web/components/widgets/sign_widget/sign_widget"; import { useSDKState } from "@oko-wallet-demo-web/state/sdk"; +import { COSMOS_CHAIN_ID } from "@oko-wallet-demo-web/constants/cosmos"; -const COSMOS_CHAIN_ID = "cosmoshub-4"; const TOKEN_MINIMAL_DENOM = "uatom"; export const CosmosOnchainSignWidget = () => { diff --git a/apps/demo_web/src/constants/cosmos.ts b/apps/demo_web/src/constants/cosmos.ts new file mode 100644 index 000000000..4f7facba5 --- /dev/null +++ b/apps/demo_web/src/constants/cosmos.ts @@ -0,0 +1 @@ +export const COSMOS_CHAIN_ID = "cosmoshub-4"; diff --git a/apps/demo_web/src/hooks/wallet.ts b/apps/demo_web/src/hooks/wallet.ts index 2a7461af9..2bfd50035 100644 --- a/apps/demo_web/src/hooks/wallet.ts +++ b/apps/demo_web/src/hooks/wallet.ts @@ -1,31 +1,52 @@ +import { useEffect, useRef, useState } from "react"; + +import { COSMOS_CHAIN_ID } from "@oko-wallet-demo-web/constants/cosmos"; import { useSDKState } from "@oko-wallet-demo-web/state/sdk"; import { useUserInfoState } from "@oko-wallet-demo-web/state/user_info"; -import { useEffect, useState } from "react"; export function useAddresses() { const okoCosmos = useSDKState((state) => state.oko_cosmos); const okoEth = useSDKState((state) => state.oko_eth); const isSignedIn = useUserInfoState((state) => state.isSignedIn); + const isSignedRef = useRef(isSignedIn); + isSignedRef.current = isSignedIn; const [cosmosAddress, setCosmosAddress] = useState(null); const [ethAddress, setEthAddress] = useState(null); useEffect(() => { + if (!isSignedIn) { + if (cosmosAddress) { + setCosmosAddress(null); + } + + if (ethAddress) { + setEthAddress(null); + } + return; + } + const loadAddresses = async () => { try { const promises = []; if (okoCosmos) { promises.push( - okoCosmos - .getKey("cosmoshub-4") - .then((key) => setCosmosAddress(key.bech32Address)), + okoCosmos.getKey(COSMOS_CHAIN_ID).then((key) => { + if (isSignedRef.current) { + setCosmosAddress(key.bech32Address); + } + }), ); } if (okoEth) { promises.push( - okoEth.getAddress().then((addr) => setEthAddress(addr)), + okoEth.getAddress().then((addr) => { + if (isSignedRef.current) { + setEthAddress(addr); + } + }), ); } @@ -38,16 +59,6 @@ export function useAddresses() { if (isSignedIn) { loadAddresses(); } - - if (!isSignedIn) { - if (cosmosAddress) { - setCosmosAddress(null); - } - - if (ethAddress) { - setEthAddress(null); - } - } }, [isSignedIn, okoCosmos, okoEth]); return { cosmosAddress, ethAddress };