-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseWalletManager.ts
54 lines (50 loc) · 1.72 KB
/
useWalletManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useState, useEffect, useCallback } from 'react';
import { WalletManager } from '@interchain-kit/core';
import { keplrWallet } from '@interchain-kit/keplr-extension';
import { chain as cosmoshubChain, assetList as cosmoshubAssetList } from '@chain-registry/v2/mainnet/cosmoshub';
import { RPC_ENDPOINT } from '../utils/constants';
import { toast } from '@interchain-ui/react';
export const useWalletManager = () => {
const [walletManager, setWalletManager] = useState<WalletManager | null>(null);
const [address, setAddress] = useState<string>('');
useEffect(() => {
(async () => {
try {
const manager = await WalletManager.create(
[cosmoshubChain],
[cosmoshubAssetList],
[keplrWallet],
{},
{
endpoints: {
cosmoshub: {
rpc: [RPC_ENDPOINT],
},
},
}
);
setWalletManager(manager);
} catch (error: any) {
console.error('Error initializing wallet manager:', error);
toast.error(error.message)
}
})();
}, [toast]);
const connectWallet = useCallback(async () => {
try {
if (!window.keplr) {
throw new Error('Please install Keplr extension');
}
await walletManager?.connect(keplrWallet.info?.name as string, cosmoshubChain.chainName);
const account = await walletManager?.getAccount(
keplrWallet.info?.name as string,
cosmoshubChain.chainName
);
setAddress(account?.address as string);
} catch (error: any) {
console.error('Error connecting wallet:', error);
toast.error(error.message)
}
}, [walletManager, toast]);
return { walletManager, address, connectWallet };
};