diff --git a/src/utils/ironfish.ts b/src/utils/ironfish.ts index 3c49ce5..8ce04ef 100644 --- a/src/utils/ironfish.ts +++ b/src/utils/ironfish.ts @@ -1,4 +1,4 @@ -import { IronfishSdk, RpcClient } from "@ironfish/sdk"; +import { IronfishSdk, RpcClient, isRpcResponseError } from "@ironfish/sdk"; import { logger } from "./logger"; type ClientParams = { @@ -28,8 +28,7 @@ class IronFishClient { ) { const clientAddress = `${host}:${port}`; const storedClient = this.clientRegistry.get(clientAddress); - - if (storedClient) { + if (storedClient && (await this.connected(storedClient))) { return storedClient; } @@ -62,6 +61,20 @@ class IronFishClient { return client; } + + async connected(client: RpcClient | undefined): Promise { + try { + const response = await client?.node.getStatus(); + if (response && !isRpcResponseError(response)) { + return true; + } + } catch (error) { + logger.error( + `Error while getting status from IronFish RPC. Reconnecting...`, + ); + } + return false; + } } export const ifClient = new IronFishClient();