Skip to content

Commit

Permalink
fix(style): lint changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon authored and Tommytrg committed Apr 6, 2024
1 parent 611bd05 commit bc02bec
Show file tree
Hide file tree
Showing 36 changed files with 716 additions and 884 deletions.
4 changes: 2 additions & 2 deletions packages/api/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module.exports = {
moduleNameMapper: {
// See https://stackoverflow.com/a/73203803
// Force module uuid to resolve with the CJS entry point, because Jest does not support package.json.exports. See https://github.com/uuidjs/uuid/issues/451
uuid: require.resolve('uuid')
}
uuid: require.resolve('uuid'),
},
}
4 changes: 2 additions & 2 deletions packages/api/migrate-mongo-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ require('dotenv/config')
const config = {
mongodb: {
url: process.env.MONGO_URI,
databaseName: process.env.MONGO_INITDB_DATABASE
databaseName: process.env.MONGO_INITDB_DATABASE,
},
migrationsDir: 'migrations',
changelogCollectionName: 'changelog',
migrationFileExtension: '.js',
useFileHash: false
useFileHash: false,
}

module.exports = config
18 changes: 9 additions & 9 deletions packages/api/migrations/1_rename-boba-networks.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
module.exports = {
async up (db) {
async up(db) {
const resultRequestCollection = db.collection('result_request')
await Promise.all(
(await resultRequestCollection.find({ feedFullName: /boba-/ }).toArray())
.map(resultRequest => ({
.map((resultRequest) => ({
...resultRequest,
feedFullName: resultRequest.feedFullName.replace(
/boba-/,
'boba-ethereum-'
)
'boba-ethereum-',
),
}))
.map(resultRequest =>
.map((resultRequest) =>
resultRequestCollection.updateOne(
{ _id: resultRequest._id },
{ $set: { feedFullName: resultRequest.feedFullName } }
)
)
{ $set: { feedFullName: resultRequest.feedFullName } },
),
),
)
}
},
}
32 changes: 16 additions & 16 deletions packages/api/migrations/2_rename-conflux-networks.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
module.exports = {
async up (db) {
async up(db) {
const resultRequestCollection = db.collection('result_request')
const updateConfluxTethysPromises = (
await resultRequestCollection
.find({ feedFullName: /conflux-tethys/ })
.toArray()
)
.map(resultRequest => ({
.map((resultRequest) => ({
...resultRequest,
feedFullName: resultRequest.feedFullName.replace(
/conflux-tethys/,
'conflux-core-mainnet'
)
'conflux-core-mainnet',
),
}))
.map(resultRequest =>
.map((resultRequest) =>
resultRequestCollection.updateOne(
{ _id: resultRequest._id },
{ $set: { feedFullName: resultRequest.feedFullName } }
)
{ $set: { feedFullName: resultRequest.feedFullName } },
),
)
const updateConfluxRinkebyPromises = (
await resultRequestCollection.find({
feedFullName: /conflux-testnet/
feedFullName: /conflux-testnet/,
})
)
.map(resultRequest => ({
.map((resultRequest) => ({
...resultRequest,
feedFullName: resultRequest.feedFullName.replace(
/conflux-testnet/,
'conflux-core-testnet'
)
'conflux-core-testnet',
),
}))
.map(resultRequest =>
.map((resultRequest) =>
resultRequestCollection.updateOne(
{ _id: resultRequest._id },
{ $set: { feedFullName: resultRequest.feedFullName } }
)
{ $set: { feedFullName: resultRequest.feedFullName } },
),
)
await Promise.all(
[updateConfluxTethysPromises, updateConfluxRinkebyPromises].flat()
[updateConfluxTethysPromises, updateConfluxRinkebyPromises].flat(),
)
}
},
}
2 changes: 1 addition & 1 deletion packages/api/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Data Feeds Explorer Api
# Data Feeds Explorer Api
8 changes: 4 additions & 4 deletions packages/api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export class MongoManager {
db: Db
client: MongoClient

async start (uri?: string): Promise<Db | null> {
async start(uri?: string): Promise<Db | null> {
const mongoDbUri = uri || process.env.MONGO_URI
return this.connect(mongoDbUri, process.env.MONGO_INITDB_DATABASE as string)
}

async connect (uri: string, name: string): Promise<Db | null> {
async connect(uri: string, name: string): Promise<Db | null> {
this.client = new MongoClient(uri)

try {
Expand All @@ -23,11 +23,11 @@ export class MongoManager {
return null
}

async stop (): Promise<void> {
async stop(): Promise<void> {
await this.client.close()
}

async drop (): Promise<void> {
async drop(): Promise<void> {
const collections = await this.db.collections()
for (const collection of collections) {
await this.db.dropCollection(collection.collectionName)
Expand Down
16 changes: 8 additions & 8 deletions packages/api/src/fetchSvgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ const STATIC_LOGOS_SVG_URL = process.env.TEST_BRANCH
? `https://raw.githubusercontent.com/witnet/data-feeds-explorer//${process.env.TEST_BRANCH}/packages/ui/assets/svg/`
: 'https://raw.githubusercontent.com/witnet/data-feeds-explorer/main/packages/ui/assets/svg/'

export async function fetchSvgs (
networksToFetch: Array<string>
export async function fetchSvgs(
networksToFetch: Array<string>,
): Promise<{ [key: string]: string }> {
const networksWithoutRepeated = removeRepeatedElements(networksToFetch)
const logosUrls = networksWithoutRepeated.map(
(networkToFetch: string) => `${STATIC_LOGOS_SVG_URL}${networkToFetch}.svg`
(networkToFetch: string) => `${STATIC_LOGOS_SVG_URL}${networkToFetch}.svg`,
)

// Fetch all logos from github
const promises = logosUrls.map(url => axios.get(url))
return new Promise(resolve => {
Promise.allSettled(promises).then(results => {
const promises = logosUrls.map((url) => axios.get(url))
return new Promise((resolve) => {
Promise.allSettled(promises).then((results) => {
const svgs = results.map((result, index) => {
if (result.status === 'rejected') {
console.log(`Error fetching logo from: ${logosUrls[index]}`)
Expand All @@ -32,9 +32,9 @@ export async function fetchSvgs (
const svgByName = networksWithoutRepeated.reduce(
(acc, val, index) => ({
...acc,
[val]: svgs[index]
[val]: svgs[index],
}),
{}
{},
)

resolve(svgByName)
Expand Down
51 changes: 29 additions & 22 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,65 @@ import { MongoManager } from './database'
import { FeedRepository } from './repository/Feed'
import { ResultRequestRepository } from './repository/ResultRequest'
import { createServer } from './server'
import { RouterDataFeedsConfig, Repositories, FeedInfo, NetworksConfig } from './types'
import {
RouterDataFeedsConfig,
Repositories,
FeedInfo,
NetworksConfig,
} from './types'
import { Web3Middleware } from './web3Middleware/index'
import { normalizeNetworkConfig } from './utils/index'
import {
normalizeAndValidateDataFeedConfig,
fetchDataFeedsRouterConfig
fetchDataFeedsRouterConfig,
} from './readDataFeeds'
import { SvgCache } from './svgCache'
import { NetworkRouter } from './web3Middleware/NetworkRouter'
import { Configuration } from './web3Middleware/Configuration'

async function main () {
async function main() {
const svgCache = new SvgCache()
const mongoManager = new MongoManager()
const db = await mongoManager.start()
const configurationFile: RouterDataFeedsConfig = await fetchDataFeedsRouterConfig()
const configurationFile: RouterDataFeedsConfig =
await fetchDataFeedsRouterConfig()
const configuration = new Configuration(configurationFile)

const legacyFeeds: Array<FeedInfo> = normalizeAndValidateDataFeedConfig(configurationFile)
const networksConfigPartial: Array<Omit<
NetworksConfig,
'logo'
>> = normalizeNetworkConfig(configurationFile)
const legacyFeeds: Array<FeedInfo> =
normalizeAndValidateDataFeedConfig(configurationFile)
const networksConfigPartial: Array<Omit<NetworksConfig, 'logo'>> =
normalizeNetworkConfig(configurationFile)

const logosToFetch = networksConfigPartial.map(
(networksConfig: NetworksConfig) => {
return networksConfig.chain.toLowerCase()
}
},
)

const networksLogos: { [key: string]: string } = await svgCache.getMany(
logosToFetch
)
const networksLogos: { [key: string]: string } =
await svgCache.getMany(logosToFetch)

const networksConfig = networksConfigPartial.map((networksConfig, index) => ({
...networksConfig,
logo: networksLogos[logosToFetch[index]]
logo: networksLogos[logosToFetch[index]],
}))

const routers = configuration.listNetworksUsingPriceFeedsContract()
.filter(config => {
const routers = configuration
.listNetworksUsingPriceFeedsContract()
.filter((config) => {
if (!config.provider) {
console.warn("No provider found for ", config.key)
console.warn('No provider found for ', config.key)
}
return config.provider
})
.map(networkInfo => new NetworkRouter(configuration, repositories, networkInfo))

.map(
(networkInfo) =>
new NetworkRouter(configuration, repositories, networkInfo),
)

const newFeeds: Array<FeedInfo> = []

for (let router of routers) {
for (const router of routers) {
const feedInfos = await router.getFeedInfos()
newFeeds.concat(feedInfos)
}
Expand All @@ -65,7 +72,7 @@ async function main () {

const repositories: Repositories = {
feedRepository: new FeedRepository(feeds),
resultRequestRepository: new ResultRequestRepository(db, feeds)
resultRequestRepository: new ResultRequestRepository(db, feeds),
}

const web3Middleware = new Web3Middleware(
Expand All @@ -78,7 +85,7 @@ async function main () {

await createServer(repositories, svgCache, {
dataFeedsConfig: feeds,
networksConfig
networksConfig,
})
}

Expand Down
24 changes: 12 additions & 12 deletions packages/api/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { SvgCache } from '../svgCache'
import {
Loaders,
Repositories,
ResultRequestDbObjectNormalized
ResultRequestDbObjectNormalized,
} from '../types'

export class LoadersFactory {
repositories: Repositories
svgCache: SvgCache

constructor (repositories: Repositories, svgCache: SvgCache) {
constructor(repositories: Repositories, svgCache: SvgCache) {
this.repositories = repositories
this.svgCache = svgCache
}
// returns a loader that fetches data using the given function
private genericLoader<T> (load: (filter) => T) {
private genericLoader<T>(load: (filter) => T) {
return new DataLoader(async (filters: Array<string>) => {
// load data from all
const data = await Promise.all(
await filters.map(async (filter, index) => ({
data: await load(filter),
index
}))
index,
})),
)
// ensure they are sorted
const fetchedDataByIndex = data.reduce((acc, val) => {
Expand All @@ -34,13 +34,13 @@ export class LoadersFactory {
})
}

getLoaders (): Loaders {
getLoaders(): Loaders {
return {
lastResult: this.genericLoader<Promise<ResultRequestDbObjectNormalized>>(
async (feedFullName: string) =>
await this.repositories.resultRequestRepository.getLastResult(
feedFullName
)
feedFullName,
),
),

requests: this.genericLoader<
Expand All @@ -53,15 +53,15 @@ export class LoadersFactory {
if (filter.timestamp.toString().length !== 10) {
// invalid timestamp
console.log(
'[requests] invalid timestamp argument -> returning last 7 days'
'[requests] invalid timestamp argument -> returning last 7 days',
)
timestamp = dateNow - sevenDaysInSeconds
}

if (dateNow < filter.timestamp) {
// received timestamp is greater than current time
console.log(
'[requests] invalid timestamp argument -> returning last 7 days'
'[requests] invalid timestamp argument -> returning last 7 days',
)
timestamp = dateNow - sevenDaysInSeconds
}
Expand All @@ -74,12 +74,12 @@ export class LoadersFactory {

return await this.repositories.resultRequestRepository.getFeedRequests(
filter.feedFullName,
timestamp
timestamp,
)
}),
logos: new DataLoader(async (logos: Array<string>) => {
return Object.values(await this.svgCache.getMany(logos))
})
}),
}
}
}
Loading

0 comments on commit bc02bec

Please sign in to comment.