-
-
Save lucadonnoh/82c6fd45e90040d60b4dde882b6f353f to your computer and use it in GitHub Desktop.
Find an anchor state past a certain L2 block
This file contains hidden or 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
| import { ethers } from 'ethers'; | |
| import { table } from 'table'; | |
| const provider = new ethers.JsonRpcProvider(process.env.PROVIDER_URL || 'http://localhost:8545'); | |
| const targetBlockNumber = 134010741; | |
| const anchorStateRegistry = '0x1c68ECfbf9C8B1E6C0677965b3B9Ecf9A104305b'; | |
| const main = async () => { | |
| // Setup | |
| const iface = new ethers.Interface(['event AnchorUpdated(address indexed game)']); | |
| const gameAbi = ['function l2BlockNumber() view returns (uint256)']; | |
| // Calculate start block (7 days ago) | |
| const currentBlock = await provider.getBlockNumber(); | |
| const daysAgo = 7; | |
| const secondsAgo = daysAgo * 24 * 60 * 60; | |
| const startBlock = currentBlock - Math.floor(secondsAgo / 12); // Assuming 12 sec per block | |
| console.log(`Fetching events from block ${startBlock} to ${currentBlock}...`); | |
| // Fetch logs | |
| const logs = await provider.getLogs({ | |
| address: anchorStateRegistry, | |
| topics: [ethers.id('AnchorUpdated(address)')], | |
| fromBlock: startBlock, | |
| toBlock: currentBlock | |
| }); | |
| console.log(`Found ${logs.length} events`); | |
| // Process logs | |
| const tableData = [['Block', 'Time', 'Game', 'L2 Block', 'Tx', '?']]; | |
| let targetReached = false; | |
| for (const log of logs) { | |
| try { | |
| const gameAddress = iface.parseLog(log)?.args[0]; | |
| const block = await provider.getBlock(log.blockNumber); | |
| const timestamp = block?.timestamp | |
| ? new Date(Number(block.timestamp) * 1000).toISOString().slice(0, 19).replace('T', ' ') | |
| : 'unknown'; | |
| try { | |
| const gameContract = new ethers.Contract(gameAddress, gameAbi, provider); | |
| const l2BlockNumber = await gameContract.l2BlockNumber(); | |
| const status = l2BlockNumber >= targetBlockNumber ? 'X' : ''; | |
| if (l2BlockNumber >= targetBlockNumber) targetReached = true; | |
| tableData.push([ | |
| log.blockNumber.toString(), | |
| timestamp, | |
| `${gameAddress.slice(0, 6)}...${gameAddress.slice(-4)}`, | |
| l2BlockNumber.toString(), | |
| `${log.transactionHash.slice(0, 6)}...${log.transactionHash.slice(-4)}`, | |
| status | |
| ]); | |
| } catch (e) { | |
| tableData.push([ | |
| log.blockNumber.toString(), | |
| timestamp, | |
| `${gameAddress.slice(0, 6)}...${gameAddress.slice(-4)}`, | |
| 'Error', | |
| `${log.transactionHash.slice(0, 6)}...${log.transactionHash.slice(-4)}`, | |
| '❌' | |
| ]); | |
| } | |
| } catch (e) { | |
| console.error('Error parsing log:', e); | |
| } | |
| } | |
| // Print results | |
| console.log(table(tableData, { | |
| columns: { | |
| 0: { alignment: 'right' }, | |
| 3: { alignment: 'right' } | |
| } | |
| })); | |
| console.log(targetReached | |
| ? `\n✅ Found events where L2 block number is >= ${targetBlockNumber}` | |
| : `\n❌ No events found where L2 block number is >= ${targetBlockNumber}`); | |
| }; | |
| main().catch(error => { | |
| console.error('Error:', error); | |
| process.exit(1); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment