|
| 1 | +import { createPublicClient, http, encodeFunctionData, parseEventLogs, keccak256, encodeAbiParameters, parseAbiParameters, parseAbi } from 'viem'; |
| 2 | +import { buildProveZircuitWithdrawal, getWithdrawals } from '@zircuit/zircuit-viem/op-stack'; |
| 3 | + |
| 4 | +const ZIRCUIT_OPTIMISM_PORTAL = '0x17bfAfA932d2e23Bd9B909Fd5B4D2e2a27043fb1'; |
| 5 | +const ZIRCUIT_L2_OUTPUT_ORACLE = '0x92Ef6Af472b39F1b363da45E35530c24619245A4'; |
| 6 | + |
| 7 | +const zircuitOptimismPortalAbi = parseAbi([ |
| 8 | + 'function proveWithdrawalTransaction((uint256 nonce, address sender, address target, uint256 value, uint256 gasLimit, bytes data) _tx, uint256 _l2OutputIndex, (bytes32 version, bytes32 stateRoot, bytes32 messagePasserStorageRoot, bytes32 latestBlockhash) _outputRootProof, bytes[] calldata _withdrawalProof)', |
| 9 | +]); |
| 10 | +const zircuitL2ToL1MessagePasserAbi = parseAbi([ |
| 11 | + 'event MessagePassed(uint256 indexed nonce, address indexed sender, address indexed target, uint256 value, uint256 gasLimit, bytes data, bytes32 withdrawalHash)', |
| 12 | +]); |
| 13 | + |
| 14 | +async function main() { |
| 15 | + const l2Client = createPublicClient({ transport: http('https://zircuit-mainnet.drpc.org') }); |
| 16 | + const l1Client = createPublicClient({ transport: http('https://ethereum.publicnode.com') }); |
| 17 | + |
| 18 | + // Get the original receipt |
| 19 | + const receipt = await l2Client.getTransactionReceipt({ |
| 20 | + hash: '0x4a5203d25bbe1fd6aa3536e013f017d5d2f21c5996167173d3ec03bdeb977426' |
| 21 | + }); |
| 22 | + console.log('Got receipt, block:', receipt.blockNumber); |
| 23 | + |
| 24 | + // Extract withdrawal using our method |
| 25 | + const logs = parseEventLogs({ abi: zircuitL2ToL1MessagePasserAbi, logs: receipt.logs }); |
| 26 | + const messagePassedEvent = logs.find((log) => log.eventName === 'MessagePassed'); |
| 27 | + if (!messagePassedEvent) { |
| 28 | + console.error('No MessagePassed event found'); |
| 29 | + return; |
| 30 | + } |
| 31 | + const args = (messagePassedEvent as any).args; |
| 32 | + const withdrawalTx = { |
| 33 | + nonce: args.nonce, |
| 34 | + sender: args.sender, |
| 35 | + target: args.target, |
| 36 | + value: args.value, |
| 37 | + gasLimit: args.gasLimit, |
| 38 | + data: args.data, |
| 39 | + }; |
| 40 | + console.log('Withdrawal nonce:', withdrawalTx.nonce.toString()); |
| 41 | + console.log('Withdrawal nonce (hex):', '0x' + withdrawalTx.nonce.toString(16)); |
| 42 | + console.log('Withdrawal sender:', withdrawalTx.sender); |
| 43 | + console.log('Withdrawal target:', withdrawalTx.target); |
| 44 | + console.log('Withdrawal value:', withdrawalTx.value.toString()); |
| 45 | + |
| 46 | + // Also get withdrawal from the library |
| 47 | + const libWithdrawals = getWithdrawals(receipt); |
| 48 | + console.log('\nLibrary withdrawal nonce:', libWithdrawals[0]?.nonce.toString()); |
| 49 | + console.log('Library withdrawal hash:', libWithdrawals[0]?.withdrawalHash); |
| 50 | + |
| 51 | + // Our computed hash |
| 52 | + const ourHash = keccak256( |
| 53 | + encodeAbiParameters( |
| 54 | + parseAbiParameters('uint256, address, address, uint256, uint256, bytes'), |
| 55 | + [withdrawalTx.nonce, withdrawalTx.sender, withdrawalTx.target, withdrawalTx.value, withdrawalTx.gasLimit, withdrawalTx.data], |
| 56 | + ), |
| 57 | + ); |
| 58 | + console.log('\nOur computed hash:', ourHash); |
| 59 | + console.log('Library hash:', libWithdrawals[0]?.withdrawalHash); |
| 60 | + console.log('Hash match:', ourHash === libWithdrawals[0]?.withdrawalHash); |
| 61 | + |
| 62 | + // Build proof |
| 63 | + console.log('\nBuilding proof...'); |
| 64 | + try { |
| 65 | + const proofResult = await buildProveZircuitWithdrawal(l2Client as any, { |
| 66 | + receipt: receipt as any, |
| 67 | + l1Client: l1Client as any, |
| 68 | + l2OutputOracleAddress: ZIRCUIT_L2_OUTPUT_ORACLE as `0x${string}`, |
| 69 | + } as any); |
| 70 | + |
| 71 | + console.log('Proof built successfully'); |
| 72 | + console.log('l2OutputIndex:', (proofResult.l2OutputIndex as bigint).toString()); |
| 73 | + console.log('withdrawalProof length:', proofResult.withdrawalProof.length); |
| 74 | + console.log('withdrawalProof[0] length:', (proofResult.withdrawalProof[0] as string).length); |
| 75 | + console.log('outputRootProof:', JSON.stringify({ |
| 76 | + version: proofResult.outputRootProof.version, |
| 77 | + stateRoot: proofResult.outputRootProof.stateRoot, |
| 78 | + messagePasserStorageRoot: proofResult.outputRootProof.messagePasserStorageRoot, |
| 79 | + latestBlockhash: proofResult.outputRootProof.latestBlockhash, |
| 80 | + }, null, 2)); |
| 81 | + |
| 82 | + // Encode the calldata |
| 83 | + const calldata = encodeFunctionData({ |
| 84 | + abi: zircuitOptimismPortalAbi, |
| 85 | + functionName: 'proveWithdrawalTransaction', |
| 86 | + args: [ |
| 87 | + withdrawalTx, |
| 88 | + proofResult.l2OutputIndex as bigint, |
| 89 | + proofResult.outputRootProof as any, |
| 90 | + proofResult.withdrawalProof as `0x${string}`[], |
| 91 | + ], |
| 92 | + }); |
| 93 | + console.log('\nCalldata length:', calldata.length); |
| 94 | + console.log('Function selector:', calldata.slice(0, 10)); |
| 95 | + |
| 96 | + // Simulate the call |
| 97 | + console.log('\nSimulating call on L1...'); |
| 98 | + try { |
| 99 | + await l1Client.call({ |
| 100 | + to: ZIRCUIT_OPTIMISM_PORTAL as `0x${string}`, |
| 101 | + data: calldata, |
| 102 | + }); |
| 103 | + console.log('*** Simulation SUCCEEDED ***'); |
| 104 | + } catch (e: any) { |
| 105 | + console.error('*** Simulation FAILED ***'); |
| 106 | + console.error('Error:', e.message?.slice(0, 1000)); |
| 107 | + } |
| 108 | + } catch (e: any) { |
| 109 | + console.error('Proof building failed:', e.message?.slice(0, 1000)); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +main().catch(console.error); |
0 commit comments