Skip to content

Commit

Permalink
[LUM-473] Add ability to catch errors on websocket implementation (#50)
Browse files Browse the repository at this point in the history
* 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 <lebascou@users.noreply.github.com>
  • Loading branch information
ThibaultJRD and lebascou authored Jun 2, 2023
1 parent f626ee9 commit c91d709
Show file tree
Hide file tree
Showing 111 changed files with 27,791 additions and 25,445 deletions.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ module.exports = {
},
},
testTimeout: 240000,
moduleNameMapper: {
'@ledgerhq/devices/hid-framing': '@ledgerhq/devices/lib/hid-framing',
},
};
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lum-network/sdk-javascript",
"version": "0.8.2",
"version": "0.8.4",
"license": "Apache-2.0",
"description": "Javascript SDK library for NodeJS and Web browsers to interact with the Lum Network.",
"homepage": "https://github.com/lum-network/sdk-javascript#readme",
Expand Down Expand Up @@ -35,21 +35,22 @@
"format": "prettier --write '**/*.{js,jsx,ts,tsx,css,json,md,html,yml}'",
"bump": "npm version",
"preget-proto": "rm -rf proto",
"get-proto": "COSMOS_REF=v0.46.7 IBC_REF=v5.0.1 LUM_REF=v1.3.1 sh ./scripts/get-proto.sh",
"get-proto": "COSMOS_REF=v0.46.7 IBC_REF=v5.0.1 LUM_REF=v1.4.1 sh ./scripts/get-proto.sh",
"define-proto": "sh ./scripts/define-proto.sh",
"postdefine-proto": "prettier --write \"src/codec/**/*.ts\""
},
"dependencies": {
"@cosmjs/amino": "0.29.5",
"@cosmjs/crypto": "0.29.5",
"@cosmjs/encoding": "0.29.5",
"@cosmjs/math": "0.29.5",
"@cosmjs/proto-signing": "0.29.5",
"@cosmjs/stargate": "0.29.5",
"@cosmjs/tendermint-rpc": "0.29.5",
"@cosmjs/utils": "0.29.5",
"@ledgerhq/hw-app-cosmos": "^6.27.10",
"@ledgerhq/hw-transport": "^6.27.10",
"@cosmjs/amino": "0.30.1",
"@cosmjs/crypto": "0.30.1",
"@cosmjs/encoding": "0.30.1",
"@cosmjs/json-rpc": "^0.30.1",
"@cosmjs/math": "0.30.1",
"@cosmjs/proto-signing": "0.30.1",
"@cosmjs/stargate": "0.30.1",
"@cosmjs/tendermint-rpc": "0.30.1",
"@cosmjs/utils": "0.30.1",
"@ledgerhq/hw-app-cosmos": "^6.28.1",
"@ledgerhq/hw-transport": "^6.28.4",
"@types/crypto-js": "^4.1.1",
"@types/ledgerhq__hw-transport": "^4.21.4",
"@types/uuid": "^9.0.0",
Expand Down
3 changes: 2 additions & 1 deletion scripts/define-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ protoc \
"$LUM_PROTO_DIR/lum-network/dfract/query.proto" \
"$LUM_PROTO_DIR/lum-network/dfract/tx.proto" \
"$LUM_PROTO_DIR/lum-network/dfract/params.proto" \
"$LUM_PROTO_DIR/lum-network/dfract/proposal.proto"
"$LUM_PROTO_DIR/lum-network/dfract/proposal.proto" \
"$LUM_PROTO_DIR/lum-network/millions/gov.proto"

# Remove unnecessary codec files
rm -rf \
Expand Down
50 changes: 40 additions & 10 deletions src/client/LumClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Tendermint34Client, StatusResponse } from '@cosmjs/tendermint-rpc';
import { Tendermint34Client, StatusResponse, RpcClient, HttpClient, WebsocketClient, Method as RpcMethod } from '@cosmjs/tendermint-rpc';
import { QueryClient as StargateQueryClient } from '@cosmjs/stargate';
import { JsonRpcRequest } from '@cosmjs/json-rpc';

import { LumWallet, LumUtils, LumTypes } from '..';
import {
Expand Down Expand Up @@ -32,6 +33,10 @@ import { setupSlashingExtension, SlashingExtension } from '../extensions/slashin
import { AuthzExtension, setupAuthzExtension } from '../extensions/authz';
import { FeegrantExtension, setupFeegrantExtension } from '../extensions/feegrant';

function defaultErrorHandler(error: unknown): void {
throw error;
}

export class LumClient {
readonly tmClient: Tendermint34Client;
readonly queryClient: StargateQueryClient &
Expand Down Expand Up @@ -93,16 +98,41 @@ export class LumClient {
// return res;
// };
}
static async detectVersion(client: RpcClient): Promise<string> {
const numbersWithoutZero = '123456789';
const req: JsonRpcRequest = {
jsonrpc: '2.0',
id: parseInt(
Array.from({ length: 12 })
.map(() => numbersWithoutZero[Math.floor(Math.random() * numbersWithoutZero.length)])
.join(''),
10,
),
method: RpcMethod.Status,
params: {},
};
const response = await client.execute(req);
const result = response.result;
if (!result || !result.node_info) {
throw new Error('Unrecognized format for status response');
}
const version = result.node_info.version;
if (typeof version !== 'string') {
throw new Error('Unrecognized version format: must be string');
}
return version;
}

/**
* Creates a new LumClient for the given endpoint
* Uses HTTP when the URL schema is http or https, uses WebSockets otherwise
*
* @param endpoint Blockchain node RPC url
*/
static connect = async (endpoint: string): Promise<LumClient> => {
const tmClient = await Tendermint34Client.connect(endpoint);

static connect = async (endpoint: string, onWebsocketError = defaultErrorHandler): Promise<LumClient> => {
let rpcClient;
if (typeof endpoint === 'object') {
rpcClient = new HttpClient(endpoint);
} else {
const useHttp = endpoint.startsWith('http://') || endpoint.startsWith('https://');
rpcClient = useHttp ? new HttpClient(endpoint) : new WebsocketClient(endpoint, onWebsocketError);
}
await this.detectVersion(rpcClient);
const tmClient = await Tendermint34Client.create(rpcClient);
return new LumClient(tmClient);
};

Expand Down
Loading

0 comments on commit c91d709

Please sign in to comment.