-
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.
feat: track supported remotes for oApps
- Loading branch information
Showing
9 changed files
with
366 additions
and
4 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
packages/backend/src/peripherals/database/OAppRemoteRepository.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,66 @@ | ||
import { Logger } from '@l2beat/backend-tools' | ||
import { ChainId } from '@lz/libs' | ||
import type { OAppRemoteRow } from 'knex/types/tables' | ||
|
||
import { BaseRepository, CheckConvention } from './shared/BaseRepository' | ||
import { Database } from './shared/Database' | ||
|
||
export interface OAppRemoteRecord { | ||
oAppId: number | ||
targetChainId: ChainId | ||
} | ||
|
||
export class OAppRemoteRepository extends BaseRepository { | ||
constructor(database: Database, logger: Logger) { | ||
super(database, logger) | ||
this.autoWrap<CheckConvention<OAppRemoteRepository>>(this) | ||
} | ||
|
||
public async addMany(records: OAppRemoteRecord[]): Promise<number> { | ||
const rows = records.map(toRow) | ||
const knex = await this.knex() | ||
|
||
await knex('oapp_remote') | ||
.insert(rows) | ||
.onConflict(['oapp_id', 'target_chain_id']) | ||
.merge() | ||
|
||
return rows.length | ||
} | ||
|
||
public async findAll(): Promise<OAppRemoteRecord[]> { | ||
const knex = await this.knex() | ||
|
||
const rows = await knex('oapp_remote').select('*') | ||
|
||
return rows.map(toRecord) | ||
} | ||
public async findByOAppIds(oAppIds: number[]): Promise<OAppRemoteRecord[]> { | ||
const knex = await this.knex() | ||
|
||
const rows = await knex('oapp_remote') | ||
.select('*') | ||
.whereIn('oapp_id', oAppIds) | ||
|
||
return rows.map(toRecord) | ||
} | ||
|
||
async deleteAll(): Promise<number> { | ||
const knex = await this.knex() | ||
return knex('oapp_remote').delete() | ||
} | ||
} | ||
|
||
function toRow(record: OAppRemoteRecord): OAppRemoteRow { | ||
return { | ||
oapp_id: record.oAppId, | ||
target_chain_id: Number(record.targetChainId), | ||
} | ||
} | ||
|
||
function toRecord(row: OAppRemoteRow): OAppRemoteRecord { | ||
return { | ||
oAppId: row.oapp_id, | ||
targetChainId: ChainId(row.target_chain_id), | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/backend/src/peripherals/database/migrations/017_oapps_remotes.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,26 @@ | ||
/* | ||
====== IMPORTANT NOTICE ====== | ||
DO NOT EDIT OR RENAME THIS FILE | ||
This is a migration file. Once created the file should not be renamed or edited, | ||
because migrations are only run once on the production server. | ||
If you find that something was incorrectly set up in the `up` function you | ||
should create a new migration file that fixes the issue. | ||
*/ | ||
|
||
import { Knex } from 'knex' | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await knex.schema.createTable('oapp_remote', (table) => { | ||
table.integer('oapp_id').notNullable() | ||
table.integer('target_chain_id').notNullable() | ||
table.unique(['oapp_id', 'target_chain_id']) | ||
}) | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.schema.dropTable('oapp_remote') | ||
} |
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
58 changes: 58 additions & 0 deletions
58
packages/backend/src/tracking/domain/indexers/OAppRemotesIndexer.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,58 @@ | ||
import { Logger } from '@l2beat/backend-tools' | ||
import { ChildIndexer, Indexer } from '@l2beat/uif' | ||
import { ChainId } from '@lz/libs' | ||
|
||
import { | ||
OAppRemoteRecord, | ||
OAppRemoteRepository, | ||
} from '../../../peripherals/database/OAppRemoteRepository' | ||
import { OAppRepository } from '../../../peripherals/database/OAppRepository' | ||
import { OAppRemotesProvider } from '../providers/OAppRemotesProvider' | ||
|
||
export class OAppRemoteIndexer extends ChildIndexer { | ||
protected height = 0 | ||
constructor( | ||
logger: Logger, | ||
private readonly chainId: ChainId, | ||
private readonly oAppRepo: OAppRepository, | ||
private readonly oAppRemotesRepo: OAppRemoteRepository, | ||
private readonly oAppRemoteProvider: OAppRemotesProvider, | ||
parents: Indexer[], | ||
) { | ||
super(logger.tag(ChainId.getName(chainId)), parents) | ||
} | ||
|
||
protected override async update(_from: number, to: number): Promise<number> { | ||
const oApps = await this.oAppRepo.getBySourceChain(this.chainId) | ||
|
||
const records: OAppRemoteRecord[][] = await Promise.all( | ||
oApps.map(async (oApp) => { | ||
const supportedRemoteChains = | ||
await this.oAppRemoteProvider.getSupportedRemotes(oApp.address) | ||
|
||
return supportedRemoteChains.map((chainId) => ({ | ||
oAppId: oApp.id, | ||
targetChainId: chainId, | ||
})) | ||
}), | ||
) | ||
|
||
await this.oAppRemotesRepo.deleteAll() | ||
await this.oAppRemotesRepo.addMany(records.flat()) | ||
|
||
return to | ||
} | ||
|
||
public override getSafeHeight(): Promise<number> { | ||
return Promise.resolve(this.height) | ||
} | ||
|
||
protected override setSafeHeight(height: number): Promise<void> { | ||
this.height = height | ||
return Promise.resolve() | ||
} | ||
|
||
protected override invalidate(targetHeight: number): Promise<number> { | ||
return Promise.resolve(targetHeight) | ||
} | ||
} |
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
Oops, something went wrong.