Skip to content

Commit

Permalink
fix(api): ignore latestPrice result on JSONRPC call error
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg committed Oct 30, 2024
1 parent bcf0e40 commit 659b454
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
24 changes: 21 additions & 3 deletions packages/api/src/web3Middleware/NetworkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export type NetworkSnapshot = {
feeds: Array<SupportedFeed & LatestPrice>
}

// LatestPrice is missing when JSONRPC `isFeedWithPrice` call fails
export type PartialNetworkSnapshot = {
network: string
feeds: Array<SupportedFeed>
}

export class NetworkRouter {
private Web3: typeof Web3
public contract: any
Expand Down Expand Up @@ -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],
Expand All @@ -108,7 +114,7 @@ export class NetworkRouter {
}, this.pollingPeriod)
}

async getSnapshot(): Promise<NetworkSnapshot> {
async getSnapshot(): Promise<NetworkSnapshot | PartialNetworkSnapshot> {
const supportedFeeds = await this.getSupportedFeeds()

const lastSupportedFeedsID = JSON.stringify(supportedFeeds)
Expand Down Expand Up @@ -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
)
}
9 changes: 7 additions & 2 deletions packages/api/test/networkRouter.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 659b454

Please sign in to comment.