Commit 5a6f539c authored by Mark Tyneway's avatar Mark Tyneway

core-utils: add `toJSON` methods on batch primitives

Allows for easy serialization and display in frontends
or for data collection
parent f2179e37
---
'@eth-optimism/core-utils': patch
---
Add toJSON methods to the batch primitives
......@@ -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
......@@ -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
......@@ -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()),
}
}
}
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