Skip to content

Commit 8b05e40

Browse files
fix add suggest chain issue and add unit test
1 parent c9f1ac1 commit 8b05e40

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

packages/core/__tests__/wallet-manager.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ describe('WalletManager', () => {
9393

9494
});
9595

96+
it('should set assetLists for wallets, after walletManager constructor', () => {
97+
expect(wallet1.assetLists).toStrictEqual([assetList1, assetList2]);
98+
expect(wallet2.assetLists).toStrictEqual([assetList1, assetList2]);
99+
})
100+
101+
it('should set chainMap for wallets, after walletManager constructor', () => {
102+
expect(wallet1.chainMap.get(chain1.chainId)).toEqual(chain1);
103+
expect(wallet2.chainMap.get(chain1.chainId)).toEqual(chain1);
104+
});
105+
96106
it('should initialize wallets', async () => {
97107
wallet1.init = jest.fn();
98108
wallet2.init = jest.fn();

packages/core/__tests__/wallets/cosmos-wallet.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('CosmosWallet', () => {
5858
expect(mockClient.enable).toHaveBeenCalledWith('cosmoshub-4');
5959
});
6060

61-
it('should suggest a chain if connection fails with a specific error', async () => {
61+
it('should suggest a chain if connection fails with not "Request rejected"', async () => {
6262
const mockClient = {
6363
enable: jest.fn().mockRejectedValue(new Error('some error')),
6464
experimentalSuggestChain: jest.fn(),
@@ -67,10 +67,23 @@ describe('CosmosWallet', () => {
6767

6868
(chainRegistryChainToKeplr as jest.Mock).mockReturnValue({ chainId: 'cosmoshub-4' });
6969

70-
await expect(wallet.connect('cosmoshub-4')).rejects.toThrow('some error');
70+
await wallet.connect('cosmoshub-4');
71+
7172
expect(mockClient.experimentalSuggestChain).toHaveBeenCalledWith({ chainId: 'cosmoshub-4' });
7273
});
7374

75+
it('should not suggest a chain if connection fails with "Request rejected"', async () => {
76+
const mockClient = {
77+
enable: jest.fn().mockRejectedValue(new Error('Request rejected')),
78+
experimentalSuggestChain: jest.fn(),
79+
};
80+
wallet.client = mockClient;
81+
82+
await expect(wallet.connect('cosmoshub-4')).rejects.toThrow('Request rejected');
83+
84+
expect(mockClient.experimentalSuggestChain).not.toHaveBeenCalled();
85+
})
86+
7487
it('should disconnect from a chain', async () => {
7588
const mockClient = { disable: jest.fn() };
7689
wallet.client = mockClient;
@@ -116,4 +129,14 @@ describe('CosmosWallet', () => {
116129
expect(chainRegistryChainToKeplr).toHaveBeenCalledWith(cosmosChain, [cosmosAssetList]);
117130
expect(mockClient.experimentalSuggestChain).toHaveBeenCalledWith({ chainId: 'cosmoshub-4' });
118131
});
132+
133+
it('should throw an error if adding a suggested chain fails', async () => {
134+
const mockClient = { experimentalSuggestChain: jest.fn().mockRejectedValue(new Error('suggestion error')) };
135+
wallet.client = mockClient;
136+
wallet.chainMap.set('cosmoshub-4', cosmosChain);
137+
138+
(chainRegistryChainToKeplr as jest.Mock).mockReturnValue({ chainId: 'cosmoshub-4' });
139+
140+
await expect(wallet.addSuggestChain('cosmoshub-4')).rejects.toThrow('suggestion error');
141+
});
119142
});

packages/core/src/wallet-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class WalletManager {
4242

4343
this.wallets.forEach(wallet => {
4444
wallet.setChainMap(this.chains)
45+
wallet.setAssetLists(this.assetLists)
4546
})
4647
}
4748

packages/core/src/wallets/cosmos-wallet.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class CosmosWallet extends BaseWallet {
3535
} catch (error) {
3636
if ((error as any).message !== `Request rejected`) {
3737
await this.addSuggestChain(chainId)
38+
return
3839
}
3940
throw error
4041
}
@@ -87,7 +88,12 @@ export class CosmosWallet extends BaseWallet {
8788
async addSuggestChain(chainId: string): Promise<void> {
8889
const chain = this.chainMap.get(chainId)
8990
const chainInfo = chainRegistryChainToKeplr(chain, this.assetLists)
90-
return this.client.experimentalSuggestChain(chainInfo);
91+
try {
92+
await this.client.experimentalSuggestChain(chainInfo)
93+
} catch (error) {
94+
console.log('add suggest chain error', error)
95+
throw error
96+
}
9197
}
9298
async getProvider() {
9399
return this.client

0 commit comments

Comments
 (0)