Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Feb 27, 2024
1 parent 8d85101 commit 9b1e7ea
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 20 deletions.
22 changes: 22 additions & 0 deletions packages/api/src/web3Middleware/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,32 @@ export class Configuration {

// return networks using the new price feeds router contract
public listNetworksUsingPriceFeedsContract(): Array<NetworkInfo> {
console.log('-------$$$$$$$$$---------')
console.log('0', this.configurationFile.chains)
console.log('1', Object.values(this.configurationFile.chains))
console.log(
'2',
Object.values(this.configurationFile.chains).flatMap((chain) =>
Object.entries(chain.networks),
),
)
console.log(
'3',
Object.values(this.configurationFile.chains)
.flatMap((chain) => Object.entries(chain.networks))
.filter(([_, network]) => network.legacy === false),
)
return Object.values(this.configurationFile.chains)
.flatMap((chain) => Object.entries(chain.networks))
.filter(([_, network]) => network.legacy === false)
.map(([networkKey, network]) => {
console.log('GET NETWORK INFO!!!!!', {
provider: getProvider(networkKey.replaceAll('.', '-') as Network),
address: network.address,
pollingPeriod: network.pollingPeriod,
key: this.fromNetworkKeyToNetwork(networkKey),
networkName: network.name,
})
return {
provider: getProvider(networkKey.replaceAll('.', '-') as Network),
address: network.address,
Expand Down
14 changes: 9 additions & 5 deletions packages/api/src/web3Middleware/NetworkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ export class NetworkRouter {
if (!provider) {
throw new Error(`Missing provider for ${name}`)
}
const provider = new Web3.providers.HttpProvider(provider, { timeout: 30000 })
console.log('provider ')
const web3: Web3 = new Web3(new Web3.providers.HttpProvider(provider, { timeout: 30000 }))
console.log('Contract----', web3)
const providerWithTimeout = new Web3.providers.HttpProvider(provider, {
timeout: 30000,
})
const web3: Web3 = new Web3(providerWithTimeout)
console.log('Contract----', web3.eth)
// TODO: why this type isn't working?
this.contract = new web3.eth.Contract(WitnetPriceFeedsABI as AbiItem[], 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
12 changes: 7 additions & 5 deletions packages/api/src/web3Middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ export class Web3Middleware {
}

public async listenWitnetPriceFeeds() {
console.log('-----------')
this.configuration
.listNetworksUsingPriceFeedsContract()
.forEach((networkInfo) =>
new NetworkRouter(
.forEach((networkInfo) => {
console.log('networkInfo.....', networkInfo)
return new NetworkRouter(
this.configuration,
this.repositories,
networkInfo,
).listen(),
)
).listen()
})
}

private async initializeAddresses(): Promise<Array<FeedInfo>> {
Expand Down Expand Up @@ -152,7 +154,7 @@ export class Web3Middleware {
try {
const provider = getProvider(feedInfo.network)
const timeout = 30000
let web3: Web3 | undefined;
let web3: Web3 | undefined
//FIXME: make timeout work
if (provider) {
web3 = new this.Web3(
Expand Down
9 changes: 5 additions & 4 deletions packages/api/test/networkRouter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { Network, Repositories } from '../types'
import { NetworkInfo, NetworkRouter } from '../src/web3Middleware/NetworkRouter'
import { Configuration } from '../src/web3Middleware/Configuration'
import dataFeedsRouter from './web3Middleware/dataFeedsRouter.json'

describe('NetworkRouter', () => {
it('should fetch network contract', async () => {
it.skip('should fetch network contract', async () => {
// FIXME: create a proper mock
const repositories = {
feedRepository: {},
resultRequestRepository: {},
} as unknown as Repositories
const configuration = ({} as unknown) as Configuration
const configuration = new Configuration(dataFeedsRouter)
const networkInfo = {
address: '0x9999999d139bdBFbF25923ba39F63bBFc7593400',
provider: 'https://rpc2.sepolia.org',
key: Network.EthereumSepolia,
pollingPeriod: 1,
networkName: 'ethereum'
networkName: 'ethereum',
} as NetworkInfo
const router = new NetworkRouter(configuration, repositories, networkInfo)

router.listen()
const snapshot = await router.getSnapshot()

expect(snapshot.feeds[0].caption).toBeTruthy()
Expand Down
22 changes: 22 additions & 0 deletions packages/api/test/web3Middleware/dataFeedsRouter.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
{
"abi": "./src/abi/WitnetPriceRouter.json",
"chains": {
"arbitrum": {
"name": "Arbitrum",
"networks": {
"arbitrum.one": {
"legacy": false,
"address": "0x9999999d139bdBFbF25923ba39F63bBFc7593400",
"blockExplorer": "https://arbiscan.io/address/{address}",
"color": "#E84142",
"mainnet": true,
"name": "Arbitrum ONE",
"pollingPeriod": 120000,
"feeds": {
"Price-ETH/USD-6": {
"label": "$",
"deviationPercentage": 3.5,
"maxSecsBetweenUpdates": 86400,
"minSecsBetweenUpdates": 900
}
}
}
}
},
"avalanche": {
"name": "Avalanche",
"hide": true,
Expand Down
7 changes: 1 addition & 6 deletions packages/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@
"downlevelIteration": true,
"types": ["jest"]
},
"include": [
"src/",
"types.ts",
"**/*.spec.ts",
"**/*.test.ts"
]
"include": ["src/", "types.ts", "**/*.spec.ts", "**/*.test.ts"]
}

0 comments on commit 9b1e7ea

Please sign in to comment.