-
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.
- Loading branch information
Showing
17 changed files
with
585 additions
and
54 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/backend/src/peripherals/database/OAppRemoteRepository.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,40 @@ | ||
import { Logger } from '@l2beat/backend-tools' | ||
import { ChainId } from '@lz/libs' | ||
import { expect } from 'earl' | ||
|
||
import { setupDatabaseTestSuite } from '../../test/database' | ||
import { OAppRemoteRecord, OAppRemoteRepository } from './OAppRemoteRepository' | ||
|
||
describe(OAppRemoteRepository.name, () => { | ||
const { database } = setupDatabaseTestSuite() | ||
const repository = new OAppRemoteRepository(database, Logger.SILENT) | ||
|
||
before(async () => await repository.deleteAll()) | ||
afterEach(async () => await repository.deleteAll()) | ||
|
||
describe(OAppRemoteRepository.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) | ||
}) | ||
}) | ||
}) | ||
|
||
function mockRecord(overrides?: Partial<OAppRemoteRecord>): OAppRemoteRecord { | ||
return { | ||
oAppId: 1, | ||
targetChainId: ChainId.ETHEREUM, | ||
...overrides, | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
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) | ||
} | ||
|
||
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
19 changes: 19 additions & 0 deletions
19
packages/backend/src/tracking/domain/indexers/InMemoryIndexer.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,19 @@ | ||
import { ChildIndexer } from '@l2beat/uif' | ||
|
||
export { InMemoryIndexer } | ||
|
||
abstract class InMemoryIndexer extends ChildIndexer { | ||
protected height = 0 | ||
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) | ||
} | ||
} |
32 changes: 15 additions & 17 deletions
32
packages/backend/src/tracking/domain/indexers/OAppConfigurationIndexer.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
Oops, something went wrong.