From 9b1e7ea488237190f9a22cd50b438b970d84e5fa Mon Sep 17 00:00:00 2001 From: gabaldon Date: Tue, 27 Feb 2024 17:09:12 +0100 Subject: [PATCH] wip --- .../api/src/web3Middleware/Configuration.ts | 22 +++++++++++++++++++ .../api/src/web3Middleware/NetworkRouter.ts | 14 +++++++----- packages/api/src/web3Middleware/index.ts | 12 +++++----- packages/api/test/networkRouter.spec.ts | 9 ++++---- .../test/web3Middleware/dataFeedsRouter.json | 22 +++++++++++++++++++ packages/api/tsconfig.json | 7 +----- 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/packages/api/src/web3Middleware/Configuration.ts b/packages/api/src/web3Middleware/Configuration.ts index 6026c388..55109805 100644 --- a/packages/api/src/web3Middleware/Configuration.ts +++ b/packages/api/src/web3Middleware/Configuration.ts @@ -41,10 +41,32 @@ export class Configuration { // return networks using the new price feeds router contract public listNetworksUsingPriceFeedsContract(): Array { + 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, diff --git a/packages/api/src/web3Middleware/NetworkRouter.ts b/packages/api/src/web3Middleware/NetworkRouter.ts index 767a06c4..f2ce3f8f 100644 --- a/packages/api/src/web3Middleware/NetworkRouter.ts +++ b/packages/api/src/web3Middleware/NetworkRouter.ts @@ -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 diff --git a/packages/api/src/web3Middleware/index.ts b/packages/api/src/web3Middleware/index.ts index 6891744e..b6e8b50c 100644 --- a/packages/api/src/web3Middleware/index.ts +++ b/packages/api/src/web3Middleware/index.ts @@ -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> { @@ -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( diff --git a/packages/api/test/networkRouter.spec.ts b/packages/api/test/networkRouter.spec.ts index 7056f96c..7da0d314 100644 --- a/packages/api/test/networkRouter.spec.ts +++ b/packages/api/test/networkRouter.spec.ts @@ -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() diff --git a/packages/api/test/web3Middleware/dataFeedsRouter.json b/packages/api/test/web3Middleware/dataFeedsRouter.json index ed49e023..c11c83e2 100644 --- a/packages/api/test/web3Middleware/dataFeedsRouter.json +++ b/packages/api/test/web3Middleware/dataFeedsRouter.json @@ -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, diff --git a/packages/api/tsconfig.json b/packages/api/tsconfig.json index 295df8aa..6bba2833 100644 --- a/packages/api/tsconfig.json +++ b/packages/api/tsconfig.json @@ -14,10 +14,5 @@ "downlevelIteration": true, "types": ["jest"] }, - "include": [ - "src/", - "types.ts", - "**/*.spec.ts", - "**/*.test.ts" - ] + "include": ["src/", "types.ts", "**/*.spec.ts", "**/*.test.ts"] }