From 4de1702ea61620d2b5ebebca105285e691db082a Mon Sep 17 00:00:00 2001 From: Bruno Menezes Date: Fri, 24 May 2024 08:34:21 +1200 Subject: [PATCH] feat: Add configuration for optimism-mainnet and optimism-sepolia. (#33) --- .env | 2 + src/config.ts | 39 +++++++++++++++++ tests/processor.test.ts | 94 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/.env b/.env index ec3e2bf..bcb2f4c 100644 --- a/.env +++ b/.env @@ -7,6 +7,8 @@ CHAIN_ID="11155111" # RPC_URL_1= # RPC_URL_11155111= # RPC_URL_31337= +# RPC_URL_10= +# RPC_URL_11155420= # NORMALLY FOR USE WITH LOCAL/DEV # GENESIS_BLOCK_31337=22 # BLOCK_CONFIRMATIONS_31337=1 diff --git a/src/config.ts b/src/config.ts index 788cb78..8a9f910 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,10 +1,15 @@ import CartesiDAppFactoryMainnet from '@cartesi/rollups/deployments/mainnet/CartesiDAppFactory.json'; import InputBoxMainnet from '@cartesi/rollups/deployments/mainnet/InputBox.json'; +import CartesiDAppFactoryOptimism from '@cartesi/rollups/deployments/optimism/CartesiDAppFactory.json'; +import InputBoxOptimism from '@cartesi/rollups/deployments/optimism/InputBox.json'; +import CartesiDAppFactoryOptimismSepolia from '@cartesi/rollups/deployments/optimism_sepolia/CartesiDAppFactory.json'; +import InputBoxOptimismSepolia from '@cartesi/rollups/deployments/optimism_sepolia/InputBox.json'; import CartesiDAppFactorySepolia from '@cartesi/rollups/deployments/sepolia/CartesiDAppFactory.json'; import InputBoxSepolia from '@cartesi/rollups/deployments/sepolia/InputBox.json'; import mainnet from '@cartesi/rollups/export/abi/mainnet.json'; import { lookupArchive } from '@subsquid/archive-registry'; import { DataSource } from '@subsquid/evm-processor'; +import { optimism, optimismSepolia } from 'viem/chains'; // addresses are the same on all chains export const CartesiDAppFactoryAddress = @@ -65,6 +70,40 @@ export const getConfig = (chainId: number): ProcessorConfig => { value: process.env[BLOCK_CONFIRMATIONS], }), }; + case 10: //Optimism-Mainnet + return { + dataSource: { + archive: lookupArchive('optimism-mainnet'), + chain: + process.env[RPC_URL] ?? + optimism.rpcUrls.default.http[0], + }, + from: Math.min( + CartesiDAppFactoryOptimism.receipt.blockNumber, + InputBoxOptimism.receipt.blockNumber, + ), + finalityConfirmation: parseIntOr({ + defaultVal: FINALITY_CONFIRMATION, + value: process.env[BLOCK_CONFIRMATIONS], + }), + }; + case 11155420: //Optimism-Sepolia + return { + dataSource: { + archive: lookupArchive('optimism-sepolia'), + chain: + process.env[RPC_URL] ?? + optimismSepolia.rpcUrls.default.http[0], + }, + from: Math.min( + CartesiDAppFactoryOptimismSepolia.receipt.blockNumber, + InputBoxOptimismSepolia.receipt.blockNumber, + ), + finalityConfirmation: parseIntOr({ + defaultVal: FINALITY_CONFIRMATION, + value: process.env[BLOCK_CONFIRMATIONS], + }), + }; case 31337: // anvil return { dataSource: { diff --git a/tests/processor.test.ts b/tests/processor.test.ts index 1ff40ca..14d993c 100644 --- a/tests/processor.test.ts +++ b/tests/processor.test.ts @@ -25,6 +25,8 @@ vi.mock('@subsquid/evm-processor', async () => { const sepolia = 11155111; const mainnet = 1; const local = 31337; +const optimism = 10; +const optimismSepolia = 11155420; describe('Processor creation', () => { beforeEach(() => { @@ -150,6 +152,98 @@ describe('Processor creation', () => { }); }); + test('Required configs for optimism', () => { + const processor = createProcessor(optimism); + + expect(processor.setDataSource).toHaveBeenCalledWith({ + archive: 'https://v2.archive.subsquid.io/network/optimism-mainnet', + chain: 'https://mainnet.optimism.io', + }); + + expect(processor.setFinalityConfirmation).toHaveBeenCalledWith(10); + expect(processor.setFields).toHaveBeenCalledWith({ + transaction: { + chainId: true, + from: true, + hash: true, + value: true, + }, + }); + expect(processor.setBlockRange).toHaveBeenCalledWith({ + from: 107432991, + }); + + const addLog = processor.addLog as unknown as MockInstance; + + expect(addLog).toHaveBeenCalledTimes(3); + expect(addLog.mock.calls[0][0]).toEqual({ + address: ['0x7122cd1221c20892234186facfe8615e6743ab02'], + topic0: [ + '0xe73165c2d277daf8713fd08b40845cb6bb7a20b2b543f3d35324a475660fcebd', + ], + }); + expect(addLog.mock.calls[1][0]).toEqual({ + address: ['0x59b22d57d4f067708ab0c00552767405926dc768'], + topic0: [ + '0x6aaa400068bf4ca337265e2a1e1e841f66b8597fd5b452fdc52a44bed28a0784', + ], + transaction: true, + }); + + expect(addLog.mock.calls[2][0]).toEqual({ + topic0: [ + '0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0', + ], + transaction: true, + }); + }); + + test('Required configs for optimism-sepolia', () => { + const processor = createProcessor(optimismSepolia); + + expect(processor.setDataSource).toHaveBeenCalledWith({ + archive: 'https://v2.archive.subsquid.io/network/optimism-sepolia', + chain: 'https://sepolia.optimism.io', + }); + + expect(processor.setFinalityConfirmation).toHaveBeenCalledWith(10); + expect(processor.setFields).toHaveBeenCalledWith({ + transaction: { + chainId: true, + from: true, + hash: true, + value: true, + }, + }); + expect(processor.setBlockRange).toHaveBeenCalledWith({ + from: 5393079, + }); + + const addLog = processor.addLog as unknown as MockInstance; + + expect(addLog).toHaveBeenCalledTimes(3); + expect(addLog.mock.calls[0][0]).toEqual({ + address: ['0x7122cd1221c20892234186facfe8615e6743ab02'], + topic0: [ + '0xe73165c2d277daf8713fd08b40845cb6bb7a20b2b543f3d35324a475660fcebd', + ], + }); + expect(addLog.mock.calls[1][0]).toEqual({ + address: ['0x59b22d57d4f067708ab0c00552767405926dc768'], + topic0: [ + '0x6aaa400068bf4ca337265e2a1e1e841f66b8597fd5b452fdc52a44bed28a0784', + ], + transaction: true, + }); + + expect(addLog.mock.calls[2][0]).toEqual({ + topic0: [ + '0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0', + ], + transaction: true, + }); + }); + test('Required configs for mainnet', () => { const processor = createProcessor(mainnet); const applicationMetadata = loadApplications(mainnet);