-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track V1 OApps latest configuration (#111)
- Loading branch information
Showing
39 changed files
with
2,165 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Env } from '@l2beat/backend-tools' | ||
import { MulticallConfig } from '@l2beat/discovery' | ||
import { EthereumAddress } from '@lz/libs' | ||
|
||
import { Config, TrackingSubmoduleConfig } from './Config' | ||
import { coreAddressesV1, ethereumMulticallConfig } from './discovery/ethereum' | ||
|
||
export function getCommonTrackingConfig(env: Env): Config['tracking'] { | ||
const createTrackingConfig = configFromTemplate(env) | ||
|
||
return { | ||
ethereum: createTrackingConfig({ | ||
chainNamePrefix: 'ETHEREUM', | ||
multicallConfig: ethereumMulticallConfig, | ||
ulnV2Address: EthereumAddress(coreAddressesV1.ultraLightNodeV2), | ||
}), | ||
} | ||
} | ||
|
||
function configFromTemplate(env: Env) { | ||
return function ({ | ||
chainNamePrefix, | ||
multicallConfig, | ||
ulnV2Address, | ||
}: { | ||
/** | ||
* The prefix of the environment variables that configure the chain. | ||
*/ | ||
chainNamePrefix: string | ||
|
||
/** | ||
* Multicall configuration for given chain | ||
*/ | ||
multicallConfig: MulticallConfig | ||
|
||
/** | ||
* Address of UlnV2 on given chain | ||
*/ | ||
ulnV2Address: EthereumAddress | ||
}): TrackingSubmoduleConfig { | ||
const isEnabled = env.boolean(`${chainNamePrefix}_TRACKING_ENABLED`, false) | ||
|
||
if (!isEnabled) { | ||
return { | ||
enabled: false, | ||
config: null, | ||
} | ||
} | ||
|
||
return { | ||
enabled: true, | ||
config: { | ||
listApiUrl: env.string( | ||
`${chainNamePrefix}_TRACKING_LIST_API_URL`, | ||
'https://l2beat-production.herokuapp.com/api/integrations/layerzero-oapps', | ||
), | ||
tickIntervalMs: env.integer('TRACKING_TICK_INTERVAL_MS', 60_000_000), | ||
rpcUrl: env.string(`${chainNamePrefix}_TRACKING_RPC_URL`), | ||
ulnV2Address: ulnV2Address, | ||
multicall: multicallConfig, | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/backend/src/peripherals/database/OAppConfigurationRepository.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Logger } from '@l2beat/backend-tools' | ||
import { ChainId, EthereumAddress } from '@lz/libs' | ||
import { expect } from 'earl' | ||
|
||
import { setupDatabaseTestSuite } from '../../test/database' | ||
import { | ||
OAppConfigurationRecord, | ||
OAppConfigurationRepository, | ||
} from './OAppConfigurationRepository' | ||
|
||
describe(OAppConfigurationRepository.name, () => { | ||
const { database } = setupDatabaseTestSuite() | ||
const repository = new OAppConfigurationRepository(database, Logger.SILENT) | ||
|
||
before(async () => await repository.deleteAll()) | ||
afterEach(async () => await repository.deleteAll()) | ||
|
||
describe(OAppConfigurationRepository.prototype.addMany.name, () => { | ||
it('merges rows on insert', async () => { | ||
const record1 = mockRecord({ oAppId: 1, targetChainId: ChainId.ETHEREUM }) | ||
const record2 = mockRecord({ oAppId: 2, targetChainId: ChainId.OPTIMISM }) | ||
|
||
await repository.addMany([record1, record2]) | ||
|
||
const recordsBeforeMerge = await repository.findAll() | ||
|
||
await repository.addMany([record1, record2]) | ||
|
||
const recordsAfterMerge = await repository.findAll() | ||
|
||
expect(recordsBeforeMerge.length).toEqual(2) | ||
expect(recordsAfterMerge.length).toEqual(2) | ||
}) | ||
}) | ||
|
||
describe(OAppConfigurationRepository.prototype.findByOAppIds.name, () => { | ||
it('returns only records with matching oAppId', async () => { | ||
const record1 = mockRecord({ | ||
oAppId: 1, | ||
}) | ||
const record2 = mockRecord({ | ||
oAppId: 2, | ||
}) | ||
const record3 = mockRecord({ | ||
oAppId: 3, | ||
}) | ||
|
||
await repository.addMany([record1, record2, record3]) | ||
|
||
const result = await repository.findByOAppIds([1, 2]) | ||
|
||
expect(result.length).toEqual(2) | ||
}) | ||
}) | ||
}) | ||
|
||
function mockRecord( | ||
overrides?: Partial<OAppConfigurationRecord>, | ||
): OAppConfigurationRecord { | ||
return { | ||
oAppId: 1, | ||
targetChainId: ChainId.ETHEREUM, | ||
configuration: { | ||
inboundBlockConfirmations: 2, | ||
outboundBlockConfirmations: 2, | ||
relayer: EthereumAddress.random(), | ||
oracle: EthereumAddress.random(), | ||
outboundProofType: 2, | ||
inboundProofLibraryVersion: 2, | ||
}, | ||
...overrides, | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/backend/src/peripherals/database/OAppConfigurationRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Logger } from '@l2beat/backend-tools' | ||
import { ChainId } from '@lz/libs' | ||
import type { OAppConfigurationRow } from 'knex/types/tables' | ||
|
||
import { OAppConfiguration } from '../../tracking/domain/configuration' | ||
import { BaseRepository, CheckConvention } from './shared/BaseRepository' | ||
import { Database } from './shared/Database' | ||
|
||
export interface OAppConfigurationRecord { | ||
oAppId: number | ||
targetChainId: ChainId | ||
configuration: OAppConfiguration | ||
} | ||
|
||
export class OAppConfigurationRepository extends BaseRepository { | ||
constructor(database: Database, logger: Logger) { | ||
super(database, logger) | ||
this.autoWrap<CheckConvention<OAppConfigurationRepository>>(this) | ||
} | ||
|
||
public async addMany(records: OAppConfigurationRecord[]): Promise<number> { | ||
const rows = records.map(toRow) | ||
const knex = await this.knex() | ||
|
||
await knex('oapp_configuration') | ||
.insert(rows) | ||
.onConflict(['oapp_id', 'target_chain_id']) | ||
.merge() | ||
|
||
return rows.length | ||
} | ||
|
||
public async findAll(): Promise<OAppConfigurationRecord[]> { | ||
const knex = await this.knex() | ||
|
||
const rows = await knex('oapp_configuration').select('*') | ||
|
||
return rows.map(toRecord) | ||
} | ||
public async findByOAppIds( | ||
oAppIds: number[], | ||
): Promise<OAppConfigurationRecord[]> { | ||
const knex = await this.knex() | ||
|
||
const rows = await knex('oapp_configuration') | ||
.select('*') | ||
.whereIn('oapp_id', oAppIds) | ||
|
||
return rows.map(toRecord) | ||
} | ||
|
||
async deleteAll(): Promise<number> { | ||
const knex = await this.knex() | ||
return knex('oapp_configuration').delete() | ||
} | ||
} | ||
|
||
function toRow(record: OAppConfigurationRecord): OAppConfigurationRow { | ||
return { | ||
oapp_id: record.oAppId, | ||
target_chain_id: Number(record.targetChainId), | ||
configuration: JSON.stringify(record.configuration), | ||
} | ||
} | ||
|
||
function toRecord(row: OAppConfigurationRow): OAppConfigurationRecord { | ||
return { | ||
oAppId: row.oapp_id, | ||
targetChainId: ChainId(row.target_chain_id), | ||
configuration: OAppConfiguration.parse(row.configuration), | ||
} | ||
} |
Oops, something went wrong.