@interchain-kit/react 0.3.23 → 0.3.25

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/hooks/useChain.js CHANGED
@@ -1,20 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useChain = void 0;
4
- const modal_1 = require("../modal");
5
4
  const useWalletManager_1 = require("./useWalletManager");
6
5
  const core_1 = require("@interchain-kit/core");
7
6
  const useSigningClient_1 = require("./useSigningClient");
7
+ const useWalletModal_1 = require("./useWalletModal");
8
8
  const useChain = (chainName) => {
9
- const { assetLists, currentWalletName, disconnect, setCurrentChainName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = (0, useWalletManager_1.useWalletManager)();
9
+ const { assetLists, currentWalletName, disconnect, setCurrentChainName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount, getStatefulWalletByName } = (0, useWalletManager_1.useWalletManager)();
10
10
  const chain = getChainByName(chainName);
11
11
  if (!chain) {
12
12
  throw new core_1.ChainNameNotExist(chainName);
13
13
  }
14
14
  const assetList = assetLists.find(a => a.chainName === chainName);
15
- const wallet = getWalletByName(currentWalletName);
15
+ const wallet = getStatefulWalletByName(currentWalletName);
16
16
  const chainWalletStateToShow = getChainWalletState(currentWalletName, chainName);
17
- const { open, close } = (0, modal_1.useWalletModal)();
17
+ const { open, close } = (0, useWalletModal_1.useWalletModal)();
18
18
  const { signingClient, isLoading: isSigningClientLoading, error: signingClientError } = (0, useSigningClient_1.useSigningClient)(chainName, currentWalletName);
19
19
  return {
20
20
  //for migration cosmos kit
@@ -4,9 +4,9 @@ exports.useChainWallet = void 0;
4
4
  const useWalletManager_1 = require("./useWalletManager");
5
5
  const useSigningClient_1 = require("./useSigningClient");
6
6
  const useChainWallet = (chainName, walletName) => {
7
- const { assetLists, disconnect, setCurrentChainName, setCurrentWalletName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = (0, useWalletManager_1.useWalletManager)();
7
+ const { assetLists, disconnect, setCurrentChainName, setCurrentWalletName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount, getStatefulWalletByName } = (0, useWalletManager_1.useWalletManager)();
8
8
  const chain = getChainByName(chainName);
9
- const wallet = getWalletByName(walletName);
9
+ const wallet = getStatefulWalletByName(walletName);
10
10
  const assetList = assetLists.find(a => a.chainName === chainName);
11
11
  const chainWalletStateToShow = getChainWalletState(walletName, chainName);
12
12
  const { signingClient, isLoading: isSigningClientLoading, error: signingClientError } = (0, useSigningClient_1.useSigningClient)(chainName, walletName);
@@ -0,0 +1,5 @@
1
+ export declare const useWalletModal: () => {
2
+ modalIsOpen: boolean;
3
+ open: () => void;
4
+ close: () => void;
5
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useWalletModal = void 0;
4
+ const useWalletManager_1 = require("./useWalletManager");
5
+ const useWalletModal = () => {
6
+ const { modalIsOpen, openModal, closeModal } = (0, useWalletManager_1.useWalletManager)();
7
+ return {
8
+ modalIsOpen, open: openModal, close: closeModal
9
+ };
10
+ };
11
+ exports.useWalletModal = useWalletModal;
package/modal/index.d.ts CHANGED
@@ -1,2 +1 @@
1
1
  export * from './modal';
2
- export * from './provider';
package/modal/index.js CHANGED
@@ -15,4 +15,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./modal"), exports);
18
- __exportStar(require("./provider"), exports);
package/modal/modal.d.ts CHANGED
@@ -1,13 +1,15 @@
1
- import { BaseWallet, DownloadInfo } from "@interchain-kit/core";
1
+ import { ReactElement } from "react";
2
+ import { DownloadInfo } from "@interchain-kit/core";
2
3
  import { Wallet as InterchainUIWalletType } from "@interchain-ui/react";
3
- export type WalletModalProps = {
4
+ import { StatefulWallet } from "../store/stateful-wallet";
5
+ export type InterchainWalletModalProps = {
4
6
  shouldShowList: boolean;
5
7
  isOpen: boolean;
6
8
  walletConnectQRCodeUri: string | null;
7
9
  wallets: InterchainUIWalletType[];
8
10
  username: string;
9
11
  address: string;
10
- currentWallet?: BaseWallet;
12
+ currentWallet?: StatefulWallet;
11
13
  isConnecting: boolean;
12
14
  isConnected: boolean;
13
15
  isRejected: boolean;
@@ -17,7 +19,7 @@ export type WalletModalProps = {
17
19
  open: () => void;
18
20
  close: () => void;
19
21
  disconnect: () => void;
20
- onSelectWallet: (wallet: BaseWallet) => void;
22
+ onSelectWallet: (wallet: StatefulWallet) => void;
21
23
  onBack: () => void;
22
24
  onReconnect: () => void;
23
25
  getDownloadLink: (walletName: string) => DownloadInfo;
@@ -27,4 +29,15 @@ export type WalletModalProps = {
27
29
  os?: string;
28
30
  };
29
31
  };
30
- export declare const WalletModal: ({ shouldShowList, isOpen, walletConnectQRCodeUri, wallets, username, address, currentWallet, isConnecting, isConnected, isRejected, isDisconnected, isNotExist, errorMessage, open, close, disconnect, onSelectWallet, onBack, onReconnect, getDownloadLink, getEnv, }: WalletModalProps) => import("react/jsx-runtime").JSX.Element;
32
+ export type WalletModalProps = {
33
+ isOpen: boolean;
34
+ wallets: StatefulWallet[];
35
+ currentWallet?: StatefulWallet;
36
+ open: () => void;
37
+ close: () => void;
38
+ };
39
+ export declare const InterchainWalletModal: () => import("react/jsx-runtime").JSX.Element;
40
+ export declare const WalletModal: ({ shouldShowList, isOpen, walletConnectQRCodeUri, wallets, username, address, currentWallet, isConnecting, isConnected, isRejected, isDisconnected, isNotExist, errorMessage, open, close, disconnect, onSelectWallet, onBack, onReconnect, getDownloadLink, getEnv, }: InterchainWalletModalProps) => import("react/jsx-runtime").JSX.Element;
41
+ export declare const ModalRenderer: ({ walletModal: ProvidedWalletModal, }: {
42
+ walletModal: (props: WalletModalProps) => ReactElement;
43
+ }) => import("react/jsx-runtime").JSX.Element;
package/modal/modal.js CHANGED
@@ -1,18 +1,55 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WalletModal = void 0;
3
+ exports.ModalRenderer = exports.WalletModal = exports.InterchainWalletModal = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const views_1 = require("./views");
6
6
  const react_1 = require("react");
7
+ const core_1 = require("@interchain-kit/core");
7
8
  const react_2 = require("@interchain-ui/react");
9
+ const hooks_1 = require("../hooks");
10
+ const utils_1 = require("../utils");
11
+ const InterchainWalletModal = () => {
12
+ const [shouldShowList, setShouldShowList] = (0, react_1.useState)(false);
13
+ const { modalIsOpen: isOpen, walletConnectQRCodeUri, wallets: statefulWallets, getChainWalletState, currentWalletName, currentChainName, openModal: open, closeModal: close, chains, setCurrentWalletName, getDownloadLink, getEnv, } = (0, hooks_1.useWalletManager)();
14
+ const walletsForUI = statefulWallets.map((w) => (0, utils_1.transferToWalletUISchema)(w));
15
+ const chainNameToConnect = currentChainName || chains[0].chainName;
16
+ const chainToConnect = chains.find((chain) => chain.chainName === chainNameToConnect);
17
+ const currentWallet = statefulWallets.find((w) => w.info.name === currentWalletName);
18
+ const { account, errorMessage } = getChainWalletState(currentWalletName, currentChainName) ||
19
+ {};
20
+ const disconnect = () => {
21
+ currentWallet.disconnect(chainToConnect.chainId);
22
+ };
23
+ const onSelectWallet = (wallet) => {
24
+ setShouldShowList(false);
25
+ return wallet.connect(chainToConnect.chainId);
26
+ };
27
+ const onBack = () => setShouldShowList(true);
28
+ const handleCloseModal = () => {
29
+ close();
30
+ setShouldShowList(false);
31
+ };
32
+ const onReconnect = () => {
33
+ currentWallet.connect(chainToConnect.chainId);
34
+ };
35
+ return ((0, jsx_runtime_1.jsx)(exports.WalletModal, { shouldShowList: shouldShowList, username: account?.username, address: account?.address, disconnect: disconnect, isOpen: isOpen, open: open, close: handleCloseModal, wallets: walletsForUI, walletConnectQRCodeUri: walletConnectQRCodeUri, currentWallet: currentWallet, isConnecting: currentWallet?.walletState === core_1.WalletState.Connecting, isConnected: currentWallet?.walletState === core_1.WalletState.Connected, isRejected: currentWallet?.walletState === core_1.WalletState.Rejected, isDisconnected: currentWallet?.walletState === core_1.WalletState.Disconnected, isNotExist: currentWallet?.walletState === core_1.WalletState.NotExist, errorMessage: errorMessage, onSelectWallet: (w) => onSelectWallet(w), onBack: () => setShouldShowList(true), onReconnect: () => onSelectWallet(currentWallet), getDownloadLink: () => getDownloadLink(currentWallet?.info.name), getEnv: getEnv }));
36
+ };
37
+ exports.InterchainWalletModal = InterchainWalletModal;
8
38
  const WalletModal = ({ shouldShowList, isOpen, walletConnectQRCodeUri, wallets, username, address, currentWallet, isConnecting, isConnected, isRejected, isDisconnected, isNotExist, errorMessage, open, close, disconnect, onSelectWallet, onBack, onReconnect, getDownloadLink, getEnv, }) => {
9
39
  const { header, content } = (0, react_1.useMemo)(() => {
10
- if (shouldShowList || isDisconnected) {
40
+ if (shouldShowList ||
41
+ (isDisconnected && currentWallet.errorMessage === "")) {
11
42
  return {
12
43
  header: (0, jsx_runtime_1.jsx)(views_1.WalletListHeader, { close: close }),
13
44
  content: ((0, jsx_runtime_1.jsx)(views_1.WalletListContent, { onSelectWallet: onSelectWallet, wallets: wallets })),
14
45
  };
15
46
  }
47
+ if (currentWallet && currentWallet.errorMessage) {
48
+ return {
49
+ header: ((0, jsx_runtime_1.jsx)(views_1.ErrorHeader, { wallet: currentWallet, close: close, onBack: onBack })),
50
+ content: (0, jsx_runtime_1.jsx)(views_1.ErrorContent, { wallet: currentWallet, onBack: onBack }),
51
+ };
52
+ }
16
53
  if (currentWallet &&
17
54
  walletConnectQRCodeUri &&
18
55
  currentWallet.info.name === "WalletConnect") {
@@ -61,3 +98,12 @@ const WalletModal = ({ shouldShowList, isOpen, walletConnectQRCodeUri, wallets,
61
98
  return ((0, jsx_runtime_1.jsx)(react_2.ConnectModal, { isOpen: isOpen, header: header, onOpen: open, onClose: close, children: content }));
62
99
  };
63
100
  exports.WalletModal = WalletModal;
101
+ const ModalRenderer = ({ walletModal: ProvidedWalletModal, }) => {
102
+ if (!ProvidedWalletModal) {
103
+ throw new Error(`InterchainWalletProvider: walletModal is required. Please provide a wallet modal component. or use InterchainkitWalletModal/n
104
+ Example: <ChainProvider chains={chains} assetLists={assetLists} wallets={wallets} walletModal={InterchainKitWalletModal} />`);
105
+ }
106
+ const { modalIsOpen, openModal, closeModal, wallets, currentWalletName } = (0, hooks_1.useWalletManager)();
107
+ return ((0, jsx_runtime_1.jsx)(ProvidedWalletModal, { wallets: wallets, isOpen: modalIsOpen, open: openModal, close: closeModal, currentWallet: wallets.find((w) => w.info.name === currentWalletName) }));
108
+ };
109
+ exports.ModalRenderer = ModalRenderer;
package/modal/provider.js CHANGED
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useWalletModal = exports.WalletModalProvider = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const react_1 = require("react");
6
- const modal_1 = require("./modal");
7
6
  const core_1 = require("@interchain-kit/core");
8
7
  const hooks_1 = require("../hooks");
9
8
  const utils_1 = require("../utils");
9
+ const modal_1 = require("./modal");
10
10
  const WalletModalContext = (0, react_1.createContext)(null);
11
11
  const WalletModalProvider = ({ children, }) => {
12
12
  const [modalIsOpen, setModalIsOpen] = (0, react_1.useState)(false);
@@ -30,7 +30,7 @@ const WalletModalProvider = ({ children, }) => {
30
30
  setCurrentWalletName(walletName);
31
31
  await connect(walletName, chainToConnect);
32
32
  };
33
- return ((0, jsx_runtime_1.jsxs)(WalletModalContext.Provider, { value: { modalIsOpen, open, close }, children: [children, (0, jsx_runtime_1.jsx)(modal_1.WalletModal, { shouldShowList: shouldShowList, username: username, address: address, disconnect: disconnect, isOpen: modalIsOpen, open: open, close: handleCloseModal, wallets: walletsForUI, walletConnectQRCodeUri: walletConnectQRCodeUri, currentWallet: wallet, isConnecting: status === core_1.WalletState.Connecting, isConnected: status === core_1.WalletState.Connected, isRejected: status === core_1.WalletState.Rejected, isDisconnected: status === core_1.WalletState.Disconnected, isNotExist: status === core_1.WalletState.NotExist, errorMessage: message, onSelectWallet: (w) => handleConnectWallet(w.info.name), onBack: () => setShouldShowList(true), onReconnect: () => handleConnectWallet(currentWalletName), getDownloadLink: () => getDownloadLink(wallet?.info.name), getEnv: getEnv })] }));
33
+ return ((0, jsx_runtime_1.jsxs)(WalletModalContext.Provider, { value: { modalIsOpen, open, close }, children: [children, (0, jsx_runtime_1.jsx)(modal_1.InterchainWalletModal, { shouldShowList: shouldShowList, username: username, address: address, disconnect: disconnect, isOpen: modalIsOpen, open: open, close: handleCloseModal, wallets: walletsForUI, walletConnectQRCodeUri: walletConnectQRCodeUri, currentWallet: wallet, isConnecting: status === core_1.WalletState.Connecting, isConnected: status === core_1.WalletState.Connected, isRejected: status === core_1.WalletState.Rejected, isDisconnected: status === core_1.WalletState.Disconnected, isNotExist: status === core_1.WalletState.NotExist, errorMessage: message, onSelectWallet: (w) => handleConnectWallet(w.info.name), onBack: () => setShouldShowList(true), onReconnect: () => handleConnectWallet(currentWalletName), getDownloadLink: () => getDownloadLink(wallet?.info.name), getEnv: getEnv })] }));
34
34
  };
35
35
  exports.WalletModalProvider = WalletModalProvider;
36
36
  const useWalletModal = () => {
@@ -0,0 +1,10 @@
1
+ import { StatefulWallet } from "../../store/stateful-wallet";
2
+ export declare const ErrorHeader: ({ wallet, close, onBack, }: {
3
+ wallet: StatefulWallet;
4
+ close: () => void;
5
+ onBack: () => void;
6
+ }) => import("react/jsx-runtime").JSX.Element;
7
+ export declare const ErrorContent: ({ wallet, onBack, }: {
8
+ wallet: StatefulWallet;
9
+ onBack: () => void;
10
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorContent = exports.ErrorHeader = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("@interchain-ui/react");
6
+ const utils_1 = require("../../utils");
7
+ const ErrorHeader = ({ wallet, close, onBack, }) => {
8
+ return ((0, jsx_runtime_1.jsx)(react_1.ConnectModalHead, { title: wallet.info.prettyName, hasBackButton: true, onClose: close, onBack: onBack, closeButtonProps: { onClick: close } }));
9
+ };
10
+ exports.ErrorHeader = ErrorHeader;
11
+ const ErrorContent = ({ wallet, onBack, }) => {
12
+ return ((0, jsx_runtime_1.jsx)(react_1.ConnectModalStatus, { status: "Error", wallet: (0, utils_1.getWalletInfo)(wallet), contentHeader: "Oops! Something wrong...", contentDesc: wallet.errorMessage, onChangeWallet: onBack }));
13
+ };
14
+ exports.ErrorContent = ErrorContent;
@@ -4,3 +4,4 @@ export * from './Connected';
4
4
  export * from './Reject';
5
5
  export * from './QRCode';
6
6
  export * from './NotExist';
7
+ export * from './Error';
@@ -20,3 +20,4 @@ __exportStar(require("./Connected"), exports);
20
20
  __exportStar(require("./Reject"), exports);
21
21
  __exportStar(require("./QRCode"), exports);
22
22
  __exportStar(require("./NotExist"), exports);
23
+ __exportStar(require("./Error"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interchain-kit/react",
3
- "version": "0.3.23",
3
+ "version": "0.3.25",
4
4
  "author": "Hyperweb <developers@hyperweb.io>",
5
5
  "description": "interchain-kit wallet connector react package",
6
6
  "main": "index.js",
@@ -34,7 +34,7 @@
34
34
  "keywords": [],
35
35
  "dependencies": {
36
36
  "@chain-registry/v2-types": "^0.53.40",
37
- "@interchain-kit/core": "^0.3.23",
37
+ "@interchain-kit/core": "^0.3.25",
38
38
  "@interchain-ui/react": "1.26.1",
39
39
  "@interchainjs/cosmos": "1.11.9",
40
40
  "@interchainjs/cosmos-types": "1.11.9",
@@ -64,5 +64,5 @@
64
64
  "react": "^19.0.0",
65
65
  "react-dom": "^19.0.0"
66
66
  },
67
- "gitHead": "35337e4470a0594ba644d31be8e24c8e295296f6"
67
+ "gitHead": "ab25db53b8cbf93397ee7ae7a1c5a72c08120afd"
68
68
  }
package/provider.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import React from "react";
1
+ import React, { ReactElement } from "react";
2
2
  import { BaseWallet, SignerOptions, EndpointOptions } from "@interchain-kit/core";
3
3
  import { AssetList, Chain } from "@chain-registry/v2-types";
4
+ import { WalletModalProps } from "./modal";
4
5
  import { InterchainStore } from "./store";
5
6
  import { StoreApi } from "zustand";
6
7
  type InterchainWalletContextType = StoreApi<InterchainStore>;
@@ -11,7 +12,8 @@ type InterchainWalletProviderProps = {
11
12
  signerOptions?: SignerOptions;
12
13
  endpointOptions?: EndpointOptions;
13
14
  children: React.ReactNode;
15
+ walletModal: (props: WalletModalProps) => ReactElement;
14
16
  };
15
- export declare const ChainProvider: ({ chains, assetLists, wallets, signerOptions, endpointOptions, children, }: InterchainWalletProviderProps) => import("react/jsx-runtime").JSX.Element;
17
+ export declare const ChainProvider: ({ chains, assetLists, wallets, signerOptions, endpointOptions, children, walletModal: ProviderWalletModal, }: InterchainWalletProviderProps) => import("react/jsx-runtime").JSX.Element;
16
18
  export declare const useInterchainWalletContext: () => InterchainWalletContextType;
17
19
  export {};
package/provider.js CHANGED
@@ -8,7 +8,7 @@ const core_1 = require("@interchain-kit/core");
8
8
  const modal_1 = require("./modal");
9
9
  const store_1 = require("./store");
10
10
  const InterchainWalletContext = (0, react_2.createContext)(null);
11
- const ChainProvider = ({ chains, assetLists, wallets, signerOptions, endpointOptions, children, }) => {
11
+ const ChainProvider = ({ chains, assetLists, wallets, signerOptions, endpointOptions, children, walletModal: ProviderWalletModal, }) => {
12
12
  // const [_, forceRender] = useState({});
13
13
  const walletManager = new core_1.WalletManager(chains, assetLists, wallets, signerOptions, endpointOptions);
14
14
  const store = (0, react_1.useRef)((0, store_1.createInterchainStore)(walletManager));
@@ -16,7 +16,7 @@ const ChainProvider = ({ chains, assetLists, wallets, signerOptions, endpointOpt
16
16
  // walletManager.init();
17
17
  store.current.getState().init();
18
18
  }, []);
19
- return ((0, jsx_runtime_1.jsx)(InterchainWalletContext.Provider, { value: store.current, children: (0, jsx_runtime_1.jsx)(modal_1.WalletModalProvider, { children: children }) }));
19
+ return ((0, jsx_runtime_1.jsxs)(InterchainWalletContext.Provider, { value: store.current, children: [children, (0, jsx_runtime_1.jsx)(modal_1.ModalRenderer, { walletModal: ProviderWalletModal })] }));
20
20
  };
21
21
  exports.ChainProvider = ChainProvider;
22
22
  const useInterchainWalletContext = () => {
@@ -0,0 +1,21 @@
1
+ import { BaseWallet, WalletAccount } from "@interchain-kit/core";
2
+ import { InterchainStore } from "./store";
3
+ import { Chain } from "@chain-registry/v2-types";
4
+ export declare class StatefulWallet extends BaseWallet {
5
+ originalWallet: BaseWallet;
6
+ walletName: string;
7
+ walletSet: (arg: (draft: StatefulWallet) => void) => void;
8
+ walletGet: () => StatefulWallet;
9
+ set: (arg: (draft: InterchainStore) => void) => void;
10
+ get: () => InterchainStore;
11
+ constructor(wallet: BaseWallet, walletSet: (arg: (draft: StatefulWallet) => void) => void, walletGet: () => StatefulWallet, set: (arg: (draft: InterchainStore) => void) => void, get: () => InterchainStore);
12
+ getChainToConnect(chainId?: Chain["chainId"]): Chain;
13
+ init(): Promise<void>;
14
+ connect(chainId: Chain["chainId"]): Promise<void>;
15
+ disconnect(chainId: Chain["chainId"]): Promise<void>;
16
+ getAccount(chainId: Chain["chainId"]): Promise<WalletAccount>;
17
+ getOfflineSigner(chainId: Chain['chainId']): Promise<import("@interchainjs/types").IGenericOfflineSigner<unknown, unknown, unknown, import("@interchainjs/types").IGenericOfflineSignArgs<unknown, unknown>, import("@interchainjs/types").AccountData>>;
18
+ addSuggestChain(chainId: Chain["chainId"]): Promise<void>;
19
+ getProvider(chainId?: Chain["chainId"]): Promise<any>;
20
+ getChainById(chainId: Chain["chainId"]): Chain;
21
+ }
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StatefulWallet = void 0;
4
+ const core_1 = require("@interchain-kit/core");
5
+ class StatefulWallet extends core_1.BaseWallet {
6
+ originalWallet;
7
+ walletName;
8
+ walletSet;
9
+ walletGet;
10
+ set;
11
+ get;
12
+ constructor(wallet, walletSet, walletGet, set, get) {
13
+ super(wallet.info);
14
+ this.originalWallet = wallet;
15
+ this.walletName = wallet.info.name;
16
+ this.walletState = core_1.WalletState.Disconnected;
17
+ this.errorMessage = "";
18
+ this.walletSet = walletSet;
19
+ this.walletGet = walletGet;
20
+ this.set = set;
21
+ this.get = get;
22
+ }
23
+ getChainToConnect(chainId) {
24
+ const { currentChainName, chains } = this.get();
25
+ const lastChainName = currentChainName;
26
+ const lastChain = chains.find((chain) => chain.chainName === lastChainName);
27
+ return chainId ? this.originalWallet.getChainById(chainId) : lastChain;
28
+ }
29
+ async init() {
30
+ return this.originalWallet.init();
31
+ }
32
+ async connect(chainId) {
33
+ const { get, set, walletName, walletGet, walletSet, originalWallet } = this;
34
+ const chainToConnect = this.getChainToConnect(chainId);
35
+ const state = get().getChainWalletState(walletName, chainToConnect.chainName)?.walletState;
36
+ if (state === core_1.WalletState.NotExist) {
37
+ return;
38
+ }
39
+ if (walletName === 'WalletConnect' && state === core_1.WalletState.Connected) {
40
+ return;
41
+ }
42
+ set(draft => {
43
+ draft.currentChainName = chainToConnect.chainName;
44
+ draft.currentWalletName = walletName;
45
+ draft.walletConnectQRCodeUri = '';
46
+ });
47
+ walletSet(draft => {
48
+ draft.walletState = core_1.WalletState.Connecting;
49
+ });
50
+ get().updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Connecting, errorMessage: '' });
51
+ try {
52
+ if (originalWallet instanceof core_1.WCWallet) {
53
+ originalWallet.setOnPairingUriCreatedCallback((uri) => {
54
+ set(draft => {
55
+ draft.walletConnectQRCodeUri = uri;
56
+ });
57
+ });
58
+ }
59
+ await originalWallet.connect(chainToConnect.chainId);
60
+ get().updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Connected });
61
+ walletSet(draft => {
62
+ draft.walletState = core_1.WalletState.Connected;
63
+ });
64
+ await walletGet().getAccount(chainToConnect.chainId);
65
+ }
66
+ catch (error) {
67
+ if (error.message === 'Request rejected') {
68
+ get().updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Rejected, errorMessage: error.message });
69
+ walletSet(draft => {
70
+ draft.walletState = core_1.WalletState.Rejected;
71
+ draft.errorMessage = error.message;
72
+ });
73
+ return;
74
+ }
75
+ get().updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Disconnected, errorMessage: error.message });
76
+ walletSet(draft => {
77
+ draft.walletState = core_1.WalletState.Disconnected;
78
+ draft.errorMessage = error.message;
79
+ });
80
+ }
81
+ }
82
+ async disconnect(chainId) {
83
+ const { get, walletName, walletSet, originalWallet } = this;
84
+ const chainToConnect = this.getChainToConnect(chainId);
85
+ try {
86
+ await originalWallet.disconnect(chainToConnect.chainId);
87
+ get().updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Disconnected, account: null });
88
+ walletSet(draft => {
89
+ draft.walletState = core_1.WalletState.Disconnected;
90
+ draft.errorMessage = "";
91
+ });
92
+ }
93
+ catch (error) {
94
+ }
95
+ }
96
+ async getAccount(chainId) {
97
+ const chainToConnect = this.getChainToConnect(chainId);
98
+ const { get, walletName, originalWallet } = this;
99
+ try {
100
+ const account = await originalWallet.getAccount(chainToConnect.chainId);
101
+ get().updateChainWalletState(walletName, chainToConnect.chainName, { account });
102
+ return account;
103
+ }
104
+ catch (error) {
105
+ console.log(error);
106
+ }
107
+ }
108
+ getOfflineSigner(chainId) {
109
+ return this.originalWallet.getOfflineSigner(chainId);
110
+ }
111
+ addSuggestChain(chainId) {
112
+ return this.originalWallet.addSuggestChain(chainId);
113
+ }
114
+ getProvider(chainId) {
115
+ return this.originalWallet.getProvider(chainId);
116
+ }
117
+ getChainById(chainId) {
118
+ return this.originalWallet.getChainById(chainId);
119
+ }
120
+ }
121
+ exports.StatefulWallet = StatefulWallet;
package/store/store.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { AssetList, Chain } from "@chain-registry/v2-types";
2
- import { BaseWallet, EndpointOptions, SignerOptions, WalletAccount, WalletManager, WalletState } from "@interchain-kit/core";
2
+ import { EndpointOptions, SignerOptions, WalletAccount, WalletManager, WalletState } from "@interchain-kit/core";
3
3
  import { HttpEndpoint } from '@interchainjs/types';
4
+ import { StatefulWallet } from './stateful-wallet';
4
5
  export type ChainWalletState = {
5
6
  chainName: string;
6
7
  walletName: string;
@@ -14,19 +15,23 @@ export interface InterchainStore extends WalletManager {
14
15
  currentWalletName: string;
15
16
  currentChainName: string;
16
17
  walletConnectQRCodeUri: string;
18
+ isReady: boolean;
19
+ wallets: StatefulWallet[];
20
+ modalIsOpen: boolean;
21
+ openModal: () => void;
22
+ closeModal: () => void;
17
23
  setCurrentChainName: (chainName: string) => void;
18
24
  setCurrentWalletName: (walletName: string) => void;
19
25
  getDraftChainWalletState: (state: InterchainStore, walletName: string, chainName: string) => ChainWalletState;
20
26
  getChainWalletState: (walletName: string, chainName: string) => ChainWalletState | undefined;
21
27
  updateChainWalletState: (walletName: string, chainName: string, data: Partial<ChainWalletState>) => void;
22
- updateWalletState: (walletName: string, data: Partial<ChainWalletState>) => void;
23
- isReady: boolean;
24
28
  createStatefulWallet: () => void;
29
+ getStatefulWalletByName: (walletName: string) => StatefulWallet | undefined;
25
30
  }
26
31
  export type InterchainStoreData = {
27
32
  chains: Chain[];
28
33
  assetLists: AssetList[];
29
- wallets: BaseWallet[];
34
+ wallets: StatefulWallet[];
30
35
  signerOptions: SignerOptions;
31
36
  endpointOptions: EndpointOptions;
32
37
  };