Skip to content

Commit

Permalink
Send address book along with tracking information (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdlyy authored Mar 26, 2024
1 parent 8dd0c86 commit ef81d78
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/backend/src/tracking/TrackingModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function createTrackingModule(dependencies: Dependencies): ApplicationModule {
oAppRepo,
oAppConfigurationRepo,
oAppDefaultConfigurationRepo,
currDiscoveryRepo,
)

const router = createTrackingRouter(controller)
Expand Down
52 changes: 51 additions & 1 deletion packages/backend/src/tracking/http/TrackingController.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,11 +29,15 @@ describe(TrackingController.name, () => {
mockObject<OAppDefaultConfigurationRepository>({
getBySourceChain: () => Promise.resolve([]),
})
const currDiscoveryRepo = mockObject<CurrentDiscoveryRepository>({
find: mockFn().resolvesTo(null),
})

const controller = new TrackingController(
oAppRepo,
oAppConfigRepo,
oAppDefaultConfigRepo,
currDiscoveryRepo,
)
const result = await controller.getOApps(chainId)

Expand Down Expand Up @@ -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<OAppRepository>({
getBySourceChain: () => Promise.resolve([oAppA, oAppB]),
})
Expand All @@ -138,11 +168,18 @@ describe(TrackingController.name, () => {
mockObject<OAppDefaultConfigurationRepository>({
getBySourceChain: () => Promise.resolve(mockDefaultConfigurations),
})
const currDiscoveryRepo = mockObject<CurrentDiscoveryRepository>({
find: mockFn().resolvesTo({
chainId,
discoveryOutput: mockDiscoveryOutput,
}),
})

const controller = new TrackingController(
oAppRepo,
oAppConfigRepo,
oAppDefaultConfigRepo,
currDiscoveryRepo,
)

const result = await controller.getOApps(chainId)
Expand Down Expand Up @@ -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,
})),
])
})
})
})
Expand Down
25 changes: 25 additions & 0 deletions packages/backend/src/tracking/http/TrackingController.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<OAppsResponse | null> {
const discovery = await this.currDiscoveryRepository.find(chainId)

if (!discovery) {
return null
}

const addressInfo = outputToAddressInfo(discovery.discoveryOutput)

const defaultConfigurations =
await this.oAppDefaultConfigRepo.getBySourceChain(chainId)

Expand Down Expand Up @@ -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[],
Expand Down
9 changes: 9 additions & 0 deletions packages/libs/src/apis/TrackingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChainId } from '../chainId'
import { branded, EthereumAddress } from '../utils'

export {
AddressInfo,
OAppsResponse,
OAppWithConfigs,
ResolvedConfiguration,
Expand Down Expand Up @@ -65,6 +66,13 @@ const OAppWithConfigs = z.object({

type OAppWithConfigs = z.infer<typeof OAppWithConfigs>

const AddressInfo = z.object({
address: branded(z.string(), EthereumAddress),
name: z.string(),
verified: z.boolean(),
})
type AddressInfo = z.infer<typeof AddressInfo>

const OAppsResponse = z.object({
sourceChainId: branded(z.number(), ChainId),
oApps: z.array(OAppWithConfigs),
Expand All @@ -74,6 +82,7 @@ const OAppsResponse = z.object({
configuration: OAppConfiguration,
}),
),
addressInfo: z.array(AddressInfo),
})

type OAppsResponse = z.infer<typeof OAppsResponse>

0 comments on commit ef81d78

Please sign in to comment.