Skip to content

Commit d98e7e6

Browse files
committed
Add rpc client local setup to enable error mgmt
1 parent f089ca5 commit d98e7e6

File tree

4 files changed

+2053
-2837
lines changed

4 files changed

+2053
-2837
lines changed

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,23 @@
4040
"postdefine-proto": "prettier --write \"src/codec/**/*.ts\""
4141
},
4242
"dependencies": {
43-
"@cosmjs/amino": "0.29.5",
44-
"@cosmjs/crypto": "0.29.5",
45-
"@cosmjs/encoding": "0.29.5",
46-
"@cosmjs/math": "0.29.5",
47-
"@cosmjs/proto-signing": "0.29.5",
48-
"@cosmjs/stargate": "0.29.5",
49-
"@cosmjs/tendermint-rpc": "0.29.5",
50-
"@cosmjs/utils": "0.29.5",
43+
"@cosmjs/amino": "0.30.1",
44+
"@cosmjs/crypto": "0.30.1",
45+
"@cosmjs/encoding": "0.30.1",
46+
"@cosmjs/json-rpc": "^0.30.1",
47+
"@cosmjs/math": "0.30.1",
48+
"@cosmjs/proto-signing": "0.30.1",
49+
"@cosmjs/stargate": "0.30.1",
50+
"@cosmjs/tendermint-rpc": "0.30.1",
51+
"@cosmjs/utils": "0.30.1",
5152
"@ledgerhq/hw-app-cosmos": "^6.27.10",
5253
"@ledgerhq/hw-transport": "^6.27.10",
5354
"@types/crypto-js": "^4.1.1",
5455
"@types/ledgerhq__hw-transport": "^4.21.4",
5556
"@types/uuid": "^9.0.0",
5657
"crypto-browserify": "^3.12.0",
5758
"crypto-js": "^4.1.1",
58-
"long": "^4.0.0",
59+
"long": "^5.2.3",
5960
"uuid": "^9.0.0"
6061
},
6162
"optionalDependencies": {},

src/client/LumClient.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Tendermint34Client, StatusResponse } from '@cosmjs/tendermint-rpc';
1+
import { Tendermint34Client, StatusResponse, RpcClient, HttpClient, WebsocketClient, Method as RpcMethod } from '@cosmjs/tendermint-rpc';
22
import { QueryClient as StargateQueryClient } from '@cosmjs/stargate';
3+
import { JsonRpcRequest } from '@cosmjs/json-rpc';
34

45
import { LumWallet, LumUtils, LumTypes } from '..';
56
import {
@@ -32,7 +33,12 @@ import { setupSlashingExtension, SlashingExtension } from '../extensions/slashin
3233
import { AuthzExtension, setupAuthzExtension } from '../extensions/authz';
3334
import { FeegrantExtension, setupFeegrantExtension } from '../extensions/feegrant';
3435

36+
function defaultErrorHandler(error: unknown): never {
37+
throw error;
38+
}
39+
3540
export class LumClient {
41+
readonly rpcClient: RpcClient;
3642
readonly tmClient: Tendermint34Client;
3743
readonly queryClient: StargateQueryClient &
3844
AuthExtension &
@@ -93,26 +99,42 @@ export class LumClient {
9399
// return res;
94100
// };
95101
}
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+
}
96126

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);
115134
}
135+
await this.detectVersion(rpcClient);
136+
const tmClient = await Tendermint34Client.create(rpcClient);
137+
return new LumClient(tmClient);
116138
};
117139

118140
/**

src/registry/aminoTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const createDefaultAminoTypes = (): AminoConverters => {
8282
...createAuthzAminoConverters(),
8383
...createDistributionAminoConverters(),
8484
...createGovAminoConverters(),
85-
...createStakingAminoConverters('lum'),
85+
...createStakingAminoConverters(),
8686
...createIbcAminoConverters(),
8787
};
8888
};

0 commit comments

Comments
 (0)