Skip to content

Commit

Permalink
feat(api): avoid call index.js on import
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg committed May 11, 2022
1 parent ea517da commit 3457626
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 95 deletions.
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint:check": "prettier-standard --check",
"dev": "nodemon",
"start": "node dist/index.js",
"test": "jest",
"test": "jest --runInBand",
"prepare": "yarn codegen",
"docker:build": "docker build . --no-cache -t witnet/data-feeds-explorer-api --stream"
},
Expand Down
92 changes: 3 additions & 89 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
require('dotenv/config')

import fs from 'fs'
import path from 'path'
import Web3 from 'web3'
import { MongoManager } from './database'
import { FeedRepository } from './repository/Feed'
import { ResultRequestRepository } from './repository/ResultRequest'
import { createServer } from './server'
import {
FeedInfo,
FeedInfoConfig,
Repositories,
RouterDataFeedsConfig,
NetworksConfig
} from './types'
import { Repositories, RouterDataFeedsConfig, NetworksConfig } from './types'
import { Web3Middleware } from './web3Middleware/index'
import { normalizeConfig, normalizeNetworkConfig } from './utils/index'
import { normalizeNetworkConfig } from './utils/index'
import axios from 'axios'
import { readDataFeeds } from './readDataFeeds'

async function getDataFeedsRouterConfig (): Promise<
RouterDataFeedsConfig | null
Expand Down Expand Up @@ -67,83 +60,4 @@ async function main () {
})
}

export async function readDataFeeds (
config: RouterDataFeedsConfig
): Promise<Array<FeedInfo>> {
const dataFeeds: Array<Omit<
FeedInfoConfig,
'abi' | 'routerAbi'
>> = normalizeConfig(config)

// Throw and error if config file is not valid
validateDataFeeds(dataFeeds)

return dataFeeds.map(dataFeed => ({
...dataFeed,
routerAbi: JSON.parse(
fs.readFileSync(
path.resolve(
process.env.DATA_FEED_ROUTER_ABI_PATH ||
'./src/abi/PriceFeedRouter.json'
),
'utf-8'
)
),
abi: JSON.parse(
fs.readFileSync(
path.resolve(
process.env.DATA_FEED_ABI_PATH || './src/abi/PriceFeed.json'
),
'utf-8'
)
)
}))
}

// Throw an error if a field is missing in the data feed config file
function validateDataFeeds (
dataFeeds: Array<Omit<FeedInfoConfig, 'abi' | 'routerAbi'>>
) {
const expectedFields = [
'feedFullName',
'id',
'address',
'contractId',
'routerAddress',
'network',
'networkName',
'chain',
'name',
'label',
'pollingPeriod',
'color',
'blockExplorer',
'deviation',
'heartbeat',
'finality'
]

dataFeeds.forEach((feedInfoConfig, index) => {
expectedFields.forEach(field => {
// Validate nested keys in a field
field.split('.').reduce((acc, val) => {
// Throw error if the key is not found or has a falsy value
if (!(val in acc) || !acc[val]) {
throw new Error(
`Missing field ${field} in index ${index} in data feed config file`
)
} else {
// Throw error if not validated new fields are added in the config file
if (Object.keys(feedInfoConfig).length !== expectedFields.length) {
throw new Error(
`There are more fields in the feed config than expected`
)
}
return acc[val]
}
}, feedInfoConfig)
})
})
}

main()
81 changes: 81 additions & 0 deletions packages/api/src/readDataFeeds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import path from 'path'
import fs from 'fs'
import { RouterDataFeedsConfig, FeedInfo, FeedInfoConfig } from './types'
import { normalizeConfig } from './utils'

export function readDataFeeds (config: RouterDataFeedsConfig): Array<FeedInfo> {
const dataFeeds: Array<Omit<
FeedInfoConfig,
'abi' | 'routerAbi'
>> = normalizeConfig(config)

// Throw and error if config file is not valid
validateDataFeeds(dataFeeds)

return dataFeeds.map(dataFeed => ({
...dataFeed,
routerAbi: JSON.parse(
fs.readFileSync(
path.resolve(
process.env.DATA_FEED_ROUTER_ABI_PATH ||
'./src/abi/PriceFeedRouter.json'
),
'utf-8'
)
),
abi: JSON.parse(
fs.readFileSync(
path.resolve(
process.env.DATA_FEED_ABI_PATH || './src/abi/PriceFeed.json'
),
'utf-8'
)
)
}))
}

// Throw an error if a field is missing in the data feed config file
function validateDataFeeds (
dataFeeds: Array<Omit<FeedInfoConfig, 'abi' | 'routerAbi'>>
) {
const expectedFields = [
'feedFullName',
'id',
'address',
'contractId',
'routerAddress',
'network',
'networkName',
'chain',
'name',
'label',
'pollingPeriod',
'color',
'blockExplorer',
'deviation',
'heartbeat',
'finality'
]

dataFeeds.forEach((feedInfoConfig, index) => {
expectedFields.forEach(field => {
// Validate nested keys in a field
field.split('.').reduce((acc, val) => {
// Throw error if the key is not found or has a falsy value
if (!(val in acc) || !acc[val]) {
throw new Error(
`Missing field ${field} in index ${index} in data feed config file`
)
} else {
// Throw error if not validated new fields are added in the config file
if (Object.keys(feedInfoConfig).length !== expectedFields.length) {
throw new Error(
`There are more fields in the feed config than expected`
)
}
return acc[val]
}
}, feedInfoConfig)
})
})
}
12 changes: 7 additions & 5 deletions packages/api/test/feeds.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { CHART_RANGE } from './constants'
import { MongoManager } from './../src/database'
import { FeedRepository } from '../src/repository/Feed'
import { ResultRequestRepository } from '../src/repository/ResultRequest'
import { readDataFeeds, readNetworks } from '../src/index'

const dataFeeds = readDataFeeds()
const networksconfig = readNetworks()
import { normalizeNetworkConfig } from '../src/utils'
import { readDataFeeds } from '../src/readDataFeeds'
import dataFeedsRouterConfig from '../../api/src/dataFeedsRouter.json'

const dataFeeds = readDataFeeds(dataFeedsRouterConfig)
const networksConfig = normalizeNetworkConfig(dataFeedsRouterConfig)

const state: {
mongoManager: MongoManager
Expand All @@ -20,7 +23,6 @@ const state: {
testClient: null,
server: null
}

describe.skip('feeds', function () {
beforeAll(async function () {
const ciUri = 'mongodb://localhost'
Expand All @@ -34,7 +36,7 @@ describe.skip('feeds', function () {
},
{
dataFeedsConfig: dataFeeds,
networksConfig: networksconfig
networksConfig: networksConfig
}
)
await new Promise(resolve => {
Expand Down

0 comments on commit 3457626

Please sign in to comment.