Skip to content

Commit b966e8d

Browse files
authored
fix: reconnect on rpc error (#80)
1 parent a79ab5f commit b966e8d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/utils/ironfish.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IronfishSdk, RpcClient } from "@ironfish/sdk";
1+
import { IronfishSdk, RpcClient, isRpcResponseError } from "@ironfish/sdk";
22
import { logger } from "./logger";
33

44
type ClientParams = {
@@ -28,8 +28,7 @@ class IronFishClient {
2828
) {
2929
const clientAddress = `${host}:${port}`;
3030
const storedClient = this.clientRegistry.get(clientAddress);
31-
32-
if (storedClient) {
31+
if (storedClient && (await this.connected(storedClient))) {
3332
return storedClient;
3433
}
3534

@@ -62,6 +61,20 @@ class IronFishClient {
6261

6362
return client;
6463
}
64+
65+
async connected(client: RpcClient | undefined): Promise<boolean> {
66+
try {
67+
const response = await client?.node.getStatus();
68+
if (response && !isRpcResponseError(response)) {
69+
return true;
70+
}
71+
} catch (error) {
72+
logger.error(
73+
`Error while getting status from IronFish RPC. Reconnecting...`,
74+
);
75+
}
76+
return false;
77+
}
6578
}
6679

6780
export const ifClient = new IronFishClient();

0 commit comments

Comments
 (0)