Skip to content

Commit 14ea6d5

Browse files
committed
getting account using interchain-kit
1 parent d24934d commit 14ea6d5

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

examples/vite-react/src/App.tsx

+23-5
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ import {
3131
DECIMAL,
3232
} from './utils/constants';
3333
import { SigningStargateClient } from '@cosmjs/stargate';
34+
import { chain as cosmoshubChain, assetList as cosmoshubAssetList } from '@chain-registry/v2/mainnet/cosmoshub'
35+
import { WalletManager } from '@interchain-kit/core'
36+
import { keplrWallet } from '@interchain-kit/keplr-extension'
3437

3538
function App() {
3639
const [address, setAddress] = useState('');
40+
const [walletManager, setWalletManager] = useState<WalletManager | null>(null)
3741
const { colorMode, toggleColorMode } = useColorMode();
3842
const toast = useToast();
3943

@@ -49,6 +53,17 @@ function App() {
4953
},
5054
});
5155

56+
useEffect(() => {
57+
(async () => {
58+
const walletManager = await WalletManager.create(
59+
[cosmoshubChain],
60+
[cosmoshubAssetList],
61+
[keplrWallet]
62+
)
63+
setWalletManager(walletManager)
64+
})()
65+
}, [])
66+
5267
const { data: balance, refetch: refetchBalance } = useQuery({
5368
queryKey: ['balance', address],
5469
queryFn: async () => {
@@ -149,11 +164,11 @@ function App() {
149164
if (!window.keplr) {
150165
throw new Error('Please install Keplr extension');
151166
}
152-
153-
await window.keplr.enable(CHAIN_ID);
154-
const key = await window.keplr.getKey(CHAIN_ID);
155-
setAddress(key.bech32Address);
167+
await walletManager?.connect(keplrWallet.info?.name as string, cosmoshubChain.chainName)
168+
const account = await walletManager?.getAccount(keplrWallet.info?.name as string, cosmoshubChain.chainName)
169+
setAddress(account?.address as string)
156170
} catch (error) {
171+
console.error('Error connecting wallet:', error);
157172
toast({
158173
title: 'Connection failed',
159174
description: (error as Error).message,
@@ -178,8 +193,11 @@ function App() {
178193
};
179194

180195
useEffect(() => {
196+
if (!walletManager) return
181197
connectWallet();
182-
}, []);
198+
}, [
199+
walletManager
200+
]);
183201

184202
const handleRefreshBalance = () => {
185203
refetchBalance();

0 commit comments

Comments
 (0)