@interchain-kit/react 0.2.220 → 0.2.222
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/index.js +3 -0
- package/esm/hooks/useAsync.js +114 -0
- package/esm/hooks/useChain.js +6 -1
- package/esm/hooks/useChainWallet.js +6 -1
- package/esm/hooks/useRpcEndpoint.js +22 -0
- package/esm/hooks/useSigningClient.js +20 -0
- package/esm/store/store.js +27 -12
- package/hooks/index.d.ts +3 -0
- package/hooks/index.js +3 -0
- package/hooks/useAsync.d.ts +27 -0
- package/hooks/useAsync.js +118 -0
- package/hooks/useChain.js +6 -1
- package/hooks/useChainWallet.js +6 -1
- package/hooks/useRpcEndpoint.d.ts +5 -0
- package/hooks/useRpcEndpoint.js +26 -0
- package/hooks/useSigningClient.d.ts +5 -0
- package/hooks/useSigningClient.js +24 -0
- package/package.json +3 -3
- package/store/store.js +27 -12
- package/types/chain.d.ts +4 -1
package/esm/hooks/index.js
CHANGED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { useState, useEffect, useRef } from 'react';
|
|
2
|
+
export const cache = new Map();
|
|
3
|
+
export const activeRequests = new Map();
|
|
4
|
+
export function useAsync({ queryKey, queryFn, enabled = true }) {
|
|
5
|
+
const [state, setState] = useState({
|
|
6
|
+
data: null,
|
|
7
|
+
isLoading: false,
|
|
8
|
+
error: null,
|
|
9
|
+
});
|
|
10
|
+
const mountedRef = useRef(false);
|
|
11
|
+
const queryFnRef = useRef(queryFn);
|
|
12
|
+
const cacheKey = Array.isArray(queryKey)
|
|
13
|
+
? JSON.stringify(queryKey)
|
|
14
|
+
: queryKey;
|
|
15
|
+
// Always update the current function
|
|
16
|
+
queryFnRef.current = queryFn;
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
mountedRef.current = true;
|
|
19
|
+
return () => {
|
|
20
|
+
mountedRef.current = false;
|
|
21
|
+
};
|
|
22
|
+
}, []);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (!enabled)
|
|
25
|
+
return;
|
|
26
|
+
if (cache.has(cacheKey)) {
|
|
27
|
+
setState(prev => ({
|
|
28
|
+
...prev,
|
|
29
|
+
data: cache.get(cacheKey),
|
|
30
|
+
isLoading: false,
|
|
31
|
+
error: null,
|
|
32
|
+
}));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (activeRequests.has(cacheKey)) {
|
|
36
|
+
activeRequests.get(cacheKey).then((data) => {
|
|
37
|
+
if (mountedRef.current) {
|
|
38
|
+
setState({
|
|
39
|
+
data,
|
|
40
|
+
isLoading: false,
|
|
41
|
+
error: null,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}, (error) => {
|
|
45
|
+
if (mountedRef.current) {
|
|
46
|
+
setState({
|
|
47
|
+
data: null,
|
|
48
|
+
isLoading: false,
|
|
49
|
+
error,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
setState(prev => ({ ...prev, isLoading: true, error: null }));
|
|
56
|
+
const requestPromise = queryFnRef.current()
|
|
57
|
+
.then((data) => {
|
|
58
|
+
cache.set(cacheKey, data);
|
|
59
|
+
if (mountedRef.current) {
|
|
60
|
+
setState({
|
|
61
|
+
data,
|
|
62
|
+
isLoading: false,
|
|
63
|
+
error: null,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return data;
|
|
67
|
+
})
|
|
68
|
+
.catch((error) => {
|
|
69
|
+
if (mountedRef.current) {
|
|
70
|
+
setState({
|
|
71
|
+
data: null,
|
|
72
|
+
isLoading: false,
|
|
73
|
+
error,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
// throw error;
|
|
77
|
+
})
|
|
78
|
+
.finally(() => {
|
|
79
|
+
activeRequests.delete(cacheKey);
|
|
80
|
+
});
|
|
81
|
+
activeRequests.set(cacheKey, requestPromise);
|
|
82
|
+
}, [cacheKey, enabled]); // 不再依赖 queryFn
|
|
83
|
+
const refetch = async () => {
|
|
84
|
+
try {
|
|
85
|
+
setState(prev => ({ ...prev, isLoading: true, error: null }));
|
|
86
|
+
const data = await queryFnRef.current();
|
|
87
|
+
cache.set(cacheKey, data);
|
|
88
|
+
if (mountedRef.current) {
|
|
89
|
+
setState({
|
|
90
|
+
data,
|
|
91
|
+
isLoading: false,
|
|
92
|
+
error: null,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (mountedRef.current) {
|
|
99
|
+
setState({
|
|
100
|
+
data: null,
|
|
101
|
+
isLoading: false,
|
|
102
|
+
error: error,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
data: state.data,
|
|
110
|
+
isLoading: state.isLoading,
|
|
111
|
+
error: state.error,
|
|
112
|
+
refetch,
|
|
113
|
+
};
|
|
114
|
+
}
|
package/esm/hooks/useChain.js
CHANGED
|
@@ -2,6 +2,7 @@ import { useWalletModal } from "../modal";
|
|
|
2
2
|
import { useWalletManager } from './useWalletManager';
|
|
3
3
|
import { ChainNameNotExist } from '@interchain-kit/core';
|
|
4
4
|
import { ChainWallet } from '../store/chain-wallet';
|
|
5
|
+
import { useSigningClient } from './useSigningClient';
|
|
5
6
|
export const useChain = (chainName) => {
|
|
6
7
|
const { assetLists, currentWalletName, disconnect, setCurrentChainName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = useWalletManager();
|
|
7
8
|
const chain = getChainByName(chainName);
|
|
@@ -12,6 +13,7 @@ export const useChain = (chainName) => {
|
|
|
12
13
|
const wallet = getWalletByName(currentWalletName);
|
|
13
14
|
const chainWalletStateToShow = getChainWalletState(currentWalletName, chainName);
|
|
14
15
|
const { open, close } = useWalletModal();
|
|
16
|
+
const { signingClient, isLoading: isSigningClientLoading, error: signingClientError } = useSigningClient(chainName, currentWalletName);
|
|
15
17
|
return {
|
|
16
18
|
//for migration cosmos kit
|
|
17
19
|
connect: () => {
|
|
@@ -34,7 +36,10 @@ export const useChain = (chainName) => {
|
|
|
34
36
|
assetList,
|
|
35
37
|
address: chainWalletStateToShow?.account?.address,
|
|
36
38
|
wallet: new ChainWallet(wallet, () => connect(currentWalletName, chainName), () => disconnect(currentWalletName, chainName), () => getAccount(currentWalletName, chainName)),
|
|
37
|
-
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
38
39
|
getSigningClient: () => getSigningClient(currentWalletName, chainName),
|
|
40
|
+
signingClient,
|
|
41
|
+
isSigningClientLoading,
|
|
42
|
+
signingClientError,
|
|
43
|
+
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
39
44
|
};
|
|
40
45
|
};
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { useWalletManager } from "./useWalletManager";
|
|
2
2
|
import { ChainWallet } from "../store/chain-wallet";
|
|
3
|
+
import { useSigningClient } from "./useSigningClient";
|
|
3
4
|
export const useChainWallet = (chainName, walletName) => {
|
|
4
5
|
const { assetLists, disconnect, setCurrentChainName, setCurrentWalletName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = useWalletManager();
|
|
5
6
|
const chain = getChainByName(chainName);
|
|
6
7
|
const wallet = getWalletByName(walletName);
|
|
7
8
|
const assetList = assetLists.find(a => a.chainName === chainName);
|
|
8
9
|
const chainWalletStateToShow = getChainWalletState(walletName, chainName);
|
|
10
|
+
const { signingClient, isLoading: isSigningClientLoading, error: signingClientError } = useSigningClient(chainName, walletName);
|
|
9
11
|
return {
|
|
10
12
|
//for migration cosmos kit
|
|
11
13
|
connect: async () => {
|
|
@@ -24,7 +26,10 @@ export const useChainWallet = (chainName, walletName) => {
|
|
|
24
26
|
assetList,
|
|
25
27
|
address: chainWalletStateToShow?.account?.address,
|
|
26
28
|
wallet: wallet ? new ChainWallet(wallet, () => connect(walletName, chainName), () => disconnect(walletName, chainName), () => getAccount(walletName, chainName)) : null,
|
|
27
|
-
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
28
29
|
getSigningClient: () => getSigningClient(walletName, chainName),
|
|
30
|
+
signingClient,
|
|
31
|
+
isSigningClientLoading,
|
|
32
|
+
signingClientError,
|
|
33
|
+
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
29
34
|
};
|
|
30
35
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useAsync } from "./useAsync";
|
|
2
|
+
import { useWalletManager } from "./useWalletManager";
|
|
3
|
+
export const useRpcEndpoint = (chainName, walletName) => {
|
|
4
|
+
const { getRpcEndpoint, updateChainWalletState, wallets } = useWalletManager();
|
|
5
|
+
const { data, error, isLoading } = useAsync({
|
|
6
|
+
queryKey: `rpc-endpoint-${chainName}`,
|
|
7
|
+
queryFn: async () => {
|
|
8
|
+
const rpc = await getRpcEndpoint(walletName, chainName);
|
|
9
|
+
wallets.forEach(wallet => {
|
|
10
|
+
updateChainWalletState(wallet.info.name, chainName, {
|
|
11
|
+
rpcEndpoint: rpc
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
return rpc;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
rpcEndpoint: data,
|
|
19
|
+
error,
|
|
20
|
+
isLoading
|
|
21
|
+
};
|
|
22
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useWalletManager } from "./useWalletManager";
|
|
2
|
+
import { useRpcEndpoint } from "./useRpcEndpoint";
|
|
3
|
+
import { useAsync } from "./useAsync";
|
|
4
|
+
import { WalletState } from "@interchain-kit/core";
|
|
5
|
+
export const useSigningClient = (chainName, walletName) => {
|
|
6
|
+
const { getSigningClient, getChainWalletState, getWalletByName } = useWalletManager();
|
|
7
|
+
const { rpcEndpoint } = useRpcEndpoint(chainName, walletName);
|
|
8
|
+
const chainWalletState = getChainWalletState(walletName, chainName);
|
|
9
|
+
const wallet = getWalletByName(walletName);
|
|
10
|
+
const { data, isLoading, error } = useAsync({
|
|
11
|
+
queryKey: `signing-client-${chainName}-${walletName}`,
|
|
12
|
+
queryFn: () => getSigningClient(walletName, chainName),
|
|
13
|
+
enabled: !!rpcEndpoint && chainWalletState?.walletState === WalletState.Connected
|
|
14
|
+
});
|
|
15
|
+
return {
|
|
16
|
+
signingClient: data,
|
|
17
|
+
error,
|
|
18
|
+
isLoading,
|
|
19
|
+
};
|
|
20
|
+
};
|
package/esm/store/store.js
CHANGED
|
@@ -37,11 +37,17 @@ export const createInterchainStore = (walletManager) => {
|
|
|
37
37
|
});
|
|
38
38
|
},
|
|
39
39
|
init: async () => {
|
|
40
|
-
const
|
|
40
|
+
const oldChainWalletStatesMap = new Map(get().chainWalletState.map(cws => [cws.walletName + cws.chainName, cws]));
|
|
41
|
+
set(draft => {
|
|
42
|
+
draft.chainWalletState = [];
|
|
43
|
+
});
|
|
41
44
|
wallets.forEach(wallet => {
|
|
42
45
|
chains.forEach(chain => {
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
set(draft => {
|
|
47
|
+
if (oldChainWalletStatesMap.has(wallet.info.name + chain.chainName)) {
|
|
48
|
+
draft.chainWalletState.push(oldChainWalletStatesMap.get(wallet.info.name + chain.chainName));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
45
51
|
draft.chainWalletState.push({
|
|
46
52
|
chainName: chain.chainName,
|
|
47
53
|
walletName: wallet.info.name,
|
|
@@ -50,8 +56,8 @@ export const createInterchainStore = (walletManager) => {
|
|
|
50
56
|
errorMessage: "",
|
|
51
57
|
account: undefined
|
|
52
58
|
});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
55
61
|
});
|
|
56
62
|
});
|
|
57
63
|
const NotExistWallets = [];
|
|
@@ -180,13 +186,17 @@ export const createInterchainStore = (walletManager) => {
|
|
|
180
186
|
}
|
|
181
187
|
},
|
|
182
188
|
getAccount: async (walletName, chainName) => {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
189
|
+
try {
|
|
190
|
+
const account = await walletManager.getAccount(walletName, chainName);
|
|
191
|
+
get().updateChainWalletState(walletName, chainName, { account });
|
|
192
|
+
return account;
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
console.log(error);
|
|
196
|
+
}
|
|
186
197
|
},
|
|
187
198
|
getRpcEndpoint: async (walletName, chainName) => {
|
|
188
|
-
|
|
189
|
-
return rpcEndpoint;
|
|
199
|
+
return walletManager.getRpcEndpoint(walletName, chainName);
|
|
190
200
|
},
|
|
191
201
|
getChainLogoUrl(chainName) {
|
|
192
202
|
return walletManager.getChainLogoUrl(chainName);
|
|
@@ -216,7 +226,7 @@ export const createInterchainStore = (walletManager) => {
|
|
|
216
226
|
getWalletByName(walletName) {
|
|
217
227
|
return walletManager.getWalletByName(walletName);
|
|
218
228
|
},
|
|
219
|
-
getSigningClient(walletName, chainName) {
|
|
229
|
+
async getSigningClient(walletName, chainName) {
|
|
220
230
|
return walletManager.getSigningClient(walletName, chainName);
|
|
221
231
|
},
|
|
222
232
|
getEnv() {
|
|
@@ -226,7 +236,12 @@ export const createInterchainStore = (walletManager) => {
|
|
|
226
236
|
name: 'interchain-kit-store',
|
|
227
237
|
storage: createJSONStorage(() => localStorage),
|
|
228
238
|
partialize: state => ({
|
|
229
|
-
chainWalletState: state.chainWalletState
|
|
239
|
+
chainWalletState: state.chainWalletState.map(cws => ({
|
|
240
|
+
chainName: cws.chainName,
|
|
241
|
+
walletName: cws.walletName,
|
|
242
|
+
account: cws.account,
|
|
243
|
+
walletState: cws.walletState,
|
|
244
|
+
})),
|
|
230
245
|
currentWalletName: state.currentWalletName,
|
|
231
246
|
currentChainName: state.currentChainName
|
|
232
247
|
}),
|
package/hooks/index.d.ts
CHANGED
package/hooks/index.js
CHANGED
|
@@ -17,3 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./useWalletManager"), exports);
|
|
18
18
|
__exportStar(require("./useChain"), exports);
|
|
19
19
|
__exportStar(require("./useChainWallet"), exports);
|
|
20
|
+
__exportStar(require("./useSigningClient"), exports);
|
|
21
|
+
__exportStar(require("./useAsync"), exports);
|
|
22
|
+
__exportStar(require("./useRpcEndpoint"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare const cache: Map<string, any>;
|
|
2
|
+
export declare const activeRequests: Map<string, Promise<any>>;
|
|
3
|
+
type UseAsyncRequestOptions<T> = {
|
|
4
|
+
/**
|
|
5
|
+
* Unique key for the query
|
|
6
|
+
*/
|
|
7
|
+
queryKey: string | any[];
|
|
8
|
+
/**
|
|
9
|
+
* Query function that returns a promise
|
|
10
|
+
* Note: For best performance, use a stable function reference
|
|
11
|
+
* (defined outside component or wrapped in useCallback)
|
|
12
|
+
* But not required - the hook will work without it
|
|
13
|
+
*/
|
|
14
|
+
queryFn: () => Promise<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Whether the query should execute
|
|
17
|
+
* @default true
|
|
18
|
+
*/
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
};
|
|
21
|
+
export declare function useAsync<T>({ queryKey, queryFn, enabled }: UseAsyncRequestOptions<T>): {
|
|
22
|
+
data: T;
|
|
23
|
+
isLoading: boolean;
|
|
24
|
+
error: Error;
|
|
25
|
+
refetch: () => Promise<T>;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.activeRequests = exports.cache = void 0;
|
|
4
|
+
exports.useAsync = useAsync;
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
exports.cache = new Map();
|
|
7
|
+
exports.activeRequests = new Map();
|
|
8
|
+
function useAsync({ queryKey, queryFn, enabled = true }) {
|
|
9
|
+
const [state, setState] = (0, react_1.useState)({
|
|
10
|
+
data: null,
|
|
11
|
+
isLoading: false,
|
|
12
|
+
error: null,
|
|
13
|
+
});
|
|
14
|
+
const mountedRef = (0, react_1.useRef)(false);
|
|
15
|
+
const queryFnRef = (0, react_1.useRef)(queryFn);
|
|
16
|
+
const cacheKey = Array.isArray(queryKey)
|
|
17
|
+
? JSON.stringify(queryKey)
|
|
18
|
+
: queryKey;
|
|
19
|
+
// Always update the current function
|
|
20
|
+
queryFnRef.current = queryFn;
|
|
21
|
+
(0, react_1.useEffect)(() => {
|
|
22
|
+
mountedRef.current = true;
|
|
23
|
+
return () => {
|
|
24
|
+
mountedRef.current = false;
|
|
25
|
+
};
|
|
26
|
+
}, []);
|
|
27
|
+
(0, react_1.useEffect)(() => {
|
|
28
|
+
if (!enabled)
|
|
29
|
+
return;
|
|
30
|
+
if (exports.cache.has(cacheKey)) {
|
|
31
|
+
setState(prev => ({
|
|
32
|
+
...prev,
|
|
33
|
+
data: exports.cache.get(cacheKey),
|
|
34
|
+
isLoading: false,
|
|
35
|
+
error: null,
|
|
36
|
+
}));
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (exports.activeRequests.has(cacheKey)) {
|
|
40
|
+
exports.activeRequests.get(cacheKey).then((data) => {
|
|
41
|
+
if (mountedRef.current) {
|
|
42
|
+
setState({
|
|
43
|
+
data,
|
|
44
|
+
isLoading: false,
|
|
45
|
+
error: null,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}, (error) => {
|
|
49
|
+
if (mountedRef.current) {
|
|
50
|
+
setState({
|
|
51
|
+
data: null,
|
|
52
|
+
isLoading: false,
|
|
53
|
+
error,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
setState(prev => ({ ...prev, isLoading: true, error: null }));
|
|
60
|
+
const requestPromise = queryFnRef.current()
|
|
61
|
+
.then((data) => {
|
|
62
|
+
exports.cache.set(cacheKey, data);
|
|
63
|
+
if (mountedRef.current) {
|
|
64
|
+
setState({
|
|
65
|
+
data,
|
|
66
|
+
isLoading: false,
|
|
67
|
+
error: null,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return data;
|
|
71
|
+
})
|
|
72
|
+
.catch((error) => {
|
|
73
|
+
if (mountedRef.current) {
|
|
74
|
+
setState({
|
|
75
|
+
data: null,
|
|
76
|
+
isLoading: false,
|
|
77
|
+
error,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
// throw error;
|
|
81
|
+
})
|
|
82
|
+
.finally(() => {
|
|
83
|
+
exports.activeRequests.delete(cacheKey);
|
|
84
|
+
});
|
|
85
|
+
exports.activeRequests.set(cacheKey, requestPromise);
|
|
86
|
+
}, [cacheKey, enabled]); // 不再依赖 queryFn
|
|
87
|
+
const refetch = async () => {
|
|
88
|
+
try {
|
|
89
|
+
setState(prev => ({ ...prev, isLoading: true, error: null }));
|
|
90
|
+
const data = await queryFnRef.current();
|
|
91
|
+
exports.cache.set(cacheKey, data);
|
|
92
|
+
if (mountedRef.current) {
|
|
93
|
+
setState({
|
|
94
|
+
data,
|
|
95
|
+
isLoading: false,
|
|
96
|
+
error: null,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return data;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (mountedRef.current) {
|
|
103
|
+
setState({
|
|
104
|
+
data: null,
|
|
105
|
+
isLoading: false,
|
|
106
|
+
error: error,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
data: state.data,
|
|
114
|
+
isLoading: state.isLoading,
|
|
115
|
+
error: state.error,
|
|
116
|
+
refetch,
|
|
117
|
+
};
|
|
118
|
+
}
|
package/hooks/useChain.js
CHANGED
|
@@ -5,6 +5,7 @@ const modal_1 = require("../modal");
|
|
|
5
5
|
const useWalletManager_1 = require("./useWalletManager");
|
|
6
6
|
const core_1 = require("@interchain-kit/core");
|
|
7
7
|
const chain_wallet_1 = require("../store/chain-wallet");
|
|
8
|
+
const useSigningClient_1 = require("./useSigningClient");
|
|
8
9
|
const useChain = (chainName) => {
|
|
9
10
|
const { assetLists, currentWalletName, disconnect, setCurrentChainName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = (0, useWalletManager_1.useWalletManager)();
|
|
10
11
|
const chain = getChainByName(chainName);
|
|
@@ -15,6 +16,7 @@ const useChain = (chainName) => {
|
|
|
15
16
|
const wallet = getWalletByName(currentWalletName);
|
|
16
17
|
const chainWalletStateToShow = getChainWalletState(currentWalletName, chainName);
|
|
17
18
|
const { open, close } = (0, modal_1.useWalletModal)();
|
|
19
|
+
const { signingClient, isLoading: isSigningClientLoading, error: signingClientError } = (0, useSigningClient_1.useSigningClient)(chainName, currentWalletName);
|
|
18
20
|
return {
|
|
19
21
|
//for migration cosmos kit
|
|
20
22
|
connect: () => {
|
|
@@ -37,8 +39,11 @@ const useChain = (chainName) => {
|
|
|
37
39
|
assetList,
|
|
38
40
|
address: chainWalletStateToShow?.account?.address,
|
|
39
41
|
wallet: new chain_wallet_1.ChainWallet(wallet, () => connect(currentWalletName, chainName), () => disconnect(currentWalletName, chainName), () => getAccount(currentWalletName, chainName)),
|
|
40
|
-
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
41
42
|
getSigningClient: () => getSigningClient(currentWalletName, chainName),
|
|
43
|
+
signingClient,
|
|
44
|
+
isSigningClientLoading,
|
|
45
|
+
signingClientError,
|
|
46
|
+
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
42
47
|
};
|
|
43
48
|
};
|
|
44
49
|
exports.useChain = useChain;
|
package/hooks/useChainWallet.js
CHANGED
|
@@ -3,12 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useChainWallet = void 0;
|
|
4
4
|
const useWalletManager_1 = require("./useWalletManager");
|
|
5
5
|
const chain_wallet_1 = require("../store/chain-wallet");
|
|
6
|
+
const useSigningClient_1 = require("./useSigningClient");
|
|
6
7
|
const useChainWallet = (chainName, walletName) => {
|
|
7
8
|
const { assetLists, disconnect, setCurrentChainName, setCurrentWalletName, getChainByName, getWalletByName, getChainWalletState, getChainLogoUrl, connect, getSigningClient, getRpcEndpoint, getAccount } = (0, useWalletManager_1.useWalletManager)();
|
|
8
9
|
const chain = getChainByName(chainName);
|
|
9
10
|
const wallet = getWalletByName(walletName);
|
|
10
11
|
const assetList = assetLists.find(a => a.chainName === chainName);
|
|
11
12
|
const chainWalletStateToShow = getChainWalletState(walletName, chainName);
|
|
13
|
+
const { signingClient, isLoading: isSigningClientLoading, error: signingClientError } = (0, useSigningClient_1.useSigningClient)(chainName, walletName);
|
|
12
14
|
return {
|
|
13
15
|
//for migration cosmos kit
|
|
14
16
|
connect: async () => {
|
|
@@ -27,8 +29,11 @@ const useChainWallet = (chainName, walletName) => {
|
|
|
27
29
|
assetList,
|
|
28
30
|
address: chainWalletStateToShow?.account?.address,
|
|
29
31
|
wallet: wallet ? new chain_wallet_1.ChainWallet(wallet, () => connect(walletName, chainName), () => disconnect(walletName, chainName), () => getAccount(walletName, chainName)) : null,
|
|
30
|
-
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
31
32
|
getSigningClient: () => getSigningClient(walletName, chainName),
|
|
33
|
+
signingClient,
|
|
34
|
+
isSigningClientLoading,
|
|
35
|
+
signingClientError,
|
|
36
|
+
rpcEndpoint: chainWalletStateToShow?.rpcEndpoint,
|
|
32
37
|
};
|
|
33
38
|
};
|
|
34
39
|
exports.useChainWallet = useChainWallet;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useRpcEndpoint = void 0;
|
|
4
|
+
const useAsync_1 = require("./useAsync");
|
|
5
|
+
const useWalletManager_1 = require("./useWalletManager");
|
|
6
|
+
const useRpcEndpoint = (chainName, walletName) => {
|
|
7
|
+
const { getRpcEndpoint, updateChainWalletState, wallets } = (0, useWalletManager_1.useWalletManager)();
|
|
8
|
+
const { data, error, isLoading } = (0, useAsync_1.useAsync)({
|
|
9
|
+
queryKey: `rpc-endpoint-${chainName}`,
|
|
10
|
+
queryFn: async () => {
|
|
11
|
+
const rpc = await getRpcEndpoint(walletName, chainName);
|
|
12
|
+
wallets.forEach(wallet => {
|
|
13
|
+
updateChainWalletState(wallet.info.name, chainName, {
|
|
14
|
+
rpcEndpoint: rpc
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
return rpc;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
rpcEndpoint: data,
|
|
22
|
+
error,
|
|
23
|
+
isLoading
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
exports.useRpcEndpoint = useRpcEndpoint;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSigningClient = void 0;
|
|
4
|
+
const useWalletManager_1 = require("./useWalletManager");
|
|
5
|
+
const useRpcEndpoint_1 = require("./useRpcEndpoint");
|
|
6
|
+
const useAsync_1 = require("./useAsync");
|
|
7
|
+
const core_1 = require("@interchain-kit/core");
|
|
8
|
+
const useSigningClient = (chainName, walletName) => {
|
|
9
|
+
const { getSigningClient, getChainWalletState, getWalletByName } = (0, useWalletManager_1.useWalletManager)();
|
|
10
|
+
const { rpcEndpoint } = (0, useRpcEndpoint_1.useRpcEndpoint)(chainName, walletName);
|
|
11
|
+
const chainWalletState = getChainWalletState(walletName, chainName);
|
|
12
|
+
const wallet = getWalletByName(walletName);
|
|
13
|
+
const { data, isLoading, error } = (0, useAsync_1.useAsync)({
|
|
14
|
+
queryKey: `signing-client-${chainName}-${walletName}`,
|
|
15
|
+
queryFn: () => getSigningClient(walletName, chainName),
|
|
16
|
+
enabled: !!rpcEndpoint && chainWalletState?.walletState === core_1.WalletState.Connected
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
signingClient: data,
|
|
20
|
+
error,
|
|
21
|
+
isLoading,
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
exports.useSigningClient = useSigningClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interchain-kit/react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.222",
|
|
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.2.
|
|
37
|
+
"@interchain-kit/core": "0.2.222",
|
|
38
38
|
"@interchain-ui/react": "1.26.1",
|
|
39
39
|
"@interchainjs/cosmos": "1.10.1",
|
|
40
40
|
"@interchainjs/cosmos-types": "1.10.1",
|
|
@@ -61,5 +61,5 @@
|
|
|
61
61
|
"jest-environment-jsdom": "^29.7.0",
|
|
62
62
|
"ts-jest": "^29.3.0"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "fa6f119bb50e3044e2de4a21e370ecec33937459"
|
|
65
65
|
}
|
package/store/store.js
CHANGED
|
@@ -40,11 +40,17 @@ const createInterchainStore = (walletManager) => {
|
|
|
40
40
|
});
|
|
41
41
|
},
|
|
42
42
|
init: async () => {
|
|
43
|
-
const
|
|
43
|
+
const oldChainWalletStatesMap = new Map(get().chainWalletState.map(cws => [cws.walletName + cws.chainName, cws]));
|
|
44
|
+
set(draft => {
|
|
45
|
+
draft.chainWalletState = [];
|
|
46
|
+
});
|
|
44
47
|
wallets.forEach(wallet => {
|
|
45
48
|
chains.forEach(chain => {
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
set(draft => {
|
|
50
|
+
if (oldChainWalletStatesMap.has(wallet.info.name + chain.chainName)) {
|
|
51
|
+
draft.chainWalletState.push(oldChainWalletStatesMap.get(wallet.info.name + chain.chainName));
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
48
54
|
draft.chainWalletState.push({
|
|
49
55
|
chainName: chain.chainName,
|
|
50
56
|
walletName: wallet.info.name,
|
|
@@ -53,8 +59,8 @@ const createInterchainStore = (walletManager) => {
|
|
|
53
59
|
errorMessage: "",
|
|
54
60
|
account: undefined
|
|
55
61
|
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
58
64
|
});
|
|
59
65
|
});
|
|
60
66
|
const NotExistWallets = [];
|
|
@@ -183,13 +189,17 @@ const createInterchainStore = (walletManager) => {
|
|
|
183
189
|
}
|
|
184
190
|
},
|
|
185
191
|
getAccount: async (walletName, chainName) => {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
192
|
+
try {
|
|
193
|
+
const account = await walletManager.getAccount(walletName, chainName);
|
|
194
|
+
get().updateChainWalletState(walletName, chainName, { account });
|
|
195
|
+
return account;
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
console.log(error);
|
|
199
|
+
}
|
|
189
200
|
},
|
|
190
201
|
getRpcEndpoint: async (walletName, chainName) => {
|
|
191
|
-
|
|
192
|
-
return rpcEndpoint;
|
|
202
|
+
return walletManager.getRpcEndpoint(walletName, chainName);
|
|
193
203
|
},
|
|
194
204
|
getChainLogoUrl(chainName) {
|
|
195
205
|
return walletManager.getChainLogoUrl(chainName);
|
|
@@ -219,7 +229,7 @@ const createInterchainStore = (walletManager) => {
|
|
|
219
229
|
getWalletByName(walletName) {
|
|
220
230
|
return walletManager.getWalletByName(walletName);
|
|
221
231
|
},
|
|
222
|
-
getSigningClient(walletName, chainName) {
|
|
232
|
+
async getSigningClient(walletName, chainName) {
|
|
223
233
|
return walletManager.getSigningClient(walletName, chainName);
|
|
224
234
|
},
|
|
225
235
|
getEnv() {
|
|
@@ -229,7 +239,12 @@ const createInterchainStore = (walletManager) => {
|
|
|
229
239
|
name: 'interchain-kit-store',
|
|
230
240
|
storage: (0, middleware_1.createJSONStorage)(() => localStorage),
|
|
231
241
|
partialize: state => ({
|
|
232
|
-
chainWalletState: state.chainWalletState
|
|
242
|
+
chainWalletState: state.chainWalletState.map(cws => ({
|
|
243
|
+
chainName: cws.chainName,
|
|
244
|
+
walletName: cws.walletName,
|
|
245
|
+
account: cws.account,
|
|
246
|
+
walletState: cws.walletState,
|
|
247
|
+
})),
|
|
233
248
|
currentWalletName: state.currentWalletName,
|
|
234
249
|
currentChainName: state.currentChainName
|
|
235
250
|
}),
|
package/types/chain.d.ts
CHANGED
|
@@ -19,8 +19,11 @@ export type UseChainReturnType = {
|
|
|
19
19
|
assetList: AssetList;
|
|
20
20
|
address: string;
|
|
21
21
|
wallet: ChainWallet<BaseWallet>;
|
|
22
|
-
rpcEndpoint: string | HttpEndpoint;
|
|
22
|
+
rpcEndpoint: string | HttpEndpoint | unknown;
|
|
23
23
|
getSigningClient: () => Promise<SigningClient>;
|
|
24
|
+
signingClient: SigningClient | null;
|
|
25
|
+
isSigningClientLoading: boolean;
|
|
26
|
+
signingClientError: Error | unknown | null;
|
|
24
27
|
} & CosmosKitUseChainReturnType;
|
|
25
28
|
export type UseChainWalletReturnType = Omit<UseChainReturnType, 'openView' | 'closeView'>;
|
|
26
29
|
export type UseInterchainClientReturnType = {
|