Skip to content

Commit c91d709

Browse files
[LUM-473] Add ability to catch errors on websocket implementation (#50)
* Add onError callback to connect function * Add rpc client local setup to enable error mgmt * Remove unused field * Fix handler types * Add gov msg vote v1 * Bump to v0.8.3 * Update proto * Register proposal register pool * Bump to v0.8.4 --------- Co-authored-by: Fabrice Bascoulergue <[email protected]>
1 parent f626ee9 commit c91d709

File tree

111 files changed

+27791
-25445
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+27791
-25445
lines changed

jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ module.exports = {
1414
},
1515
},
1616
testTimeout: 240000,
17+
moduleNameMapper: {
18+
'@ledgerhq/devices/hid-framing': '@ledgerhq/devices/lib/hid-framing',
19+
},
1720
};

package.json

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lum-network/sdk-javascript",
3-
"version": "0.8.2",
3+
"version": "0.8.4",
44
"license": "Apache-2.0",
55
"description": "Javascript SDK library for NodeJS and Web browsers to interact with the Lum Network.",
66
"homepage": "https://github.com/lum-network/sdk-javascript#readme",
@@ -35,21 +35,22 @@
3535
"format": "prettier --write '**/*.{js,jsx,ts,tsx,css,json,md,html,yml}'",
3636
"bump": "npm version",
3737
"preget-proto": "rm -rf proto",
38-
"get-proto": "COSMOS_REF=v0.46.7 IBC_REF=v5.0.1 LUM_REF=v1.3.1 sh ./scripts/get-proto.sh",
38+
"get-proto": "COSMOS_REF=v0.46.7 IBC_REF=v5.0.1 LUM_REF=v1.4.1 sh ./scripts/get-proto.sh",
3939
"define-proto": "sh ./scripts/define-proto.sh",
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",
51-
"@ledgerhq/hw-app-cosmos": "^6.27.10",
52-
"@ledgerhq/hw-transport": "^6.27.10",
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",
52+
"@ledgerhq/hw-app-cosmos": "^6.28.1",
53+
"@ledgerhq/hw-transport": "^6.28.4",
5354
"@types/crypto-js": "^4.1.1",
5455
"@types/ledgerhq__hw-transport": "^4.21.4",
5556
"@types/uuid": "^9.0.0",

scripts/define-proto.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ protoc \
106106
"$LUM_PROTO_DIR/lum-network/dfract/query.proto" \
107107
"$LUM_PROTO_DIR/lum-network/dfract/tx.proto" \
108108
"$LUM_PROTO_DIR/lum-network/dfract/params.proto" \
109-
"$LUM_PROTO_DIR/lum-network/dfract/proposal.proto"
109+
"$LUM_PROTO_DIR/lum-network/dfract/proposal.proto" \
110+
"$LUM_PROTO_DIR/lum-network/millions/gov.proto"
110111

111112
# Remove unnecessary codec files
112113
rm -rf \

src/client/LumClient.ts

+40-10
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,6 +33,10 @@ 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): void {
37+
throw error;
38+
}
39+
3540
export class LumClient {
3641
readonly tmClient: Tendermint34Client;
3742
readonly queryClient: StargateQueryClient &
@@ -93,16 +98,41 @@ export class LumClient {
9398
// return res;
9499
// };
95100
}
101+
static async detectVersion(client: RpcClient): Promise<string> {
102+
const numbersWithoutZero = '123456789';
103+
const req: JsonRpcRequest = {
104+
jsonrpc: '2.0',
105+
id: parseInt(
106+
Array.from({ length: 12 })
107+
.map(() => numbersWithoutZero[Math.floor(Math.random() * numbersWithoutZero.length)])
108+
.join(''),
109+
10,
110+
),
111+
method: RpcMethod.Status,
112+
params: {},
113+
};
114+
const response = await client.execute(req);
115+
const result = response.result;
116+
if (!result || !result.node_info) {
117+
throw new Error('Unrecognized format for status response');
118+
}
119+
const version = result.node_info.version;
120+
if (typeof version !== 'string') {
121+
throw new Error('Unrecognized version format: must be string');
122+
}
123+
return version;
124+
}
96125

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-
*/
103-
static connect = async (endpoint: string): Promise<LumClient> => {
104-
const tmClient = await Tendermint34Client.connect(endpoint);
105-
126+
static connect = async (endpoint: string, onWebsocketError = defaultErrorHandler): Promise<LumClient> => {
127+
let rpcClient;
128+
if (typeof endpoint === 'object') {
129+
rpcClient = new HttpClient(endpoint);
130+
} else {
131+
const useHttp = endpoint.startsWith('http://') || endpoint.startsWith('https://');
132+
rpcClient = useHttp ? new HttpClient(endpoint) : new WebsocketClient(endpoint, onWebsocketError);
133+
}
134+
await this.detectVersion(rpcClient);
135+
const tmClient = await Tendermint34Client.create(rpcClient);
106136
return new LumClient(tmClient);
107137
};
108138

0 commit comments

Comments
 (0)