Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/store/__tests__/chain-wallet-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('ChainWalletStore', () => {
const walletState = chainWalletStore.walletState;

expect(walletState).toBe(WalletState.Connected);
expect(mockInterchainStore.getChainWalletState).toHaveBeenCalledWith('cosmoshub', 'keplr');
expect(mockInterchainStore.getChainWalletState).toHaveBeenCalledWith('keplr', 'cosmoshub');
});

it('should return NotExist when no state exists', () => {
Expand Down Expand Up @@ -161,6 +161,7 @@ describe('ChainWalletStore', () => {
const errorMessage = chainWalletStore.errorMessage;

expect(errorMessage).toBe('Connection failed');
expect(mockInterchainStore.getChainWalletState).toHaveBeenCalledWith('keplr', 'cosmoshub');
});

it('should return empty string when no state exists', () => {
Expand Down
33 changes: 33 additions & 0 deletions packages/store/__tests__/wallet-manager-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,39 @@ describe('WalletManagerStore', () => {
currentChainName: savedState.currentChainName,
});
});
it('should restore a saved NotExist state as Disconnected', async () => {
const savedState: { chainWalletStates: ChainWalletState[]; currentWalletName: string; currentChainName: string } = {
chainWalletStates: [
{
chainName: 'cosmoshub',
walletName: 'keplr',
walletState: WalletState.NotExist,
rpcEndpoint: '',
errorMessage: 'Client not exist',
account: undefined,
},
],
currentWalletName: '',
currentChainName: '',
};

mockLocalStorage.load.mockReturnValue(savedState);

await walletManagerStore.init();

// restore() writes the saved state first; later calls only add or drop entries.
expect(mockInterchainStore.setState).toHaveBeenNthCalledWith(1, {
chainWalletStates: [
{
...savedState.chainWalletStates[0],
walletState: WalletState.Disconnected,
errorMessage: '',
},
],
currentWalletName: '',
currentChainName: '',
});
});
});

describe('State Management', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/store/src/wallet-manager/chain-wallet-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export class ChainWalletStore extends BaseWallet {
}

get walletState(): WalletState {
return this.store.getChainWalletState(this.chain.chainName, this.wallet.info.name)?.walletState || WalletState.NotExist;
return this.store.getChainWalletState(this.wallet.info.name, this.chain.chainName)?.walletState || WalletState.NotExist;
}

get errorMessage(): string {
return this.store.getChainWalletState(this.chain.chainName, this.wallet.info.name)?.errorMessage || '';
return this.store.getChainWalletState(this.wallet.info.name, this.chain.chainName)?.errorMessage || '';
}

async init(): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion packages/store/src/wallet-manager/wallet-manager-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ export class WalletManagerStore implements WalletManager {
const oldState = this.localStorage.load();

this.store.setState({
chainWalletStates: oldState.chainWalletStates || [],
// NotExist only means the extension was not injected yet on the load that saved it.
// Restoring it would block connect() for good, so start that wallet over as Disconnected;
// init() sets NotExist again if the extension is really missing.
chainWalletStates: (oldState.chainWalletStates || []).map(cws => cws.walletState === WalletState.NotExist
? { ...cws, walletState: WalletState.Disconnected, errorMessage: '' }
: cws),
currentWalletName: oldState.currentWalletName || '',
currentChainName: oldState.currentChainName || ''
});
Expand Down
Loading