@interchain-kit/react 0.2.206 → 0.2.208
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/esm/hooks/useAccount.js +39 -0
- package/esm/modal/modal.js +4 -10
- package/esm/modal/views/WalletList.js +16 -16
- package/esm/store/index.js +10 -3
- package/esm/store/store.js +191 -0
- package/hooks/useAccount.d.ts +2 -0
- package/hooks/useAccount.js +43 -0
- package/modal/modal.js +3 -9
- package/modal/views/WalletList.js +16 -16
- package/package.json +9 -3
- package/store/index.d.ts +3 -1
- package/store/index.js +24 -3
- package/store/store.d.ts +45 -0
- package/store/store.js +195 -0
- package/utils/wallet.d.ts +2 -2
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { WalletManagerState, WalletState } from "@interchain-kit/core";
|
|
3
|
+
import { useWalletManager } from './useWalletManager';
|
|
4
|
+
export const useAccount = (chainName, walletName) => {
|
|
5
|
+
const walletManager = useWalletManager();
|
|
6
|
+
const [isFetching, setIsFetching] = useState(false);
|
|
7
|
+
const wallet = useMemo(() => {
|
|
8
|
+
return walletManager.wallets.find(w => w.info.name === walletName);
|
|
9
|
+
}, [walletManager, walletName]);
|
|
10
|
+
const [account, setAccount] = useState(null);
|
|
11
|
+
const chain = useMemo(() => {
|
|
12
|
+
return walletManager.chains.find(c => c.chainName === chainName);
|
|
13
|
+
}, [walletManager, chainName]);
|
|
14
|
+
const getAccount = async () => {
|
|
15
|
+
if (wallet && chain) {
|
|
16
|
+
if (wallet.walletState === WalletState.Connected && account === null && !isFetching) {
|
|
17
|
+
setIsFetching(true);
|
|
18
|
+
const account = await walletManager.getAccount(walletName, chainName);
|
|
19
|
+
setAccount(account);
|
|
20
|
+
setIsFetching(false);
|
|
21
|
+
}
|
|
22
|
+
if (wallet.walletState === WalletState.Disconnected) {
|
|
23
|
+
setAccount(null);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!wallet) {
|
|
27
|
+
setAccount(null);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (wallet && walletManager.state === WalletManagerState.Initialized) {
|
|
32
|
+
wallet.events.on('accountChanged', getAccount);
|
|
33
|
+
}
|
|
34
|
+
}, [wallet, walletManager.state]);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
getAccount();
|
|
37
|
+
}, [wallet, chainName, wallet?.walletState]);
|
|
38
|
+
return account;
|
|
39
|
+
};
|
package/esm/modal/modal.js
CHANGED
|
@@ -3,22 +3,16 @@ import { ConnectedContent, ConnectedHeader, ConnectingContent, ConnectingHeader,
|
|
|
3
3
|
import { useWalletModal } from "./provider";
|
|
4
4
|
import { useChainWallet, useWalletManager } from "../hooks";
|
|
5
5
|
import { useEffect, useMemo, useState } from "react";
|
|
6
|
-
import { WalletState
|
|
6
|
+
import { WalletState } from "@interchain-kit/core";
|
|
7
7
|
import { ConnectModal } from "@interchain-ui/react";
|
|
8
8
|
export const WalletModal = () => {
|
|
9
|
-
const
|
|
10
|
-
const { currentWalletName, currentChainName, wallets, connect, getAccount, setCurrentWalletName, } = useWalletManager();
|
|
9
|
+
const { currentWalletName, currentChainName, wallets, connect, getAccount, setCurrentWalletName, walletConnectQRCodeUri, } = useWalletManager();
|
|
11
10
|
const { modalIsOpen, open, close } = useWalletModal();
|
|
12
11
|
const [modalType, setModalType] = useState("wallet-list");
|
|
13
12
|
const [selectedWallet, setSelectedWallet] = useState(null);
|
|
14
13
|
const { chain, status, wallet } = useChainWallet(currentChainName, currentWalletName);
|
|
15
14
|
const handleConnect = async () => {
|
|
16
15
|
try {
|
|
17
|
-
if (wallet.originalWallet instanceof WCWallet) {
|
|
18
|
-
wallet.originalWallet.setOnPairingUriCreatedCallback((uri) => {
|
|
19
|
-
setQRCode(uri);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
16
|
await connect(selectedWallet?.info?.name, chain.chainName);
|
|
23
17
|
await getAccount(selectedWallet?.info?.name, chain.chainName);
|
|
24
18
|
setSelectedWallet(null);
|
|
@@ -53,7 +47,7 @@ export const WalletModal = () => {
|
|
|
53
47
|
if (status === WalletState.Disconnected) {
|
|
54
48
|
setModalType("wallet-list");
|
|
55
49
|
}
|
|
56
|
-
if (
|
|
50
|
+
if (walletConnectQRCodeUri) {
|
|
57
51
|
setModalType("qr-code");
|
|
58
52
|
}
|
|
59
53
|
}
|
|
@@ -63,7 +57,7 @@ export const WalletModal = () => {
|
|
|
63
57
|
status,
|
|
64
58
|
modalIsOpen,
|
|
65
59
|
selectedWallet,
|
|
66
|
-
|
|
60
|
+
walletConnectQRCodeUri,
|
|
67
61
|
]);
|
|
68
62
|
const goBackList = () => setModalType("wallet-list");
|
|
69
63
|
const { header, content } = useMemo(() => {
|
|
@@ -2,6 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { ConnectModalHead, ConnectModalWalletList } from "@interchain-ui/react";
|
|
3
3
|
import { useWalletModal } from "../provider";
|
|
4
4
|
import { useWalletManager } from "../../hooks";
|
|
5
|
+
import { isInstanceOf, WCWallet } from "@interchain-kit/core";
|
|
5
6
|
export const WalletListHeader = () => {
|
|
6
7
|
const { close } = useWalletModal();
|
|
7
8
|
return (_jsx(ConnectModalHead, { title: "Select your wallet", hasBackButton: false, onClose: close, closeButtonProps: { onClick: close } }));
|
|
@@ -9,6 +10,17 @@ export const WalletListHeader = () => {
|
|
|
9
10
|
export const WalletListContent = ({ onSelectWallet, }) => {
|
|
10
11
|
const walletManager = useWalletManager();
|
|
11
12
|
const wallets = walletManager.wallets.map((w) => {
|
|
13
|
+
if (isInstanceOf(w, WCWallet) && w.session) {
|
|
14
|
+
return {
|
|
15
|
+
name: w.session.peer.metadata?.name,
|
|
16
|
+
prettyName: `${w.session.peer.metadata?.name} - Mobile`,
|
|
17
|
+
logo: w.session.peer.metadata?.icons?.[0],
|
|
18
|
+
mobileDisabled: true,
|
|
19
|
+
shape: "list",
|
|
20
|
+
originalWallet: { ...w, session: w.session },
|
|
21
|
+
subLogo: w.info.logo,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
12
24
|
return {
|
|
13
25
|
name: w.info.name,
|
|
14
26
|
prettyName: w.info.prettyName,
|
|
@@ -18,23 +30,11 @@ export const WalletListContent = ({ onSelectWallet, }) => {
|
|
|
18
30
|
originalWallet: {
|
|
19
31
|
...w,
|
|
20
32
|
pairing: null,
|
|
33
|
+
session: null,
|
|
21
34
|
},
|
|
22
35
|
};
|
|
23
36
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
activePairings.forEach((pairing) => {
|
|
28
|
-
wallets.push({
|
|
29
|
-
name: pairing?.peerMetadata?.name,
|
|
30
|
-
prettyName: pairing?.peerMetadata?.name,
|
|
31
|
-
logo: pairing?.peerMetadata?.icons?.[0],
|
|
32
|
-
mobileDisabled: true,
|
|
33
|
-
shape: "list",
|
|
34
|
-
originalWallet: { ...wcWallet, pairing },
|
|
35
|
-
subLogo: wcWallet.info.logo,
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
return (_jsx(ConnectModalWalletList, { wallets: wallets, onWalletItemClick: onSelectWallet }));
|
|
37
|
+
return (_jsx(ConnectModalWalletList, { wallets: wallets, onWalletItemClick: (w) => {
|
|
38
|
+
onSelectWallet(w);
|
|
39
|
+
} }));
|
|
40
40
|
};
|
package/esm/store/index.js
CHANGED
|
@@ -29,6 +29,7 @@ export const createInterchainStore = (walletManager) => {
|
|
|
29
29
|
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
30
30
|
signerOptionMap: { ...walletManager.signerOptionMap },
|
|
31
31
|
endpointOptionsMap: { ...walletManager.endpointOptionsMap },
|
|
32
|
+
walletConnectQRCodeUri: '',
|
|
32
33
|
updateChainWalletState: (walletName, chainName, data) => {
|
|
33
34
|
set(draft => {
|
|
34
35
|
let targetIndex = draft.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
@@ -120,7 +121,11 @@ export const createInterchainStore = (walletManager) => {
|
|
|
120
121
|
connect: async (walletName, chainName) => {
|
|
121
122
|
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Connecting });
|
|
122
123
|
try {
|
|
123
|
-
await walletManager.connect(walletName, chainName)
|
|
124
|
+
await walletManager.connect(walletName, chainName, (uri) => {
|
|
125
|
+
set(draft => {
|
|
126
|
+
draft.walletConnectQRCodeUri = uri;
|
|
127
|
+
});
|
|
128
|
+
});
|
|
124
129
|
set(draft => {
|
|
125
130
|
draft.currentChainName = chainName;
|
|
126
131
|
draft.currentWalletName = walletName;
|
|
@@ -199,16 +204,18 @@ export const createInterchainStore = (walletManager) => {
|
|
|
199
204
|
currentChainName: state.currentChainName
|
|
200
205
|
}),
|
|
201
206
|
onRehydrateStorage: (state) => {
|
|
202
|
-
console.log('interchain-kit store hydration starts')
|
|
207
|
+
// console.log('interchain-kit store hydration starts')
|
|
203
208
|
// optional
|
|
204
209
|
return (state, error) => {
|
|
205
210
|
if (error) {
|
|
206
211
|
console.log('an error happened during hydration', error);
|
|
207
212
|
}
|
|
208
213
|
else {
|
|
209
|
-
console.log('interchain-kit store hydration finished')
|
|
214
|
+
// console.log('interchain-kit store hydration finished')
|
|
210
215
|
}
|
|
211
216
|
};
|
|
212
217
|
},
|
|
213
218
|
}));
|
|
214
219
|
};
|
|
220
|
+
export * from './chain-wallet';
|
|
221
|
+
export * from './store';
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { WalletState } from "@interchain-kit/core";
|
|
2
|
+
import { createStore } from "zustand";
|
|
3
|
+
import { immer } from "zustand/middleware/immer";
|
|
4
|
+
import { persist, createJSONStorage } from 'zustand/middleware';
|
|
5
|
+
const immerSyncUp = (newWalletManager) => {
|
|
6
|
+
return (draft) => {
|
|
7
|
+
draft.chains = newWalletManager.chains;
|
|
8
|
+
draft.assetLists = newWalletManager.assetLists;
|
|
9
|
+
draft.wallets = newWalletManager.wallets;
|
|
10
|
+
draft.signerOptions = newWalletManager.signerOptions;
|
|
11
|
+
draft.endpointOptions = newWalletManager.endpointOptions;
|
|
12
|
+
draft.signerOptionMap = newWalletManager.signerOptionMap;
|
|
13
|
+
draft.endpointOptionsMap = newWalletManager.endpointOptionsMap;
|
|
14
|
+
draft.preferredSignTypeMap = newWalletManager.preferredSignTypeMap;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export const createInterchainStore = (walletManager) => {
|
|
18
|
+
const { chains, assetLists, wallets, signerOptions, endpointOptions } = walletManager;
|
|
19
|
+
// const walletManager = new WalletManager(chains, assetLists, wallets, signerOptions, endpointOptions)
|
|
20
|
+
const chainWalletState = [];
|
|
21
|
+
wallets.forEach(wallet => {
|
|
22
|
+
chains.forEach(chain => {
|
|
23
|
+
chainWalletState.push({
|
|
24
|
+
chainName: chain.chainName,
|
|
25
|
+
walletName: wallet.info.name,
|
|
26
|
+
walletState: WalletState.Disconnected,
|
|
27
|
+
rpcEndpoint: "",
|
|
28
|
+
errorMessage: "",
|
|
29
|
+
signerOption: undefined,
|
|
30
|
+
account: undefined
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
return createStore(persist(immer((set, get) => ({
|
|
35
|
+
chainWalletState,
|
|
36
|
+
currentWalletName: '',
|
|
37
|
+
currentChainName: '',
|
|
38
|
+
chains: [...walletManager.chains],
|
|
39
|
+
assetLists: [...walletManager.assetLists],
|
|
40
|
+
wallets: walletManager.wallets,
|
|
41
|
+
signerOptions: walletManager.signerOptions,
|
|
42
|
+
endpointOptions: walletManager.endpointOptions,
|
|
43
|
+
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
44
|
+
signerOptionMap: { ...walletManager.signerOptionMap },
|
|
45
|
+
endpointOptionsMap: { ...walletManager.endpointOptionsMap },
|
|
46
|
+
updateChainWalletState: (walletName, chainName, data) => {
|
|
47
|
+
set(draft => {
|
|
48
|
+
let targetIndex = draft.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
49
|
+
draft.chainWalletState[targetIndex] = { ...draft.chainWalletState[targetIndex], ...data };
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
init: () => walletManager.init(),
|
|
53
|
+
setCurrentChainName: (chainName) => {
|
|
54
|
+
set(draft => { draft.currentChainName = chainName; });
|
|
55
|
+
},
|
|
56
|
+
setCurrentWalletName: (walletName) => {
|
|
57
|
+
set(draft => { draft.currentWalletName = walletName; });
|
|
58
|
+
},
|
|
59
|
+
getDraftChainWalletState: (state, walletName, chainName) => {
|
|
60
|
+
const targetIndex = state.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
61
|
+
return state.chainWalletState[targetIndex];
|
|
62
|
+
},
|
|
63
|
+
getChainWalletState: (walletName, chainName) => {
|
|
64
|
+
return get().chainWalletState.find(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
65
|
+
},
|
|
66
|
+
addChains: (chains, assetLists, signerOptions, endpointOptions) => {
|
|
67
|
+
walletManager.addChains(chains, assetLists, signerOptions, endpointOptions);
|
|
68
|
+
// console.log(walletManager.chains, walletManager.assetLists)
|
|
69
|
+
// set(immerSyncUp(walletManager))
|
|
70
|
+
// set(draft => {
|
|
71
|
+
// draft.chains = walletManager.chains
|
|
72
|
+
// })
|
|
73
|
+
set(draft => {
|
|
74
|
+
chains.forEach(newChain => {
|
|
75
|
+
const existChain = draft.chains.find(c => c.chainId === newChain.chainId);
|
|
76
|
+
if (!existChain) {
|
|
77
|
+
draft.chains.push(newChain);
|
|
78
|
+
const assetList = assetLists.find(a => a.chainName === newChain.chainName);
|
|
79
|
+
draft.assetLists.push(assetList);
|
|
80
|
+
draft.wallets.forEach(w => {
|
|
81
|
+
draft.chainWalletState.push({
|
|
82
|
+
chainName: newChain.chainName,
|
|
83
|
+
walletName: w.info.name,
|
|
84
|
+
walletState: WalletState.Disconnected,
|
|
85
|
+
rpcEndpoint: endpointOptions?.endpoints?.[newChain.chainName]?.rpc?.[0],
|
|
86
|
+
errorMessage: "",
|
|
87
|
+
signerOption: signerOptions?.signing?.(newChain.chainName),
|
|
88
|
+
account: undefined
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
draft.signerOptionMap[newChain.chainName] = signerOptions?.signing?.(newChain.chainName);
|
|
93
|
+
draft.endpointOptionsMap[newChain.chainName] = endpointOptions?.endpoints?.[newChain.chainName];
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
connect: async (walletName, chainName) => {
|
|
98
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Connecting });
|
|
99
|
+
try {
|
|
100
|
+
await walletManager.connect(walletName, chainName);
|
|
101
|
+
set(draft => {
|
|
102
|
+
draft.currentChainName = chainName;
|
|
103
|
+
draft.currentWalletName = walletName;
|
|
104
|
+
});
|
|
105
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Connected });
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (error.message === 'Request rejected') {
|
|
109
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Rejected, errorMessage: error.message });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Disconnected, errorMessage: error.message });
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
disconnect: async (walletName, chainName) => {
|
|
116
|
+
try {
|
|
117
|
+
await walletManager.disconnect(walletName, chainName);
|
|
118
|
+
get().updateChainWalletState(walletName, chainName, { walletState: WalletState.Disconnected, account: null });
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
getAccount: async (walletName, chainName) => {
|
|
124
|
+
const account = await walletManager.getAccount(walletName, chainName);
|
|
125
|
+
get().updateChainWalletState(walletName, chainName, { account });
|
|
126
|
+
return account;
|
|
127
|
+
},
|
|
128
|
+
getRpcEndpoint: async (walletName, chainName) => {
|
|
129
|
+
const exist = get().getChainWalletState(walletName, chainName).rpcEndpoint;
|
|
130
|
+
if (exist)
|
|
131
|
+
return exist;
|
|
132
|
+
const rpcEndpoint = await walletManager.getRpcEndpoint(walletName, chainName);
|
|
133
|
+
get().updateChainWalletState(walletName, chainName, { rpcEndpoint });
|
|
134
|
+
return rpcEndpoint;
|
|
135
|
+
},
|
|
136
|
+
getChainLogoUrl(chainName) {
|
|
137
|
+
return walletManager.getChainLogoUrl(chainName);
|
|
138
|
+
},
|
|
139
|
+
getChainByName(chainName) {
|
|
140
|
+
return walletManager.getChainByName(chainName);
|
|
141
|
+
},
|
|
142
|
+
getAssetListByName(chainName) {
|
|
143
|
+
return walletManager.getAssetListByName(chainName);
|
|
144
|
+
},
|
|
145
|
+
getDownloadLink(walletName) {
|
|
146
|
+
return walletManager.getDownloadLink(walletName);
|
|
147
|
+
},
|
|
148
|
+
getOfflineSigner(walletName, chainName) {
|
|
149
|
+
return walletManager.getOfflineSigner(walletName, chainName);
|
|
150
|
+
},
|
|
151
|
+
getPreferSignType(chainName) {
|
|
152
|
+
const result = walletManager.getPreferSignType(chainName);
|
|
153
|
+
set(immerSyncUp(walletManager));
|
|
154
|
+
return result;
|
|
155
|
+
},
|
|
156
|
+
getSignerOptions(chainName) {
|
|
157
|
+
const result = walletManager.getSignerOptions(chainName);
|
|
158
|
+
set(immerSyncUp(walletManager));
|
|
159
|
+
return result;
|
|
160
|
+
},
|
|
161
|
+
getWalletByName(walletName) {
|
|
162
|
+
return walletManager.getWalletByName(walletName);
|
|
163
|
+
},
|
|
164
|
+
getSigningClient(walletName, chainName) {
|
|
165
|
+
return walletManager.getSigningClient(walletName, chainName);
|
|
166
|
+
},
|
|
167
|
+
getEnv() {
|
|
168
|
+
return walletManager.getEnv();
|
|
169
|
+
},
|
|
170
|
+
})), {
|
|
171
|
+
name: 'interchain-kit-store',
|
|
172
|
+
storage: createJSONStorage(() => localStorage),
|
|
173
|
+
partialize: state => ({
|
|
174
|
+
chainWalletState: state.chainWalletState,
|
|
175
|
+
currentWalletName: state.currentWalletName,
|
|
176
|
+
currentChainName: state.currentChainName
|
|
177
|
+
}),
|
|
178
|
+
onRehydrateStorage: (state) => {
|
|
179
|
+
// console.log('interchain-kit store hydration starts')
|
|
180
|
+
// optional
|
|
181
|
+
return (state, error) => {
|
|
182
|
+
if (error) {
|
|
183
|
+
console.log('an error happened during hydration', error);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
console.log('interchain-kit store hydration finished');
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
}));
|
|
191
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAccount = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const core_1 = require("@interchain-kit/core");
|
|
6
|
+
const useWalletManager_1 = require("./useWalletManager");
|
|
7
|
+
const useAccount = (chainName, walletName) => {
|
|
8
|
+
const walletManager = (0, useWalletManager_1.useWalletManager)();
|
|
9
|
+
const [isFetching, setIsFetching] = (0, react_1.useState)(false);
|
|
10
|
+
const wallet = (0, react_1.useMemo)(() => {
|
|
11
|
+
return walletManager.wallets.find(w => w.info.name === walletName);
|
|
12
|
+
}, [walletManager, walletName]);
|
|
13
|
+
const [account, setAccount] = (0, react_1.useState)(null);
|
|
14
|
+
const chain = (0, react_1.useMemo)(() => {
|
|
15
|
+
return walletManager.chains.find(c => c.chainName === chainName);
|
|
16
|
+
}, [walletManager, chainName]);
|
|
17
|
+
const getAccount = async () => {
|
|
18
|
+
if (wallet && chain) {
|
|
19
|
+
if (wallet.walletState === core_1.WalletState.Connected && account === null && !isFetching) {
|
|
20
|
+
setIsFetching(true);
|
|
21
|
+
const account = await walletManager.getAccount(walletName, chainName);
|
|
22
|
+
setAccount(account);
|
|
23
|
+
setIsFetching(false);
|
|
24
|
+
}
|
|
25
|
+
if (wallet.walletState === core_1.WalletState.Disconnected) {
|
|
26
|
+
setAccount(null);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (!wallet) {
|
|
30
|
+
setAccount(null);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
(0, react_1.useEffect)(() => {
|
|
34
|
+
if (wallet && walletManager.state === core_1.WalletManagerState.Initialized) {
|
|
35
|
+
wallet.events.on('accountChanged', getAccount);
|
|
36
|
+
}
|
|
37
|
+
}, [wallet, walletManager.state]);
|
|
38
|
+
(0, react_1.useEffect)(() => {
|
|
39
|
+
getAccount();
|
|
40
|
+
}, [wallet, chainName, wallet?.walletState]);
|
|
41
|
+
return account;
|
|
42
|
+
};
|
|
43
|
+
exports.useAccount = useAccount;
|
package/modal/modal.js
CHANGED
|
@@ -9,19 +9,13 @@ const react_1 = require("react");
|
|
|
9
9
|
const core_1 = require("@interchain-kit/core");
|
|
10
10
|
const react_2 = require("@interchain-ui/react");
|
|
11
11
|
const WalletModal = () => {
|
|
12
|
-
const
|
|
13
|
-
const { currentWalletName, currentChainName, wallets, connect, getAccount, setCurrentWalletName, } = (0, hooks_1.useWalletManager)();
|
|
12
|
+
const { currentWalletName, currentChainName, wallets, connect, getAccount, setCurrentWalletName, walletConnectQRCodeUri, } = (0, hooks_1.useWalletManager)();
|
|
14
13
|
const { modalIsOpen, open, close } = (0, provider_1.useWalletModal)();
|
|
15
14
|
const [modalType, setModalType] = (0, react_1.useState)("wallet-list");
|
|
16
15
|
const [selectedWallet, setSelectedWallet] = (0, react_1.useState)(null);
|
|
17
16
|
const { chain, status, wallet } = (0, hooks_1.useChainWallet)(currentChainName, currentWalletName);
|
|
18
17
|
const handleConnect = async () => {
|
|
19
18
|
try {
|
|
20
|
-
if (wallet.originalWallet instanceof core_1.WCWallet) {
|
|
21
|
-
wallet.originalWallet.setOnPairingUriCreatedCallback((uri) => {
|
|
22
|
-
setQRCode(uri);
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
19
|
await connect(selectedWallet?.info?.name, chain.chainName);
|
|
26
20
|
await getAccount(selectedWallet?.info?.name, chain.chainName);
|
|
27
21
|
setSelectedWallet(null);
|
|
@@ -56,7 +50,7 @@ const WalletModal = () => {
|
|
|
56
50
|
if (status === core_1.WalletState.Disconnected) {
|
|
57
51
|
setModalType("wallet-list");
|
|
58
52
|
}
|
|
59
|
-
if (
|
|
53
|
+
if (walletConnectQRCodeUri) {
|
|
60
54
|
setModalType("qr-code");
|
|
61
55
|
}
|
|
62
56
|
}
|
|
@@ -66,7 +60,7 @@ const WalletModal = () => {
|
|
|
66
60
|
status,
|
|
67
61
|
modalIsOpen,
|
|
68
62
|
selectedWallet,
|
|
69
|
-
|
|
63
|
+
walletConnectQRCodeUri,
|
|
70
64
|
]);
|
|
71
65
|
const goBackList = () => setModalType("wallet-list");
|
|
72
66
|
const { header, content } = (0, react_1.useMemo)(() => {
|
|
@@ -5,6 +5,7 @@ const jsx_runtime_1 = require("react/jsx-runtime");
|
|
|
5
5
|
const react_1 = require("@interchain-ui/react");
|
|
6
6
|
const provider_1 = require("../provider");
|
|
7
7
|
const hooks_1 = require("../../hooks");
|
|
8
|
+
const core_1 = require("@interchain-kit/core");
|
|
8
9
|
const WalletListHeader = () => {
|
|
9
10
|
const { close } = (0, provider_1.useWalletModal)();
|
|
10
11
|
return ((0, jsx_runtime_1.jsx)(react_1.ConnectModalHead, { title: "Select your wallet", hasBackButton: false, onClose: close, closeButtonProps: { onClick: close } }));
|
|
@@ -13,6 +14,17 @@ exports.WalletListHeader = WalletListHeader;
|
|
|
13
14
|
const WalletListContent = ({ onSelectWallet, }) => {
|
|
14
15
|
const walletManager = (0, hooks_1.useWalletManager)();
|
|
15
16
|
const wallets = walletManager.wallets.map((w) => {
|
|
17
|
+
if ((0, core_1.isInstanceOf)(w, core_1.WCWallet) && w.session) {
|
|
18
|
+
return {
|
|
19
|
+
name: w.session.peer.metadata?.name,
|
|
20
|
+
prettyName: `${w.session.peer.metadata?.name} - Mobile`,
|
|
21
|
+
logo: w.session.peer.metadata?.icons?.[0],
|
|
22
|
+
mobileDisabled: true,
|
|
23
|
+
shape: "list",
|
|
24
|
+
originalWallet: { ...w, session: w.session },
|
|
25
|
+
subLogo: w.info.logo,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
16
28
|
return {
|
|
17
29
|
name: w.info.name,
|
|
18
30
|
prettyName: w.info.prettyName,
|
|
@@ -22,24 +34,12 @@ const WalletListContent = ({ onSelectWallet, }) => {
|
|
|
22
34
|
originalWallet: {
|
|
23
35
|
...w,
|
|
24
36
|
pairing: null,
|
|
37
|
+
session: null,
|
|
25
38
|
},
|
|
26
39
|
};
|
|
27
40
|
});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
activePairings.forEach((pairing) => {
|
|
32
|
-
wallets.push({
|
|
33
|
-
name: pairing?.peerMetadata?.name,
|
|
34
|
-
prettyName: pairing?.peerMetadata?.name,
|
|
35
|
-
logo: pairing?.peerMetadata?.icons?.[0],
|
|
36
|
-
mobileDisabled: true,
|
|
37
|
-
shape: "list",
|
|
38
|
-
originalWallet: { ...wcWallet, pairing },
|
|
39
|
-
subLogo: wcWallet.info.logo,
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
return ((0, jsx_runtime_1.jsx)(react_1.ConnectModalWalletList, { wallets: wallets, onWalletItemClick: onSelectWallet }));
|
|
41
|
+
return ((0, jsx_runtime_1.jsx)(react_1.ConnectModalWalletList, { wallets: wallets, onWalletItemClick: (w) => {
|
|
42
|
+
onSelectWallet(w);
|
|
43
|
+
} }));
|
|
44
44
|
};
|
|
45
45
|
exports.WalletListContent = WalletListContent;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interchain-kit/react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.208",
|
|
4
4
|
"author": "Hyperweb <developers@hyperweb.io>",
|
|
5
5
|
"description": "interchain-kit wallet connector react package",
|
|
6
6
|
"main": "index.js",
|
|
@@ -33,18 +33,24 @@
|
|
|
33
33
|
"keywords": [],
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@chain-registry/v2-types": "^0.53.40",
|
|
36
|
-
"@interchain-kit/core": "0.2.
|
|
36
|
+
"@interchain-kit/core": "0.2.208",
|
|
37
37
|
"@interchain-ui/react": "1.26.1",
|
|
38
38
|
"@interchainjs/cosmos": "1.9.16",
|
|
39
39
|
"@interchainjs/cosmos-types": "1.9.16",
|
|
40
40
|
"@react-icons/all-files": "^4.1.0",
|
|
41
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
41
42
|
"@types/react": "^18.3.3",
|
|
42
43
|
"@types/react-dom": "^18.3.0",
|
|
43
44
|
"@walletconnect/types": "^2.17.3",
|
|
44
45
|
"interchainjs": "1.9.16",
|
|
46
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
45
47
|
"react": "^18.3.1",
|
|
46
48
|
"react-dom": "^18.3.1",
|
|
47
49
|
"zustand": "^5.0.3"
|
|
48
50
|
},
|
|
49
|
-
"
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@testing-library/dom": "^10.4.0",
|
|
53
|
+
"@testing-library/react-hooks": "^8.0.1"
|
|
54
|
+
},
|
|
55
|
+
"gitHead": "61b9e94520bff49454d3a509abff6c041fd6bd5e"
|
|
50
56
|
}
|
package/store/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export interface InterchainStore extends WalletManager {
|
|
|
13
13
|
chainWalletState: ChainWalletState[];
|
|
14
14
|
currentWalletName: string;
|
|
15
15
|
currentChainName: string;
|
|
16
|
+
walletConnectQRCodeUri: string;
|
|
16
17
|
setCurrentChainName: (chainName: string) => void;
|
|
17
18
|
setCurrentWalletName: (walletName: string) => void;
|
|
18
19
|
getDraftChainWalletState: (state: InterchainStore, walletName: string, chainName: string) => ChainWalletState;
|
|
@@ -40,4 +41,5 @@ export declare const createInterchainStore: (walletManager: WalletManager) => Om
|
|
|
40
41
|
setState(nextStateOrUpdater: InterchainStore | Partial<InterchainStore> | ((state: import("immer").WritableDraft<InterchainStore>) => void), shouldReplace?: false): void;
|
|
41
42
|
setState(nextStateOrUpdater: InterchainStore | ((state: import("immer").WritableDraft<InterchainStore>) => void), shouldReplace: true): void;
|
|
42
43
|
};
|
|
43
|
-
export
|
|
44
|
+
export * from './chain-wallet';
|
|
45
|
+
export * from './store';
|
package/store/index.js
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
17
|
exports.createInterchainStore = void 0;
|
|
4
18
|
const core_1 = require("@interchain-kit/core");
|
|
@@ -32,6 +46,7 @@ const createInterchainStore = (walletManager) => {
|
|
|
32
46
|
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
33
47
|
signerOptionMap: { ...walletManager.signerOptionMap },
|
|
34
48
|
endpointOptionsMap: { ...walletManager.endpointOptionsMap },
|
|
49
|
+
walletConnectQRCodeUri: '',
|
|
35
50
|
updateChainWalletState: (walletName, chainName, data) => {
|
|
36
51
|
set(draft => {
|
|
37
52
|
let targetIndex = draft.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
@@ -123,7 +138,11 @@ const createInterchainStore = (walletManager) => {
|
|
|
123
138
|
connect: async (walletName, chainName) => {
|
|
124
139
|
get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Connecting });
|
|
125
140
|
try {
|
|
126
|
-
await walletManager.connect(walletName, chainName)
|
|
141
|
+
await walletManager.connect(walletName, chainName, (uri) => {
|
|
142
|
+
set(draft => {
|
|
143
|
+
draft.walletConnectQRCodeUri = uri;
|
|
144
|
+
});
|
|
145
|
+
});
|
|
127
146
|
set(draft => {
|
|
128
147
|
draft.currentChainName = chainName;
|
|
129
148
|
draft.currentWalletName = walletName;
|
|
@@ -202,17 +221,19 @@ const createInterchainStore = (walletManager) => {
|
|
|
202
221
|
currentChainName: state.currentChainName
|
|
203
222
|
}),
|
|
204
223
|
onRehydrateStorage: (state) => {
|
|
205
|
-
console.log('interchain-kit store hydration starts')
|
|
224
|
+
// console.log('interchain-kit store hydration starts')
|
|
206
225
|
// optional
|
|
207
226
|
return (state, error) => {
|
|
208
227
|
if (error) {
|
|
209
228
|
console.log('an error happened during hydration', error);
|
|
210
229
|
}
|
|
211
230
|
else {
|
|
212
|
-
console.log('interchain-kit store hydration finished')
|
|
231
|
+
// console.log('interchain-kit store hydration finished')
|
|
213
232
|
}
|
|
214
233
|
};
|
|
215
234
|
},
|
|
216
235
|
}));
|
|
217
236
|
};
|
|
218
237
|
exports.createInterchainStore = createInterchainStore;
|
|
238
|
+
__exportStar(require("./chain-wallet"), exports);
|
|
239
|
+
__exportStar(require("./store"), exports);
|
package/store/store.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AssetList, Chain } from "@chain-registry/v2-types";
|
|
2
|
+
import { BaseWallet, EndpointOptions, SignerOptions, WalletAccount, WalletManager, WalletState } from "@interchain-kit/core";
|
|
3
|
+
import { SignerOptions as InterchainSignerOptions } from '@interchainjs/cosmos/types/signing-client';
|
|
4
|
+
import { HttpEndpoint } from '@interchainjs/types';
|
|
5
|
+
type ChainWalletState = {
|
|
6
|
+
chainName: string;
|
|
7
|
+
walletName: string;
|
|
8
|
+
walletState: WalletState;
|
|
9
|
+
rpcEndpoint: string | HttpEndpoint;
|
|
10
|
+
errorMessage: string;
|
|
11
|
+
signerOption: InterchainSignerOptions;
|
|
12
|
+
account: WalletAccount;
|
|
13
|
+
};
|
|
14
|
+
export interface InterchainStore extends WalletManager {
|
|
15
|
+
chainWalletState: ChainWalletState[];
|
|
16
|
+
currentWalletName: string;
|
|
17
|
+
currentChainName: string;
|
|
18
|
+
setCurrentChainName: (chainName: string) => void;
|
|
19
|
+
setCurrentWalletName: (walletName: string) => void;
|
|
20
|
+
getDraftChainWalletState: (state: InterchainStore, walletName: string, chainName: string) => ChainWalletState;
|
|
21
|
+
getChainWalletState: (walletName: string, chainName: string) => ChainWalletState | undefined;
|
|
22
|
+
updateChainWalletState: (walletName: string, chainName: string, data: Partial<ChainWalletState>) => void;
|
|
23
|
+
}
|
|
24
|
+
export type InterchainStoreData = {
|
|
25
|
+
chains: Chain[];
|
|
26
|
+
assetLists: AssetList[];
|
|
27
|
+
wallets: BaseWallet[];
|
|
28
|
+
signerOptions: SignerOptions;
|
|
29
|
+
endpointOptions: EndpointOptions;
|
|
30
|
+
};
|
|
31
|
+
export declare const createInterchainStore: (walletManager: WalletManager) => Omit<Omit<import("zustand").StoreApi<InterchainStore>, "persist"> & {
|
|
32
|
+
persist: {
|
|
33
|
+
setOptions: (options: Partial<import("zustand/middleware").PersistOptions<InterchainStore, unknown>>) => void;
|
|
34
|
+
clearStorage: () => void;
|
|
35
|
+
rehydrate: () => Promise<void> | void;
|
|
36
|
+
hasHydrated: () => boolean;
|
|
37
|
+
onHydrate: (fn: (state: InterchainStore) => void) => () => void;
|
|
38
|
+
onFinishHydration: (fn: (state: InterchainStore) => void) => () => void;
|
|
39
|
+
getOptions: () => Partial<import("zustand/middleware").PersistOptions<InterchainStore, unknown>>;
|
|
40
|
+
};
|
|
41
|
+
}, "setState"> & {
|
|
42
|
+
setState(nextStateOrUpdater: InterchainStore | Partial<InterchainStore> | ((state: import("immer").WritableDraft<InterchainStore>) => void), shouldReplace?: false): void;
|
|
43
|
+
setState(nextStateOrUpdater: InterchainStore | ((state: import("immer").WritableDraft<InterchainStore>) => void), shouldReplace: true): void;
|
|
44
|
+
};
|
|
45
|
+
export {};
|
package/store/store.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createInterchainStore = void 0;
|
|
4
|
+
const core_1 = require("@interchain-kit/core");
|
|
5
|
+
const zustand_1 = require("zustand");
|
|
6
|
+
const immer_1 = require("zustand/middleware/immer");
|
|
7
|
+
const middleware_1 = require("zustand/middleware");
|
|
8
|
+
const immerSyncUp = (newWalletManager) => {
|
|
9
|
+
return (draft) => {
|
|
10
|
+
draft.chains = newWalletManager.chains;
|
|
11
|
+
draft.assetLists = newWalletManager.assetLists;
|
|
12
|
+
draft.wallets = newWalletManager.wallets;
|
|
13
|
+
draft.signerOptions = newWalletManager.signerOptions;
|
|
14
|
+
draft.endpointOptions = newWalletManager.endpointOptions;
|
|
15
|
+
draft.signerOptionMap = newWalletManager.signerOptionMap;
|
|
16
|
+
draft.endpointOptionsMap = newWalletManager.endpointOptionsMap;
|
|
17
|
+
draft.preferredSignTypeMap = newWalletManager.preferredSignTypeMap;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
const createInterchainStore = (walletManager) => {
|
|
21
|
+
const { chains, assetLists, wallets, signerOptions, endpointOptions } = walletManager;
|
|
22
|
+
// const walletManager = new WalletManager(chains, assetLists, wallets, signerOptions, endpointOptions)
|
|
23
|
+
const chainWalletState = [];
|
|
24
|
+
wallets.forEach(wallet => {
|
|
25
|
+
chains.forEach(chain => {
|
|
26
|
+
chainWalletState.push({
|
|
27
|
+
chainName: chain.chainName,
|
|
28
|
+
walletName: wallet.info.name,
|
|
29
|
+
walletState: core_1.WalletState.Disconnected,
|
|
30
|
+
rpcEndpoint: "",
|
|
31
|
+
errorMessage: "",
|
|
32
|
+
signerOption: undefined,
|
|
33
|
+
account: undefined
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
return (0, zustand_1.createStore)((0, middleware_1.persist)((0, immer_1.immer)((set, get) => ({
|
|
38
|
+
chainWalletState,
|
|
39
|
+
currentWalletName: '',
|
|
40
|
+
currentChainName: '',
|
|
41
|
+
chains: [...walletManager.chains],
|
|
42
|
+
assetLists: [...walletManager.assetLists],
|
|
43
|
+
wallets: walletManager.wallets,
|
|
44
|
+
signerOptions: walletManager.signerOptions,
|
|
45
|
+
endpointOptions: walletManager.endpointOptions,
|
|
46
|
+
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
47
|
+
signerOptionMap: { ...walletManager.signerOptionMap },
|
|
48
|
+
endpointOptionsMap: { ...walletManager.endpointOptionsMap },
|
|
49
|
+
updateChainWalletState: (walletName, chainName, data) => {
|
|
50
|
+
set(draft => {
|
|
51
|
+
let targetIndex = draft.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
52
|
+
draft.chainWalletState[targetIndex] = { ...draft.chainWalletState[targetIndex], ...data };
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
init: () => walletManager.init(),
|
|
56
|
+
setCurrentChainName: (chainName) => {
|
|
57
|
+
set(draft => { draft.currentChainName = chainName; });
|
|
58
|
+
},
|
|
59
|
+
setCurrentWalletName: (walletName) => {
|
|
60
|
+
set(draft => { draft.currentWalletName = walletName; });
|
|
61
|
+
},
|
|
62
|
+
getDraftChainWalletState: (state, walletName, chainName) => {
|
|
63
|
+
const targetIndex = state.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
64
|
+
return state.chainWalletState[targetIndex];
|
|
65
|
+
},
|
|
66
|
+
getChainWalletState: (walletName, chainName) => {
|
|
67
|
+
return get().chainWalletState.find(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
68
|
+
},
|
|
69
|
+
addChains: (chains, assetLists, signerOptions, endpointOptions) => {
|
|
70
|
+
walletManager.addChains(chains, assetLists, signerOptions, endpointOptions);
|
|
71
|
+
// console.log(walletManager.chains, walletManager.assetLists)
|
|
72
|
+
// set(immerSyncUp(walletManager))
|
|
73
|
+
// set(draft => {
|
|
74
|
+
// draft.chains = walletManager.chains
|
|
75
|
+
// })
|
|
76
|
+
set(draft => {
|
|
77
|
+
chains.forEach(newChain => {
|
|
78
|
+
const existChain = draft.chains.find(c => c.chainId === newChain.chainId);
|
|
79
|
+
if (!existChain) {
|
|
80
|
+
draft.chains.push(newChain);
|
|
81
|
+
const assetList = assetLists.find(a => a.chainName === newChain.chainName);
|
|
82
|
+
draft.assetLists.push(assetList);
|
|
83
|
+
draft.wallets.forEach(w => {
|
|
84
|
+
draft.chainWalletState.push({
|
|
85
|
+
chainName: newChain.chainName,
|
|
86
|
+
walletName: w.info.name,
|
|
87
|
+
walletState: core_1.WalletState.Disconnected,
|
|
88
|
+
rpcEndpoint: endpointOptions?.endpoints?.[newChain.chainName]?.rpc?.[0],
|
|
89
|
+
errorMessage: "",
|
|
90
|
+
signerOption: signerOptions?.signing?.(newChain.chainName),
|
|
91
|
+
account: undefined
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
draft.signerOptionMap[newChain.chainName] = signerOptions?.signing?.(newChain.chainName);
|
|
96
|
+
draft.endpointOptionsMap[newChain.chainName] = endpointOptions?.endpoints?.[newChain.chainName];
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
connect: async (walletName, chainName) => {
|
|
101
|
+
get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Connecting });
|
|
102
|
+
try {
|
|
103
|
+
await walletManager.connect(walletName, chainName);
|
|
104
|
+
set(draft => {
|
|
105
|
+
draft.currentChainName = chainName;
|
|
106
|
+
draft.currentWalletName = walletName;
|
|
107
|
+
});
|
|
108
|
+
get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Connected });
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
if (error.message === 'Request rejected') {
|
|
112
|
+
get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Rejected, errorMessage: error.message });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Disconnected, errorMessage: error.message });
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
disconnect: async (walletName, chainName) => {
|
|
119
|
+
try {
|
|
120
|
+
await walletManager.disconnect(walletName, chainName);
|
|
121
|
+
get().updateChainWalletState(walletName, chainName, { walletState: core_1.WalletState.Disconnected, account: null });
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
getAccount: async (walletName, chainName) => {
|
|
127
|
+
const account = await walletManager.getAccount(walletName, chainName);
|
|
128
|
+
get().updateChainWalletState(walletName, chainName, { account });
|
|
129
|
+
return account;
|
|
130
|
+
},
|
|
131
|
+
getRpcEndpoint: async (walletName, chainName) => {
|
|
132
|
+
const exist = get().getChainWalletState(walletName, chainName).rpcEndpoint;
|
|
133
|
+
if (exist)
|
|
134
|
+
return exist;
|
|
135
|
+
const rpcEndpoint = await walletManager.getRpcEndpoint(walletName, chainName);
|
|
136
|
+
get().updateChainWalletState(walletName, chainName, { rpcEndpoint });
|
|
137
|
+
return rpcEndpoint;
|
|
138
|
+
},
|
|
139
|
+
getChainLogoUrl(chainName) {
|
|
140
|
+
return walletManager.getChainLogoUrl(chainName);
|
|
141
|
+
},
|
|
142
|
+
getChainByName(chainName) {
|
|
143
|
+
return walletManager.getChainByName(chainName);
|
|
144
|
+
},
|
|
145
|
+
getAssetListByName(chainName) {
|
|
146
|
+
return walletManager.getAssetListByName(chainName);
|
|
147
|
+
},
|
|
148
|
+
getDownloadLink(walletName) {
|
|
149
|
+
return walletManager.getDownloadLink(walletName);
|
|
150
|
+
},
|
|
151
|
+
getOfflineSigner(walletName, chainName) {
|
|
152
|
+
return walletManager.getOfflineSigner(walletName, chainName);
|
|
153
|
+
},
|
|
154
|
+
getPreferSignType(chainName) {
|
|
155
|
+
const result = walletManager.getPreferSignType(chainName);
|
|
156
|
+
set(immerSyncUp(walletManager));
|
|
157
|
+
return result;
|
|
158
|
+
},
|
|
159
|
+
getSignerOptions(chainName) {
|
|
160
|
+
const result = walletManager.getSignerOptions(chainName);
|
|
161
|
+
set(immerSyncUp(walletManager));
|
|
162
|
+
return result;
|
|
163
|
+
},
|
|
164
|
+
getWalletByName(walletName) {
|
|
165
|
+
return walletManager.getWalletByName(walletName);
|
|
166
|
+
},
|
|
167
|
+
getSigningClient(walletName, chainName) {
|
|
168
|
+
return walletManager.getSigningClient(walletName, chainName);
|
|
169
|
+
},
|
|
170
|
+
getEnv() {
|
|
171
|
+
return walletManager.getEnv();
|
|
172
|
+
},
|
|
173
|
+
})), {
|
|
174
|
+
name: 'interchain-kit-store',
|
|
175
|
+
storage: (0, middleware_1.createJSONStorage)(() => localStorage),
|
|
176
|
+
partialize: state => ({
|
|
177
|
+
chainWalletState: state.chainWalletState,
|
|
178
|
+
currentWalletName: state.currentWalletName,
|
|
179
|
+
currentChainName: state.currentChainName
|
|
180
|
+
}),
|
|
181
|
+
onRehydrateStorage: (state) => {
|
|
182
|
+
// console.log('interchain-kit store hydration starts')
|
|
183
|
+
// optional
|
|
184
|
+
return (state, error) => {
|
|
185
|
+
if (error) {
|
|
186
|
+
console.log('an error happened during hydration', error);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
console.log('interchain-kit store hydration finished');
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
},
|
|
193
|
+
}));
|
|
194
|
+
};
|
|
195
|
+
exports.createInterchainStore = createInterchainStore;
|
package/utils/wallet.d.ts
CHANGED