UNPKG

viem

Version:

TypeScript Interface for Ethereum

53 lines 1.73 kB
import { decodeEventLog, isAddressEqual } from '../../../utils/index.js'; import { zksyncAbi } from '../../constants/abis.js'; import { TxHashNotFoundInLogsError, } from '../../errors/bridge.js'; /** * Returns the hash of the L2 priority operation from a given L1 transaction receipt. * * @param receipt - The L1 transaction receipt. * @param zksync - The address of the ZKsync Era main contract. * @returns hash - The hash of the L2 priority operation. * * @example * import { createPublicClient, http } from 'viem' * import { zksync, mainnet } from 'viem/chains' * import { publicActionsL2, getL2HashFromPriorityOp } from 'viem/zksync' * * const client = createPublicClient({ * chain: mainnet, * transport: http(), * }) * * const zksyncClient = const client = createPublicClient({ * chain: zksync, * transport: http(), * }) * * const receipt = await client.waitForTransactionReceipt({hash: '0x...'}) * const l2Hash = getL2HashFromPriorityOp( * receipt, * await zksyncClient.getMainContractAddress() * ) */ export function getL2HashFromPriorityOp(receipt, zksync) { let hash = null; for (const log of receipt.logs) { if (!isAddressEqual(log.address, zksync)) continue; try { const priorityQueueLog = decodeEventLog({ abi: zksyncAbi, data: log.data, topics: log.topics, }); if (priorityQueueLog && priorityQueueLog.args.txHash !== null) hash = priorityQueueLog.args.txHash; } catch (_e) { } } if (!hash) { throw new TxHashNotFoundInLogsError(); } return hash; } //# sourceMappingURL=getL2HashFromPriorityOp.js.map