Skip to content

Commit e5bb831

Browse files
authored
Merge pull request #36 from 2wheeh/fix/wallet-chain-sync
fix: sync current wallet/chain state on updateChainWalletState
2 parents 83422c6 + 44ebea9 commit e5bb831

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/store/__tests__/interchain-store.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,30 @@ describe('InterchainStore', () => {
238238
expect(osmosisKeplr).toBeUndefined();
239239
});
240240
});
241+
242+
describe('Current Wallet/Chain Auto-Sync', () => {
243+
it('should set currentWalletName and currentChainName when connected', () => {
244+
store.updateChainWalletState('keplr', 'cosmoshub', {
245+
walletState: WalletState.Connected,
246+
});
247+
248+
const state = store.getState();
249+
expect(state.currentWalletName).toBe('keplr');
250+
expect(state.currentChainName).toBe('cosmoshub');
251+
});
252+
253+
it('should clear current when disconnecting the current wallet/chain', () => {
254+
store.updateChainWalletState('keplr', 'cosmoshub', {
255+
walletState: WalletState.Connected,
256+
});
257+
258+
store.updateChainWalletState('keplr', 'cosmoshub', {
259+
walletState: WalletState.Disconnected,
260+
});
261+
262+
const state = store.getState();
263+
expect(state.currentWalletName).toBe('');
264+
expect(state.currentChainName).toBe('');
265+
});
266+
});
241267
});

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

0 commit comments

Comments
 (0)