1
1
import * as RD from '@devexperts/remote-data-ts'
2
- import { Client , getChainId , getDefaultChainIds } from '@xchainjs/xchain-cosmos'
2
+ import { Client , getChainId } from '@xchainjs/xchain-cosmos'
3
3
import { CosmosChain } from '@xchainjs/xchain-util'
4
4
import * as FP from 'fp-ts/lib/function'
5
5
import * as O from 'fp-ts/lib/Option'
@@ -23,41 +23,39 @@ import type { Client$, ClientState, ClientState$ } from './types'
23
23
*/
24
24
const clientState$ : ClientState$ = FP . pipe (
25
25
Rx . combineLatest ( [ keystoreService . keystoreState$ , clientNetwork$ , Rx . of ( getClientUrls ( ) ) ] ) ,
26
- RxOp . switchMap ( ( [ keystore , network , clientUrls ] ) : ClientState$ => {
27
- console . log ( 'network:' , network )
28
- return FP . pipe (
29
- // request chain id (for testnet only, cosmoshub.keplr.app does not an endpoint for it)
30
- Rx . of ( network === 'testnet' ? getChainId ( clientUrls [ network ] ) : getDefaultChainIds ( ) [ network ] ) ,
31
- RxOp . switchMap ( ( chainId ) => {
32
- console . log ( 'chainId:' , chainId )
33
- console . log ( 'clientUrls:' , clientUrls [ network ] )
34
- return Rx . of (
35
- FP . pipe (
36
- getPhrase ( keystore ) ,
37
- O . map < string , ClientState > ( ( phrase ) => {
38
- try {
39
- const client = new Client ( {
40
- network,
41
- phrase,
42
- clientUrls : getClientUrls ( ) ,
43
- chainIds : { ...INITIAL_CHAIN_IDS , [ network ] : chainId }
44
- } )
45
- return RD . success ( client )
46
- } catch ( error ) {
47
- return RD . failure < Error > ( isError ( error ) ? error : new Error ( 'Failed to create Cosmos client' ) )
48
- }
49
- } ) ,
50
- // Set back to `initial` if no phrase is available (locked wallet)
51
- O . getOrElse < ClientState > ( ( ) => RD . initial )
26
+ RxOp . switchMap (
27
+ ( [ keystore , network , clientUrls ] ) : ClientState$ =>
28
+ FP . pipe (
29
+ // request chain id whenever network or keystore are changed
30
+ Rx . from ( getChainId ( clientUrls [ network ] ) ) ,
31
+ RxOp . switchMap ( ( chainId ) =>
32
+ Rx . of (
33
+ FP . pipe (
34
+ getPhrase ( keystore ) ,
35
+ O . map < string , ClientState > ( ( phrase ) => {
36
+ try {
37
+ const client = new Client ( {
38
+ network,
39
+ phrase,
40
+ clientUrls : getClientUrls ( ) ,
41
+ chainIds : { ...INITIAL_CHAIN_IDS , [ network ] : chainId }
42
+ } )
43
+ return RD . success ( client )
44
+ } catch ( error ) {
45
+ return RD . failure < Error > ( isError ( error ) ? error : new Error ( 'Failed to create Cosmos client' ) )
46
+ }
47
+ } ) ,
48
+ // Set back to `initial` if no phrase is available (locked wallet)
49
+ O . getOrElse < ClientState > ( ( ) => RD . initial )
50
+ )
52
51
)
53
- )
54
- } ) ,
55
- RxOp . catchError ( ( error ) =>
56
- Rx . of ( RD . failure < Error > ( new Error ( `Failed to get Cosmos' chain id (${ error ?. msg ?? error . toString ( ) } )` ) ) )
57
- ) ,
58
- RxOp . startWith ( RD . pending )
59
- )
60
- } ) ,
52
+ ) ,
53
+ RxOp . catchError ( ( error ) =>
54
+ Rx . of ( RD . failure < Error > ( new Error ( `Failed to get Cosmos' chain id (${ error ?. msg ?? error . toString ( ) } )` ) ) )
55
+ ) ,
56
+ RxOp . startWith ( RD . pending )
57
+ )
58
+ ) ,
61
59
RxOp . startWith ( RD . initial ) ,
62
60
RxOp . shareReplay ( 1 )
63
61
)
0 commit comments