@ensdomains/unruggable-gateways
Version:
Trustless Ethereum Multichain CCIP-Read Gateway
108 lines (107 loc) • 4.49 kB
JavaScript
import { ZKSyncProver } from './ZKSyncProver.mjs';
import { DIAMOND_ABI } from './types.mjs';
import { ZeroHash } from 'ethers/constants';
import { Contract, EventLog } from 'ethers/contract';
import { CHAINS } from '../chains.mjs';
import { AbstractRollup, } from '../rollup.mjs';
import { ABI_CODER } from '../utils.mjs';
export class ZKSyncRollup extends AbstractRollup {
// https://docs.zksync.io/build/developer-reference/era-contracts/l1-contracts
static mainnetConfig = {
chain1: CHAINS.MAINNET,
chain2: CHAINS.ZKSYNC,
DiamondProxy: '0x32400084c286cf3e17e7b677ea9583e60a000324',
};
static sepoliaConfig = {
chain1: CHAINS.SEPOLIA,
chain2: CHAINS.ZKSYNC_SEPOLIA,
DiamondProxy: '0x9A6DE0f62Aa270A8bCB1e2610078650D539B1Ef9',
};
// https://docs.zero.network/main-features/system-contracts#zer%CE%B8-network
static zeroMainnetConfig = {
chain1: CHAINS.MAINNET,
chain2: CHAINS.ZERO,
DiamondProxy: '0xdbD849acC6bA61F461CB8A41BBaeE2D673CA02d9',
};
static zeroSepoliaConfig = {
chain1: CHAINS.SEPOLIA,
chain2: CHAINS.ZERO_SEPOLIA,
DiamondProxy: '0x9A62B01fFa3bD358d03508ef60bB522ABA5d1bEb',
};
// https://docs.abs.xyz/how-abstract-works/architecture/components/l1-rollup-contracts#list-of-l1-rollup-contracts
static abstractSepoliaConfig = {
chain1: CHAINS.SEPOLIA,
chain2: CHAINS.ABSTRACT_SEPOLIA,
DiamondProxy: '0x8aD52ff836A30f063dF51A00C99518880B8b36ac',
};
DiamondProxy;
constructor(providers, config) {
super(providers);
this.DiamondProxy = new Contract(config.DiamondProxy, DIAMOND_ABI, this.provider1);
}
async fetchLatestCommitIndex() {
const count = await this.DiamondProxy.getTotalBatchesExecuted({
blockTag: this.latestBlockTag,
});
return count - 1n;
}
async _fetchCommit(index) {
const prover = new ZKSyncProver(this.provider2, Number(index));
const details = await prover.fetchBatchDetails();
if (!details.commitTxHash)
throw new Error('no commitTxHash');
// 20240810: this check randomly fails even though the block is finalized
// if (details.status !== 'verified') {
// throw new Error(`not verified: ${details.status}`);
// }
const [tx, [log], l2LogsTreeRoot] = await Promise.all([
this.provider1.getTransaction(details.commitTxHash),
this.DiamondProxy.queryFilter(this.DiamondProxy.filters.BlockCommit(index, details.rootHash)),
this.DiamondProxy.l2LogsRootHash(index),
]);
if (!tx)
throw new Error(`missing commit tx: ${details.commitTxHash}`);
if (!(log instanceof EventLog))
throw new Error(`no BlockCommit event`);
if (l2LogsTreeRoot === ZeroHash)
throw new Error('not finalized');
const commits = DIAMOND_ABI.decodeFunctionData('commitBatchesSharedBridge', tx.data).newBatchesData;
const batchInfo = commits.find((x) => x.batchNumber == index);
if (!batchInfo)
throw new Error(`commit not in batch`);
const abiEncodedBatch = ABI_CODER.encode([
'uint64', // batchNumber
'bytes32', // batchHash
'uint64', // indexRepeatedStorageChanges
'uint256', // numberOfLayer1Txs
'bytes32', // priorityOperationsHash
'bytes32', // l2LogsTreeRoot
'uint256', // timestamp
'bytes32', // commitment
], [
batchInfo.batchNumber, // == index
batchInfo.newStateRoot, // == details.rootHash
batchInfo.indexRepeatedStorageChanges,
batchInfo.numberOfLayer1Txs,
batchInfo.priorityOperationsHash,
l2LogsTreeRoot,
batchInfo.timestamp,
log.args.commitment,
]);
return {
index,
prover,
stateRoot: details.rootHash,
abiEncodedBatch,
};
}
encodeWitness(commit, proofSeq) {
return ABI_CODER.encode(['(bytes, bytes[], bytes)'], [[commit.abiEncodedBatch, proofSeq.proofs, proofSeq.order]]);
}
windowFromSec(sec) {
// finalization time not on-chain
// approximately 1 batch every hour, sequential
// https://explorer.zksync.io/batches/
return Math.ceil(sec / 3600); // units of commit index
}
}