diff --git a/packages/store/__tests__/interchain-store.test.ts b/packages/store/__tests__/interchain-store.test.ts index f7d88b871..7c9ed642c 100644 --- a/packages/store/__tests__/interchain-store.test.ts +++ b/packages/store/__tests__/interchain-store.test.ts @@ -238,4 +238,30 @@ describe('InterchainStore', () => { expect(osmosisKeplr).toBeUndefined(); }); }); + + describe('Current Wallet/Chain Auto-Sync', () => { + it('should set currentWalletName and currentChainName when connected', () => { + store.updateChainWalletState('keplr', 'cosmoshub', { + walletState: WalletState.Connected, + }); + + const state = store.getState(); + expect(state.currentWalletName).toBe('keplr'); + expect(state.currentChainName).toBe('cosmoshub'); + }); + + it('should clear current when disconnecting the current wallet/chain', () => { + store.updateChainWalletState('keplr', 'cosmoshub', { + walletState: WalletState.Connected, + }); + + store.updateChainWalletState('keplr', 'cosmoshub', { + walletState: WalletState.Disconnected, + }); + + const state = store.getState(); + expect(state.currentWalletName).toBe(''); + expect(state.currentChainName).toBe(''); + }); + }); }); diff --git a/packages/store/src/store/index.ts b/packages/store/src/store/index.ts index 14ede0c3a..192e5b35f 100644 --- a/packages/store/src/store/index.ts +++ b/packages/store/src/store/index.ts @@ -78,6 +78,21 @@ export class InterchainStore { } else { this.addChainWalletState(walletName, chainName, state); } + + switch (state.walletState) { + case WalletState.Connected: + this.setCurrentWalletName(walletName); + this.setCurrentChainName(chainName); + break; + case WalletState.Disconnected: + if (this.state.currentWalletName === walletName && this.state.currentChainName === chainName) { + // TODO: for supporting multiple wallet connections, + // we should set these to the previous instead of clearing + this.setCurrentWalletName(''); + this.setCurrentChainName(''); + } + break; + } } // 添加新的 chain wallet state