From ef81d78ec6d2a04594b82455ef4e6abfabe415f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Podsiad=C5=82y?= <67391475+sdlyy@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:46:59 +0100 Subject: [PATCH] Send address book along with tracking information (#114) --- .../backend/src/tracking/TrackingModule.ts | 1 + .../tracking/http/TrackingController.test.ts | 52 ++++++++++++++++++- .../src/tracking/http/TrackingController.ts | 25 +++++++++ packages/libs/src/apis/TrackingApi.ts | 9 ++++ 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/tracking/TrackingModule.ts b/packages/backend/src/tracking/TrackingModule.ts index 690ae0f..b7e6aa4 100644 --- a/packages/backend/src/tracking/TrackingModule.ts +++ b/packages/backend/src/tracking/TrackingModule.ts @@ -90,6 +90,7 @@ function createTrackingModule(dependencies: Dependencies): ApplicationModule { oAppRepo, oAppConfigurationRepo, oAppDefaultConfigurationRepo, + currDiscoveryRepo, ) const router = createTrackingRouter(controller) diff --git a/packages/backend/src/tracking/http/TrackingController.test.ts b/packages/backend/src/tracking/http/TrackingController.test.ts index 7062df2..06d2829 100644 --- a/packages/backend/src/tracking/http/TrackingController.test.ts +++ b/packages/backend/src/tracking/http/TrackingController.test.ts @@ -1,7 +1,8 @@ import { assert } from '@l2beat/backend-tools' import { ChainId, EthereumAddress } from '@lz/libs' -import { expect, mockObject } from 'earl' +import { expect, mockFn, mockObject } from 'earl' +import { CurrentDiscoveryRepository } from '../../peripherals/database/CurrentDiscoveryRepository' import { OAppConfigurationRecord, OAppConfigurationRepository, @@ -28,11 +29,15 @@ describe(TrackingController.name, () => { mockObject({ getBySourceChain: () => Promise.resolve([]), }) + const currDiscoveryRepo = mockObject({ + find: mockFn().resolvesTo(null), + }) const controller = new TrackingController( oAppRepo, oAppConfigRepo, oAppDefaultConfigRepo, + currDiscoveryRepo, ) const result = await controller.getOApps(chainId) @@ -127,6 +132,31 @@ describe(TrackingController.name, () => { }, ] + const mockDiscoveryOutput = { + contracts: [ + { + name: 'Oracle', + address: EthereumAddress.random(), + unverified: false, + }, + { + name: 'Relayer', + address: EthereumAddress.random(), + unverified: true, + }, + { + name: 'Endpoint', + address: EthereumAddress.random(), + unverified: false, + }, + ], + eoas: [ + EthereumAddress.random(), + EthereumAddress.random(), + EthereumAddress.random(), + ], + } + const oAppRepo = mockObject({ getBySourceChain: () => Promise.resolve([oAppA, oAppB]), }) @@ -138,11 +168,18 @@ describe(TrackingController.name, () => { mockObject({ getBySourceChain: () => Promise.resolve(mockDefaultConfigurations), }) + const currDiscoveryRepo = mockObject({ + find: mockFn().resolvesTo({ + chainId, + discoveryOutput: mockDiscoveryOutput, + }), + }) const controller = new TrackingController( oAppRepo, oAppConfigRepo, oAppDefaultConfigRepo, + currDiscoveryRepo, ) const result = await controller.getOApps(chainId) @@ -199,6 +236,19 @@ describe(TrackingController.name, () => { configuration: c.configuration, })), ) + + expect(result.addressInfo).toEqual([ + ...mockDiscoveryOutput.contracts.map((contract) => ({ + name: contract.name, + address: contract.address, + verified: !contract.unverified, + })), + ...mockDiscoveryOutput.eoas.map((eoa) => ({ + name: 'EOA', + address: eoa, + verified: true, + })), + ]) }) }) }) diff --git a/packages/backend/src/tracking/http/TrackingController.ts b/packages/backend/src/tracking/http/TrackingController.ts index e7cf63d..ad38832 100644 --- a/packages/backend/src/tracking/http/TrackingController.ts +++ b/packages/backend/src/tracking/http/TrackingController.ts @@ -1,11 +1,14 @@ import { assert } from '@l2beat/backend-tools' +import { DiscoveryOutput } from '@l2beat/discovery-types' import { + AddressInfo, ChainId, OAppsResponse, OAppWithConfigs, ResolvedConfigurationWithAppId, } from '@lz/libs' +import { CurrentDiscoveryRepository } from '../../peripherals/database/CurrentDiscoveryRepository' import { OAppConfigurationRecord, OAppConfigurationRepository, @@ -27,9 +30,18 @@ class TrackingController { private readonly oAppRepo: OAppRepository, private readonly oAppConfigurationRepo: OAppConfigurationRepository, private readonly oAppDefaultConfigRepo: OAppDefaultConfigurationRepository, + private readonly currDiscoveryRepository: CurrentDiscoveryRepository, ) {} async getOApps(chainId: ChainId): Promise { + const discovery = await this.currDiscoveryRepository.find(chainId) + + if (!discovery) { + return null + } + + const addressInfo = outputToAddressInfo(discovery.discoveryOutput) + const defaultConfigurations = await this.oAppDefaultConfigRepo.getBySourceChain(chainId) @@ -62,10 +74,23 @@ class TrackingController { targetChainId: record.targetChainId, configuration: record.configuration, })), + + addressInfo, } } } +function outputToAddressInfo(output: DiscoveryOutput): AddressInfo[] { + const { eoas, contracts } = output + + return contracts + .map((contract) => ({ + address: contract.address, + name: contract.name, + verified: !contract.unverified, + })) + .concat(eoas.map((eoa) => ({ address: eoa, name: 'EOA', verified: true }))) +} function attachConfigurations( oApps: OAppRecord[], configurations: ResolvedConfigurationWithAppId[], diff --git a/packages/libs/src/apis/TrackingApi.ts b/packages/libs/src/apis/TrackingApi.ts index f9a9529..e1351f9 100644 --- a/packages/libs/src/apis/TrackingApi.ts +++ b/packages/libs/src/apis/TrackingApi.ts @@ -4,6 +4,7 @@ import { ChainId } from '../chainId' import { branded, EthereumAddress } from '../utils' export { + AddressInfo, OAppsResponse, OAppWithConfigs, ResolvedConfiguration, @@ -65,6 +66,13 @@ const OAppWithConfigs = z.object({ type OAppWithConfigs = z.infer +const AddressInfo = z.object({ + address: branded(z.string(), EthereumAddress), + name: z.string(), + verified: z.boolean(), +}) +type AddressInfo = z.infer + const OAppsResponse = z.object({ sourceChainId: branded(z.number(), ChainId), oApps: z.array(OAppWithConfigs), @@ -74,6 +82,7 @@ const OAppsResponse = z.object({ configuration: OAppConfiguration, }), ), + addressInfo: z.array(AddressInfo), }) type OAppsResponse = z.infer