-
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: add oapp remotes resolver layer
refac: abstract away resolvers refac: migrate memory height into abstract indexer
- Loading branch information
Showing
13 changed files
with
331 additions
and
163 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, | ||
} | ||
} |
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) | ||
} | ||
} |
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/providers/OAppRemotesProvider.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,58 @@ | ||
import { Logger } from '@l2beat/backend-tools' | ||
import { MulticallClient } from '@l2beat/discovery' | ||
// eslint-disable-next-line import/no-internal-modules | ||
import { MulticallResponse } from '@l2beat/discovery/dist/discovery/provider/multicall/types' | ||
// eslint-disable-next-line import/no-internal-modules | ||
import { Bytes } from '@l2beat/discovery/dist/utils/Bytes' | ||
import { ChainId, EthereumAddress } from '@lz/libs' | ||
import { expect, mockFn, mockObject } from 'earl' | ||
import { providers } from 'ethers' | ||
|
||
import { OAppInterfaceResolver } from './interface-resolvers/resolver' | ||
import { BlockchainOAppRemotesProvider } from './OAppRemotesProvider' | ||
|
||
describe(BlockchainOAppRemotesProvider.name, () => { | ||
it('resolves available remotes for given OApp', async () => { | ||
const blockNumber = 123 | ||
const oAppAddress = EthereumAddress.random() | ||
const rpcProvider = mockObject<providers.StaticJsonRpcProvider>({ | ||
getBlockNumber: mockFn().resolvesTo(blockNumber), | ||
}) | ||
|
||
const mcResponse: MulticallResponse[] = ChainId.getAll().map(() => ({ | ||
success: true, | ||
data: Bytes.fromHex('0x0'), | ||
})) | ||
|
||
const multicall = mockObject<MulticallClient>({ | ||
multicall: mockFn().resolvesTo(mcResponse), | ||
}) | ||
|
||
const supportedChains = [ChainId.ETHEREUM, ChainId.ARBITRUM] | ||
|
||
const resolverA: OAppInterfaceResolver = { | ||
isSupported: async () => false, | ||
encode: () => ({ address: oAppAddress, data: Bytes.fromHex('0x0') }), | ||
decode: () => true, | ||
} | ||
|
||
const resolverB: OAppInterfaceResolver = { | ||
isSupported: async () => true, | ||
encode: () => ({ address: oAppAddress, data: Bytes.fromHex('0x0') }), | ||
decode: () => true, | ||
} | ||
|
||
const provider = new BlockchainOAppRemotesProvider( | ||
rpcProvider, | ||
multicall, | ||
ChainId.ETHEREUM, | ||
supportedChains, | ||
[resolverA, resolverB], | ||
Logger.SILENT, | ||
) | ||
|
||
const result = await provider.getSupportedRemotes(oAppAddress) | ||
|
||
expect(result).toEqual(supportedChains) | ||
}) | ||
}) |
Oops, something went wrong.