Skip to content

Commit

Permalink
fix: use pnpm in ci and fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Mar 8, 2024
1 parent 17e6daa commit 5ca3d9f
Show file tree
Hide file tree
Showing 29 changed files with 554 additions and 126 deletions.
23 changes: 12 additions & 11 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ jobs:

steps:
- uses: actions/checkout@v3

- uses: actions/[email protected]
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: '14'
node-version: ${{ matrix.node-version }}
cache: 'pnpm'

- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: '4.0'
mongodb-port: 27017

- run: yarn
- run: yarn bootstrap
- run: yarn lint:check

- run: yarn test

- run: yarn build

- run: pnpm install
- run: pnpm lint
- run: pnpm test
- run: pnpm build
3 changes: 2 additions & 1 deletion packages/api/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('dotenv/config')
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config({ path: './test/.test.env' })

module.exports = {
preset: 'ts-jest',
Expand Down
13 changes: 8 additions & 5 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,29 @@
"@graphql-codegen/typescript-mongodb": "2.4.6",
"@graphql-codegen/typescript-resolvers": "3.2.1",
"@graphql-tools/schema": "^9.0.19",
"@types/jest": "^29.5.0",
"@types/jest": "^29.5.12",
"@types/mocha": "^10.0.6",
"@types/node": "18.11.9",
"husky": "^8.0.3",
"mocha": "^10.3.0",
"nodemon": "^2.0.22",
"ts-jest": "29.1.0",
"ts-node": "10.9.1",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"web3-eth-contract": "^4.2.0"
},
"dependencies": {
"@apollo/server": "^4.7.0",
"axios": "^1.3.6",
"dataloader": "2.2.2",
"dotenv": "^16.0.3",
"graphql": "^15.5.0",
"graphql-tag": "^2.12.6",
"jest": "^29.5.0",
"migrate-mongo": "^8.2.3",
"mongodb": "4.16.0",
"node-cache": "^5.1.2",
"web3": "^1.9.0",
"web3-utils": "^1.9.0",
"graphql": "^15.5.0"
"web3": "^1.10.4",
"web3-utils": "^1.9.0"
}
}
2 changes: 1 addition & 1 deletion packages/api/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoClient } from 'mongodb'
import { Db } from './types'
import { Db } from '../types'

export class MongoManager {
db: Db
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Repositories,
FeedInfo,
NetworksConfig,
} from './types'
} from '../types'
import { Web3Middleware } from './web3Middleware/index'
import { normalizeNetworkConfig } from './utils/index'
import {
Expand Down Expand Up @@ -58,7 +58,7 @@ async function main() {
})
.map(
(networkInfo) =>
new NetworkRouter(configuration, repositories, networkInfo),
new NetworkRouter(configuration, Web3, repositories, networkInfo),
)

const newFeeds: Array<FeedInfo> = []
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Loaders,
Repositories,
ResultRequestDbObjectNormalized,
} from '../types'
} from '../../types'

export class LoadersFactory {
repositories: Repositories
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/readDataFeeds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios'
import path from 'path'
import fs from 'fs'
import { RouterDataFeedsConfig, FeedInfo, FeedInfoConfig } from './types'
import { RouterDataFeedsConfig, FeedInfo, FeedInfoConfig } from '../types'
import { normalizeConfig } from './utils'

const CONFIG_URL = process.env.TEST_BRANCH
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/repository/Feed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PaginatedFeedsObject, FeedInfo } from '../types'
import { PaginatedFeedsObject, FeedInfo } from '../../types'

export class FeedRepository {
dataFeeds: Array<FeedInfo>
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/repository/ResultRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Collection,
FeedInfo,
WithoutId,
} from '../types'
} from '../../types'
import { containFalsyValues } from './containFalsyValues'

export class ResultRequestRepository {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from './types'
import { Context } from '../types'

const resolvers = {
Query: {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Loaders,
NetworksConfig,
Repositories,
} from './types'
} from '../types'
import { startStandaloneServer } from '@apollo/server/standalone'
import { LoadersFactory } from './loaders'
import SvgCache from './svgCache'
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
NetworksConfig,
Chain,
FeedConfig,
} from '../types'
} from '../../types'
// parse network name to fit schema
export function parseNetworkName(value) {
return value.toLowerCase().split('.').join('-')
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/web3Middleware/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Network,
NetworksConfig,
RouterDataFeedsConfig,
} from '../types'
} from '../../types'
import { getNetworksListByChain, sortAlphabeticallyByLabel } from '../utils'
import { NetworkInfo } from './NetworkRouter'
import { getProvider } from './provider'
Expand Down Expand Up @@ -45,6 +45,7 @@ export class Configuration {
.flatMap((chain) => Object.entries(chain.networks))
.filter(([_, network]) => network.legacy === false)
.map(([networkKey, network]) => {
console.log(networkKey.replaceAll('.', '-') as Network)
return {
provider: getProvider(networkKey.replaceAll('.', '-') as Network),
address: network.address,
Expand Down
16 changes: 11 additions & 5 deletions packages/api/src/web3Middleware/NetworkRouter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Web3 from 'web3'
import WitnetPriceFeedsABI from './../abi/WitnetPriceFeeds.json'
import { FeedInfo, Network, Repositories } from './../types'
import { toHex } from 'web3-utils'
import { FeedInfo, Network, Repositories } from '../../types'
import { AbiItem, toHex } from 'web3-utils'
import { createFeedFullName } from '../utils'
import { PriceFeed } from './PriceFeed'
import { Configuration } from './Configuration'
Expand Down Expand Up @@ -41,6 +41,7 @@ export type NetworkSnapshot = {
}

export class NetworkRouter {
private Web3: typeof Web3
public contract: any
public network: Network
public networkName: string
Expand All @@ -53,19 +54,24 @@ export class NetworkRouter {

constructor(
configuration: Configuration,
web3Dep: typeof Web3,
repositories: Repositories,
networkInfo: NetworkInfo,
) {
this.Web3 = web3Dep
const { provider, address, pollingPeriod, key } = networkInfo

if (!provider) {
throw new Error(`Missing provider for ${name}`)
throw new Error(`Missing provider for ${key}`)
}
const web3 = new Web3(
const web3: Web3 = new this.Web3(
new Web3.providers.HttpProvider(provider, { timeout: 30000 }),
)
// TODO: why this type isn't working?
this.contract = new web3.eth.Contract(WitnetPriceFeedsABI as any, address)
this.contract = new web3.eth.Contract(
WitnetPriceFeedsABI as AbiItem[],
address,
)
;(this.pollingPeriod = pollingPeriod), (this.repositories = repositories)
this.network = key
this.configuration = configuration
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/web3Middleware/PriceFeed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbiItem, FeedInfo, Network } from '../types'
import { AbiItem, FeedInfo, Network } from '../../types'
import { SupportedFeed } from './NetworkRouter'
import WitnetPriceFeedsABI from './../abi/WitnetPriceFeeds.json'
import { createFeedFullName } from '../utils'
Expand Down
23 changes: 13 additions & 10 deletions packages/api/src/web3Middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
FeedInfo,
Repositories,
ContractInfo,
Contract,
} from '../types'
} from '../../types'
import { isZeroAddress } from '../utils/index'
import { getProvider } from './provider'
import { NetworkRouter } from './NetworkRouter'
Expand All @@ -18,14 +17,14 @@ export class Web3Middleware {
public repositories: Repositories
private Web3: typeof Web3
public legacyDataFeeds: Array<FeedInfo>
public routerContractByNetwork: Record<string, Contract> = {}
public routerContractByNetwork: Record<string, any> = {}
public contractIdByFeedId: Record<string, string> = {}
// feedFullname -> address
public currentFeedAddresses: Record<string, string> = {}
public networkRouters: Array<NetworkRouter>
public configuration: Configuration

private intervals = []
private intervals: NodeJS.Timer[] = []

constructor(
configuration: Configuration,
Expand All @@ -49,6 +48,7 @@ export class Web3Middleware {
.forEach((networkInfo) =>
new NetworkRouter(
this.configuration,
this.Web3,
this.repositories,
networkInfo,
).listen(),
Expand Down Expand Up @@ -114,7 +114,7 @@ export class Web3Middleware {
[feedInfo.feedFullName]: {
feedInfo,
},
}
} as Record<string, { feedInfo: FeedInfo; feedId: ObjectId }>
},
{},
)
Expand All @@ -131,7 +131,7 @@ export class Web3Middleware {
})

const promises = Object.values(feedDictionary).map(
async (entry) => await this.listenToDataFeed(entry.feedInfo),
async (entry) => await this.listenToDataFeed(entry?.feedInfo),
)

Promise.all(promises).catch((err) => {
Expand All @@ -153,16 +153,19 @@ export class Web3Middleware {
try {
const provider = getProvider(feedInfo.network)
const timeout = 30000
let web3: Web3 | undefined
//FIXME: make timeout work
const web3 = new this.Web3(
new Web3.providers.HttpProvider(provider, { timeout }),
)
if (provider) {
web3 = new this.Web3(
new Web3.providers.HttpProvider(provider, { timeout }),
)
}
//FIXME: use web3 timeout instead of custom
setTimeout(() => {
reject('Timeout')
}, timeout)

if (!this.routerContractByNetwork[feedInfo.network]) {
if (web3 && !this.routerContractByNetwork[feedInfo.network]) {
this.routerContractByNetwork[feedInfo.network] =
new web3.eth.Contract(feedInfo.routerAbi, feedInfo.routerAddress)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/web3Middleware/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Network } from './../types'
import { Network } from '../../types'

export function getProvider(network: Network) {
// The provider env variable name should be the network name specified in the config plus '_PROVIDER' ex.MOONBEAM_MOONBASE_PROVIDER
Expand Down
3 changes: 3 additions & 0 deletions packages/api/test/.test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ARBITRUM_ONE_PROVIDER=provider-arbitrum-one
ARBITRUM_GOERLI_PROVIDER=provider-arbitrum-goerli
AVALANCHE_FUJI_PROVIDER=provider-avalanche-fuji
2 changes: 2 additions & 0 deletions packages/api/test/dotenv-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config({ path: '.test.env' })
6 changes: 6 additions & 0 deletions packages/api/test/getNetworksConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ describe('validateNetworkConfig', () => {
it('check if the structure is correct', async () => {
const networksConfig = normalizeNetworkConfig(dataFeedsRouterConfig)
const expected = [
{
chain: 'Arbitrum',
key: 'arbitrum-one',
label: 'Arbitrum ONE',
mainnet: true,
},
{
key: 'boba-mainnet',
label: 'Boba Mainnet',
Expand Down
25 changes: 17 additions & 8 deletions packages/api/test/networkRouter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Repositories } from '../../src/types.js'
import { NetworkRouter } from '../../src/web3Middleware/NetworkRouter'
import { Network, Repositories } from '../types'
import { NetworkInfo, NetworkRouter } from '../src/web3Middleware/NetworkRouter'
import { Configuration } from '../src/web3Middleware/Configuration'
import dataFeedsRouter from './web3Middleware/dataFeedsRouter.json'
// FIXME: create a proper mock for web3
import web3 from 'web3'

describe('NetworkRouter', () => {
it('should fetch network contract', async () => {
Expand All @@ -8,15 +12,20 @@ describe('NetworkRouter', () => {
feedRepository: {},
resultRequestRepository: {},
} as unknown as Repositories
const configuration = new Configuration(dataFeedsRouter)
const networkInfo = {
address: '0x9999999d139bdBFbF25923ba39F63bBFc7593400',
provider: 'https://rpc2.sepolia.org',
name: 'ethereum.sepholia',
key: Network.EthereumSepolia,
pollingPeriod: 1,
maxSecsBetweenUpdates: 1,
}
const router = new NetworkRouter(repositories, networkInfo)

networkName: 'ethereum',
} as NetworkInfo
const router = new NetworkRouter(
configuration,
web3,
repositories,
networkInfo,
)
const snapshot = await router.getSnapshot()

expect(snapshot.feeds[0].caption).toBeTruthy()
Expand All @@ -27,6 +36,6 @@ describe('NetworkRouter', () => {
expect(snapshot.feeds[0].timestamp).toBeTruthy()
expect(snapshot.feeds[0].value).toBeTruthy()

expect(snapshot.network).toBe('ethereum.sepholia')
expect(snapshot.network).toBe('ethereum-sepolia')
})
})
Loading

0 comments on commit 5ca3d9f

Please sign in to comment.