@@ -2,7 +2,7 @@ import { WalletManager } from '../src/wallet-manager';
22import { Chain , AssetList } from '@chain-registry/v2-types' ;
33import { BaseWallet } from '../src/wallets/base-wallet' ;
44import { SignerOptions , EndpointOptions , SignType } from '../src/types' ;
5- import { WalletNotExist , ChainNameNotExist , NoValidRpcEndpointFound } from '../src/utils' ;
5+ import { WalletNotExist , ChainNameNotExist , NoValidRpcEndpointFound , getValidRpcEndpoint } from '../src/utils' ;
66
77import { createMockAssetList } from './helpers/mock-asset-list-factory' ;
88import { createMockChain } from './helpers/mock-chain-factory' ;
@@ -11,6 +11,13 @@ import { AminoGenericOfflineSigner } from '@interchainjs/cosmos/types/wallet';
1111import { SigningClient } from '@interchainjs/cosmos/signing-client' ;
1212import { createSignerOption } from './helpers/mock-setting-factory' ;
1313
14+ jest . mock ( '../src/utils/endpoint.ts' , ( ) => {
15+ return {
16+ ...jest . requireActual ( '../src/utils/endpoint.ts' ) ,
17+ getValidRpcEndpoint : jest . fn ( )
18+ } ;
19+ } ) ;
20+
1421describe ( 'WalletManager' , ( ) => {
1522 ( global as any ) . window = {
1623
@@ -22,6 +29,7 @@ describe('WalletManager', () => {
2229 } ;
2330
2431
32+
2533 let chains : Chain [ ] ;
2634 let assetLists : AssetList [ ] ;
2735 let wallets : BaseWallet [ ] ;
@@ -36,7 +44,11 @@ describe('WalletManager', () => {
3644 let assetList1 : AssetList
3745 let assetList2 : AssetList
3846
47+
48+
3949 beforeEach ( ( ) => {
50+
51+
4052 chain1 = createMockChain ( { chainName : 'chain-1' , chainId : '1' , rpcEndpoint : [ { address : 'http://localhost:26657' } ] , chainType : 'cosmos' } ) ;
4153 chain2 = createMockChain ( { chainName : 'chain-2' , chainId : '2' , rpcEndpoint : [ { address : 'http://localhost:26658' } ] , chainType : 'cosmos' } ) ;
4254 assetList1 = createMockAssetList ( {
@@ -91,6 +103,8 @@ describe('WalletManager', () => {
91103
92104 walletManager = new WalletManager ( chains , assetLists , wallets , signerOptions , endpointOptions ) ;
93105
106+
107+ ( getValidRpcEndpoint as jest . Mock ) . mockClear ( )
94108 } ) ;
95109
96110 it ( 'should set assetLists for wallets, after walletManager constructor' , ( ) => {
@@ -103,6 +117,15 @@ describe('WalletManager', () => {
103117 expect ( wallet2 . chainMap . get ( chain1 . chainId ) ) . toEqual ( chain1 ) ;
104118 } ) ;
105119
120+ it ( 'should have right signerOptions and endpointOptions for each chain' , ( ) => {
121+ expect ( walletManager . signerOptionMap [ chain1 . chainName ] ) . toEqual ( signerOptions . signing ?.( chain1 . chainName ) ) ;
122+ expect ( walletManager . signerOptionMap [ chain2 . chainName ] ) . toEqual ( signerOptions . signing ?.( chain2 . chainName ) ) ;
123+ expect ( walletManager . endpointOptionsMap [ chain1 . chainName ] ) . toEqual ( endpointOptions . endpoints ?. [ chain1 . chainName ] ) ;
124+ expect ( walletManager . endpointOptionsMap [ chain2 . chainName ] ) . toEqual ( undefined ) ;
125+ expect ( walletManager . preferredSignTypeMap [ chain1 . chainName ] ) . toEqual ( 'amino' ) ;
126+ expect ( walletManager . preferredSignTypeMap [ chain2 . chainName ] ) . toEqual ( 'direct' ) ;
127+ } ) ;
128+
106129 it ( 'should initialize wallets' , async ( ) => {
107130 wallet1 . init = jest . fn ( ) ;
108131 wallet2 . init = jest . fn ( ) ;
@@ -116,10 +139,10 @@ describe('WalletManager', () => {
116139 expect ( wm ) . toBeInstanceOf ( WalletManager ) ;
117140 } ) ;
118141
119- it ( 'should add new chains' , ( ) => {
142+ it ( 'should add new chains' , async ( ) => {
120143 const newChain = createMockChain ( { chainName : 'chainToAdd' , chainId : '3' , rpcEndpoint : [ { address : 'http://localhost:26659' } ] , chainType : 'cosmos' } ) ;
121144 const newAssetList = createMockAssetList ( { chainName : 'chainToAdd' , assets : [ ] } ) ;
122- walletManager . addChains ( [ newChain ] , [ newAssetList ] , signerOptions , endpointOptions ) ;
145+ await walletManager . addChains ( [ newChain ] , [ newAssetList ] , signerOptions , endpointOptions ) ;
123146 expect ( walletManager . chains ) . toContain ( newChain ) ;
124147 expect ( walletManager . assetLists ) . toContain ( newAssetList ) ;
125148 expect ( walletManager . signerOptionMap [ newChain . chainName ] ) . toEqual ( signerOptions . signing ?.( newChain . chainName ) ) ;
@@ -185,6 +208,7 @@ describe('WalletManager', () => {
185208 } ) ;
186209
187210 it ( 'should throw error if no valid RPC endpoint found' , async ( ) => {
211+ ( getValidRpcEndpoint as jest . Mock ) . mockReturnValue ( Promise . resolve ( '' ) ) ;
188212 if ( walletManager . endpointOptions ?. endpoints ?. [ 'chain-1' ] ) {
189213 walletManager . endpointOptions . endpoints [ 'chain-1' ] . rpc = [ ] ;
190214 }
@@ -253,14 +277,80 @@ describe('WalletManager', () => {
253277 expect ( chain ) . toBeUndefined ( ) ;
254278 } ) ;
255279
256- it ( 'new chains should be added into chainMaps of wallet, after addChains' , ( ) => {
280+ it ( 'new chains should be added into chainMaps of wallet, after addChains' , async ( ) => {
257281 const newChain = createMockChain ( { chainName : 'chainToAdd' , chainId : '3' , rpcEndpoint : [ { address : 'http://localhost:26659' } ] , chainType : 'cosmos' } ) ;
258282 const newAssetList = createMockAssetList ( { chainName : 'chainToAdd' , assets : [ ] } ) ;
259- walletManager . addChains ( [ newChain ] , [ newAssetList ] , signerOptions , endpointOptions ) ;
283+ await walletManager . addChains ( [ newChain ] , [ newAssetList ] , signerOptions , endpointOptions ) ;
260284 expect ( wallet1 . chainMap . get ( newChain . chainId ) ) . toEqual ( newChain ) ;
261285 expect ( wallet2 . chainMap . get ( newChain . chainId ) ) . toEqual ( newChain ) ;
262286 } )
263287
288+ it ( 'addChains should update right signerOptions for new chains' , async ( ) => {
289+ const newChain = createMockChain ( { chainName : 'chainToAdd' , chainId : '3' , rpcEndpoint : [ { address : 'http://localhost:26659' } ] , chainType : 'cosmos' } ) ;
290+ const newAssetList = createMockAssetList ( { chainName : 'chainToAdd' , assets : [ ] } ) ;
291+
292+ const endpointOptions1 = {
293+ endpoints : {
294+ 'chainToAdd' : {
295+ rpc : [ 'http://localhost:26679' ]
296+ }
297+ }
298+ } as EndpointOptions
299+
300+ const signerOptions1 = {
301+ signing : jest . fn ( ) . mockImplementation ( ( chainName : string ) => {
302+ return {
303+ 'chainToAdd' : {
304+ gasPrice : '1000uatom' ,
305+ }
306+ } [ chainName ]
307+ } )
308+ } as SignerOptions
309+
310+
311+ await walletManager . addChains ( [ newChain ] , [ newAssetList ] , signerOptions1 , endpointOptions1 ) ;
312+ expect ( walletManager . chains ) . toContain ( newChain ) ;
313+ expect ( walletManager . assetLists ) . toContain ( newAssetList ) ;
314+ expect ( walletManager . signerOptionMap [ newChain . chainName ] ) . toEqual ( { gasPrice : '1000uatom' } ) ;
315+ expect ( walletManager . endpointOptionsMap [ newChain . chainName ] ) . toEqual ( { rpc : [ 'http://localhost:26679' ] } ) ;
316+
317+ const endpointOptions2 = {
318+ endpoints : {
319+ 'chainToAdd' : {
320+ rpc : [ 'http://localhost:26659' ]
321+ }
322+ }
323+ } as EndpointOptions
324+
325+ const signerOptions2 = {
326+ signing : jest . fn ( ) . mockImplementation ( ( chainName : string ) => {
327+ return {
328+ 'chainToAdd' : {
329+ gasPrice : '2000uatom' ,
330+ }
331+ } [ chainName ]
332+ } )
333+ } as SignerOptions
334+
335+
336+ await walletManager . addChains ( [ newChain ] , [ newAssetList ] , signerOptions2 , endpointOptions2 ) ;
337+ expect ( walletManager . signerOptionMap [ newChain . chainName ] ) . toEqual ( { gasPrice : '2000uatom' } ) ;
338+ expect ( walletManager . endpointOptionsMap [ newChain . chainName ] ) . toEqual ( { rpc : [ 'http://localhost:26659' ] } ) ;
339+ } )
340+
341+ it ( 'addChains should update endpoint from chain rpc list' , async ( ) => {
342+ ( getValidRpcEndpoint as jest . Mock ) . mockReturnValue ( 'https://rpc.example.com' ) ;
343+
344+ const newChain = createMockChain ( { chainName : 'chainToAdd' , chainId : '3' , rpcEndpoint : [ { address : 'https://rpc.example.com' } ] , chainType : 'cosmos' } ) ;
345+ const newAssetList = createMockAssetList ( { chainName : 'chainToAdd' , assets : [ ] } ) ;
346+
347+ await walletManager . addChains ( [ newChain ] , [ newAssetList ] ) ;
348+ expect ( walletManager . chains ) . toContain ( newChain ) ;
349+ expect ( walletManager . assetLists ) . toContain ( newAssetList ) ;
350+ expect ( walletManager . endpointOptionsMap [ newChain . chainName ] ) . toEqual ( { rpc : [ 'https://rpc.example.com' ] } ) ;
351+
352+ } )
353+
264354} ) ;
265355
266356
0 commit comments