@@ -31,9 +31,13 @@ import {
31
31
DECIMAL ,
32
32
} from './utils/constants' ;
33
33
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'
34
37
35
38
function App ( ) {
36
39
const [ address , setAddress ] = useState ( '' ) ;
40
+ const [ walletManager , setWalletManager ] = useState < WalletManager | null > ( null )
37
41
const { colorMode, toggleColorMode } = useColorMode ( ) ;
38
42
const toast = useToast ( ) ;
39
43
@@ -49,6 +53,17 @@ function App() {
49
53
} ,
50
54
} ) ;
51
55
56
+ useEffect ( ( ) => {
57
+ ( async ( ) => {
58
+ const walletManager = await WalletManager . create (
59
+ [ cosmoshubChain ] ,
60
+ [ cosmoshubAssetList ] ,
61
+ [ keplrWallet ]
62
+ )
63
+ setWalletManager ( walletManager )
64
+ } ) ( )
65
+ } , [ ] )
66
+
52
67
const { data : balance , refetch : refetchBalance } = useQuery ( {
53
68
queryKey : [ 'balance' , address ] ,
54
69
queryFn : async ( ) => {
@@ -149,11 +164,11 @@ function App() {
149
164
if ( ! window . keplr ) {
150
165
throw new Error ( 'Please install Keplr extension' ) ;
151
166
}
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 )
156
170
} catch ( error ) {
171
+ console . error ( 'Error connecting wallet:' , error ) ;
157
172
toast ( {
158
173
title : 'Connection failed' ,
159
174
description : ( error as Error ) . message ,
@@ -178,8 +193,11 @@ function App() {
178
193
} ;
179
194
180
195
useEffect ( ( ) => {
196
+ if ( ! walletManager ) return
181
197
connectWallet ( ) ;
182
- } , [ ] ) ;
198
+ } , [
199
+ walletManager
200
+ ] ) ;
183
201
184
202
const handleRefreshBalance = ( ) => {
185
203
refetchBalance ( ) ;
0 commit comments