@interchain-kit/react 0.3.46 → 0.3.47
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/useChain.js +4 -4
- package/esm/hooks/useChainWallet.js +5 -5
- package/esm/hooks/useForceUpdate.js +5 -0
- package/esm/hooks/useSigningClient.js +3 -3
- package/esm/hooks/useWalletManager.js +12 -4
- package/esm/hooks/useWalletModal.js +1 -1
- package/esm/index.js +3 -4
- package/esm/modal/modal.js +2 -2
- package/esm/provider.js +3 -4
- package/esm/utils/bindContext.js +54 -0
- package/hooks/useChain.js +4 -4
- package/hooks/useChainWallet.d.ts +1 -1
- package/hooks/useChainWallet.js +4 -4
- package/hooks/useForceUpdate.d.ts +1 -0
- package/hooks/useForceUpdate.js +9 -0
- package/hooks/useSigningClient.js +2 -2
- package/hooks/useWalletManager.d.ts +1 -1
- package/hooks/useWalletManager.js +11 -3
- package/index.d.ts +3 -4
- package/index.js +3 -4
- package/modal/modal.d.ts +7 -7
- package/modal/modal.js +8 -8
- package/modal/views/Error.d.ts +3 -3
- package/modal/views/NotExist.d.ts +3 -3
- package/modal/views/Reject.d.ts +3 -3
- package/package.json +8 -7
- package/provider.d.ts +4 -6
- package/provider.js +3 -4
- package/types/chain.d.ts +20 -3
- package/utils/bindContext.d.ts +21 -0
- package/utils/bindContext.js +57 -0
- package/esm/store/chain-wallet.js +0 -35
- package/esm/store/index.js +0 -2
- package/esm/store/stateful-wallet.js +0 -182
- package/esm/store/store.js +0 -262
- package/store/chain-wallet.d.ts +0 -16
- package/store/chain-wallet.js +0 -39
- package/store/index.d.ts +0 -2
- package/store/index.js +0 -18
- package/store/stateful-wallet.d.ts +0 -21
- package/store/stateful-wallet.js +0 -186
- package/store/store.d.ts +0 -51
- package/store/store.js +0 -266
package/store/stateful-wallet.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StatefulWallet = void 0;
|
|
4
|
-
const core_1 = require("@interchain-kit/core");
|
|
5
|
-
const isSameConstructor_1 = require("../utils/isSameConstructor");
|
|
6
|
-
class StatefulWallet extends core_1.BaseWallet {
|
|
7
|
-
originalWallet;
|
|
8
|
-
walletName;
|
|
9
|
-
get;
|
|
10
|
-
constructor(wallet, get) {
|
|
11
|
-
super(wallet.info);
|
|
12
|
-
this.originalWallet = wallet;
|
|
13
|
-
this.walletName = wallet.info.name;
|
|
14
|
-
this.get = get;
|
|
15
|
-
}
|
|
16
|
-
get store() {
|
|
17
|
-
return this.get();
|
|
18
|
-
}
|
|
19
|
-
get walletState() {
|
|
20
|
-
// 獲取此錢包在所有鏈上的狀態
|
|
21
|
-
const states = (this.store.chainWalletState || [])
|
|
22
|
-
.filter(cws => cws.walletName === this.walletName)
|
|
23
|
-
.map(cws => cws.walletState);
|
|
24
|
-
// If any chain is in the connected state, return connected
|
|
25
|
-
if (states.includes(core_1.WalletState.Connected)) {
|
|
26
|
-
return core_1.WalletState.Connected;
|
|
27
|
-
}
|
|
28
|
-
// 如果有任何一個鏈正在連接中,則返回連接中
|
|
29
|
-
if (states.includes(core_1.WalletState.Connecting)) {
|
|
30
|
-
return core_1.WalletState.Connecting;
|
|
31
|
-
}
|
|
32
|
-
// 如果所有鏈都是不存在狀態,則返回不存在
|
|
33
|
-
if (states.length > 0 && states.every(state => state === core_1.WalletState.NotExist)) {
|
|
34
|
-
return core_1.WalletState.NotExist;
|
|
35
|
-
}
|
|
36
|
-
// 如果有任何一個鏈是被拒絕狀態,則返回被拒絕
|
|
37
|
-
if (states.includes(core_1.WalletState.Rejected)) {
|
|
38
|
-
return core_1.WalletState.Rejected;
|
|
39
|
-
}
|
|
40
|
-
// 預設返回未連接
|
|
41
|
-
return core_1.WalletState.Disconnected;
|
|
42
|
-
}
|
|
43
|
-
get errorMessage() {
|
|
44
|
-
// 獲取此錢包在所有鏈上的錯誤訊息
|
|
45
|
-
const errors = (this.store.chainWalletState || [])
|
|
46
|
-
.filter(cws => cws.walletName === this.walletName)
|
|
47
|
-
.map(cws => cws.errorMessage)
|
|
48
|
-
.filter(error => error && error.trim() !== '');
|
|
49
|
-
// 返回第一個非空錯誤訊息,如果沒有則返回空字串
|
|
50
|
-
return errors.length > 0 ? errors[0] : '';
|
|
51
|
-
}
|
|
52
|
-
async init() {
|
|
53
|
-
try {
|
|
54
|
-
await this.originalWallet.init();
|
|
55
|
-
this.originalWallet.events.on('accountChanged', async () => {
|
|
56
|
-
const chains = Array.from(this.originalWallet.chainMap.values());
|
|
57
|
-
for (const chain of chains) {
|
|
58
|
-
await this.getAccount(chain.chainId);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
this.originalWallet.events.on('disconnect', () => {
|
|
62
|
-
// Update all chains for this wallet to disconnected state
|
|
63
|
-
this.store.chains.forEach(chain => {
|
|
64
|
-
this.store.updateChainWalletState(this.walletName, chain.chainName, {
|
|
65
|
-
walletState: core_1.WalletState.Disconnected,
|
|
66
|
-
account: null,
|
|
67
|
-
errorMessage: ''
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
this.store.chains.forEach(chain => {
|
|
72
|
-
const lastChainWalletState = this.store.getChainWalletState(this.walletName, chain.chainName)?.walletState;
|
|
73
|
-
if (lastChainWalletState === core_1.WalletState.NotExist) {
|
|
74
|
-
this.store.updateChainWalletState(this.walletName, chain.chainName, {
|
|
75
|
-
walletState: core_1.WalletState.Disconnected,
|
|
76
|
-
errorMessage: ''
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
if (error === core_1.clientNotExistError) {
|
|
83
|
-
this.store.chains.forEach(chain => {
|
|
84
|
-
this.store.updateChainWalletState(this.walletName, chain.chainName, {
|
|
85
|
-
walletState: core_1.WalletState.NotExist,
|
|
86
|
-
errorMessage: core_1.clientNotExistError.message
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
async connect(chainId) {
|
|
93
|
-
const { store, walletName, originalWallet } = this;
|
|
94
|
-
const chainToConnect = this.getChainById(chainId);
|
|
95
|
-
const state = store.getChainWalletState(walletName, chainToConnect.chainName)?.walletState;
|
|
96
|
-
if (state === core_1.WalletState.NotExist) {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
if (walletName === 'WalletConnect' && state === core_1.WalletState.Connected) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
store.setCurrentChainName(chainToConnect.chainName);
|
|
103
|
-
store.setCurrentWalletName(walletName);
|
|
104
|
-
store.setWalletConnectQRCodeUri('');
|
|
105
|
-
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Connecting, errorMessage: '' });
|
|
106
|
-
try {
|
|
107
|
-
if (originalWallet instanceof core_1.WCWallet) {
|
|
108
|
-
originalWallet.setOnPairingUriCreatedCallback((uri) => {
|
|
109
|
-
store.setWalletConnectQRCodeUri(uri);
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
await originalWallet.connect(chainToConnect.chainId);
|
|
113
|
-
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Connected });
|
|
114
|
-
await this.getAccount(chainToConnect.chainId);
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
if (error.message.includes('rejected')) {
|
|
118
|
-
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Rejected, errorMessage: error.message });
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Disconnected, errorMessage: error.message });
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
async disconnect(chainId) {
|
|
127
|
-
const { store, walletName, originalWallet } = this;
|
|
128
|
-
const chainToConnect = this.getChainById(chainId);
|
|
129
|
-
try {
|
|
130
|
-
if (this.walletState === core_1.WalletState.Connected) {
|
|
131
|
-
await originalWallet.disconnect(chainToConnect.chainId);
|
|
132
|
-
}
|
|
133
|
-
store.updateChainWalletState(walletName, chainToConnect.chainName, { walletState: core_1.WalletState.Disconnected, account: null });
|
|
134
|
-
}
|
|
135
|
-
catch (error) {
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
async getAccount(chainId) {
|
|
139
|
-
const chainToConnect = this.getChainById(chainId);
|
|
140
|
-
const { store, walletName, originalWallet } = this;
|
|
141
|
-
try {
|
|
142
|
-
const account = await originalWallet.getAccount(chainToConnect.chainId);
|
|
143
|
-
store.updateChainWalletState(walletName, chainToConnect.chainName, { account });
|
|
144
|
-
if (this.originalWallet instanceof core_1.WCWallet) {
|
|
145
|
-
this.originalWallet.setAccountToRestore(account);
|
|
146
|
-
}
|
|
147
|
-
return account;
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
console.log(error);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
getOfflineSigner(chainId) {
|
|
154
|
-
return this.originalWallet.getOfflineSigner(chainId);
|
|
155
|
-
}
|
|
156
|
-
addSuggestChain(chainId) {
|
|
157
|
-
return this.originalWallet.addSuggestChain(chainId);
|
|
158
|
-
}
|
|
159
|
-
getProvider(chainId) {
|
|
160
|
-
return this.originalWallet.getProvider(chainId);
|
|
161
|
-
}
|
|
162
|
-
getChainById(chainId) {
|
|
163
|
-
return this.originalWallet.getChainById(chainId);
|
|
164
|
-
}
|
|
165
|
-
getWalletOfType(WalletClass) {
|
|
166
|
-
if (this.originalWallet instanceof WalletClass) {
|
|
167
|
-
return this.originalWallet;
|
|
168
|
-
}
|
|
169
|
-
if (this.originalWallet instanceof core_1.MultiChainWallet) {
|
|
170
|
-
if ((0, isSameConstructor_1.isSameConstructor)(WalletClass, core_1.CosmosWallet)) {
|
|
171
|
-
const cosmosWallet = this.originalWallet.getWalletByChainType('cosmos');
|
|
172
|
-
if (cosmosWallet) {
|
|
173
|
-
return cosmosWallet;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
if ((0, isSameConstructor_1.isSameConstructor)(WalletClass, core_1.EthereumWallet)) {
|
|
177
|
-
const ethereumWallet = this.originalWallet.getWalletByChainType('eip155');
|
|
178
|
-
if (ethereumWallet) {
|
|
179
|
-
return ethereumWallet;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return undefined;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
exports.StatefulWallet = StatefulWallet;
|
package/store/store.d.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { AssetList, Chain } from "@chain-registry/types";
|
|
2
|
-
import { EndpointOptions, SignerOptions, WalletAccount, WalletManager, WalletState } from "@interchain-kit/core";
|
|
3
|
-
import { HttpEndpoint } from '@interchainjs/types';
|
|
4
|
-
import { StatefulWallet } from './stateful-wallet';
|
|
5
|
-
export type ChainWalletState = {
|
|
6
|
-
chainName: string;
|
|
7
|
-
walletName: string;
|
|
8
|
-
walletState: WalletState;
|
|
9
|
-
rpcEndpoint: string | HttpEndpoint;
|
|
10
|
-
errorMessage: string;
|
|
11
|
-
account: WalletAccount;
|
|
12
|
-
};
|
|
13
|
-
export interface InterchainStore extends WalletManager {
|
|
14
|
-
chainWalletState: ChainWalletState[];
|
|
15
|
-
currentWalletName: string;
|
|
16
|
-
currentChainName: string;
|
|
17
|
-
walletConnectQRCodeUri: string;
|
|
18
|
-
isReady: boolean;
|
|
19
|
-
wallets: StatefulWallet[];
|
|
20
|
-
modalIsOpen: boolean;
|
|
21
|
-
openModal: () => void;
|
|
22
|
-
closeModal: () => void;
|
|
23
|
-
setCurrentChainName: (chainName: string) => void;
|
|
24
|
-
setCurrentWalletName: (walletName: string) => void;
|
|
25
|
-
getDraftChainWalletState: (state: InterchainStore, walletName: string, chainName: string) => ChainWalletState;
|
|
26
|
-
getChainWalletState: (walletName: string, chainName: string) => ChainWalletState | undefined;
|
|
27
|
-
updateChainWalletState: (walletName: string, chainName: string, data: Partial<ChainWalletState>) => void;
|
|
28
|
-
getStatefulWalletByName: (walletName: string) => StatefulWallet | undefined;
|
|
29
|
-
setWalletConnectQRCodeUri: (uri: string) => void;
|
|
30
|
-
}
|
|
31
|
-
export type InterchainStoreData = {
|
|
32
|
-
chains: Chain[];
|
|
33
|
-
assetLists: AssetList[];
|
|
34
|
-
wallets: StatefulWallet[];
|
|
35
|
-
signerOptions: SignerOptions;
|
|
36
|
-
endpointOptions: EndpointOptions;
|
|
37
|
-
};
|
|
38
|
-
export declare const createInterchainStore: (walletManager: WalletManager) => Omit<Omit<import("zustand").StoreApi<InterchainStore>, "persist"> & {
|
|
39
|
-
persist: {
|
|
40
|
-
setOptions: (options: Partial<import("zustand/middleware").PersistOptions<InterchainStore, unknown>>) => void;
|
|
41
|
-
clearStorage: () => void;
|
|
42
|
-
rehydrate: () => Promise<void> | void;
|
|
43
|
-
hasHydrated: () => boolean;
|
|
44
|
-
onHydrate: (fn: (state: InterchainStore) => void) => () => void;
|
|
45
|
-
onFinishHydration: (fn: (state: InterchainStore) => void) => () => void;
|
|
46
|
-
getOptions: () => Partial<import("zustand/middleware").PersistOptions<InterchainStore, unknown>>;
|
|
47
|
-
};
|
|
48
|
-
}, "setState"> & {
|
|
49
|
-
setState(nextStateOrUpdater: InterchainStore | Partial<InterchainStore> | ((state: import("immer").WritableDraft<InterchainStore>) => void), shouldReplace?: false): void;
|
|
50
|
-
setState(nextStateOrUpdater: InterchainStore | ((state: import("immer").WritableDraft<InterchainStore>) => void), shouldReplace: true): void;
|
|
51
|
-
};
|
package/store/store.js
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
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 utils_1 = require("../utils");
|
|
9
|
-
const stateful_wallet_1 = require("./stateful-wallet");
|
|
10
|
-
const immerSyncUp = (newWalletManager) => {
|
|
11
|
-
return (draft) => {
|
|
12
|
-
draft.chains = newWalletManager.chains;
|
|
13
|
-
draft.assetLists = newWalletManager.assetLists;
|
|
14
|
-
draft.wallets = newWalletManager.wallets;
|
|
15
|
-
draft.signerOptions = newWalletManager.signerOptions;
|
|
16
|
-
draft.endpointOptions = newWalletManager.endpointOptions;
|
|
17
|
-
draft.signerOptionMap = newWalletManager.signerOptionMap;
|
|
18
|
-
draft.endpointOptionsMap = newWalletManager.endpointOptionsMap;
|
|
19
|
-
draft.preferredSignTypeMap = newWalletManager.preferredSignTypeMap;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
const createInterchainStore = (walletManager) => {
|
|
23
|
-
return (0, zustand_1.createStore)((0, middleware_1.persist)((0, immer_1.immer)((set, get) => ({
|
|
24
|
-
chainWalletState: [],
|
|
25
|
-
currentWalletName: '',
|
|
26
|
-
currentChainName: '',
|
|
27
|
-
chains: [...walletManager.chains],
|
|
28
|
-
assetLists: [...walletManager.assetLists],
|
|
29
|
-
wallets: walletManager.wallets.map(wallet => new stateful_wallet_1.StatefulWallet(wallet, get)),
|
|
30
|
-
signerOptions: walletManager.signerOptions,
|
|
31
|
-
endpointOptions: walletManager.endpointOptions,
|
|
32
|
-
preferredSignTypeMap: { ...walletManager.preferredSignTypeMap },
|
|
33
|
-
signerOptionMap: { ...walletManager.signerOptionMap },
|
|
34
|
-
endpointOptionsMap: { ...walletManager.endpointOptionsMap },
|
|
35
|
-
walletConnectQRCodeUri: '',
|
|
36
|
-
isReady: false,
|
|
37
|
-
modalIsOpen: false,
|
|
38
|
-
openModal: () => {
|
|
39
|
-
set(draft => {
|
|
40
|
-
draft.modalIsOpen = true;
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
closeModal: () => {
|
|
44
|
-
set(draft => {
|
|
45
|
-
draft.modalIsOpen = false;
|
|
46
|
-
draft.walletConnectQRCodeUri = ''; // reset the QR code uri when modal is closed
|
|
47
|
-
});
|
|
48
|
-
},
|
|
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: async () => {
|
|
56
|
-
const oldChainWalletStatesMap = new Map(get().chainWalletState.map(cws => [cws.walletName + cws.chainName, cws]));
|
|
57
|
-
// get().createStatefulWallet()
|
|
58
|
-
// should remove wallet that already disconnected ,for hydrain back from localstorage
|
|
59
|
-
// const oldChainWalletStateMap = new Map()
|
|
60
|
-
// get().chainWalletState.forEach(cws => {
|
|
61
|
-
// if(cws.walletState === WalletState.Connected) {
|
|
62
|
-
// oldChainWalletStateMap.set(cws.walletName + cws.chainName, cws)
|
|
63
|
-
// }
|
|
64
|
-
// })
|
|
65
|
-
get().wallets.forEach(wallet => {
|
|
66
|
-
get().chains.forEach(chain => {
|
|
67
|
-
set(draft => {
|
|
68
|
-
if (!oldChainWalletStatesMap.has(wallet.info.name + chain.chainName)) {
|
|
69
|
-
draft.chainWalletState.push({
|
|
70
|
-
chainName: chain.chainName,
|
|
71
|
-
walletName: wallet.info.name,
|
|
72
|
-
walletState: core_1.WalletState.Disconnected,
|
|
73
|
-
rpcEndpoint: "",
|
|
74
|
-
errorMessage: "",
|
|
75
|
-
account: undefined
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
await Promise.all(get().wallets.map(async (wallet) => wallet.init()));
|
|
82
|
-
set(draft => {
|
|
83
|
-
draft.isReady = true;
|
|
84
|
-
});
|
|
85
|
-
},
|
|
86
|
-
setCurrentChainName: (chainName) => {
|
|
87
|
-
set(draft => { draft.currentChainName = chainName; });
|
|
88
|
-
},
|
|
89
|
-
setCurrentWalletName: (walletName) => {
|
|
90
|
-
set(draft => { draft.currentWalletName = walletName; });
|
|
91
|
-
},
|
|
92
|
-
setWalletConnectQRCodeUri: (uri) => {
|
|
93
|
-
set(draft => { draft.walletConnectQRCodeUri = uri; });
|
|
94
|
-
},
|
|
95
|
-
getDraftChainWalletState: (state, walletName, chainName) => {
|
|
96
|
-
const targetIndex = state.chainWalletState.findIndex(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
97
|
-
return state.chainWalletState[targetIndex];
|
|
98
|
-
},
|
|
99
|
-
getChainWalletState: (walletName, chainName) => {
|
|
100
|
-
return get().chainWalletState.find(cws => cws.walletName === walletName && cws.chainName === chainName);
|
|
101
|
-
},
|
|
102
|
-
addChains: async (newChains, newAssetLists, newSignerOptions, newEndpointOptions) => {
|
|
103
|
-
await walletManager.addChains(newChains, newAssetLists, newSignerOptions, newEndpointOptions);
|
|
104
|
-
// console.log(walletManager.chains, walletManager.assetLists)
|
|
105
|
-
// set(immerSyncUp(walletManager))
|
|
106
|
-
// set(draft => {
|
|
107
|
-
// draft.chains = walletManager.chains
|
|
108
|
-
// })
|
|
109
|
-
set(draft => {
|
|
110
|
-
const existedChainMap = new Map(get().chains.map(chain => [chain.chainName, chain]));
|
|
111
|
-
const newAssetListMap = new Map(newAssetLists.map(assetList => [assetList.chainName, assetList]));
|
|
112
|
-
newChains.forEach(newChain => {
|
|
113
|
-
if (!existedChainMap.has(newChain.chainName)) {
|
|
114
|
-
draft.chains.push(newChain);
|
|
115
|
-
draft.assetLists.push(newAssetListMap.get(newChain.chainName));
|
|
116
|
-
}
|
|
117
|
-
draft.signerOptionMap[newChain.chainName] = newSignerOptions?.signing(newChain.chainName);
|
|
118
|
-
draft.endpointOptionsMap[newChain.chainName] = newEndpointOptions?.endpoints?.[newChain.chainName];
|
|
119
|
-
});
|
|
120
|
-
get().chains.forEach(chain => {
|
|
121
|
-
draft.signerOptionMap[chain.chainName] = {
|
|
122
|
-
...get().signerOptionMap[chain.chainName],
|
|
123
|
-
...newSignerOptions?.signing(chain.chainName)
|
|
124
|
-
};
|
|
125
|
-
draft.endpointOptionsMap[chain.chainName] = {
|
|
126
|
-
...get().endpointOptionsMap[chain.chainName],
|
|
127
|
-
...newEndpointOptions?.endpoints?.[chain.chainName]
|
|
128
|
-
};
|
|
129
|
-
});
|
|
130
|
-
const existedChainWalletStatesMap = new Map(get().chainWalletState.map(cws => [cws.walletName + cws.chainName, cws]));
|
|
131
|
-
get().wallets.forEach(wallet => {
|
|
132
|
-
newChains.forEach(newChain => {
|
|
133
|
-
if (!existedChainWalletStatesMap.has(wallet.info.name + newChain.chainName)) {
|
|
134
|
-
draft.chainWalletState.push({
|
|
135
|
-
chainName: newChain.chainName,
|
|
136
|
-
walletName: wallet.info.name,
|
|
137
|
-
walletState: core_1.WalletState.Disconnected,
|
|
138
|
-
rpcEndpoint: "",
|
|
139
|
-
errorMessage: "",
|
|
140
|
-
account: undefined
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
draft.chainWalletState = draft.chainWalletState.map(cws => {
|
|
146
|
-
return { ...cws, rpcEndpoint: newEndpointOptions?.endpoints?.[cws.chainName]?.rpc?.[0] || cws.rpcEndpoint };
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
},
|
|
150
|
-
connect: async (walletName, chainName) => {
|
|
151
|
-
const wallet = get().wallets.find(w => w.info.name === walletName);
|
|
152
|
-
const chain = get().chains.find(c => c.chainName === chainName);
|
|
153
|
-
if (!wallet) {
|
|
154
|
-
throw new Error(`Wallet ${walletName} not found`);
|
|
155
|
-
}
|
|
156
|
-
if (!chain) {
|
|
157
|
-
throw new Error(`Chain ${chainName} not found`);
|
|
158
|
-
}
|
|
159
|
-
return wallet.connect(chain.chainId);
|
|
160
|
-
},
|
|
161
|
-
disconnect: async (walletName, chainName) => {
|
|
162
|
-
const wallet = get().wallets.find(w => w.info.name === walletName);
|
|
163
|
-
const chain = get().chains.find(c => c.chainName === chainName);
|
|
164
|
-
if (!wallet) {
|
|
165
|
-
throw new Error(`Wallet ${walletName} not found`);
|
|
166
|
-
}
|
|
167
|
-
if (!chain) {
|
|
168
|
-
throw new Error(`Chain ${chainName} not found`);
|
|
169
|
-
}
|
|
170
|
-
return wallet.disconnect(chain.chainId);
|
|
171
|
-
},
|
|
172
|
-
getAccount: async (walletName, chainName) => {
|
|
173
|
-
const wallet = get().wallets.find(w => w.info.name === walletName);
|
|
174
|
-
const chain = get().chains.find(c => c.chainName === chainName);
|
|
175
|
-
if (!wallet) {
|
|
176
|
-
throw new Error(`Wallet ${walletName} not found`);
|
|
177
|
-
}
|
|
178
|
-
if (!chain) {
|
|
179
|
-
throw new Error(`Chain ${chainName} not found`);
|
|
180
|
-
}
|
|
181
|
-
const existedAccount = get().chainWalletState.find(cws => cws.walletName === walletName && cws.chainName === chainName)?.account;
|
|
182
|
-
if (existedAccount) {
|
|
183
|
-
return existedAccount;
|
|
184
|
-
}
|
|
185
|
-
return wallet.getAccount(chain.chainId);
|
|
186
|
-
},
|
|
187
|
-
getRpcEndpoint: async (walletName, chainName) => {
|
|
188
|
-
return (0, utils_1.dedupeAsync)(`${chainName}-rpcEndpoint`, async () => {
|
|
189
|
-
const rpcEndpoint = await walletManager.getRpcEndpoint(walletName, chainName);
|
|
190
|
-
get().wallets.map(wallet => {
|
|
191
|
-
get().updateChainWalletState(wallet.info.name, chainName, { rpcEndpoint });
|
|
192
|
-
});
|
|
193
|
-
return rpcEndpoint;
|
|
194
|
-
});
|
|
195
|
-
},
|
|
196
|
-
getChainLogoUrl(chainName) {
|
|
197
|
-
return walletManager.getChainLogoUrl(chainName);
|
|
198
|
-
},
|
|
199
|
-
getChainByName(chainName) {
|
|
200
|
-
return walletManager.getChainByName(chainName);
|
|
201
|
-
},
|
|
202
|
-
getAssetListByName(chainName) {
|
|
203
|
-
return walletManager.getAssetListByName(chainName);
|
|
204
|
-
},
|
|
205
|
-
getDownloadLink(walletName) {
|
|
206
|
-
return walletManager.getDownloadLink(walletName);
|
|
207
|
-
},
|
|
208
|
-
async getOfflineSigner(walletName, chainName) {
|
|
209
|
-
return walletManager.getOfflineSigner(walletName, chainName);
|
|
210
|
-
},
|
|
211
|
-
getPreferSignType(chainName) {
|
|
212
|
-
const result = walletManager.getPreferSignType(chainName);
|
|
213
|
-
// set(immerSyncUp(walletManager))
|
|
214
|
-
return result;
|
|
215
|
-
},
|
|
216
|
-
getSignerOptions(chainName) {
|
|
217
|
-
const result = walletManager.getSignerOptions(chainName);
|
|
218
|
-
// set(immerSyncUp(walletManager))
|
|
219
|
-
return result;
|
|
220
|
-
},
|
|
221
|
-
getWalletByName(walletName) {
|
|
222
|
-
return walletManager.getWalletByName(walletName);
|
|
223
|
-
},
|
|
224
|
-
getStatefulWalletByName(walletName) {
|
|
225
|
-
return get().wallets.find(w => w.info.name === walletName);
|
|
226
|
-
},
|
|
227
|
-
async getSigningClient(walletName, chainName) {
|
|
228
|
-
return walletManager.getSigningClient(walletName, chainName);
|
|
229
|
-
},
|
|
230
|
-
getEnv() {
|
|
231
|
-
return walletManager.getEnv();
|
|
232
|
-
},
|
|
233
|
-
})), {
|
|
234
|
-
name: 'interchain-kit-store',
|
|
235
|
-
storage: (0, middleware_1.createJSONStorage)(() => localStorage),
|
|
236
|
-
partialize: state => ({
|
|
237
|
-
chainWalletState: state.chainWalletState.map(cws => ({
|
|
238
|
-
chainName: cws.chainName,
|
|
239
|
-
walletName: cws.walletName,
|
|
240
|
-
account: cws.account,
|
|
241
|
-
walletState: cws.walletState,
|
|
242
|
-
})),
|
|
243
|
-
currentWalletName: state.currentWalletName,
|
|
244
|
-
currentChainName: state.currentChainName
|
|
245
|
-
}),
|
|
246
|
-
onRehydrateStorage: (state) => {
|
|
247
|
-
// console.log('interchain-kit store hydration starts')
|
|
248
|
-
// optional
|
|
249
|
-
return (state, error) => {
|
|
250
|
-
if (error) {
|
|
251
|
-
console.log('an error happened during hydration', error);
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
// console.log('interchain-kit store hydration finished')
|
|
255
|
-
state.chainWalletState = state.chainWalletState.map(cws => {
|
|
256
|
-
return {
|
|
257
|
-
...cws,
|
|
258
|
-
account: cws.account ? (0, utils_1.restoreAccountFromLocalStorage)(cws.account) : null
|
|
259
|
-
};
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
},
|
|
264
|
-
}));
|
|
265
|
-
};
|
|
266
|
-
exports.createInterchainStore = createInterchainStore;
|