Skip to content

Commit 0a4e41e

Browse files
committed
refactor: encapsulate current wallet/chain state sync in InterchainStore
1 parent 05f2684 commit 0a4e41e

File tree

3 files changed

+15
-52
lines changed

3 files changed

+15
-52
lines changed

packages/store/__tests__/chain-wallet-store.test.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -208,33 +208,6 @@ describe('ChainWalletStore', () => {
208208
);
209209
});
210210

211-
it('should set currentWalletName and currentChainName on successful connect', async () => {
212-
const mockAccount: WalletAccount = {
213-
address: 'cosmos1abc123',
214-
pubkey: new Uint8Array([1, 2, 3]),
215-
isNanoLedger: false,
216-
algo: 'secp256k1' as Algo,
217-
};
218-
219-
(mockWallet.connect as jest.Mock).mockResolvedValue(undefined);
220-
(mockWallet.getAccount as jest.Mock).mockResolvedValue(mockAccount);
221-
222-
await chainWalletStore.connect();
223-
224-
expect(mockInterchainStore.setCurrentWalletName).toHaveBeenCalledWith('keplr');
225-
expect(mockInterchainStore.setCurrentChainName).toHaveBeenCalledWith('cosmoshub');
226-
});
227-
228-
it('should not set current values on connection error', async () => {
229-
const error = new Error('Connection failed');
230-
(mockWallet.connect as jest.Mock).mockRejectedValue(error);
231-
232-
await chainWalletStore.connect();
233-
234-
expect(mockInterchainStore.setCurrentWalletName).not.toHaveBeenCalled();
235-
expect(mockInterchainStore.setCurrentChainName).not.toHaveBeenCalled();
236-
});
237-
238211
it('should handle connection errors', async () => {
239212
const error = new Error('Connection failed');
240213
(mockWallet.connect as jest.Mock).mockRejectedValue(error);
@@ -291,27 +264,6 @@ describe('ChainWalletStore', () => {
291264
);
292265
});
293266

294-
it('should reset currentWalletName and currentChainName on successful disconnect', async () => {
295-
(mockWallet.disconnect as jest.Mock).mockResolvedValue(undefined);
296-
297-
await chainWalletStore.disconnect();
298-
299-
expect(mockInterchainStore.setCurrentWalletName).toHaveBeenCalledWith('');
300-
expect(mockInterchainStore.setCurrentChainName).toHaveBeenCalledWith('');
301-
});
302-
303-
it('should not reset current values on disconnection error', async () => {
304-
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
305-
const error = new Error('Disconnection failed');
306-
(mockWallet.disconnect as jest.Mock).mockRejectedValue(error);
307-
308-
await chainWalletStore.disconnect();
309-
310-
expect(mockInterchainStore.setCurrentWalletName).not.toHaveBeenCalled();
311-
expect(mockInterchainStore.setCurrentChainName).not.toHaveBeenCalled();
312-
consoleSpy.mockRestore();
313-
});
314-
315267
it('should handle disconnection errors', async () => {
316268
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
317269
const error = new Error('Disconnection failed');

packages/store/src/store/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ export class InterchainStore {
7878
} else {
7979
this.addChainWalletState(walletName, chainName, state);
8080
}
81+
82+
switch (state.walletState) {
83+
case WalletState.Connected:
84+
this.setCurrentWalletName(walletName);
85+
this.setCurrentChainName(chainName);
86+
break;
87+
case WalletState.Disconnected:
88+
if (this.state.currentWalletName === walletName && this.state.currentChainName === chainName) {
89+
// TODO: for supporting multiple wallet connections,
90+
// we should set these to the previous instead of clearing
91+
this.setCurrentWalletName('');
92+
this.setCurrentChainName('');
93+
}
94+
break;
95+
}
8196
}
8297

8398
// 添加新的 chain wallet state

packages/store/src/wallet-manager/chain-wallet-store.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ export class ChainWalletStore extends BaseWallet {
5656
await this.wallet.connect(this.chain.chainId);
5757
const account = await this.getAccount();
5858
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Connected, account });
59-
this.store.setCurrentWalletName(this.wallet.info.name);
60-
this.store.setCurrentChainName(this.chain.chainName);
6159
} catch (error) {
6260
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Disconnected, errorMessage: (error as any).message });
6361
}
@@ -73,8 +71,6 @@ export class ChainWalletStore extends BaseWallet {
7371
try {
7472
await this.wallet.disconnect(this.chain.chainId);
7573
this.store.updateChainWalletState(this.wallet.info.name, this.chain.chainName, { walletState: WalletState.Disconnected, account: undefined, errorMessage: '' });
76-
this.store.setCurrentWalletName('');
77-
this.store.setCurrentChainName('');
7874
} catch (error) {
7975
console.error(error);
8076
}

0 commit comments

Comments
 (0)