|
1 | | -import { Tendermint34Client, StatusResponse } from '@cosmjs/tendermint-rpc'; |
| 1 | +import { Tendermint34Client, StatusResponse, RpcClient, HttpClient, WebsocketClient, Method as RpcMethod } from '@cosmjs/tendermint-rpc'; |
2 | 2 | import { QueryClient as StargateQueryClient } from '@cosmjs/stargate'; |
| 3 | +import { JsonRpcRequest } from '@cosmjs/json-rpc'; |
3 | 4 |
|
4 | 5 | import { LumWallet, LumUtils, LumTypes } from '..'; |
5 | 6 | import { |
@@ -32,7 +33,12 @@ import { setupSlashingExtension, SlashingExtension } from '../extensions/slashin |
32 | 33 | import { AuthzExtension, setupAuthzExtension } from '../extensions/authz'; |
33 | 34 | import { FeegrantExtension, setupFeegrantExtension } from '../extensions/feegrant'; |
34 | 35 |
|
| 36 | +function defaultErrorHandler(error: unknown): never { |
| 37 | + throw error; |
| 38 | +} |
| 39 | + |
35 | 40 | export class LumClient { |
| 41 | + readonly rpcClient: RpcClient; |
36 | 42 | readonly tmClient: Tendermint34Client; |
37 | 43 | readonly queryClient: StargateQueryClient & |
38 | 44 | AuthExtension & |
@@ -93,26 +99,42 @@ export class LumClient { |
93 | 99 | // return res; |
94 | 100 | // }; |
95 | 101 | } |
| 102 | + static async detectVersion(client: RpcClient): Promise<string> { |
| 103 | + const numbersWithoutZero = '123456789'; |
| 104 | + const req: JsonRpcRequest = { |
| 105 | + jsonrpc: '2.0', |
| 106 | + id: parseInt( |
| 107 | + Array.from({ length: 12 }) |
| 108 | + .map(() => numbersWithoutZero[Math.floor(Math.random() * numbersWithoutZero.length)]) |
| 109 | + .join(''), |
| 110 | + 10, |
| 111 | + ), |
| 112 | + method: RpcMethod.Status, |
| 113 | + params: {}, |
| 114 | + }; |
| 115 | + const response = await client.execute(req); |
| 116 | + const result = response.result; |
| 117 | + if (!result || !result.node_info) { |
| 118 | + throw new Error('Unrecognized format for status response'); |
| 119 | + } |
| 120 | + const version = result.node_info.version; |
| 121 | + if (typeof version !== 'string') { |
| 122 | + throw new Error('Unrecognized version format: must be string'); |
| 123 | + } |
| 124 | + return version; |
| 125 | + } |
96 | 126 |
|
97 | | - /** |
98 | | - * Creates a new LumClient for the given endpoint |
99 | | - * Uses HTTP when the URL schema is http or https, uses WebSockets otherwise |
100 | | - * |
101 | | - * @param endpoint Blockchain node RPC url |
102 | | - * @param onError Callback for errors |
103 | | - */ |
104 | | - static connect = async (endpoint: string, onError?: (e: unknown) => void): Promise<LumClient> => { |
105 | | - try { |
106 | | - const tmClient = await Tendermint34Client.connect(endpoint); |
107 | | - |
108 | | - return new LumClient(tmClient); |
109 | | - } catch (e) { |
110 | | - if (onError) { |
111 | | - onError(e); |
112 | | - } |
113 | | - |
114 | | - throw e; |
| 127 | + static connect = async (endpoint: string, onWebsocketError = defaultErrorHandler): Promise<LumClient> => { |
| 128 | + let rpcClient; |
| 129 | + if (typeof endpoint === 'object') { |
| 130 | + rpcClient = new HttpClient(endpoint); |
| 131 | + } else { |
| 132 | + const useHttp = endpoint.startsWith('http://') || endpoint.startsWith('https://'); |
| 133 | + rpcClient = useHttp ? new HttpClient(endpoint) : new WebsocketClient(endpoint, onWebsocketError); |
115 | 134 | } |
| 135 | + await this.detectVersion(rpcClient); |
| 136 | + const tmClient = await Tendermint34Client.create(rpcClient); |
| 137 | + return new LumClient(tmClient); |
116 | 138 | }; |
117 | 139 |
|
118 | 140 | /** |
|
0 commit comments