UNPKG

@unruggable/gateways

Version:

Trustless Ethereum Multichain CCIP-Read Gateway

70 lines (69 loc) 2.96 kB
import { AbstractRollup, } from '../rollup.mjs'; import { CHAINS } from '../chains.mjs'; import { ROLLUP_ABI } from './types.mjs'; import { Contract } from 'ethers/contract'; import { ZKEVMProver } from './ZKEVMProver.mjs'; import { ZeroHash } from 'ethers/constants'; import { ABI_CODER, toUnpaddedHex } from '../utils.mjs'; // https://hackmd.io/@4cbvqzFdRBSWMHNeI8Wbwg/Syz8PeEo0 // https://github.com/0xPolygonHermez/cdk-erigon/commit/33acc63073f16a13398ef868bb4dbdd49da720ae // https://github.com/0xPolygonHermez/cdk-erigon/commit/33acc63073f16a13398ef868bb4dbdd49da720ae#diff-715521c7a2c24ae8e05a5c9eb0c80c348cd4ac0a1151467a4eb41d5f1a570684R1717 export class ZKEVMRollup extends AbstractRollup { RollupManager; rollupID; // https://docs.polygon.technology/zkEVM/architecture/high-level/smart-contracts/addresses/#mainnet-contracts static mainnetConfig = { chain1: CHAINS.MAINNET, chain2: CHAINS.ZKEVM, RollupManager: '0x5132A183E9F3CB7C848b0AAC5Ae0c4f0491B7aB2', }; // https://github.com/0xPolygonHermez/cdk-erigon/tree/zkevm#networks static sepoliaConfig = { chain1: CHAINS.SEPOLIA, chain2: CHAINS.ZKEVM_CARDONA, RollupManager: '0x32d33D5137a7cFFb54c5Bf8371172bcEc5f310ff', }; static async create(providers, config) { const RollupManager = new Contract(config.RollupManager, ROLLUP_ABI, providers.provider1); const network = await providers.provider2.getNetwork(); const rollupID = await RollupManager.chainIDToRollupID(network.chainId); return new this(providers, RollupManager, rollupID); } // TODO: refactor to make this public constructor(providers, RollupManager, rollupID) { super(providers); this.RollupManager = RollupManager; this.rollupID = rollupID; } fetchLatestCommitIndex() { return this.RollupManager.getLastVerifiedBatch(this.rollupID, { blockTag: this.latestBlockTag, }); } fetchBatchStateRoot(batchIndex) { return this.RollupManager.getRollupBatchNumToStateRoot(this.rollupID, batchIndex); } fetchBatchInfo(batchIndex) { return this.provider2.send('zkevm_getBatchByNumber', [ toUnpaddedHex(batchIndex), ]); } async _fetchCommit(index) { const [batchInfo, stateRoot] = await Promise.all([ this.fetchBatchInfo(index), this.fetchBatchStateRoot(index), ]); if (stateRoot == ZeroHash) throw new Error('not finalized'); const prover = new ZKEVMProver(this.provider2, batchInfo.number); return { index, prover }; } encodeWitness(commit, proofSeq) { return ABI_CODER.encode(['tuple(uint256, bytes[], bytes)'], [[commit.index, proofSeq.proofs, proofSeq.order]]); } windowFromSec(sec) { // finalization is kinda on-chain // sequencing time is available return sec; } }