viem
Version:
59 lines • 2.16 kB
JavaScript
import { getBlock, } from '../../../actions/public/getBlock.js';
import { getProof, } from '../../../actions/public/getProof.js';
import { contracts } from '../contracts.js';
import { getWithdrawalHashStorageSlot, } from '../utils/getWithdrawalHashStorageSlot.js';
const outputRootProofVersion = '0x0000000000000000000000000000000000000000000000000000000000000000';
/**
* Builds the transaction that proves a withdrawal was initiated on an L2. Used in the Withdrawal flow.
*
* - Docs: https://viem.sh/op-stack/actions/buildProveWithdrawal.html
*
* @param client - Client to use
* @param parameters - {@link BuildProveWithdrawalParameters}
* @returns The prove withdraw transaction request. {@link BuildProveWithdrawalReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
* import { optimism } from 'viem/chains'
* import { buildProveWithdrawal } from 'viem/op-stack'
*
* const publicClientL2 = createPublicClient({
* chain: optimism,
* transport: http(),
* })
*
* const args = await buildProveWithdrawal(publicClientL2, {
* output: { ... },
* withdrawal: { ... },
* })
*/
export async function buildProveWithdrawal(client, args) {
const { account, chain = client.chain, output, withdrawal } = args;
const { withdrawalHash } = withdrawal;
const { l2BlockNumber } = output;
const slot = getWithdrawalHashStorageSlot({ withdrawalHash });
const [proof, block] = await Promise.all([
getProof(client, {
address: contracts.l2ToL1MessagePasser.address,
storageKeys: [slot],
blockNumber: l2BlockNumber,
}),
getBlock(client, {
blockNumber: l2BlockNumber,
}),
]);
return {
account,
l2OutputIndex: output.outputIndex,
outputRootProof: {
latestBlockhash: block.hash,
messagePasserStorageRoot: proof.storageHash,
stateRoot: block.stateRoot,
version: outputRootProofVersion,
},
targetChain: chain,
withdrawalProof: proof.storageProof[0].proof,
withdrawal,
};
}
//# sourceMappingURL=buildProveWithdrawal.js.map