From 659b4549bee03982463faf08cc8f2a6ad0b3e648 Mon Sep 17 00:00:00 2001 From: tommytrg Date: Wed, 30 Oct 2024 10:10:44 +0100 Subject: [PATCH] fix(api): ignore latestPrice result on JSONRPC call error --- .../api/src/web3Middleware/NetworkRouter.ts | 24 ++++++++++++++++--- packages/api/test/networkRouter.spec.ts | 9 +++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/api/src/web3Middleware/NetworkRouter.ts b/packages/api/src/web3Middleware/NetworkRouter.ts index 274bf25a..faaad7af 100644 --- a/packages/api/src/web3Middleware/NetworkRouter.ts +++ b/packages/api/src/web3Middleware/NetworkRouter.ts @@ -41,6 +41,12 @@ export type NetworkSnapshot = { feeds: Array } +// LatestPrice is missing when JSONRPC `isFeedWithPrice` call fails +export type PartialNetworkSnapshot = { + network: string + feeds: Array +} + export class NetworkRouter { private Web3: typeof Web3 public contract: any @@ -85,8 +91,8 @@ export class NetworkRouter { setInterval(async () => { const snapshot = await this.getSnapshot() const insertPromises = snapshot.feeds - .filter((feed) => feed.timestamp !== '0') - .map((feed) => ({ + .filter((feed) => isFeedWithPrice(feed) && feed.timestamp !== '0') + .map((feed: SupportedFeed & LatestPrice) => ({ feedFullName: createFeedFullName( this.network, feed.caption.split('-').reverse()[1], @@ -108,7 +114,7 @@ export class NetworkRouter { }, this.pollingPeriod) } - async getSnapshot(): Promise { + async getSnapshot(): Promise { const supportedFeeds = await this.getSupportedFeeds() const lastSupportedFeedsID = JSON.stringify(supportedFeeds) @@ -192,3 +198,15 @@ export class NetworkRouter { } } } + +// Type guard function to ensure received feed has type SupportedFeed & LatestPrice +function isFeedWithPrice( + feed: SupportedFeed | (SupportedFeed & LatestPrice), +): feed is SupportedFeed & LatestPrice { + return ( + 'value' in feed && + 'timestamp' in feed && + 'tallyHash' in feed && + 'status' in feed + ) +} diff --git a/packages/api/test/networkRouter.spec.ts b/packages/api/test/networkRouter.spec.ts index a8c17b47..788e239d 100644 --- a/packages/api/test/networkRouter.spec.ts +++ b/packages/api/test/networkRouter.spec.ts @@ -1,5 +1,9 @@ import { Network, Repositories, RouterDataFeedsConfig } from '../types' -import { NetworkInfo, NetworkRouter } from '../src/web3Middleware/NetworkRouter' +import { + NetworkInfo, + NetworkRouter, + NetworkSnapshot, +} from '../src/web3Middleware/NetworkRouter' import { Configuration } from '../src/web3Middleware/Configuration' import dataFeedsRouter from './web3Middleware/dataFeedsRouter.json' // FIXME: create a proper mock for web3 @@ -30,7 +34,8 @@ describe('NetworkRouter', () => { repositories, networkInfo, ) - const snapshot = await router.getSnapshot() + const snapshot: NetworkSnapshot = + (await router.getSnapshot()) as NetworkSnapshot expect(snapshot.feeds[0].caption).toBeTruthy() expect(snapshot.feeds[0].id).toBeTruthy()