@interchain-kit/react 0.3.22 → 0.3.24

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.
package/store/store.js CHANGED
@@ -6,7 +6,7 @@ const zustand_1 = require("zustand");
6
6
  const immer_1 = require("zustand/middleware/immer");
7
7
  const middleware_1 = require("zustand/middleware");
8
8
  const utils_1 = require("../utils");
9
- const decorateWallet_1 = require("../utils/decorateWallet");
9
+ const stateful_wallet_1 = require("./stateful-wallet");
10
10
  const immerSyncUp = (newWalletManager) => {
11
11
  return (draft) => {
12
12
  draft.chains = newWalletManager.chains;
@@ -20,15 +20,18 @@ const immerSyncUp = (newWalletManager) => {
20
20
  };
21
21
  };
22
22
  const createInterchainStore = (walletManager) => {
23
- const { chains, assetLists, wallets, signerOptions, endpointOptions } = walletManager;
24
- // const walletManager = new WalletManager(chains, assetLists, wallets, signerOptions, endpointOptions)
25
23
  return (0, zustand_1.createStore)((0, middleware_1.persist)((0, immer_1.immer)((set, get) => ({
26
24
  chainWalletState: [],
27
25
  currentWalletName: '',
28
26
  currentChainName: '',
29
27
  chains: [...walletManager.chains],
30
28
  assetLists: [...walletManager.assetLists],
31
- wallets: [],
29
+ wallets: walletManager.wallets.map(wallet => {
30
+ const walletSet = (fn) => {
31
+ set((draft) => fn(draft.wallets.find(w => w.info.name === wallet.info.name)));
32
+ };
33
+ return new stateful_wallet_1.StatefulWallet(wallet, walletSet, () => get().wallets.find(w => w.info.name === wallet.info.name), set, get);
34
+ }),
32
35
  signerOptions: walletManager.signerOptions,
33
36
  endpointOptions: walletManager.endpointOptions,
34
37
  preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
@@ -36,151 +39,49 @@ const createInterchainStore = (walletManager) => {
36
39
  endpointOptionsMap: { ...walletManager.endpointOptionsMap },
37
40
  walletConnectQRCodeUri: '',
38
41
  isReady: false,
39
- updateWalletState: (walletName, data) => {
42
+ modalIsOpen: false,
43
+ openModal: () => {
40
44
  set(draft => {
41
- let targetWalletIndex = draft.wallets.findIndex(w => w.info.name === walletName);
42
- draft.wallets[targetWalletIndex].walletState = data.walletState;
45
+ draft.modalIsOpen = true;
46
+ });
47
+ },
48
+ closeModal: () => {
49
+ set(draft => {
50
+ draft.modalIsOpen = false;
51
+ draft.walletConnectQRCodeUri = ''; // reset the QR code uri when modal is closed
43
52
  });
44
53
  },
45
54
  updateChainWalletState: (walletName, chainName, data) => {
46
55
  set(draft => {
47
56
  let targetIndex = draft.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
48
57
  draft.chainWalletState[targetIndex] = { ...draft.chainWalletState[targetIndex], ...data };
49
- if (data.walletState) {
50
- get().updateWalletState(walletName, data);
51
- }
52
58
  });
53
59
  },
54
60
  createStatefulWallet: () => {
55
61
  const wallets = walletManager.wallets.map(wallet => {
56
- // safeStrictBatchPatch(wallet, {
57
- // connect: async (original, chainId) => {
58
- // const walletName = wallet.info.name
59
- // const chainName = get().chains.find(chain => chain.chainId === chainId)?.chainName
60
- // const state = get().getChainWalletState(walletName, chainName)?.walletState
61
- // if (state === WalletState.NotExist) {
62
- // return
63
- // }
64
- // if (walletName === 'WalletConnect' && state === WalletState.Connected) {
65
- // return
66
- // }
67
- // set(draft => {
68
- // draft.currentChainName = chainName
69
- // draft.currentWalletName = walletName
70
- // draft.walletConnectQRCodeUri = ''
71
- // })
72
- // get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Connecting, errorMessage: '' })
73
- // try {
74
- // if (wallet instanceof WCWallet) {
75
- // wallet.setOnPairingUriCreatedCallback((uri) => {
76
- // set(draft => {
77
- // draft.walletConnectQRCodeUri = uri
78
- // })
79
- // })
80
- // }
81
- // await original(chainId)
82
- // get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Connected })
83
- // await get().getAccount(walletName, chainName)
84
- // } catch (error) {
85
- // if ((error as any).message === 'Request rejected') {
86
- // get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Rejected, errorMessage: (error as any).message })
87
- // return
88
- // }
89
- // get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Disconnected, errorMessage: (error as any).message })
90
- // }
91
- // },
92
- // disconnect: async (original, chainId) => {
93
- // const walletName = wallet.info.name
94
- // const chainName = get().chains.find(chain => chain.chainId === chainId)?.chainName
95
- // try {
96
- // await original(chainId)
97
- // get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Disconnected, account: null })
98
- // } catch (error) {
99
- // }
100
- // },
101
- // getAccount: async (original, chainId) => {
102
- // const walletName = wallet.info.name
103
- // const chainName = get().chains.find(chain => chain.chainId === chainId)?.chainName
104
- // try {
105
- // const account = await original(chainId)
106
- // get().updateChainWalletState(walletName, chainName, { account })
107
- // return account
108
- // } catch (error) {
109
- // console.log(error)
110
- // }
111
- // },
112
- // walletState: get().getChainWalletState(wallet.info.name, walletManager.chains?.[0].chainName)?.walletState || WalletState.Disconnected
113
- // })
114
- // return wallet
115
- return (0, decorateWallet_1.decorateWallet)(wallet, {
116
- connect: async (chainId) => {
117
- const walletName = wallet.info.name;
118
- const chainName = get().chains.find(chain => chain.chainId === chainId)?.chainName;
119
- const state = get().getChainWalletState(walletName, chainName)?.walletState;
120
- if (state === core_1.WalletState.NotExist) {
121
- return;
122
- }
123
- if (walletName === 'WalletConnect' && state === core_1.WalletState.Connected) {
124
- return;
125
- }
126
- set(draft => {
127
- draft.currentChainName = chainName;
128
- draft.currentWalletName = walletName;
129
- draft.walletConnectQRCodeUri = '';
130
- });
131
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Connecting, errorMessage: '' });
132
- try {
133
- if (wallet instanceof core_1.WCWallet) {
134
- wallet.setOnPairingUriCreatedCallback((uri) => {
135
- set(draft => {
136
- draft.walletConnectQRCodeUri = uri;
137
- });
138
- });
139
- }
140
- await wallet.connect(chainId);
141
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Connected });
142
- await get().getAccount(walletName, chainName);
143
- }
144
- catch (error) {
145
- if (error.message === 'Request rejected') {
146
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Rejected, errorMessage: error.message });
147
- return;
148
- }
149
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Disconnected, errorMessage: error.message });
150
- }
151
- },
152
- disconnect: async (chainId) => {
153
- const walletName = wallet.info.name;
154
- const chainName = get().chains.find(chain => chain.chainId === chainId)?.chainName;
155
- try {
156
- await wallet.disconnect(chainId);
157
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Disconnected, account: null });
158
- }
159
- catch (error) {
160
- }
161
- },
162
- getAccount: async (chainId) => {
163
- const walletName = wallet.info.name;
164
- const chainName = get().chains.find(chain => chain.chainId === chainId)?.chainName;
165
- try {
166
- const account = await wallet.getAccount(chainId);
167
- get().updateChainWalletState(walletName, chainName, { account });
168
- return account;
169
- }
170
- catch (error) {
171
- console.log(error);
172
- }
173
- },
174
- walletState: get().getChainWalletState(wallet.info.name, walletManager.chains?.[0].chainName)?.walletState || core_1.WalletState.Disconnected
175
- });
62
+ const walletSet = (fn) => {
63
+ set((draft) => fn(draft.wallets.find(w => w.info.name === wallet.info.name)));
64
+ };
65
+ return new stateful_wallet_1.StatefulWallet(wallet, walletSet, () => get().wallets.find(w => w.info.name === wallet.info.name), set, get);
176
66
  });
177
67
  set(draft => {
178
68
  draft.wallets = wallets;
179
69
  });
70
+ const defaultWalletStates = get().chainWalletState.reduce((acc, cws) => {
71
+ if (acc[cws.walletName] && cws.walletState === core_1.WalletState.Connected) {
72
+ return acc;
73
+ }
74
+ return { ...acc, [cws.walletName]: cws.walletState };
75
+ }, {});
76
+ set(draft => {
77
+ draft.wallets.forEach(wallet => {
78
+ wallet.walletState = defaultWalletStates[wallet.info.name];
79
+ });
80
+ });
180
81
  },
181
82
  init: async () => {
182
83
  const oldChainWalletStatesMap = new Map(get().chainWalletState.map(cws => [cws.walletName + cws.chainName, cws]));
183
- get().createStatefulWallet();
84
+ // get().createStatefulWallet()
184
85
  // should remove wallet that already disconnected ,for hydrain back from localstorage
185
86
  // const oldChainWalletStateMap = new Map()
186
87
  // get().chainWalletState.forEach(cws => {
@@ -220,7 +121,6 @@ const createInterchainStore = (walletManager) => {
220
121
  set(draft => {
221
122
  draft.chainWalletState = draft.chainWalletState.map(cws => {
222
123
  if (NotExistWallets.includes(cws.walletName)) {
223
- get().updateWalletState(cws.walletName, { walletState: core_1.WalletState.NotExist });
224
124
  return { ...cws, walletState: core_1.WalletState.NotExist };
225
125
  }
226
126
  return cws;
@@ -228,7 +128,6 @@ const createInterchainStore = (walletManager) => {
228
128
  draft.chainWalletState = draft.chainWalletState.map(cws => {
229
129
  if (ExistWallets.includes(cws.walletName)) {
230
130
  const newState = cws.walletState === core_1.WalletState.NotExist ? core_1.WalletState.Disconnected : cws.walletState;
231
- get().updateWalletState(cws.walletName, { walletState: newState });
232
131
  return { ...cws, walletState: newState };
233
132
  }
234
133
  return cws;
@@ -298,53 +197,37 @@ const createInterchainStore = (walletManager) => {
298
197
  });
299
198
  },
300
199
  connect: async (walletName, chainName) => {
301
- const state = get().getChainWalletState(walletName, chainName)?.walletState;
302
- if (state === core_1.WalletState.NotExist) {
303
- return;
304
- }
305
- if (walletName === 'WalletConnect' && state === core_1.WalletState.Connected) {
306
- return;
307
- }
308
- set(draft => {
309
- draft.currentChainName = chainName;
310
- draft.currentWalletName = walletName;
311
- draft.walletConnectQRCodeUri = '';
312
- });
313
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Connecting, errorMessage: '' });
314
- try {
315
- await walletManager.connect(walletName, chainName, (uri) => {
316
- set(draft => {
317
- draft.walletConnectQRCodeUri = uri;
318
- });
319
- });
320
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Connected });
321
- await get().getAccount(walletName, chainName);
200
+ const wallet = get().wallets.find(w => w.info.name === walletName);
201
+ const chain = get().chains.find(c => c.chainName === chainName);
202
+ if (!wallet) {
203
+ throw new Error(`Wallet ${walletName} not found`);
322
204
  }
323
- catch (error) {
324
- if (error.message === 'Request rejected') {
325
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Rejected, errorMessage: error.message });
326
- return;
327
- }
328
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Disconnected, errorMessage: error.message });
205
+ if (!chain) {
206
+ throw new Error(`Chain ${chainName} not found`);
329
207
  }
208
+ return wallet.connect(chain.chainId);
330
209
  },
331
210
  disconnect: async (walletName, chainName) => {
332
- try {
333
- await walletManager.disconnect(walletName, chainName);
334
- get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Disconnected, account: null });
211
+ const wallet = get().wallets.find(w => w.info.name === walletName);
212
+ const chain = get().chains.find(c => c.chainName === chainName);
213
+ if (!wallet) {
214
+ throw new Error(`Wallet ${walletName} not found`);
335
215
  }
336
- catch (error) {
216
+ if (!chain) {
217
+ throw new Error(`Chain ${chainName} not found`);
337
218
  }
219
+ return wallet.disconnect(chain.chainId);
338
220
  },
339
221
  getAccount: async (walletName, chainName) => {
340
- try {
341
- const account = await walletManager.getAccount(walletName, chainName);
342
- get().updateChainWalletState(walletName, chainName, { account });
343
- return account;
222
+ const wallet = get().wallets.find(w => w.info.name === walletName);
223
+ const chain = get().chains.find(c => c.chainName === chainName);
224
+ if (!wallet) {
225
+ throw new Error(`Wallet ${walletName} not found`);
344
226
  }
345
- catch (error) {
346
- console.log(error);
227
+ if (!chain) {
228
+ throw new Error(`Chain ${chainName} not found`);
347
229
  }
230
+ return wallet.getAccount(chain.chainId);
348
231
  },
349
232
  getRpcEndpoint: async (walletName, chainName) => {
350
233
  return (0, utils_1.dedupeAsync)(`${chainName}-rpcEndpoint`, async () => {
@@ -383,6 +266,9 @@ const createInterchainStore = (walletManager) => {
383
266
  getWalletByName(walletName) {
384
267
  return walletManager.getWalletByName(walletName);
385
268
  },
269
+ getStatefulWalletByName(walletName) {
270
+ return get().wallets.find(w => w.info.name === walletName);
271
+ },
386
272
  async getSigningClient(walletName, chainName) {
387
273
  return walletManager.getSigningClient(walletName, chainName);
388
274
  },
package/types/chain.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { HttpEndpoint } from '@interchainjs/types';
2
2
  import { Chain, AssetList } from '@chain-registry/v2-types';
3
- import { BaseWallet, WalletState } from '@interchain-kit/core';
3
+ import { WalletState } from '@interchain-kit/core';
4
4
  import { SigningClient } from './sign-client';
5
+ import { StatefulWallet } from '../store/stateful-wallet';
5
6
  export type CosmosKitUseChainReturnType = {
6
7
  connect: () => void;
7
8
  disconnect: () => void;
@@ -17,7 +18,7 @@ export type UseChainReturnType = {
17
18
  chain: Chain;
18
19
  assetList: AssetList;
19
20
  address: string;
20
- wallet: BaseWallet;
21
+ wallet: StatefulWallet;
21
22
  rpcEndpoint: string | HttpEndpoint | unknown;
22
23
  getSigningClient: () => Promise<SigningClient>;
23
24
  signingClient: SigningClient | null;