You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
4.8 KiB
118 lines
4.8 KiB
import { multicall, } from '../../actions/public/multicall.js';
|
|
import { readContract } from '../../actions/public/readContract.js';
|
|
import { BaseError } from '../../errors/base.js';
|
|
import { l2OutputOracleAbi, portal2Abi, portalAbi } from '../abis.js';
|
|
import { getPortalVersion } from './getPortalVersion.js';
|
|
const buffer = 10;
|
|
/**
|
|
* Returns the time until the withdrawal transaction can be finalized. Used for the [Withdrawal](/op-stack/guides/withdrawals) flow.
|
|
*
|
|
* - Docs: https://viem.sh/op-stack/actions/getTimeToFinalize
|
|
*
|
|
* @param client - Client to use
|
|
* @param parameters - {@link GetTimeToFinalizeParameters}
|
|
* @returns Time until finalize. {@link GetTimeToFinalizeReturnType}
|
|
*
|
|
* @example
|
|
* import { createPublicClient, http } from 'viem'
|
|
* import { getBlockNumber } from 'viem/actions'
|
|
* import { mainnet, optimism } from 'viem/chains'
|
|
* import { getTimeToFinalize } from 'viem/op-stack'
|
|
*
|
|
* const publicClientL1 = createPublicClient({
|
|
* chain: mainnet,
|
|
* transport: http(),
|
|
* })
|
|
* const publicClientL2 = createPublicClient({
|
|
* chain: optimism,
|
|
* transport: http(),
|
|
* })
|
|
*
|
|
* const receipt = await publicClientL2.getTransactionReceipt({
|
|
* hash: '0x9a2f4283636ddeb9ac32382961b22c177c9e86dd3b283735c154f897b1a7ff4a',
|
|
* })
|
|
*
|
|
* const [withdrawal] = getWithdrawals(receipt)
|
|
*
|
|
* const { seconds } = await getTimeToFinalize(publicClientL1, {
|
|
* withdrawalHash: withdrawal.withdrawalHash,
|
|
* targetChain: optimism
|
|
* })
|
|
*/
|
|
export async function getTimeToFinalize(client, parameters) {
|
|
const { chain = client.chain, withdrawalHash, targetChain } = parameters;
|
|
const portalAddress = (() => {
|
|
if (parameters.portalAddress)
|
|
return parameters.portalAddress;
|
|
if (chain)
|
|
return targetChain.contracts.portal[chain.id].address;
|
|
return Object.values(targetChain.contracts.portal)[0].address;
|
|
})();
|
|
const portalVersion = await getPortalVersion(client, { portalAddress });
|
|
// Legacy
|
|
if (portalVersion.major < 3) {
|
|
const l2OutputOracleAddress = (() => {
|
|
if (parameters.l2OutputOracleAddress)
|
|
return parameters.l2OutputOracleAddress;
|
|
if (chain)
|
|
return targetChain.contracts.l2OutputOracle[chain.id].address;
|
|
return Object.values(targetChain.contracts.l2OutputOracle)[0].address;
|
|
})();
|
|
const [[_outputRoot, proveTimestamp, _l2OutputIndex], period] = await multicall(client, {
|
|
allowFailure: false,
|
|
contracts: [
|
|
{
|
|
abi: portalAbi,
|
|
address: portalAddress,
|
|
functionName: 'provenWithdrawals',
|
|
args: [withdrawalHash],
|
|
},
|
|
{
|
|
abi: l2OutputOracleAbi,
|
|
address: l2OutputOracleAddress,
|
|
functionName: 'FINALIZATION_PERIOD_SECONDS',
|
|
},
|
|
],
|
|
});
|
|
const secondsSinceProven = Date.now() / 1000 - Number(proveTimestamp);
|
|
const secondsToFinalize = Number(period) - secondsSinceProven;
|
|
const seconds = Math.floor(secondsToFinalize < 0 ? 0 : secondsToFinalize + buffer);
|
|
const timestamp = Date.now() + seconds * 1000;
|
|
return { period: Number(period), seconds, timestamp };
|
|
}
|
|
const numProofSubmitters = await readContract(client, {
|
|
abi: portal2Abi,
|
|
address: portalAddress,
|
|
functionName: 'numProofSubmitters',
|
|
args: [withdrawalHash],
|
|
}).catch(() => 1n);
|
|
const proofSubmitter = await readContract(client, {
|
|
abi: portal2Abi,
|
|
address: portalAddress,
|
|
functionName: 'proofSubmitters',
|
|
args: [withdrawalHash, numProofSubmitters - 1n],
|
|
}).catch(() => undefined);
|
|
const [[_disputeGameProxy, proveTimestamp], proofMaturityDelaySeconds] = await Promise.all([
|
|
proofSubmitter
|
|
? readContract(client, {
|
|
abi: portal2Abi,
|
|
address: portalAddress,
|
|
functionName: 'provenWithdrawals',
|
|
args: [withdrawalHash, proofSubmitter],
|
|
})
|
|
: Promise.resolve(['0x', 0n]),
|
|
readContract(client, {
|
|
abi: portal2Abi,
|
|
address: portalAddress,
|
|
functionName: 'proofMaturityDelaySeconds',
|
|
}),
|
|
]);
|
|
if (proveTimestamp === 0n)
|
|
throw new BaseError('Withdrawal has not been proven on L1.');
|
|
const secondsSinceProven = Date.now() / 1000 - Number(proveTimestamp);
|
|
const secondsToFinalize = Number(proofMaturityDelaySeconds) - secondsSinceProven;
|
|
const seconds = Math.floor(secondsToFinalize < 0n ? 0 : secondsToFinalize + buffer);
|
|
const timestamp = Date.now() + seconds * 1000;
|
|
return { period: Number(proofMaturityDelaySeconds), seconds, timestamp };
|
|
}
|
|
//# sourceMappingURL=getTimeToFinalize.js.map
|