Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-pears-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/contracts': patch
---

Add a fetch batches hardhat task
5 changes: 5 additions & 0 deletions .changeset/soft-dogs-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/core-utils': patch
---

Add toJSON methods to the batch primitives
1 change: 1 addition & 0 deletions packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './tasks/validate-address-dictator'
import './tasks/validate-chugsplash-dictator'
import './tasks/whitelist'
import './tasks/withdraw-fees'
import './tasks/fetch-batches'
import 'hardhat-gas-reporter'
import '@primitivefi/hardhat-dodoc'
import 'hardhat-output-validator'
Expand Down
59 changes: 59 additions & 0 deletions packages/contracts/tasks/fetch-batches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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))
})
39 changes: 39 additions & 0 deletions packages/core-utils/src/optimism/batch-encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ export class Context extends Struct {
this.blockNumber = br.readU40BE()
return this
}

toJSON() {
return {
numSequencedTransactions: this.numSequencedTransactions,
numSubsequentQueueTransactions: this.numSubsequentQueueTransactions,
timestamp: this.timestamp,
blockNumber: this.blockNumber,
}
}
}

// transaction
Expand Down Expand Up @@ -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
// but unnecessary right now
// this should be fromHexTransaction
Expand Down Expand Up @@ -390,4 +420,13 @@ export class SequencerBatch extends Struct {
toHex(): string {
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()),
}
}
}