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

Add moonbeam price feeds and get networks from config #148

Merged
merged 19 commits into from
Mar 18, 2022
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
88 changes: 87 additions & 1 deletion packages/api/src/dataFeedsRouter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,35 @@
"abi": "./src/abi/WitnetPriceRouter.json",
"chains": {
"avalanche": {
"name": "Avalanche",
"networks": {
"avalanche.mainnet": {
"address": "0xBaaF31F4AAc5ab5334b6E239a83bf4E855C55ea7",
"blockExplorer": "https://snowtrace.io/address/{address}#readContract",
"color": "#070fdf",
"name": "Avalanche Mainnet",
"pollingPeriod": 300000,
"feeds": {
"Price-AVAX/USD-6": {
"label": "$",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 3600
},
"Price-BTC/USD-6": {
"label": "$",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 3600
},
"Price-ETH/USD-6": {
"label": "$",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 3600
}
}
},
"avalanche.fuji": {
"address": "0x99Af0CF37d1C6b9Bdfe33cc0A89C00D97D3c42F4",
"blockExplorer": "https://testnet.snowtrace.io/address/{address}",
Expand Down Expand Up @@ -33,6 +61,7 @@
}
},
"boba": {
"name": "Boba",
"networks": {
"boba.rinkeby": {
"address": "0x36928Aeedaaf7D85bcA39aDfB2A39ec529ce221a",
Expand Down Expand Up @@ -175,6 +204,7 @@
}
},
"celo": {
"name": "Celo",
"networks": {
"celo.alfajores": {
"address": "0x6f8A7E2bBc1eDb8782145cD1089251f6e2C738AE",
Expand Down Expand Up @@ -206,6 +236,12 @@
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 3600,
"minSecsBetweenUpdates": 300
},
"Price-IMMO/MCUSD-6": {
"label": "$",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 3600,
"minSecsBetweenUpdates": 300
}
}
},
Expand Down Expand Up @@ -239,12 +275,19 @@
"deviationPercentage": 3.5,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 3600
},
"Price-IMMO/MCUSD-6": {
"label": "$",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 3600
}
}
}
}
},
"conflux": {
"name": "Conflux",
"networks": {
"conflux.testnet": {
"address": "0x8F61C7b18F69bB87D6151B8a5D733E1945ea6c25",
Expand Down Expand Up @@ -303,6 +346,7 @@
}
},
"ethereum": {
"name": "Ethereum",
"networks": {
"ethereum.goerli": {
"address": "0x1cF3Aa9DBF4880d797945726B94B9d29164211BE",
Expand Down Expand Up @@ -370,6 +414,7 @@
}
},
"harmony": {
"name": "Harmony",
"networks": {
"harmony.testnet": {
"address": "0x08d479a544b05B297454e5CAc133abA3a584AB8E",
Expand All @@ -395,6 +440,7 @@
}
},
"kcc": {
"name": "KCC",
"networks": {
"kcc.testnet": {
"address": "0xba7CF62498340fa3734EC51Ca8A69928F0d9E03a",
Expand Down Expand Up @@ -453,6 +499,7 @@
}
},
"metis": {
"name": "Metis",
"networks": {
"metis.rinkeby": {
"address": "0x5134EAF08bcf8cE1922991150AAad1774e93751f",
Expand Down Expand Up @@ -498,13 +545,52 @@
}
}
},
"moonbeam": {
"name": "Moonbeam",
"networks": {
"moonbeam.moonbase": {
"address": "0x56834Ff8D4b27db647Da97CA3bd8540f7fA0e89D",
"blockExplorer": "https://moonbase.moonscan.io/address/{address}#readContract",
"color": "#ff6600",
"name": "Moonbase Alpha",
"pollingPeriod": 10000,
"feeds": {
"Price-BTC/USD-6": {
"label": "$",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 900
},
"Price-ETH/USD-6": {
"label": "$",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 300
},
"Price-GLMR/USDT-6": {
"label": "₮",
"deviationPercentage": 1.0,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 900
},
"Price-USDT/USD-6": {
"label": "$",
"deviationPercentage": 0.1,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 900
}
}
}
}
},
"polygon": {
"name": "Polygon",
"networks": {
"polygon.goerli": {
"address": "0x6d5544ca5b35bf2e7a78ace4E7B8d191fe5C9FAb",
"blockExplorer": "https://mumbai.polygonscan.com/address/{address}",
"color": "#66ff00",
"name": "Polygon Goerli",
"name": "Polygon Mumbai",
"pollingPeriod": 15000,
"feeds": {
"Price-BTC/USD-6": {
Expand Down
16 changes: 13 additions & 3 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ import {
FeedInfo,
FeedInfoConfig,
Repositories,
RouterDataFeedsConfig
RouterDataFeedsConfig,
NetworksConfig
} from './types'
import { Web3Middleware } from './web3Middleware/index'
import { normalizeConfig } from './utils/index'
import { normalizeConfig, normalizeNetworkConfig } from './utils/index'
import dataFeedsRouterConfig from './dataFeedsRouter.json'

async function main () {
const mongoManager = new MongoManager()
const db = await mongoManager.start()
const dataFeeds = readDataFeeds()
const networksConfig = readNetworks()
gabaldon marked this conversation as resolved.
Show resolved Hide resolved

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

const web3Middleware = new Web3Middleware(
{ repositories, Web3: Web3 },
dataFeeds
)
web3Middleware.listen()

const server = await createServer(repositories, dataFeeds)
const server = await createServer(repositories, config)

server
.listen({ host: '0.0.0.0', port: process.env.SERVER_PORT })
Expand All @@ -42,6 +48,10 @@ async function main () {
})
}

export function readNetworks (): Array<NetworksConfig> {
return normalizeNetworkConfig(dataFeedsRouterConfig as RouterDataFeedsConfig)
}

gabaldon marked this conversation as resolved.
Show resolved Hide resolved
export function readDataFeeds (): Array<FeedInfo> {
const dataFeeds: Array<Omit<
FeedInfoConfig,
Expand Down
9 changes: 0 additions & 9 deletions packages/api/src/repository/ResultRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ export class ResultRequestRepository {

constructor (db: Db, _dataFeeds: Array<FeedInfo>) {
this.collection = db.collection('result_request')
this.collection.createIndex(
{ feedFullName: 1, timestamp: -1 },
{
collation: {
locale: 'en_US',
numericOrdering: true
}
}
)
}

async getFeedRequests (
Expand Down
20 changes: 9 additions & 11 deletions packages/api/src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Context, Network } from './types'
import { Context } from './types'
const resolvers = {
Query: {
feeds: async (_parent, args, { feedRepository }: Context) => {
return await feedRepository.getFeedsByNetwork(args.network)
},

networks: async (_parent, _args) => {
return Object.keys(Network).map(key => ({
label: Network[key]
}))
networks: (_parent, _args, { config }): Context => {
return config.networksConfig
},

requests: async (_parent, args, { resultRequestRepository }: Context) => {
Expand Down Expand Up @@ -37,24 +35,24 @@ const resolvers = {
return (await loaders.lastResult.load(parent.feedFullName))?.timestamp
},
color: async (parent, _args, { config }: Context) => {
return config[parent.feedFullName]?.color || ''
return config.feedsConfig[parent.feedFullName]?.color || ''
},
blockExplorer: async (parent, _args, { config }: Context) => {
return config[parent.feedFullName]?.blockExplorer || ''
return config.feedsConfig[parent.feedFullName]?.blockExplorer || ''
},
proxyAddress: async (parent, _args, { config }: Context) => {
return config[parent.feedFullName]?.routerAddress || ''
return config.feedsConfig[parent.feedFullName]?.routerAddress || ''
},
deviation: async (parent, _args, { config }: Context) => {
return config[parent.feedFullName]?.deviation || ''
return config.feedsConfig[parent.feedFullName]?.deviation || ''
},
heartbeat: async (parent, _args, { config }: Context) => {
// Heartbeat plus aproximate time in milliseconds that takes to resolve the witnet dr
return config[parent.feedFullName]?.heartbeat || ''
return config.feedsConfig[parent.feedFullName]?.heartbeat || ''
},
finality: async (parent, _args, { config }: Context) => {
// Heartbeat plus aproximate time in milliseconds that takes to resolve the witnet dr
return config[parent.feedFullName]?.finality || ''
return config.feedsConfig[parent.feedFullName]?.finality || ''
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions packages/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import { ApolloServer } from 'apollo-server'
import typeDefs from './typeDefs'
import { DIRECTIVES } from '@graphql-codegen/typescript-mongodb'
import resolvers from './resolvers'
import { ConfigByFullName, FeedInfo, Repositories } from './types'
import { Loaders } from './loaders'
import {
ConfigByFullName,
FeedInfo,
Repositories,
NetworksConfig
} from './types'

export async function createServer (
repositories: Repositories,
config: Array<FeedInfo>
config: {
dataFeedsConfig: Array<FeedInfo>
networksConfig: Array<NetworksConfig>
}
): Promise<ApolloServer> {
return new ApolloServer({
typeDefs: [DIRECTIVES, typeDefs],
resolvers,
context: () => {
const configByFullName: ConfigByFullName = config.reduce(
const configByFullName: ConfigByFullName = config.dataFeedsConfig.reduce(
(acc, feedInfo) => ({
...acc,
[`${feedInfo.feedFullName}`]: feedInfo
Expand All @@ -22,10 +30,12 @@ export async function createServer (
)

const loaders = new Loaders(repositories)

return {
...repositories,
config: configByFullName,
config: {
feedsConfig: configByFullName,
networksConfig: config.networksConfig
},
loaders: loaders.getLoaders()
}
}
Expand Down
14 changes: 8 additions & 6 deletions packages/api/src/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const typeDefs = gql`
label: String!
name: String!
network: String!
networkName: String
gabaldon marked this conversation as resolved.
Show resolved Hide resolved
lastResult: String
lastResultTimestamp: String
deviation: String!
Expand All @@ -29,6 +30,12 @@ const typeDefs = gql`
total: Int!
}

type NetworksConfig @entity {
chain: String
label: String
key: String
}

type ResultRequest @entity {
id: String! @id
drTxHash: String! @column
Expand All @@ -39,11 +46,6 @@ const typeDefs = gql`
timestamp: String! @column
}

type Network @entity {
id: String! @id
label: String
}

# type DataRequest @entity(embedded: true) {
# retrieval: String! @column
# aggregation: String! @column
Expand All @@ -54,7 +56,7 @@ const typeDefs = gql`
feed(feedFullName: String!): Feed
feeds(network: String): FeedsPage!
requests(feedFullName: String!, page: Int!, size: Int!): [ResultRequest]!
networks: [Network]!
networks: [NetworksConfig]!
}
`

Expand Down
Loading