Commit 65f4514e authored by Matthew Slipper's avatar Matthew Slipper Committed by GitHub

Merge pull request #1807 from ethereum-optimism/feat/deserialize-batches

contracts: fetch batches hardhat task
parents 5b966580 4106fd55
---
'@eth-optimism/contracts': patch
---
Add a fetch batches hardhat task
---
'@eth-optimism/core-utils': patch
---
Add toJSON methods to the batch primitives
...@@ -20,6 +20,7 @@ import './tasks/validate-address-dictator' ...@@ -20,6 +20,7 @@ import './tasks/validate-address-dictator'
import './tasks/validate-chugsplash-dictator' import './tasks/validate-chugsplash-dictator'
import './tasks/whitelist' import './tasks/whitelist'
import './tasks/withdraw-fees' import './tasks/withdraw-fees'
import './tasks/fetch-batches'
import 'hardhat-gas-reporter' import 'hardhat-gas-reporter'
import '@primitivefi/hardhat-dodoc' import '@primitivefi/hardhat-dodoc'
import 'hardhat-output-validator' import 'hardhat-output-validator'
......
import { ethers } from 'ethers'
import { task } from 'hardhat/config'
import * as types from 'hardhat/internal/core/params/argumentTypes'
import { SequencerBatch } from '@eth-optimism/core-utils'
import { names } from '../src/address-names'
import { getContractFromArtifact } from '../src/deploy-utils'
// Need to export env vars
// CONTRACTS_TARGET_NETWORK
// CONTRACTS_DEPLOYER_KEY
// CONTRACTS_RPC_URL
task('fetch-batches')
.addOptionalParam(
'contractsRpcUrl',
'Ethereum HTTP Endpoint',
process.env.CONTRACTS_RPC_URL || 'http://127.0.0.1:8545',
types.string
)
.addOptionalParam('start', 'Start block height', 0, types.int)
.addOptionalParam('end', 'End block height', undefined, types.int)
.setAction(async (args, hre) => {
const provider = new ethers.providers.StaticJsonRpcProvider(
args.contractsRpcUrl
)
let CanonicalTransactionChain = await getContractFromArtifact(
hre,
names.managed.contracts.CanonicalTransactionChain
)
CanonicalTransactionChain = CanonicalTransactionChain.connect(provider)
const start = args.start
let end = args.end
if (!end) {
end = await provider.getBlockNumber()
}
const batches = []
for (let i = start; i <= end; i += 2001) {
const tip = Math.min(i + 2000, end)
console.error(`Querying events ${i}-${tip}`)
const events = await CanonicalTransactionChain.queryFilter(
CanonicalTransactionChain.filters.SequencerBatchAppended(),
i,
tip
)
for (const event of events) {
const tx = await provider.getTransaction(event.transactionHash)
const batch = (SequencerBatch as any).fromHex(tx.data)
batches.push(batch.toJSON())
}
}
console.log(JSON.stringify(batches, null, 2))
})
...@@ -149,6 +149,15 @@ export class Context extends Struct { ...@@ -149,6 +149,15 @@ export class Context extends Struct {
this.blockNumber = br.readU40BE() this.blockNumber = br.readU40BE()
return this return this
} }
toJSON() {
return {
numSequencedTransactions: this.numSequencedTransactions,
numSubsequentQueueTransactions: this.numSubsequentQueueTransactions,
timestamp: this.timestamp,
blockNumber: this.blockNumber,
}
}
} }
// transaction // transaction
...@@ -229,6 +238,27 @@ export class BatchedTx extends Struct { ...@@ -229,6 +238,27 @@ export class BatchedTx extends Struct {
) )
} }
toJSON() {
if (!this.tx) {
this.tx = parse(this.raw)
}
return {
nonce: this.tx.nonce,
gasPrice: this.tx.gasPrice.toString(),
gasLimit: this.tx.gasLimit.toString(),
to: this.tx.to,
value: this.tx.value.toString(),
data: this.tx.data,
v: this.tx.v,
r: this.tx.r,
s: this.tx.s,
chainId: this.tx.chainId,
hash: this.tx.hash,
from: this.tx.from,
}
}
// TODO: inconsistent API with toTransaction // TODO: inconsistent API with toTransaction
// but unnecessary right now // but unnecessary right now
// this should be fromHexTransaction // this should be fromHexTransaction
...@@ -390,4 +420,13 @@ export class SequencerBatch extends Struct { ...@@ -390,4 +420,13 @@ export class SequencerBatch extends Struct {
toHex(): string { toHex(): string {
return '0x' + this.encode().toString('hex') return '0x' + this.encode().toString('hex')
} }
toJSON() {
return {
shouldStartAtElement: this.shouldStartAtElement,
totalElementsToAppend: this.totalElementsToAppend,
contexts: this.contexts.map((c) => c.toJSON()),
transactions: this.transactions.map((tx) => tx.toJSON()),
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment