Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
1 change: 1 addition & 0 deletions apps/demo_web/src/constants/cosmos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const COSMOS_CHAIN_ID = "cosmoshub-4";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constants can be anything. Too general this file is.

41 changes: 26 additions & 15 deletions apps/demo_web/src/hooks/wallet.ts
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reference the latest state of isSignedIn in the callback, add a ref variable.

^ as I mentioned PR, Within the callback function, it is necessary to retrieve the most recent isSignedIn state.

Copy link
Collaborator

@eldenpark eldenpark Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blacktoast I still don't see why this ref is necessary. Could we hop on a short call when you are available?


const [cosmosAddress, setCosmosAddress] = useState<string | null>(null);
const [ethAddress, setEthAddress] = useState<string | null>(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);
}
}),
);
}

Expand All @@ -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 };
Expand Down