Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send address book along with tracking information #114

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading