From efc657b790f93232dad6c765650a0a7dca69936d Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 11 Jul 2023 01:50:42 -0400 Subject: [PATCH] refactor: move optimistic_update fetch logic to utility function --- src/node/client.ts | 13 +++---------- src/util.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/node/client.ts b/src/node/client.ts index 0b9605c..d88d4c7 100644 --- a/src/node/client.ts +++ b/src/node/client.ts @@ -6,6 +6,7 @@ import { Bytes32, capella, LightClientUpdate } from "#types.js"; import { consensusClient, deserializePubkeys, + getConsensusOptimisticUpdate, optimisticUpdateFromJSON, optimisticUpdateVerify, } from "#util.js"; @@ -87,16 +88,8 @@ export default class Client extends BaseClient { } protected async getLatestExecution(): Promise { - const resp = await axios.get( - `/eth/v1/beacon/light_client/optimistic_update`, - ); - - const updateJSON = resp.data; - - if (!updateJSON) { - throw Error(`fetching optimistic update failed`); - } - const update = optimisticUpdateFromJSON(updateJSON.data); + const updateJSON = await getConsensusOptimisticUpdate(); + const update = optimisticUpdateFromJSON(updateJSON); const verify = await optimisticUpdateVerify( this.latestCommittee as Uint8Array[], update, diff --git a/src/util.ts b/src/util.ts index e8947f4..5e6e72d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -77,3 +77,15 @@ export function getCommitteeHash(committee: Uint8Array[]): Uint8Array { export const consensusClient = axios.create(); axiosRetry(consensusClient, { retries: 3 }); + +export async function getConsensusOptimisticUpdate() { + const resp = await consensusClient.get( + `/eth/v1/beacon/light_client/optimistic_update`, + ); + + const updateJSON = resp.data; + + if (!updateJSON) { + throw Error(`fetching optimistic update failed`); + } +}