Skip to content

Commit

Permalink
refactor(api): add missing return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg committed Aug 17, 2021
1 parent 144872d commit 72e1376
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
24 changes: 12 additions & 12 deletions packages/api/src/dataFeeds.json
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
[
{
"abi": "./abi/BtcUsd.json",
"abi": "./src/abi/BtcUsd.json",
"address": "0xF2712e7114A237625EFC8bBA6a6ed1Bb8b6029c9",
"network": "mainnet",
"name": "btc/usd",
"label": "$",
"pollingPeriod": 3600000,
"witnetRequestBoard": {
"address": "0x400DbF3645b345823124aaB22D04013A46D9ceD5",
"abi": "./abi/WitnetRequestBoardProxy.json"
"abi": "./src/abi/WitnetRequestBoardProxy.json"
}
},
{
"abi": "./abi/EthUsd.json",
"abi": "./src/abi/EthUsd.json",
"address": "0x1ebD93231a7fE551E1d6405404Df34909eff4c2C",
"network": "mainnet",
"name": "eth/usd",
"label": "$",
"pollingPeriod": 3600000,
"witnetRequestBoard": {
"address": "0x400DbF3645b345823124aaB22D04013A46D9ceD5",
"abi": "./abi/WitnetRequestBoardProxy.json"
"abi": "./src/abi/WitnetRequestBoardProxy.json"
}
},
{
"abi": "./abi/BtcUsd.json",
"abi": "./src/abi/BtcUsd.json",
"address": "0x58995FaD03158fB9cd64397347bA97714EF8fC12",
"witnetRequestBoard": {
"address": "0x9b42b0D80C428B17A5828dF5C2c96454ca54bD04",
"abi": "./abi/WitnetRequestBoardProxy.json"
"abi": "./src/abi/WitnetRequestBoardProxy.json"
},
"network": "rinkeby",
"name": "btc/usd",
"label": "$",
"pollingPeriod": 900000
},
{
"abi": "./abi/EthUsd.json",
"abi": "./src/abi/EthUsd.json",
"address": "0xAe9821fbA4Bd76fd6D39859bd7c3d4A90b2ceE40",
"witnetRequestBoard": {
"address": "0x9b42b0D80C428B17A5828dF5C2c96454ca54bD04",
"abi": "./abi/WitnetRequestBoardProxy.json"
"abi": "./src/abi/WitnetRequestBoardProxy.json"
},
"network": "rinkeby",
"name": "eth/usd",
"label": "$",
"pollingPeriod": 900000
},
{
"abi": "./abi/BtcUsd.json",
"abi": "./src/abi/BtcUsd.json",
"address": "0x4958806608D2E3Aa22BD8818B555A0a24fe6c38E",
"network": "goerli",
"name": "btc/usd",
"label": "$",
"pollingPeriod": 900000,
"witnetRequestBoard": {
"address": "0x0C4be6AA667df48de54BA174bE7948875fdf152B",
"abi": "./abi/WitnetRequestBoardProxy.json"
"abi": "./src/abi/WitnetRequestBoardProxy.json"
}
},
{
"abi": "./abi/EthUsd.json",
"abi": "./src/abi/EthUsd.json",
"address": "0xAa0AA725aEb1d382F909a8dE3041e9eaD6507501",
"network": "goerli",
"name": "eth/usd",
"label": "$",
"pollingPeriod": 900000,
"witnetRequestBoard": {
"address": "0x0C4be6AA667df48de54BA174bE7948875fdf152B",
"abi": "./abi/WitnetRequestBoardProxy.json"
"abi": "./src/abi/WitnetRequestBoardProxy.json"
}
}
]
10 changes: 7 additions & 3 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ async function main () {
}

function readDataFeeds (): Array<FeedInfo> {
console.log('DIRNAME', __dirname)
console.log('DATA_FEED_CONFIG_PATH:>', process.env.DATA_FEED_CONFIG_PATH)
console.log('resolve path: ', path.resolve(process.env.DATA_FEED_CONFIG_PATH || './dataFeeds.json'))

const dataFeeds: Array<FeedInfoConfig> = JSON.parse(
fs.readFileSync(path.join(__dirname, process.env.DATA_FEED_CONFIG_PATH || '/dataFeeds.json'), 'utf-8')
fs.readFileSync(path.resolve(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')
fs.readFileSync(path.resolve(dataFeed.abi), 'utf-8')
),
witnetRequestBoard: {
...dataFeed.witnetRequestBoard,
abi: JSON.parse(
fs.readFileSync(
path.join(__dirname, dataFeed.witnetRequestBoard.abi),
path.resolve(dataFeed.witnetRequestBoard.abi),
'utf-8'
)
)
Expand Down
22 changes: 14 additions & 8 deletions packages/api/src/repository/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ObjectId,
FeedInfo
} from '../types'
import { containFalsyValues } from './containFalsyValues'

export class FeedRepository {
collection: Collection<FeedDbObject>
Expand All @@ -17,25 +18,27 @@ export class FeedRepository {
this.dataFeedsAddresses = dataFeeds.map(dataFeed => dataFeed.address)
}

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

async insert (feed: Omit<FeedDbObject, '_id'>) {
async insert (feed: Omit<FeedDbObject, '_id'>): Promise<FeedDbObjectNormalized | null> {
if (this.isValidFeed(feed)) {
const response = await this.collection.insertOne(feed)

return this.normalizeId(response.ops[0])
} else {
console.error('Error inserting feed: Validation Error', feed)

return null
}
}

async addResultRequest (feedId: ObjectId, resultRequestId: ObjectId) {
async addResultRequest (feedId: ObjectId, resultRequestId: ObjectId): Promise<FeedDbObjectNormalized> {
const response = await this.collection.findOneAndUpdate(
{ _id: feedId },
{ $push: { requests: resultRequestId } },
Expand All @@ -45,13 +48,13 @@ export class FeedRepository {
return this.normalizeId(response.value)
}

async get (id: string) {
async get (id: string): Promise<FeedDbObjectNormalized> {
return this.normalizeId(
await this.collection.findOne({ _id: new ObjectId(id) })
)
}

async getFeeds (page: number, size: number) {
async getFeeds (page: number, size: number): Promise<Array<FeedDbObjectNormalized>> {
return (
await this.collection
.find({ address: { $in: this.dataFeedsAddresses } })
Expand All @@ -61,17 +64,20 @@ export class FeedRepository {
).map(this.normalizeId)
}

async getByAddress (address: string) {
async getByAddress (address: string): Promise<FeedDbObjectNormalized> {
return this.normalizeId(await this.collection.findOne({ address }))
}

public getTotalCount () {
public getTotalCount (): Promise<number> {
return this.collection.count()
}

private normalizeId (feed: FeedDbObject): FeedDbObjectNormalized {
private normalizeId (feed: FeedDbObject): FeedDbObjectNormalized | null {
if (feed && feed._id) {
return { ...feed, id: feed._id.toString() }
} else {
// this code should be unreachable: value from db always contains _id
return null
}
}

Expand Down
16 changes: 11 additions & 5 deletions packages/api/src/repository/ResultRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ObjectId,
FeedInfo
} from '../types'
import { containFalsyValues } from './containFalsyValues'

export class ResultRequestRepository {
collection: Collection<ResultRequestDbObject>
Expand All @@ -17,7 +18,7 @@ export class ResultRequestRepository {
this.dataFeedsAddresses = dataFeeds.map(dataFeed => dataFeed.address)
}

async getFeedRequests (feedId: ObjectId) {
async getFeedRequests (feedId: ObjectId): Promise<Array<ResultRequestDbObjectNormalized>> {
return (
await this.collection
.find({
Expand All @@ -28,7 +29,7 @@ export class ResultRequestRepository {
).map(this.normalizeId)
}

async getFeedRequestsPage (feedId: ObjectId, page, size) {
async getFeedRequestsPage (feedId: ObjectId, page: number, size: number): Promise<Array<ResultRequestDbObjectNormalized>> {
return (
await this.collection
.find({
Expand All @@ -41,7 +42,7 @@ export class ResultRequestRepository {
).map(this.normalizeId)
}

async getLastResult (feedId: ObjectId) {
async getLastResult (feedId: ObjectId): Promise<ResultRequestDbObjectNormalized> {
const lastResultRequest = await this.collection.findOne(
{
feedId: feedId.toString()
Expand All @@ -60,7 +61,7 @@ export class ResultRequestRepository {
return this.normalizeId(lastResultRequest)
}

async insert (resultRequest: Omit<ResultRequestDbObject, '_id'>) {
async insert (resultRequest: Omit<ResultRequestDbObject, '_id'>): Promise<ResultRequestDbObjectNormalized | null> {
if (this.isValidResultRequest(resultRequest)) {
const response = await this.collection.insertOne(resultRequest)

Expand All @@ -70,14 +71,19 @@ export class ResultRequestRepository {
'Error inserting result request: Validation Error',
resultRequest
)
return null
}
}

private normalizeId (
resultRequest: ResultRequestDbObject
): ResultRequestDbObjectNormalized {
if (resultRequest?._id)
if (resultRequest?._id) {
return { ...resultRequest, id: resultRequest._id.toString() }
} else {
// This should be unreachable: obj from db always contains _id
return null
}
}

private isValidResultRequest (
Expand Down
3 changes: 2 additions & 1 deletion packages/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"outDir": "dist",
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"noImplicitReturns": true,
},
"include": ["src/"],
"exclude": ["node_modules", "test"]
Expand Down

0 comments on commit 72e1376

Please sign in to comment.