@interchain-kit/react 0.0.1-beta.5 → 0.0.1-beta.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/README.md +165 -12
  2. package/esm/hooks/index.js +3 -1
  3. package/esm/hooks/useAccount.js +20 -19
  4. package/esm/hooks/useChain.js +59 -20
  5. package/esm/hooks/useChainWallet.js +33 -10
  6. package/esm/hooks/useCurrentChainWallet.js +11 -0
  7. package/esm/hooks/useCurrentWallet.js +18 -0
  8. package/esm/hooks/useInterchainClient.js +14 -50
  9. package/esm/hooks/useOfflineSigner.js +14 -2
  10. package/esm/hooks/useRpcEndpoint.js +26 -0
  11. package/esm/hooks/useWalletManager.js +4 -3
  12. package/esm/modal/modal.js +71 -12
  13. package/esm/modal/provider.js +1 -2
  14. package/esm/modal/views/Astronaut.js +4 -0
  15. package/esm/modal/views/Connected.js +33 -0
  16. package/esm/modal/views/Connecting.js +24 -0
  17. package/esm/modal/views/NotExist.js +44 -0
  18. package/esm/modal/views/QRCode.js +20 -0
  19. package/esm/modal/views/Reject.js +14 -0
  20. package/esm/modal/views/WalletList.js +40 -0
  21. package/esm/modal/views/index.js +6 -0
  22. package/esm/provider.js +12 -9
  23. package/esm/types/index.js +1 -0
  24. package/esm/types/sign-client.js +1 -0
  25. package/esm/utils/helpers.js +11 -0
  26. package/esm/utils/index.js +1 -1
  27. package/esm/utils/wallet.js +8 -0
  28. package/hooks/index.d.ts +3 -1
  29. package/hooks/index.js +3 -1
  30. package/hooks/useAccount.d.ts +6 -2
  31. package/hooks/useAccount.js +20 -19
  32. package/hooks/useChain.d.ts +2 -2
  33. package/hooks/useChain.js +59 -20
  34. package/hooks/useChainWallet.d.ts +2 -2
  35. package/hooks/useChainWallet.js +33 -10
  36. package/hooks/useConfig.d.ts +1 -1
  37. package/hooks/useCurrentChainWallet.d.ts +2 -0
  38. package/hooks/useCurrentChainWallet.js +15 -0
  39. package/hooks/useCurrentWallet.d.ts +2 -0
  40. package/hooks/useCurrentWallet.js +22 -0
  41. package/hooks/useInterchainClient.d.ts +3 -11
  42. package/hooks/useInterchainClient.js +14 -50
  43. package/hooks/useOfflineSigner.d.ts +3 -0
  44. package/hooks/useOfflineSigner.js +14 -2
  45. package/hooks/useRpcEndpoint.d.ts +6 -0
  46. package/hooks/useRpcEndpoint.js +30 -0
  47. package/hooks/useWalletManager.js +3 -2
  48. package/modal/modal.js +70 -11
  49. package/modal/provider.js +1 -2
  50. package/modal/views/Astronaut.d.ts +2 -0
  51. package/modal/views/Astronaut.js +8 -0
  52. package/modal/views/Connected.d.ts +4 -0
  53. package/modal/views/Connected.js +38 -0
  54. package/modal/views/Connecting.d.ts +8 -0
  55. package/modal/views/Connecting.js +29 -0
  56. package/modal/views/NotExist.d.ts +8 -0
  57. package/modal/views/NotExist.js +49 -0
  58. package/modal/views/QRCode.d.ts +4 -0
  59. package/modal/views/QRCode.js +25 -0
  60. package/modal/views/Reject.d.ts +8 -0
  61. package/modal/views/Reject.js +19 -0
  62. package/modal/views/WalletList.d.ts +5 -0
  63. package/modal/views/WalletList.js +45 -0
  64. package/modal/views/index.d.ts +6 -0
  65. package/modal/views/index.js +22 -0
  66. package/package.json +15 -5
  67. package/provider.d.ts +8 -8
  68. package/provider.js +12 -9
  69. package/types/chain.d.ts +16 -10
  70. package/types/index.d.ts +1 -0
  71. package/types/index.js +1 -0
  72. package/types/sign-client.d.ts +2 -0
  73. package/types/sign-client.js +2 -0
  74. package/types/wallet.d.ts +1 -1
  75. package/utils/helpers.d.ts +1 -0
  76. package/utils/helpers.js +15 -0
  77. package/utils/index.d.ts +1 -0
  78. package/utils/index.js +16 -0
  79. package/utils/wallet.d.ts +7 -0
  80. package/utils/wallet.js +12 -0
  81. package/esm/hooks/useActiveWallet.js +0 -5
  82. package/hooks/useActiveWallet.d.ts +0 -1
  83. package/hooks/useActiveWallet.js +0 -9
package/README.md CHANGED
@@ -1,22 +1,175 @@
1
- # react
2
-
3
1
  <p align="center">
4
2
  <img src="https://user-images.githubusercontent.com/545047/188804067-28e67e5e-0214-4449-ab04-2e0c564a6885.svg" width="80"><br />
5
- interchain-kit wallet connector react package
3
+ @interchain-kit/react
6
4
  </p>
7
5
 
8
- ## install
9
-
6
+ ## Install
7
+ Using npm:
8
+ ```sh
9
+ npm install @interchain-kit/react
10
+ ```
11
+ Using yarn:
10
12
  ```sh
11
- npm install react
13
+ yarn add @interchain-kit/react
14
+ ```
15
+
16
+ ## Usage
17
+ ### Setup
18
+ #### import chain registry info that you need
19
+ ```js
20
+ import { ChainProvider, useChain } from "@interchain-kit/react";
21
+ import { keplrWallet } from "@interchain-kit/keplr-extension";
22
+ import { ThemeProvider } from "@interchain-ui/react";
23
+ import "@interchain-ui/react/styles";
24
+
25
+ import { chain as junoChain, assetList as junoAssetList } from "@chain-registry/v2/mainnet/juno";
26
+ import { chain as osmosisChain,assetList as osmosisAssetList } from "@chain-registry/v2/mainnet/osmosis";
27
+ import { chain as cosmoshubChain, assetList as cosmoshubAssetList } from "@chain-registry/v2/mainnet/cosmoshub";
28
+
29
+ const Show = () => {
30
+ const {address} = useChain('osmosis');
31
+ // will show cosmoshub address from what you selected wallet in modal
32
+ return <div>{address}</div>;
33
+ };
34
+
35
+ function App() {
36
+ return (
37
+ <ThemeProvider>
38
+ <ChainProvider
39
+ chains={[osmosisChain, junoChain, cosmoshubChain]}
40
+ assetLists={[osmosisAssetList, junoAssetList, cosmoshubAssetList]}
41
+ wallets={[keplrWallet]}
42
+ signerOptions={{}}
43
+ endpointOptions={{}}
44
+ >
45
+ <Show />
46
+ </ChainProvider>
47
+ </ThemeProvider>
48
+ );
49
+ }
50
+
51
+ export default App;
52
+ ```
53
+
54
+ #### or import all chain registry
55
+ ```js
56
+ import { ChainProvider, useChain } from "@interchain-kit/react";
57
+ import { keplrWallet } from "@interchain-kit/keplr-extension";
58
+ import { ThemeProvider } from "@interchain-ui/react";
59
+ import "@interchain-ui/react/styles";
60
+ import { chains, assetLists } from '@chain-registry/v2/mainnet'
61
+
62
+
63
+ const Show = () => {
64
+ const {address} = useChain('osmosis');
65
+ // will show cosmoshub address from what you selected wallet in modal
66
+ return <div>{address}</div>;
67
+ };
68
+
69
+ function App() {
70
+ return (
71
+ <ThemeProvider>
72
+ <ChainProvider
73
+ chains={chains}
74
+ assetLists={assetLists}
75
+ wallets={[keplrWallet]}
76
+ signerOptions={{}}
77
+ endpointOptions={{}}
78
+ >
79
+ <Show />
80
+ </ChainProvider>
81
+ </ThemeProvider>
82
+ );
83
+ }
84
+
85
+ export default App;
86
+ ```
87
+
88
+ ### useChain
89
+ ```js
90
+
91
+ const chainName = 'cosmoshub'
92
+ const { chain, assetList, address, wallet, queryClient, signingClient } = useChain(chainName)
93
+
94
+ console.log(wallet) //keprl extension wallet info
95
+ console.log(chain) // chain info for cosmoshub
96
+ console.log(assetList) // assets info for cosmoshub
97
+ console.log(address) // address for cosmoshub in keplr-extension wallet
98
+
99
+ //query
100
+ const { balance } = await queryClient.balance({
101
+ address,
102
+ denom: 'uosmo'
103
+ })
104
+ console.log(balance)
105
+ // { amount: 23423, denom: 'uosmos' }
106
+
107
+ ```
108
+
109
+ ### useChainWallet
110
+ ```js
111
+ import { chain as junoChain, assetList as junoAssetList } from "@chain-registry/v2/mainnet/juno";
112
+ import { chain as stargazeChain,assetList as stargazeAssetList } from "@chain-registry/v2/mainnet/stargaze";
113
+ import { keplrWallet } from "@interchain-kit/keplr-extension";
114
+ import { leapWallet } from "@interchain-kit/leap-extension";
115
+
116
+ const Show = () => {
117
+ const juno = useChainWallet('juno', 'keplr-extension')
118
+ const stargaze = useChainWallet('stargaze', 'leap-extension')
119
+ console.log(juno.address) // juno1xxxxxxx in keplr extension wallet
120
+ console.log(stargaze.addresss) // stargaze1xxxxxx in leap extension wallet
121
+ };
122
+
123
+ const chainNames
124
+
125
+ function App() {
126
+ return (
127
+ <ThemeProvider>
128
+ <ChainProvider
129
+ chains={[junoChain, stargazeChain]}
130
+ assetLists={[junoAssetList, stargazeAssetList]}
131
+ wallets={[keplrWallet, leapWallet]}
132
+ signerOptions={{}}
133
+ endpointOptions={{}}
134
+ >
135
+ <Show />
136
+ </ChainProvider>
137
+ </ThemeProvider>
138
+ );
139
+ }
140
+
141
+ export default App;
142
+ ```
143
+
144
+ ### useCurrentWallet
145
+ ```js
146
+ const wallet = useCurrentWallet()
147
+
148
+ console.log(wallet) // current connected wallet
149
+
150
+ ```
151
+
152
+ ### useAccount
153
+ ```js
154
+ const account = useAccount('cosmoshub', 'keplr-extension')
155
+
156
+ console.log(account.address) // cosmoshub address in keplr-extension wallet
157
+
158
+ ```
159
+
160
+ ### useOfflineSigner
161
+ ```js
162
+ const offlineSigner = useOfflineSigner('cosmoshub', 'keplr-extension')
163
+
164
+ console.log(offlineSigner) // cosmoshub offlineSigner in keplr-extension wallet
165
+ ```
166
+
167
+ ### useChains
168
+
169
+ ```js
170
+ WIP
12
171
  ```
13
- ## Table of contents
14
172
 
15
- - [react](#react)
16
- - [Install](#install)
17
- - [Table of contents](#table-of-contents)
18
- - [Developing](#developing)
19
- - [Credits](#credits)
20
173
 
21
174
  ## Developing
22
175
 
@@ -1,7 +1,9 @@
1
1
  export * from './useWalletManager';
2
- export * from './useActiveWallet';
2
+ export * from './useCurrentWallet';
3
3
  export * from './useAccount';
4
4
  export * from './useChain';
5
5
  export * from './useConnect';
6
6
  export * from './useChainWallet';
7
7
  export * from './useConfig';
8
+ export * from './useOfflineSigner';
9
+ export * from './useRpcEndpoint';
@@ -1,25 +1,26 @@
1
- import { useEffect, useState } from "react";
1
+ import { WalletState } from "@interchain-kit/core";
2
2
  import { useWalletManager } from './useWalletManager';
3
+ import { useEffect } from "react";
3
4
  export const useAccount = (chainName, walletName) => {
4
5
  const walletManager = useWalletManager();
5
- const wallet = walletManager.wallets.find(w => w.option.name === walletName);
6
- const [account, setAccount] = useState(null);
7
- const chain = walletManager.chains.find(c => c.chainName === chainName);
8
- const getAccount = async () => {
9
- const account = await wallet.getAccount(chain.chainId);
10
- setAccount(account);
11
- };
12
- useEffect(() => {
13
- if (wallet) {
14
- wallet.events.on('keystoreChange', getAccount);
15
- }
16
- }, [wallet]);
6
+ const chainAccount = walletManager.getWalletRepositoryByName(walletName)?.getChainAccountByName(chainName);
17
7
  useEffect(() => {
18
- if (!wallet) {
19
- setAccount(null);
20
- return;
8
+ if (chainAccount?.walletState === WalletState.Connected && chainName && walletName) {
9
+ chainAccount.getAccount();
21
10
  }
22
- getAccount();
23
- }, [wallet, chainName, wallet?.walletState]);
24
- return account;
11
+ }, [chainName, walletName, chainAccount?.walletState]);
12
+ if (chainAccount?.walletState === WalletState.Connected && chainName && walletName) {
13
+ return {
14
+ account: chainAccount?.account,
15
+ isLoading: chainAccount.getAccountState().loading,
16
+ error: chainAccount.getAccountState().error,
17
+ getAccount: () => chainAccount?.getAccount()
18
+ };
19
+ }
20
+ return {
21
+ account: undefined,
22
+ isLoading: false,
23
+ error: null,
24
+ getAccount: () => chainAccount?.getAccount()
25
+ };
25
26
  };
@@ -1,39 +1,78 @@
1
1
  import { useWalletManager } from "./useWalletManager";
2
2
  import { useAccount } from "./useAccount";
3
- import { useActiveWallet } from './useActiveWallet';
4
3
  import { useInterchainClient } from './useInterchainClient';
5
4
  import { useWalletModal } from "../modal";
5
+ import { useRpcEndpoint } from "./useRpcEndpoint";
6
+ import { WalletState } from "@interchain-kit/core";
7
+ import { useCurrentChainWallet } from "./useCurrentChainWallet";
6
8
  export const useChain = (chainName) => {
7
9
  const walletManager = useWalletManager();
8
- const chainToShow = walletManager.chains.find((c) => c.chainName === chainName);
9
- const assetList = walletManager.assetLists.find((a) => a.chainName === chainName);
10
- const activeWallet = useActiveWallet();
11
- const account = useAccount(chainName, activeWallet.option.name);
12
- const interchainClient = useInterchainClient(chainName, activeWallet.option.name);
10
+ const chainAccount = useCurrentChainWallet();
11
+ const chain = walletManager.getChainByName(chainName);
12
+ walletManager.currentChainName = chainName;
13
+ const walletName = chainAccount?.info?.name;
14
+ const rpcEndpointHook = useRpcEndpoint(chainName, walletName);
15
+ const accountHook = useAccount(chainName, walletName);
16
+ const signingClientHook = useInterchainClient(chainName, walletName);
13
17
  const { open, close } = useWalletModal();
14
- const clientFactory = walletManager.createClientFactory(activeWallet, chainName);
15
18
  const cosmosKitUserChainReturnType = {
16
19
  connect: () => {
17
- if (activeWallet) {
20
+ if (chainAccount?.walletState === WalletState.Connected) {
18
21
  return;
19
22
  }
23
+ walletManager.currentChainName = chainName;
24
+ open();
25
+ },
26
+ disconnect: async () => {
27
+ walletManager.currentChainName = chainName;
28
+ await chainAccount?.disconnect();
29
+ },
30
+ openView: () => {
31
+ walletManager.currentChainName = chainName;
20
32
  open();
21
33
  },
22
- openView: open,
23
34
  closeView: close,
24
- getRpcEndpoint: () => walletManager.getRpcEndpoint(activeWallet, chainName),
25
- status: activeWallet.walletState,
26
- username: account?.username,
27
- message: activeWallet.errorMessage,
28
- getSigningCosmWasmClient: () => clientFactory.then((c) => c.getSigningCosmwasmClient()),
29
- getSigningStargateClient: () => clientFactory.then((c) => c.getSigningStargateClient())
35
+ getRpcEndpoint: () => chainAccount.getRpcEndpoint(),
36
+ status: chainAccount?.walletState,
37
+ username: accountHook.account?.username,
38
+ message: chainAccount?.errorMessage
30
39
  };
40
+ if (chainAccount && chainAccount?.walletState === WalletState.Connected) {
41
+ return {
42
+ logoUrl: walletManager.getChainLogoUrl(chainName),
43
+ chain,
44
+ assetList: chainAccount?.assetList,
45
+ address: accountHook.account?.address,
46
+ wallet: chainAccount,
47
+ rpcEndpoint: rpcEndpointHook.rpcEndpoint,
48
+ ...cosmosKitUserChainReturnType, //for migration cosmos kit
49
+ signingClient: signingClientHook.signingClient,
50
+ getSigningClient: () => signingClientHook.getSigningClient(),
51
+ isRpcEndpointLoading: rpcEndpointHook.isLoading,
52
+ isAccountLoading: accountHook.isLoading,
53
+ isSigningClientLoading: signingClientHook.isLoading,
54
+ isLoading: rpcEndpointHook.isLoading || accountHook.isLoading || signingClientHook.isLoading,
55
+ getRpcEndpointError: rpcEndpointHook.error,
56
+ getSigningClientError: signingClientHook.error,
57
+ getAccountError: accountHook.error
58
+ };
59
+ }
31
60
  return {
32
- chain: chainToShow,
33
- assetList,
34
- address: account?.address,
35
- wallet: activeWallet,
61
+ logoUrl: walletManager.getChainLogoUrl(chainName),
62
+ chain,
63
+ assetList: chainAccount?.assetList,
64
+ address: accountHook.account?.address,
65
+ wallet: chainAccount,
66
+ rpcEndpoint: rpcEndpointHook.rpcEndpoint,
36
67
  ...cosmosKitUserChainReturnType, //for migration cosmos kit
37
- ...interchainClient
68
+ signingClient: signingClientHook.signingClient,
69
+ getSigningClient: () => signingClientHook.getSigningClient(),
70
+ isRpcEndpointLoading: rpcEndpointHook.isLoading,
71
+ isAccountLoading: accountHook.isLoading,
72
+ isSigningClientLoading: signingClientHook.isLoading,
73
+ isLoading: rpcEndpointHook.isLoading || accountHook.isLoading || signingClientHook.isLoading,
74
+ getRpcEndpointError: rpcEndpointHook.error,
75
+ getSigningClientError: signingClientHook.error,
76
+ getAccountError: accountHook.error
38
77
  };
39
78
  };
@@ -1,18 +1,41 @@
1
1
  import { useWalletManager } from "./useWalletManager";
2
2
  import { useAccount } from "./useAccount";
3
3
  import { useInterchainClient } from "./useInterchainClient";
4
+ import { useRpcEndpoint } from "./useRpcEndpoint";
5
+ import { WalletState } from "@interchain-kit/core";
4
6
  export const useChainWallet = (chainName, walletName) => {
5
7
  const walletManager = useWalletManager();
6
- const chainToShow = walletManager.chains.find((c) => c.chainName === chainName);
7
- const assetList = walletManager.assetLists.find((a) => a.chainName === chainName);
8
- const wallet = walletManager.wallets.find((w) => w.option.name === walletName);
9
- const account = useAccount(chainName, walletName);
10
- const interchainClient = useInterchainClient(chainName, walletName);
8
+ const walletRepository = walletManager.getWalletRepositoryByName(walletName);
9
+ const chainAccount = walletRepository.getChainAccountByName(chainName);
10
+ const rpcEndpointHook = useRpcEndpoint(chainName, walletName);
11
+ const accountHook = useAccount(chainName, walletName);
12
+ const signingClientHook = useInterchainClient(chainName, walletName);
11
13
  return {
12
- chain: chainToShow,
13
- assetList,
14
- address: account?.address,
15
- wallet,
16
- ...interchainClient
14
+ connect: () => {
15
+ if (!chainAccount && chainAccount.walletState === WalletState.Connected) {
16
+ return;
17
+ }
18
+ chainAccount.connect();
19
+ },
20
+ disconnect: () => chainAccount.disconnect(),
21
+ getRpcEndpoint: () => chainAccount.getRpcEndpoint(),
22
+ status: chainAccount.walletState,
23
+ username: accountHook.account?.username,
24
+ message: chainAccount.errorMessage,
25
+ logoUrl: walletManager.getChainLogoUrl(chainName),
26
+ rpcEndpoint: rpcEndpointHook.rpcEndpoint,
27
+ chain: chainAccount.chain,
28
+ assetList: chainAccount.assetList,
29
+ address: accountHook.account?.address,
30
+ wallet: walletRepository,
31
+ signingClient: signingClientHook.signingClient,
32
+ getSigningClient: () => signingClientHook.getSigningClient(),
33
+ isRpcEndpointLoading: rpcEndpointHook.isLoading,
34
+ isAccountLoading: accountHook.isLoading,
35
+ isSigningClientLoading: signingClientHook.isLoading,
36
+ isLoading: rpcEndpointHook.isLoading || accountHook.isLoading || signingClientHook.isLoading,
37
+ getRpcEndpointError: rpcEndpointHook.error,
38
+ getSigningClientError: signingClientHook.error,
39
+ getAccountError: accountHook.error
17
40
  };
18
41
  };
@@ -0,0 +1,11 @@
1
+ import { bindAllMethodsToContext } from "../utils/helpers";
2
+ import { useWalletManager } from "./useWalletManager";
3
+ export const useCurrentChainWallet = () => {
4
+ const walletManager = useWalletManager();
5
+ const { currentChainName, currentWalletName } = walletManager;
6
+ const chainWallet = walletManager.getWalletRepositoryByName(currentWalletName)?.getChainAccountByName(currentChainName);
7
+ if (chainWallet) {
8
+ return bindAllMethodsToContext(chainWallet);
9
+ }
10
+ return {};
11
+ };
@@ -0,0 +1,18 @@
1
+ import { useWalletManager } from "./useWalletManager";
2
+ import { useWalletModal } from "../modal";
3
+ import { bindAllMethodsToContext } from "../utils/helpers";
4
+ export const useCurrentWallet = () => {
5
+ const walletManager = useWalletManager();
6
+ const { open } = useWalletModal();
7
+ // useEffect(() => {
8
+ // if (!walletManager.currentWalletName && walletManager.state === WalletManagerState.Initialized) {
9
+ // open()
10
+ // }
11
+ // }, [walletManager.currentWalletName, walletManager.state])
12
+ const { currentWalletName, currentChainName, getWalletRepositoryByName } = walletManager;
13
+ const wallet = getWalletRepositoryByName(currentWalletName);
14
+ if (wallet) {
15
+ return bindAllMethodsToContext(wallet);
16
+ }
17
+ return {};
18
+ };
@@ -1,56 +1,20 @@
1
- import { useEffect, useState } from "react";
2
1
  import { useWalletManager } from './useWalletManager';
3
- import { useAccount } from './useAccount';
2
+ import { WalletState } from '@interchain-kit/core';
4
3
  export const useInterchainClient = (chainName, walletName) => {
5
- const [rpcEndpoint, setRpcEndpoint] = useState();
6
- const [signingClient, setSigningClient] = useState(null);
7
- const [queryClient, setQueryClient] = useState(null);
8
- const [signingStargateClient, setSigningStargateClient] = useState(null);
9
- const [signingCosmWasmClient, setSigningCosmWasmClient] = useState(null);
10
- const [error, setError] = useState(null);
11
- const [isLoading, setIsLoading] = useState(false);
12
4
  const walletManager = useWalletManager();
13
- const account = useAccount(chainName, walletName);
14
- const initialize = async () => {
15
- const wallet = walletManager.wallets.find((w) => w.option.name === walletName);
16
- const chainToShow = walletManager.chains.find((c) => c.chainName === chainName);
17
- if (!wallet) {
18
- throw new Error('Wallet not found');
19
- }
20
- if (!chainToShow) {
21
- throw new Error('Chain not found');
22
- }
23
- try {
24
- setIsLoading(true);
25
- const rpcEndpoint = await walletManager.getRpcEndpoint(wallet, chainName);
26
- setRpcEndpoint(rpcEndpoint);
27
- const clientFactory = await walletManager.createClientFactory(wallet, chainName);
28
- const queryClient = await clientFactory.getClient();
29
- setQueryClient(queryClient);
30
- const signingClient = await clientFactory.getSigningClient();
31
- setSigningClient(signingClient);
32
- const signingStargateClient = await clientFactory.getSigningStargateClient(chainToShow.bech32Prefix);
33
- setSigningStargateClient(signingStargateClient);
34
- const signingCosmwasmClient = await clientFactory.getSigningCosmwasmClient();
35
- setSigningCosmWasmClient(signingCosmwasmClient);
36
- }
37
- catch (error) {
38
- setError(error);
39
- }
40
- finally {
41
- setIsLoading(false);
42
- }
43
- };
44
- useEffect(() => {
45
- initialize();
46
- }, [chainName, walletName, account]);
5
+ const chainAccount = walletManager.getWalletRepositoryByName(walletName)?.getChainAccountByName(chainName);
6
+ if (chainAccount && chainAccount.walletState === WalletState.Connected) {
7
+ return {
8
+ signingClient: chainAccount?.signingClient,
9
+ isLoading: chainAccount?.getSigningClientState().loading,
10
+ error: chainAccount?.getSigningClientState().error,
11
+ getSigningClient: () => chainAccount?.getSigningClient()
12
+ };
13
+ }
47
14
  return {
48
- rpcEndpoint,
49
- signingClient,
50
- queryClient,
51
- signingStargateClient,
52
- signingCosmWasmClient,
53
- error,
54
- isLoading
15
+ signingClient: undefined,
16
+ isLoading: false,
17
+ error: undefined,
18
+ getSigningClient: () => chainAccount?.getSigningClient()
55
19
  };
56
20
  };
@@ -1,8 +1,20 @@
1
1
  import { useWalletManager } from "./useWalletManager";
2
+ import { WalletState } from "@interchain-kit/core";
2
3
  export const useOfflineSigner = (chainName, walletName) => {
3
4
  const walletManager = useWalletManager();
4
- const wallet = walletManager.wallets.find((w) => w.option.name === walletName);
5
+ const chainAccount = walletManager.getWalletRepositoryByName(walletName)?.getChainAccountByName(chainName);
6
+ if (chainAccount && chainAccount.walletState === WalletState.Connected) {
7
+ return {
8
+ offlineSigner: chainAccount?.offlineSigner,
9
+ isLoading: chainAccount?.getOfflineSignerState().loading,
10
+ error: chainAccount?.getOfflineSignerState().error,
11
+ getOfflineSigner: () => chainAccount?.getOfflineSigner()
12
+ };
13
+ }
5
14
  return {
6
- offlineSigner: walletManager.getOfflineSigner(wallet, chainName),
15
+ offlineSigner: undefined,
16
+ isLoading: false,
17
+ error: undefined,
18
+ getOfflineSigner: () => chainAccount?.getOfflineSigner()
7
19
  };
8
20
  };
@@ -0,0 +1,26 @@
1
+ import { useEffect } from "react";
2
+ import { useWalletManager } from "./useWalletManager";
3
+ import { WalletState } from "@interchain-kit/core";
4
+ export const useRpcEndpoint = (chainName, walletName) => {
5
+ const walletManager = useWalletManager();
6
+ const chainAccount = walletManager.getWalletRepositoryByName(walletName)?.getChainAccountByName(chainName);
7
+ useEffect(() => {
8
+ if (chainAccount?.walletState === WalletState.Connected) {
9
+ chainAccount.getRpcEndpoint();
10
+ }
11
+ }, [chainName, walletName, chainAccount?.walletState]);
12
+ if (chainAccount && chainAccount.walletState === WalletState.Connected) {
13
+ return {
14
+ rpcEndpoint: chainAccount?.rpcEndpoint,
15
+ isLoading: chainAccount?.getRpcEndpointState().loading,
16
+ error: chainAccount?.getRpcEndpointState().error,
17
+ getRpcEndpoint: () => chainAccount?.getRpcEndpoint()
18
+ };
19
+ }
20
+ return {
21
+ rpcEndpoint: undefined,
22
+ isLoading: false,
23
+ error: undefined,
24
+ getRpcEndpoint: () => chainAccount?.getRpcEndpoint()
25
+ };
26
+ };
@@ -1,5 +1,6 @@
1
- import { useInterChainWalletContext } from "../provider";
1
+ import { useInterchainWalletContext } from "../provider";
2
+ import { bindAllMethodsToContext } from '../utils/helpers';
2
3
  export const useWalletManager = () => {
3
- const { walletManager } = useInterChainWalletContext();
4
- return walletManager;
4
+ const { walletManager } = useInterchainWalletContext();
5
+ return bindAllMethodsToContext(walletManager);
5
6
  };
@@ -1,16 +1,75 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { ConnectedContent, ConnectedHeader, ConnectingContent, ConnectingHeader, NotExistContent, NotExistHeader, QRCodeContent, QRCodeHeader, RejectContent, RejectHeader, WalletListContent, WalletListHeader, } from "./views";
3
+ import { useWalletModal } from "./provider";
2
4
  import { useWalletManager } from "../hooks";
3
- const style = {
4
- position: 'absolute',
5
- top: '50%',
6
- left: '50%',
7
- transform: 'translate(-50%,-50%)',
8
- width: '500px',
9
- backgroundColor: 'white',
10
- };
5
+ import { useEffect, useMemo, useState } from "react";
6
+ import { WalletState, } from "@interchain-kit/core";
7
+ import { ConnectModal } from "@interchain-ui/react";
8
+ import { useCurrentChainWallet } from "../hooks/useCurrentChainWallet";
11
9
  export const WalletModal = () => {
10
+ const { modalIsOpen, open, close } = useWalletModal();
12
11
  const walletManager = useWalletManager();
13
- return (_jsx("ul", { style: style, children: walletManager.wallets.map(wallet => {
14
- return (_jsxs("li", { children: [_jsx("span", { children: wallet.option.prettyName }), _jsx("button", { onClick: () => walletManager.connect(wallet.option.name), children: "connect" })] }));
15
- }) }));
12
+ const handleSelectWallet = async (wallet) => {
13
+ walletManager.currentWalletName = wallet.info.name;
14
+ const currentWallet = walletManager
15
+ .getWalletRepositoryByName(wallet.info.name)
16
+ .getChainAccountByName(walletManager.currentChainName);
17
+ setModalView({
18
+ header: _jsx(ConnectingHeader, { wallet: wallet, onBack: gotoWalletList }),
19
+ content: _jsx(ConnectingContent, { wallet: wallet }),
20
+ });
21
+ if (currentWallet.wallet?.info.mode === "extension" &&
22
+ !currentWallet.wallet.isExtensionInstalled) {
23
+ setModalView({
24
+ header: (_jsx(NotExistHeader, { wallet: currentWallet, onBack: gotoWalletList })),
25
+ content: _jsx(NotExistContent, { wallet: currentWallet }),
26
+ });
27
+ return;
28
+ }
29
+ try {
30
+ if (currentWallet.wallet?.info.mode === "wallet-connect") {
31
+ currentWallet.wallet.setOnPairingUriCreatedCallback(() => {
32
+ setModalView({
33
+ header: _jsx(QRCodeHeader, { onBack: gotoWalletList }),
34
+ content: _jsx(QRCodeContent, {}),
35
+ });
36
+ });
37
+ currentWallet.wallet.setPairingToConnect(wallet.pairing);
38
+ }
39
+ await currentWallet.connect();
40
+ await currentWallet.getAccount();
41
+ setModalView({
42
+ header: _jsx(ConnectedHeader, { onBack: gotoWalletList }),
43
+ content: _jsx(ConnectedContent, {}),
44
+ });
45
+ }
46
+ catch (error) {
47
+ console.log(error);
48
+ setModalView({
49
+ header: _jsx(RejectHeader, { wallet: wallet, onBack: gotoWalletList }),
50
+ content: _jsx(RejectContent, { wallet: wallet }),
51
+ });
52
+ }
53
+ };
54
+ const defaultModalView = useMemo(() => {
55
+ return {
56
+ header: _jsx(WalletListHeader, {}),
57
+ content: _jsx(WalletListContent, { onSelectWallet: handleSelectWallet }),
58
+ };
59
+ }, []);
60
+ const [modalView, setModalView] = useState(defaultModalView);
61
+ const gotoWalletList = () => setModalView(defaultModalView);
62
+ const currentWallet = useCurrentChainWallet();
63
+ useEffect(() => {
64
+ if (modalIsOpen && currentWallet?.walletState === WalletState.Connected) {
65
+ setModalView({
66
+ header: _jsx(ConnectedHeader, { onBack: gotoWalletList }),
67
+ content: _jsx(ConnectedContent, {}),
68
+ });
69
+ }
70
+ else {
71
+ setModalView(defaultModalView);
72
+ }
73
+ }, [modalIsOpen]);
74
+ return (_jsx(ConnectModal, { isOpen: modalIsOpen, header: modalView.header, onOpen: open, onClose: close, children: modalView.content }));
16
75
  };
@@ -1,13 +1,12 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { createContext, useContext, useState } from "react";
3
- import { createPortal } from "react-dom";
4
3
  import { WalletModal } from "./modal";
5
4
  const WalletModalContext = createContext(null);
6
5
  export const WalletModalProvider = ({ children }) => {
7
6
  const [modalIsOpen, setModalIsOpen] = useState(false);
8
7
  const open = () => setModalIsOpen(true);
9
8
  const close = () => setModalIsOpen(false);
10
- return (_jsxs(WalletModalContext.Provider, { value: { modalIsOpen, open, close }, children: [children, modalIsOpen && createPortal(_jsx(WalletModal, {}), document.body)] }));
9
+ return (_jsxs(WalletModalContext.Provider, { value: { modalIsOpen, open, close }, children: [children, _jsx(WalletModal, {})] }));
11
10
  };
12
11
  export const useWalletModal = () => {
13
12
  const context = useContext(WalletModalContext);