@ensdomains/unruggable-gateways
Version:
Trustless Ethereum Multichain CCIP-Read Gateway
72 lines (71 loc) • 2.74 kB
JavaScript
import { AbstractRollup, } from '../rollup.mjs';
import { Contract } from 'ethers/contract';
import { CHAINS } from '../chains.mjs';
import { EthProver } from '../eth/EthProver.mjs';
import { TAIKO_ABI, } from './types.mjs';
import { ABI_CODER } from '../utils.mjs';
export class TaikoRollup extends AbstractRollup {
TaikoL1;
commitStep;
// https://docs.taiko.xyz/network-reference/mainnet-addresses
static mainnetConfig = {
chain1: CHAINS.MAINNET,
chain2: CHAINS.TAIKO,
TaikoL1: '0x06a9Ab27c7e2255df1815E6CC0168d7755Feb19a', // based.taiko.eth
commitBatchSpan: 1,
};
static heklaConfig = {
chain1: CHAINS.HOLESKY,
chain2: CHAINS.TAIKO_HEKLA,
TaikoL1: '0x79C9109b764609df928d16fC4a91e9081F7e87DB',
commitBatchSpan: 1,
};
static async create(providers, config) {
const TaikoL1 = new Contract(config.TaikoL1, TAIKO_ABI, providers.provider1);
let commitStep;
if (config.commitBatchSpan > 0) {
const cfg = await TaikoL1.getConfig();
commitStep = cfg.stateRootSyncInternal * BigInt(config.commitBatchSpan);
}
else {
commitStep = 1n;
}
return new this(providers, TaikoL1, commitStep);
}
constructor(providers, TaikoL1, commitStep) {
super(providers);
this.TaikoL1 = TaikoL1;
this.commitStep = commitStep;
}
async fetchLatestCommitIndex() {
// https://github.com/taikoxyz/taiko-mono/blob/main/packages/protocol/contracts/L1/libs/LibUtils.sol
// by definition this is shouldSyncStateRoot()
// eg. (block % 16) == 15
const res = await this.TaikoL1.getLastSyncedBlock({
blockTag: this.latestBlockTag,
});
return res.blockId;
}
async _fetchParentCommitIndex(commit) {
if (this.commitStep > 1) {
// remove any unaligned remainder (see above)
const rem = (commit.index + 1n) % this.commitStep;
if (rem)
return commit.index - rem;
}
return commit.index - this.commitStep;
}
async _fetchCommit(index) {
const prover = new EthProver(this.provider2, index);
const blockInfo = await prover.fetchBlock();
return { index, prover, parentHash: blockInfo.parentHash };
}
encodeWitness(commit, proofSeq) {
return ABI_CODER.encode(['(uint256, bytes32, bytes[], bytes)'], [[commit.index, commit.parentHash, proofSeq.proofs, proofSeq.order]]);
}
windowFromSec(sec) {
// taiko is a based rollup
const avgBlockSec = 16; // block every block 12-20 sec
return Math.ceil(sec / avgBlockSec); // units of blocks
}
}