@interchain-kit/react 0.0.1-beta.8 → 0.1.0

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 (86) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +173 -24
  3. package/esm/hooks/index.js +0 -4
  4. package/esm/hooks/useChain.js +28 -30
  5. package/esm/hooks/useChainWallet.js +25 -12
  6. package/esm/hooks/useWalletManager.js +4 -3
  7. package/esm/index.js +1 -0
  8. package/esm/modal/modal.js +96 -24
  9. package/esm/modal/provider.js +2 -2
  10. package/esm/modal/views/Connected.js +16 -17
  11. package/esm/modal/views/Connecting.js +14 -23
  12. package/esm/modal/views/NotExist.js +44 -0
  13. package/esm/modal/views/QRCode.js +19 -0
  14. package/esm/modal/views/Reject.js +4 -9
  15. package/esm/modal/views/WalletList.js +27 -19
  16. package/esm/modal/views/index.js +2 -0
  17. package/esm/provider.js +13 -13
  18. package/esm/store/chain-wallet.js +59 -0
  19. package/esm/store/index.js +191 -0
  20. package/esm/types/index.js +1 -1
  21. package/esm/utils/index.js +1 -8
  22. package/esm/utils/wallet.js +8 -1
  23. package/hooks/index.d.ts +0 -4
  24. package/hooks/index.js +0 -4
  25. package/hooks/useChain.d.ts +2 -2
  26. package/hooks/useChain.js +28 -30
  27. package/hooks/useChainWallet.d.ts +2 -2
  28. package/hooks/useChainWallet.js +25 -12
  29. package/hooks/useWalletManager.d.ts +1 -2
  30. package/hooks/useWalletManager.js +3 -2
  31. package/index.d.ts +1 -0
  32. package/index.js +1 -0
  33. package/modal/modal.js +95 -23
  34. package/modal/provider.d.ts +1 -1
  35. package/modal/provider.js +2 -2
  36. package/modal/views/Astronaut.js +1 -2
  37. package/modal/views/Connected.d.ts +6 -2
  38. package/modal/views/Connected.js +15 -16
  39. package/modal/views/Connecting.d.ts +6 -2
  40. package/modal/views/Connecting.js +12 -21
  41. package/modal/views/NotExist.d.ts +8 -0
  42. package/modal/views/NotExist.js +49 -0
  43. package/modal/views/QRCode.d.ts +4 -0
  44. package/modal/views/QRCode.js +24 -0
  45. package/modal/views/Reject.d.ts +7 -2
  46. package/modal/views/Reject.js +4 -9
  47. package/modal/views/WalletList.d.ts +4 -1
  48. package/modal/views/WalletList.js +27 -19
  49. package/modal/views/index.d.ts +2 -0
  50. package/modal/views/index.js +2 -0
  51. package/package.json +15 -8
  52. package/provider.d.ts +11 -11
  53. package/provider.js +13 -13
  54. package/store/chain-wallet.d.ts +26 -0
  55. package/store/chain-wallet.js +63 -0
  56. package/store/index.d.ts +45 -0
  57. package/store/index.js +195 -0
  58. package/types/chain.d.ts +12 -13
  59. package/types/index.d.ts +1 -1
  60. package/types/index.js +1 -1
  61. package/types/sign-client.d.ts +2 -0
  62. package/utils/index.d.ts +1 -7
  63. package/utils/index.js +15 -10
  64. package/utils/wallet.d.ts +7 -1
  65. package/utils/wallet.js +10 -15
  66. package/esm/hooks/useAccount.js +0 -28
  67. package/esm/hooks/useActiveWallet.js +0 -13
  68. package/esm/hooks/useConfig.js +0 -10
  69. package/esm/hooks/useConnect.js +0 -11
  70. package/esm/hooks/useInterchainClient.js +0 -52
  71. package/esm/hooks/useOfflineSigner.js +0 -15
  72. package/hooks/useAccount.d.ts +0 -2
  73. package/hooks/useAccount.js +0 -32
  74. package/hooks/useActiveWallet.d.ts +0 -1
  75. package/hooks/useActiveWallet.js +0 -17
  76. package/hooks/useConfig.d.ts +0 -8
  77. package/hooks/useConfig.js +0 -14
  78. package/hooks/useConnect.d.ts +0 -1
  79. package/hooks/useConnect.js +0 -15
  80. package/hooks/useInterchainClient.d.ts +0 -14
  81. package/hooks/useInterchainClient.js +0 -56
  82. package/hooks/useOfflineSigner.d.ts +0 -4
  83. package/hooks/useOfflineSigner.js +0 -19
  84. package/types/wallet.d.ts +0 -11
  85. /package/esm/types/{wallet.js → sign-client.js} +0 -0
  86. /package/types/{wallet.js → sign-client.js} +0 -0
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2024 Cosmology Developers <developers@cosmology.zone>
3
+ Copyright (c) 2024 Cosmology Developers <developers@hyperweb.io>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,22 +1,168 @@
1
- # react
1
+ # @interchain-kit/react
2
2
 
3
- <p align="center">
4
- <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
+ <p align="center" width="100%">
4
+ <img height="90" src="https://user-images.githubusercontent.com/545047/190171432-5526db8f-9952-45ce-a745-bea4302f912b.svg" />
6
5
  </p>
7
6
 
8
- ## install
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/hyperweb-io/cosmos-kit/actions/workflows/run-tests.yml">
9
+ <img height="20" src="https://github.com/hyperweb-io/cosmos-kit/actions/workflows/run-tests.yml/badge.svg" />
10
+ </a>
11
+ <a href="https://github.com/hyperweb-io/lib-count">
12
+ <img height="20" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fhyperweb-io%2Flib-count%2Fmain%2Foutput%2Fbadges%2Fproducts%2Fcosmos-kit%2Ftotal.json"/>
13
+ </a>
14
+ <a href="https://github.com/hyperweb-io/lib-count">
15
+ <img height="20" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fhyperweb-io%2Flib-count%2Fmain%2Foutput%2Fbadges%2Fproducts%2Fcosmos-kit%2Fmonthly.json"/>
16
+ </a>
17
+ <br />
18
+ <a href="https://github.com/hyperweb-io/cosmos-kit/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-BSD%203--Clause%20Clear-blue.svg"></a>
19
+ <a href="https://www.npmjs.com/package/cosmos-kit"><img height="20" src="https://img.shields.io/github/package-json/v/hyperweb-io/cosmos-kit?filename=packages%2Fcosmos-kit%2Fpackage.json"></a>
20
+ </p>
9
21
 
22
+ ## Install
23
+ Using npm:
24
+ ```sh
25
+ npm install @interchain-kit/react
26
+ ```
27
+ Using yarn:
10
28
  ```sh
11
- npm install react
29
+ yarn add @interchain-kit/react
30
+ ```
31
+
32
+ ## Usage
33
+ ### Setup
34
+ #### import chain registry info that you need
35
+ ```js
36
+ import { ChainProvider, useChain } from "@interchain-kit/react";
37
+ import { keplrWallet } from "@interchain-kit/keplr-extension";
38
+ import { ThemeProvider } from "@interchain-ui/react";
39
+ import "@interchain-ui/react/styles";
40
+
41
+ import { chain as junoChain, assetList as junoAssetList } from "@chain-registry/v2/mainnet/juno";
42
+ import { chain as osmosisChain,assetList as osmosisAssetList } from "@chain-registry/v2/mainnet/osmosis";
43
+ import { chain as cosmoshubChain, assetList as cosmoshubAssetList } from "@chain-registry/v2/mainnet/cosmoshub";
44
+
45
+ const Show = () => {
46
+ const {address} = useChain('osmosis');
47
+ // will show cosmoshub address from what you selected wallet in modal
48
+ return <div>{address}</div>;
49
+ };
50
+
51
+ function App() {
52
+ return (
53
+ <ThemeProvider>
54
+ <ChainProvider
55
+ chains={[osmosisChain, junoChain, cosmoshubChain]}
56
+ assetLists={[osmosisAssetList, junoAssetList, cosmoshubAssetList]}
57
+ wallets={[keplrWallet]}
58
+ signerOptions={{}}
59
+ endpointOptions={{}}
60
+ >
61
+ <Show />
62
+ </ChainProvider>
63
+ </ThemeProvider>
64
+ );
65
+ }
66
+
67
+ export default App;
68
+ ```
69
+
70
+ #### or import all chain registry
71
+ ```js
72
+ import { ChainProvider, useChain } from "@interchain-kit/react";
73
+ import { keplrWallet } from "@interchain-kit/keplr-extension";
74
+ import { ThemeProvider } from "@interchain-ui/react";
75
+ import "@interchain-ui/react/styles";
76
+ import { chains, assetLists } from '@chain-registry/v2/mainnet'
77
+
78
+
79
+ const Show = () => {
80
+ const {address} = useChain('osmosis');
81
+ // will show cosmoshub address from what you selected wallet in modal
82
+ return <div>{address}</div>;
83
+ };
84
+
85
+ function App() {
86
+ return (
87
+ <ThemeProvider>
88
+ <ChainProvider
89
+ chains={chains}
90
+ assetLists={assetLists}
91
+ wallets={[keplrWallet]}
92
+ signerOptions={{}}
93
+ endpointOptions={{}}
94
+ >
95
+ <Show />
96
+ </ChainProvider>
97
+ </ThemeProvider>
98
+ );
99
+ }
100
+
101
+ export default App;
102
+ ```
103
+
104
+ ### useChain
105
+ ```js
106
+
107
+ const chainName = 'cosmoshub'
108
+ const { chain, assetList, address, wallet } = useChain(chainName)
109
+
110
+ console.log(wallet) //keprl extension wallet info
111
+ console.log(chain) // chain info for cosmoshub
112
+ console.log(assetList) // assets info for cosmoshub
113
+ console.log(address) // address for cosmoshub in keplr-extension wallet
114
+
115
+ ```
116
+
117
+ ### useChainWallet
118
+ ```js
119
+ import { chain as junoChain, assetList as junoAssetList } from "@chain-registry/v2/mainnet/juno";
120
+ import { chain as stargazeChain,assetList as stargazeAssetList } from "@chain-registry/v2/mainnet/stargaze";
121
+ import { keplrWallet } from "@interchain-kit/keplr-extension";
122
+ import { leapWallet } from "@interchain-kit/leap-extension";
123
+
124
+ const Show = () => {
125
+ const juno = useChainWallet('juno', 'keplr-extension')
126
+ const stargaze = useChainWallet('stargaze', 'leap-extension')
127
+ console.log(juno.address) // juno1xxxxxxx in keplr extension wallet
128
+ console.log(stargaze.addresss) // stargaze1xxxxxx in leap extension wallet
129
+ };
130
+
131
+ const chainNames
132
+
133
+ function App() {
134
+ return (
135
+ <ThemeProvider>
136
+ <ChainProvider
137
+ chains={[junoChain, stargazeChain]}
138
+ assetLists={[junoAssetList, stargazeAssetList]}
139
+ wallets={[keplrWallet, leapWallet]}
140
+ signerOptions={{}}
141
+ endpointOptions={{}}
142
+ >
143
+ <Show />
144
+ </ChainProvider>
145
+ </ThemeProvider>
146
+ );
147
+ }
148
+
149
+ export default App;
150
+ ```
151
+
152
+ ### useChains
153
+
154
+ ```js
155
+ WIP
12
156
  ```
13
- ## Table of contents
157
+ ### use wallet methods
158
+ ```js
159
+ const { wallet } = useChain('osmosis')
14
160
 
15
- - [react](#react)
16
- - [Install](#install)
17
- - [Table of contents](#table-of-contents)
18
- - [Developing](#developing)
19
- - [Credits](#credits)
161
+ //use method from wallet that you select
162
+ await wallet.signAmino(chainId, signAddress, stdDoc)
163
+ await wallet.verifyArbitrary(chainId, signAddress, stdDoc)
164
+
165
+ ```
20
166
 
21
167
  ## Developing
22
168
 
@@ -33,25 +179,28 @@ Or if you want to make your dev process smoother, you can run:
33
179
  ```sh
34
180
  yarn
35
181
  # build the dev packages with .map files, this enables navigation from references to their source code between packages.
36
- yarn build:dev
182
+ yarn watch:dev
37
183
  ```
38
184
 
39
- ## Related
185
+ ## Interchain JavaScript Stack
40
186
 
41
- Checkout these related projects:
187
+ A unified toolkit for building applications and smart contracts in the Interchain ecosystem ⚛️
42
188
 
43
- * [@cosmology/telescope](https://github.com/cosmology-tech/telescope) Your Frontend Companion for Building with TypeScript with Cosmos SDK Modules.
44
- * [@cosmwasm/ts-codegen](https://github.com/CosmWasm/ts-codegen) Convert your CosmWasm smart contracts into dev-friendly TypeScript classes.
45
- * [chain-registry](https://github.com/cosmology-tech/chain-registry) Everything from token symbols, logos, and IBC denominations for all assets you want to support in your application.
46
- * [cosmos-kit](https://github.com/cosmology-tech/cosmos-kit) Experience the convenience of connecting with a variety of web3 wallets through a single, streamlined interface.
47
- * [create-cosmos-app](https://github.com/cosmology-tech/create-cosmos-app) Set up a modern Cosmos app by running one command.
48
- * [interchain-ui](https://github.com/cosmology-tech/interchain-ui) The Interchain Design System, empowering developers with a flexible, easy-to-use UI kit.
49
- * [starship](https://github.com/cosmology-tech/starship) Unified Testing and Development for the Interchain.
189
+ | Category | Tools | Description |
190
+ |----------------------|------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
191
+ | **Chain Information** | [**Chain Registry**](https://github.com/hyperweb-io/chain-registry), [**Utils**](https://www.npmjs.com/package/@chain-registry/utils), [**Client**](https://www.npmjs.com/package/@chain-registry/client) | Everything from token symbols, logos, and IBC denominations for all assets you want to support in your application. |
192
+ | **Wallet Connectors**| [**Interchain Kit**](https://github.com/hyperweb-io/interchain-kit)<sup>beta</sup>, [**Cosmos Kit**](https://github.com/hyperweb-io/cosmos-kit) | Experience the convenience of connecting with a variety of web3 wallets through a single, streamlined interface. |
193
+ | **Signing Clients** | [**InterchainJS**](https://github.com/hyperweb-io/interchainjs)<sup>beta</sup>, [**CosmJS**](https://github.com/cosmos/cosmjs) | A single, universal signing interface for any network |
194
+ | **SDK Clients** | [**Telescope**](https://github.com/hyperweb-io/telescope) | Your Frontend Companion for Building with TypeScript with Cosmos SDK Modules. |
195
+ | **Starter Kits** | [**Create Interchain App**](https://github.com/hyperweb-io/create-interchain-app)<sup>beta</sup>, [**Create Cosmos App**](https://github.com/hyperweb-io/create-cosmos-app) | Set up a modern Interchain app by running one command. |
196
+ | **UI Kits** | [**Interchain UI**](https://github.com/hyperweb-io/interchain-ui) | The Interchain Design System, empowering developers with a flexible, easy-to-use UI kit. |
197
+ | **Testing Frameworks** | [**Starship**](https://github.com/hyperweb-io/starship) | Unified Testing and Development for the Interchain. |
198
+ | **TypeScript Smart Contracts** | [**Create Hyperweb App**](https://github.com/hyperweb-io/create-hyperweb-app) | Build and deploy full-stack blockchain applications with TypeScript |
199
+ | **CosmWasm Contracts** | [**CosmWasm TS Codegen**](https://github.com/CosmWasm/ts-codegen) | Convert your CosmWasm smart contracts into dev-friendly TypeScript classes. |
50
200
 
51
201
  ## Credits
52
202
 
53
- 🛠 Built by Cosmology — if you like our tools, please consider delegating to [our validator ⚛️](https://cosmology.zone/validator)
54
-
203
+ 🛠 Built by Hyperweb (formerly Cosmology) — if you like our tools, please checkout and contribute to [our github ⚛️](https://github.com/hyperweb-io)
55
204
 
56
205
  ## Disclaimer
57
206
 
@@ -1,7 +1,3 @@
1
1
  export * from './useWalletManager';
2
- export * from './useActiveWallet';
3
- export * from './useAccount';
4
2
  export * from './useChain';
5
- export * from './useConnect';
6
3
  export * from './useChainWallet';
7
- export * from './useConfig';
@@ -1,42 +1,40 @@
1
- import { useWalletManager } from "./useWalletManager";
2
- import { useAccount } from "./useAccount";
3
- import { useActiveWallet } from './useActiveWallet';
4
- import { useInterchainClient } from './useInterchainClient';
5
1
  import { useWalletModal } from "../modal";
6
- import { ChainNameNotExist } from "@interChain-kit/core";
2
+ import { useWalletManager } from './useWalletManager';
3
+ import { ChainNameNotExist } from '@interchain-kit/core';
4
+ import { ChainWallet } from '../store/chain-wallet';
7
5
  export const useChain = (chainName) => {
8
- const walletManager = useWalletManager();
9
- const chainToShow = walletManager.chains.find((c) => c.chainName === chainName);
10
- const assetList = walletManager.assetLists.find((a) => a.chainName === chainName);
11
- const activeWallet = useActiveWallet();
12
- const account = useAccount(chainName, activeWallet?.option?.name);
13
- const interchainClient = useInterchainClient(chainName, activeWallet?.option?.name);
14
- if (!chainToShow) {
6
+ const { assetLists, currentWalletName, disconnect, setCurrentChainName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = useWalletManager();
7
+ const chain = getChainByName(chainName);
8
+ if (!chain) {
15
9
  throw new ChainNameNotExist(chainName);
16
10
  }
11
+ const assetList = assetLists.find(a => a.chainName === chainName);
12
+ const wallet = getWalletByName(currentWalletName);
13
+ const chainWalletStateToShow = getChainWalletState(currentWalletName, chainName);
17
14
  const { open, close } = useWalletModal();
18
- const cosmosKitUserChainReturnType = {
15
+ return {
16
+ //for migration cosmos kit
19
17
  connect: () => {
20
- if (activeWallet) {
21
- return;
22
- }
18
+ setCurrentChainName(chainName);
19
+ open();
20
+ },
21
+ disconnect: async () => disconnect(currentWalletName, chainName),
22
+ openView: () => {
23
+ setCurrentChainName(chainName);
23
24
  open();
24
25
  },
25
- openView: open,
26
26
  closeView: close,
27
- getRpcEndpoint: () => walletManager.getRpcEndpoint(activeWallet, chainName),
28
- status: activeWallet?.walletState,
29
- username: account?.username,
30
- message: activeWallet?.errorMessage,
31
- getSigningCosmWasmClient: () => walletManager.createClientFactory(activeWallet, chainName).then((c) => c.getSigningCosmwasmClient()),
32
- getSigningStargateClient: () => walletManager.createClientFactory(activeWallet, chainName).then((c) => c.getSigningStargateClient())
33
- };
34
- return {
35
- chain: chainToShow,
27
+ getRpcEndpoint: () => getRpcEndpoint(currentWalletName, chainName),
28
+ status: chainWalletStateToShow?.walletState,
29
+ username: chainWalletStateToShow?.account?.username,
30
+ message: chainWalletStateToShow?.errorMessage,
31
+ // new api
32
+ logoUrl: getChainLogoUrl(chainName),
33
+ chain,
36
34
  assetList,
37
- address: account?.address,
38
- wallet: activeWallet,
39
- ...cosmosKitUserChainReturnType, //for migration cosmos kit
40
- ...interchainClient
35
+ address: chainWalletStateToShow?.account?.address,
36
+ wallet: new ChainWallet(wallet, () => connect(currentWalletName, chainName), () => disconnect(currentWalletName, chainName), () => getAccount(currentWalletName, chainName)),
37
+ rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
38
+ getSigningClient: () => getSigningClient(currentWalletName, chainName),
41
39
  };
42
40
  };
@@ -1,18 +1,31 @@
1
1
  import { useWalletManager } from "./useWalletManager";
2
- import { useAccount } from "./useAccount";
3
- import { useInterchainClient } from "./useInterchainClient";
2
+ import { ChainWallet } from "../store/chain-wallet";
4
3
  export const useChainWallet = (chainName, walletName) => {
5
- 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);
4
+ const { assetLists, disconnect, setCurrentChainName, setCurrentWalletName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = useWalletManager();
5
+ const chain = getChainByName(chainName);
6
+ const wallet = getWalletByName(walletName);
7
+ const assetList = assetLists.find(a => a.chainName === chainName);
8
+ const chainWalletStateToShow = getChainWalletState(walletName, chainName);
11
9
  return {
12
- chain: chainToShow,
10
+ //for migration cosmos kit
11
+ connect: async () => {
12
+ await connect(walletName, chainName);
13
+ setCurrentWalletName(walletName);
14
+ setCurrentChainName(chainName);
15
+ await getAccount(walletName, chainName);
16
+ },
17
+ disconnect: () => disconnect(walletName, chainName),
18
+ getRpcEndpoint: () => getRpcEndpoint(walletName, chainName),
19
+ status: chainWalletStateToShow?.walletState,
20
+ username: chainWalletStateToShow?.account?.username,
21
+ message: chainWalletStateToShow?.errorMessage,
22
+ // new api
23
+ logoUrl: getChainLogoUrl(chainName),
24
+ chain,
13
25
  assetList,
14
- address: account?.address,
15
- wallet,
16
- ...interchainClient
26
+ address: chainWalletStateToShow?.account?.address,
27
+ wallet: new ChainWallet(wallet, () => connect(walletName, chainName), () => disconnect(walletName, chainName), () => getAccount(walletName, chainName)),
28
+ rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
29
+ getSigningClient: () => getSigningClient(walletName, chainName),
17
30
  };
18
31
  };
@@ -1,5 +1,6 @@
1
- import { useInterChainWalletContext } from "../provider";
1
+ import { useInterchainWalletContext } from "../provider";
2
+ import { useStore } from 'zustand';
2
3
  export const useWalletManager = () => {
3
- const { walletManager } = useInterChainWalletContext();
4
- return walletManager;
4
+ const store = useInterchainWalletContext();
5
+ return useStore(store);
5
6
  };
package/esm/index.js CHANGED
@@ -3,3 +3,4 @@ export * from './hooks';
3
3
  export * from './types';
4
4
  export * from './enum';
5
5
  export * from './modal';
6
+ export * from './store';
@@ -1,31 +1,103 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { ConnectModal } from "@interchain-ui/react";
3
- import { ConnectedContent, ConnectedHeader, ConnectingContent, ConnectingHeader, RejectContent, RejectHeader, WalletListContent, WalletListHeader } from "./views";
2
+ import { ConnectedContent, ConnectedHeader, ConnectingContent, ConnectingHeader, NotExistContent, NotExistHeader, QRCodeContent, QRCodeHeader, RejectContent, RejectHeader, WalletListContent, WalletListHeader, } from "./views";
4
3
  import { useWalletModal } from "./provider";
5
- import { useActiveWallet } from "../hooks";
6
- import { useEffect, useState } from "react";
7
- const defaultModalView = {
8
- header: _jsx(WalletListHeader, {}), content: _jsx(WalletListContent, {})
9
- };
4
+ import { useChainWallet, useWalletManager } from "../hooks";
5
+ import { useEffect, useMemo, useState } from "react";
6
+ import { WalletState, WCWallet } from "@interchain-kit/core";
7
+ import { ConnectModal } from "@interchain-ui/react";
10
8
  export const WalletModal = () => {
9
+ const [qrCode, setQRCode] = useState(null);
10
+ const { currentWalletName, currentChainName, wallets, connect, getAccount, setCurrentWalletName, } = useWalletManager();
11
11
  const { modalIsOpen, open, close } = useWalletModal();
12
- const activeWallet = useActiveWallet();
13
- const [modalView, setModalView] = useState(defaultModalView);
14
- const gotoWalletList = () => setModalView(defaultModalView);
12
+ const [modalType, setModalType] = useState("wallet-list");
13
+ const [selectedWallet, setSelectedWallet] = useState(null);
14
+ const { chain, status, wallet } = useChainWallet(currentChainName, currentWalletName);
15
+ const handleConnect = async () => {
16
+ if (wallet.originalWallet instanceof WCWallet) {
17
+ wallet.originalWallet.setOnPairingUriCreatedCallback((uri) => {
18
+ setQRCode(uri);
19
+ });
20
+ }
21
+ await connect(selectedWallet?.info?.name, chain.chainName);
22
+ await getAccount(selectedWallet?.info?.name, chain.chainName);
23
+ setSelectedWallet(null);
24
+ };
15
25
  useEffect(() => {
16
- switch (true) {
17
- case activeWallet?.walletState === "Connecting" /* WalletState.Connecting */:
18
- setModalView({ header: _jsx(ConnectingHeader, { onBack: gotoWalletList }), content: _jsx(ConnectingContent, {}) });
19
- break;
20
- case activeWallet?.walletState === "Connected" /* WalletState.Connected */:
21
- setModalView({ header: _jsx(ConnectedHeader, { onBack: gotoWalletList }), content: _jsx(ConnectedContent, {}) });
22
- break;
23
- case activeWallet?.walletState === "Reject" /* WalletState.Reject */:
24
- setModalView({ header: _jsx(RejectHeader, { onBack: gotoWalletList }), content: _jsx(RejectContent, {}) });
25
- break;
26
- default:
27
- setModalView(defaultModalView);
26
+ if (selectedWallet && currentWalletName && currentChainName) {
27
+ handleConnect();
28
+ }
29
+ }, [selectedWallet]);
30
+ const handleSelectWallet = async (selectedWallet) => {
31
+ setSelectedWallet(selectedWallet);
32
+ setCurrentWalletName(selectedWallet?.info?.name);
33
+ };
34
+ useEffect(() => {
35
+ if (!selectedWallet) {
36
+ setModalType("wallet-list");
37
+ }
38
+ if (currentWalletName && currentChainName) {
39
+ if (status === WalletState.Connecting) {
40
+ setModalType("connecting");
41
+ }
42
+ if (status === WalletState.Connected) {
43
+ setModalType("connected");
44
+ }
45
+ if (status === WalletState.Rejected) {
46
+ setModalType("reject");
47
+ }
48
+ if (status === WalletState.Disconnected) {
49
+ setModalType("wallet-list");
50
+ }
51
+ if (qrCode) {
52
+ setModalType("qr-code");
53
+ }
54
+ }
55
+ }, [
56
+ currentWalletName,
57
+ currentChainName,
58
+ status,
59
+ modalIsOpen,
60
+ selectedWallet,
61
+ qrCode
62
+ ]);
63
+ const goBackList = () => setModalType("wallet-list");
64
+ const { header, content } = useMemo(() => {
65
+ switch (modalType) {
66
+ case "wallet-list":
67
+ return {
68
+ header: _jsx(WalletListHeader, {}),
69
+ content: _jsx(WalletListContent, { onSelectWallet: handleSelectWallet }),
70
+ };
71
+ case "connecting":
72
+ return {
73
+ header: _jsx(ConnectingHeader, { wallet: wallet, onBack: goBackList }),
74
+ content: _jsx(ConnectingContent, { wallet: wallet }),
75
+ };
76
+ case "connected":
77
+ return {
78
+ header: _jsx(ConnectedHeader, { wallet: wallet, onBack: goBackList }),
79
+ content: _jsx(ConnectedContent, { afterDisconnect: goBackList }),
80
+ };
81
+ case "reject":
82
+ return {
83
+ header: _jsx(RejectHeader, { wallet: wallet, onBack: goBackList }),
84
+ content: (_jsx(RejectContent, { wallet: wallet, onReconnect: () => {
85
+ setModalType("connecting");
86
+ wallet.connect(chain.chainId);
87
+ wallet.getAccount(chain.chainId);
88
+ } })),
89
+ };
90
+ case "not-exist":
91
+ return {
92
+ header: _jsx(NotExistHeader, { wallet: wallet, onBack: goBackList }),
93
+ content: _jsx(NotExistContent, { wallet: wallet }),
94
+ };
95
+ case "qr-code":
96
+ return {
97
+ header: _jsx(QRCodeHeader, { onBack: goBackList }),
98
+ content: _jsx(QRCodeContent, {}),
99
+ };
28
100
  }
29
- }, [activeWallet, activeWallet?.walletState]);
30
- return (_jsx(ConnectModal, { isOpen: modalIsOpen, header: modalView.header, onOpen: open, onClose: close, children: modalView.content }));
101
+ }, [modalType]);
102
+ return (_jsx(ConnectModal, { isOpen: modalIsOpen, header: header, onOpen: open, onClose: close, children: content }));
31
103
  };
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { createContext, useContext, useState } from "react";
3
3
  import { WalletModal } from "./modal";
4
4
  const WalletModalContext = createContext(null);
5
- export const WalletModalProvider = ({ children }) => {
5
+ export const WalletModalProvider = ({ children, }) => {
6
6
  const [modalIsOpen, setModalIsOpen] = useState(false);
7
7
  const open = () => setModalIsOpen(true);
8
8
  const close = () => setModalIsOpen(false);
@@ -11,7 +11,7 @@ export const WalletModalProvider = ({ children }) => {
11
11
  export const useWalletModal = () => {
12
12
  const context = useContext(WalletModalContext);
13
13
  if (!context) {
14
- throw new Error('useWalletModal must be used within a WalletModalProvider');
14
+ throw new Error("useWalletModal must be used within a WalletModalProvider");
15
15
  }
16
16
  return context;
17
17
  };
@@ -1,32 +1,31 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { ConnectModalHead, ConnectModalStatus } from "@interchain-ui/react";
3
- import { useAccount, useActiveWallet, useWalletManager } from "../../hooks";
3
+ import { useChainWallet, useWalletManager } from "../../hooks";
4
4
  import { useWalletModal } from "../provider";
5
5
  import { getWalletInfo } from "../../utils";
6
6
  import { AstronautSvg } from "./Astronaut";
7
- export const ConnectedHeader = ({ onBack }) => {
8
- const activeWallet = useActiveWallet();
7
+ export const ConnectedHeader = ({ wallet, onBack, }) => {
9
8
  const { close } = useWalletModal();
10
- return (_jsx(ConnectModalHead, { title: activeWallet?.option?.prettyName || '', hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: { onClick: close } }));
9
+ return (_jsx(ConnectModalHead, { title: wallet?.info?.prettyName || "", hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: { onClick: close } }));
11
10
  };
12
- export const ConnectedContent = () => {
13
- const activeWallet = useActiveWallet();
14
- const walletManager = useWalletManager();
15
- const account = useAccount(walletManager.chains[0].chainName, activeWallet?.option?.name);
11
+ export const ConnectedContent = ({ afterDisconnect, }) => {
12
+ const { currentChainName, currentWalletName } = useWalletManager();
13
+ console.log({ currentChainName, currentWalletName });
14
+ const { address, username, wallet } = useChainWallet(currentChainName, currentWalletName);
16
15
  const { close } = useWalletModal();
17
- if (!activeWallet) {
16
+ if (!wallet) {
18
17
  return null;
19
18
  }
20
- return (_jsx(ConnectModalStatus, { wallet: getWalletInfo(activeWallet), status: 'Connected', connectedInfo: {
21
- name: account?.username || 'Wallet',
19
+ return (_jsx(ConnectModalStatus, { wallet: getWalletInfo(wallet), status: "Connected", connectedInfo: {
20
+ name: username || "Wallet",
22
21
  avatar: (_jsx(AstronautSvg, { style: {
23
- fontSize: 'inherit',
24
- width: '100%',
25
- height: '100%',
22
+ fontSize: "inherit",
23
+ width: "100%",
24
+ height: "100%",
26
25
  } })),
27
- address: account?.address,
26
+ address: address,
28
27
  }, onDisconnect: async () => {
29
- await walletManager.disconnect(activeWallet?.option?.name);
30
- close();
28
+ await wallet.disconnect(currentChainName);
29
+ afterDisconnect();
31
30
  } }));
32
31
  };
@@ -1,33 +1,24 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { ConnectModalHead, ConnectModalStatus } from '@interchain-ui/react';
3
- import { useActiveWallet, useWalletManager } from '../../hooks';
4
- import { useWalletModal } from '../provider';
5
- export const ConnectingHeader = ({ onBack }) => {
6
- const wallet = useActiveWallet();
2
+ import { ConnectModalHead, ConnectModalStatus } from "@interchain-ui/react";
3
+ import { useWalletModal } from "../provider";
4
+ export const ConnectingHeader = ({ wallet, onBack, }) => {
7
5
  const { close } = useWalletModal();
8
- const walletManager = useWalletManager();
9
- if (!wallet)
10
- return null;
11
- return (_jsx(ConnectModalHead, { title: wallet.option.prettyName, hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: {
12
- onClick: async () => {
13
- await walletManager.disconnect(wallet.option.name);
14
- close();
15
- }
6
+ return (_jsx(ConnectModalHead, { title: wallet.info.prettyName, hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: {
7
+ onClick: close,
16
8
  } }));
17
9
  };
18
- export const ConnectingContent = () => {
19
- const activeWallet = useActiveWallet();
20
- const { option: { prettyName, mode } } = activeWallet;
21
- let title = 'Requesting Connection';
22
- let desc = mode === 'wallet-connect'
10
+ export const ConnectingContent = ({ wallet }) => {
11
+ const { info: { prettyName, mode }, } = wallet;
12
+ let title = "Requesting Connection";
13
+ let desc = mode === "wallet-connect"
23
14
  ? `Approve ${prettyName} connection request on your mobile.`
24
15
  : `Open the ${prettyName} browser extension to connect your wallet.`;
25
- if (!activeWallet)
16
+ if (!wallet)
26
17
  return null;
27
18
  return (_jsx(ConnectModalStatus, { wallet: {
28
- name: activeWallet.option.name,
29
- prettyName: activeWallet.option.prettyName,
30
- logo: activeWallet.option.logo,
19
+ name: wallet.info.name,
20
+ prettyName: wallet.info.prettyName,
21
+ logo: wallet.info.logo,
31
22
  mobileDisabled: true,
32
- }, status: 'Connecting', contentHeader: title, contentDesc: desc }));
23
+ }, status: "Connecting", contentHeader: title, contentDesc: desc }));
33
24
  };