Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): load data feeds from config json file #46

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/api/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ MONGO_DATABASE_PASSWORD=your_password
MONGO_INITDB_DATABASE=database
GOERLI_PROVIDER=https://goerli.infura.io/v3/<TOKEN>
KOVAN_PROVIDER=https://kovan.infura.io/v3/<TOKEN>
RINKEBY_PROVIDER=https://rinkeby.infura.io/v3/<TOKEN>
RINKEBY_PROVIDER=https://rinkeby.infura.io/v3/<TOKEN>
DATA_FEED_CONFIG_PATH=/dataFeeds.json
74 changes: 74 additions & 0 deletions packages/api/src/dataFeeds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[
{
"abi": "./abi/BtcUsd.json",
"address": "0xF2712e7114A237625EFC8bBA6a6ed1Bb8b6029c9",
"network": "mainnet",
"name": "btc/usd",
"label": "$",
"pollingPeriod": 3600000,
"witnetRequestBoard": {
"address": "0x400DbF3645b345823124aaB22D04013A46D9ceD5",
"abi": "./abi/WitnetRequestBoardProxy.json"
}
},
{
"abi": "./abi/EthUsd.json",
"address": "0x1ebD93231a7fE551E1d6405404Df34909eff4c2C",
"network": "mainnet",
"name": "eth/usd",
"label": "$",
"pollingPeriod": 3600000,
"witnetRequestBoard": {
"address": "0x400DbF3645b345823124aaB22D04013A46D9ceD5",
"abi": "./abi/WitnetRequestBoardProxy.json"
}
},
{
"abi": "./abi/BtcUsd.json",
"address": "0x58995FaD03158fB9cd64397347bA97714EF8fC12",
"witnetRequestBoard": {
"address": "0x9b42b0D80C428B17A5828dF5C2c96454ca54bD04",
"abi": "./abi/WitnetRequestBoardProxy.json"
},
"network": "rinkeby",
"name": "btc/usd",
"label": "$",
"pollingPeriod": 900000
},
{
"abi": "./abi/EthUsd.json",
"address": "0xAe9821fbA4Bd76fd6D39859bd7c3d4A90b2ceE40",
"witnetRequestBoard": {
"address": "0x9b42b0D80C428B17A5828dF5C2c96454ca54bD04",
"abi": "./abi/WitnetRequestBoardProxy.json"
},
"network": "rinkeby",
"name": "eth/usd",
"label": "$",
"pollingPeriod": 900000
},
{
"abi": "./abi/BtcUsd.json",
"address": "0x4958806608D2E3Aa22BD8818B555A0a24fe6c38E",
"network": "goerli",
"name": "btc/usd",
"label": "$",
"pollingPeriod": 900000,
"witnetRequestBoard": {
"address": "0x0C4be6AA667df48de54BA174bE7948875fdf152B",
"abi": "./abi/WitnetRequestBoardProxy.json"
}
},
{
"abi": "./abi/EthUsd.json",
"address": "0xAa0AA725aEb1d382F909a8dE3041e9eaD6507501",
"network": "goerli",
"name": "eth/usd",
"label": "$",
"pollingPeriod": 900000,
"witnetRequestBoard": {
"address": "0x0C4be6AA667df48de54BA174bE7948875fdf152B",
"abi": "./abi/WitnetRequestBoardProxy.json"
}
}
]
32 changes: 28 additions & 4 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
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 { Repositories } from './types'
import { FeedInfo, FeedInfoConfig, Repositories } from './types'
import { Web3Middleware } from './web3Middleware/index'
import { dataFeeds } from './web3Middleware/dataFeeds'

async function main () {
const mongoManager = new MongoManager()
const db = await mongoManager.start()
const dataFeeds = readDataFeeds()

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

const web3Middleware = new Web3Middleware(
Expand All @@ -33,4 +35,26 @@ async function main () {
})
}

function readDataFeeds (): Array<FeedInfo> {
const dataFeeds: Array<FeedInfoConfig> = JSON.parse(
fs.readFileSync(path.join(__dirname, process.env.DATA_FEED_CONFIG_PATH || '/dataFeeds.json'), 'utf-8')
)

return dataFeeds.map(dataFeed => ({
...dataFeed,
abi: JSON.parse(
fs.readFileSync(path.join(__dirname, dataFeed.abi), 'utf-8')
),
witnetRequestBoard: {
...dataFeed.witnetRequestBoard,
abi: JSON.parse(
fs.readFileSync(
path.join(__dirname, dataFeed.witnetRequestBoard.abi),
'utf-8'
)
)
}
}))
}

main()
16 changes: 12 additions & 4 deletions packages/api/src/repository/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ import {
FeedDbObject,
Collection,
Db,
ObjectId
ObjectId,
FeedInfo
} from '../types'

export class FeedRepository {
collection: Collection<FeedDbObject>
// list of addresses to include in the search queries using address as an id for each data feed
dataFeedsAddresses: Array<string>

constructor (db: Db) {
constructor (db: Db, dataFeeds: Array<FeedInfo>) {
this.collection = db.collection('feed')
this.dataFeedsAddresses = dataFeeds.map(dataFeed => dataFeed.address)
}

async getAll () {
return (await this.collection.find({}).toArray()).map(this.normalizeId)
return (
await this.collection
.find({ address: { $in: this.dataFeedsAddresses } })
.toArray()
).map(this.normalizeId)
}

async insert (feed: Omit<FeedDbObject, '_id'>) {
Expand Down Expand Up @@ -42,7 +50,7 @@ export class FeedRepository {
async getFeeds (page: number, size: number) {
return (
await this.collection
.find({})
.find({ address: { $in: this.dataFeedsAddresses } })
.skip(size * (page - 1))
.limit(size)
.toArray()
Expand Down
12 changes: 9 additions & 3 deletions packages/api/src/repository/ResultRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import {
ResultRequestDbObject,
Db,
Collection,
ObjectId
ObjectId,
FeedInfo
} from '../types'

export class ResultRequestRepository {
collection: Collection<ResultRequestDbObject>
// list of addresses to include in the search queries using address as an id for each data feed
dataFeedsAddresses: Array<string>

constructor (db: Db) {
constructor (db: Db, dataFeeds: Array<FeedInfo>) {
this.collection = db.collection('result_request')
this.dataFeedsAddresses = dataFeeds.map(dataFeed => dataFeed.address)
}

async getFeedRequests (feedId: ObjectId) {
Expand All @@ -27,7 +31,9 @@ export class ResultRequestRepository {
async getFeedRequestsPage (feedId: ObjectId, page, size) {
return (
await this.collection
.find({ feedId: feedId.toString() })
.find({
feedId: feedId.toString()
})
.sort({ timestamp: -1 })
.skip(size * (page - 1))
.limit(size)
Expand Down
8 changes: 5 additions & 3 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ export enum Network {
Rinkeby = 'rinkeby'
}

export type FeedInfo = {
abi: Array<AbiItem>
export type FeedInfoGeneric<ABI> = {
abi: ABI
address: string
network: Network
name: string
pollingPeriod: number
label: string
witnetRequestBoard: {
address: string
abi: Array<AbiItem>
abi: ABI
}
}
export type FeedInfo = FeedInfoGeneric<Array<AbiItem>>
export type FeedInfoConfig = FeedInfoGeneric<string>

export type FeedDbObjectNormalized = FeedDbObject & { id: string }
export type ResultRequestDbObjectNormalized = ResultRequestDbObject & {
Expand Down
119 changes: 0 additions & 119 deletions packages/api/src/web3Middleware/dataFeeds.ts

This file was deleted.

Loading