Skip to content
Open
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/op-stack-post-exec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `viem/op-stack` support for the post-exec transaction type (`0x7d`) from OP Stack Upgrade 22 (Lagoon), which the formatters now return as `type: 'postExec'` and `parseTransaction` and `serializeTransaction` now handle.
13 changes: 12 additions & 1 deletion site/pages/op-stack/utilities/parseTransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Converts a serialized transaction to a structured transaction, with

# parseTransaction (OP Stack)

Parses a serialized RLP-encoded transaction. Supports signed & unsigned Deposit, EIP-1559, EIP-2930 and Legacy Transactions.
Parses a serialized RLP-encoded transaction. Supports signed & unsigned Deposit, Post-exec, EIP-1559, EIP-2930 and Legacy Transactions.

## Import
```ts
Expand All @@ -29,6 +29,17 @@ import { parseTransaction } from 'viem/op-stack'
const transaction = parseTransaction('0x7ef83ca018040f35752170c3339ddcd850f185c9cc46bdef4d6e1f2ab323f4d3d710431994977f82a600a1414e583f7f13623f1ac5d58b1c0b808080808080')
```

### Post-Exec Transactions

It also parses post-exec transactions (`0x7d`-prefixed), which a sequencer appends to a block from OP Stack Upgrade 22 (Lagoon). The payload after the type byte is returned as `data`:

```ts
import { parseTransaction } from 'viem/op-stack'

const transaction = parseTransaction('0x7dc60110c3c2012a')
// @log: { data: '0xc60110c3c2012a', type: 'postExec' }
```

## Returns

`TransactionSerializable`
Expand Down
19 changes: 17 additions & 2 deletions site/pages/op-stack/utilities/serializeTransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Serializes a transaction object, with support for OP Stack.

# serializeTransaction (OP Stack)

Serializes a transaction object, with support for OP Stack transactions. Supports Deposit, EIP-1559, EIP-2930, and Legacy transactions.
Serializes a transaction object, with support for OP Stack transactions. Supports Deposit, Post-exec, EIP-1559, EIP-2930, and Legacy transactions.

## Import

Expand Down Expand Up @@ -46,11 +46,26 @@ const serialized = serializeTransaction({
})
```

### Post-Exec Transactions

It also serializes post-exec transactions from OP Stack Upgrade 22 (Lagoon), as the `0x7d` type byte followed by the payload:

```ts
import { serializeTransaction } from 'viem/op-stack'

const serialized = serializeTransaction({
data: '0xc60110c3c2012a',
type: 'postExec'
})
// @log: '0x7dc60110c3c2012a'
```

## Returns

Returns a template `Hex` value based on transaction type:

- `deposit`: [TransactionSerializedDeposit](/docs/glossary/types#TransactionSerializedDeposit)
- `postExec`: `TransactionSerializedPostExec`
- `eip1559`: [TransactionSerializedEIP1559](/docs/glossary/types#transactionserializedeip1559)
- `eip2930`: [TransactionSerializedEIP2930](/docs/glossary/types#transactionserializedeip2930)
- `legacy`: [TransactionSerializedLegacy](/docs/glossary/types#transactionserializedlegacy)
Expand Down Expand Up @@ -79,7 +94,7 @@ const serialized = serializeTransaction({

- **Type:** `Hex`

Optional signature to include. **Ignored for deposit transactions.**
Optional signature to include. **Ignored for deposit and post-exec transactions.**

```ts
const serialized = serializeTransaction({
Expand Down
4 changes: 4 additions & 0 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,9 @@ export type {
} from '../op-stack/types/block.js'
export type {
OpStackDepositTransaction,
OpStackPostExecTransaction,
OpStackRpcDepositTransaction,
OpStackRpcPostExecTransaction,
OpStackRpcTransaction,
OpStackRpcTransactionReceipt,
OpStackRpcTransactionReceiptOverrides,
Expand All @@ -888,7 +890,9 @@ export type {
OpStackTransactionSerialized,
OpStackTransactionType,
TransactionSerializableDeposit,
TransactionSerializablePostExec,
TransactionSerializedDeposit,
TransactionSerializedPostExec,
} from '../op-stack/types/transaction.js'
export type {
/** @deprecated */
Expand Down
14 changes: 13 additions & 1 deletion src/op-stack/formatters.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ describe('smoke', () => {
})

expectTypeOf(transaction.type).toEqualTypeOf<
'legacy' | 'eip2930' | 'eip1559' | 'eip4844' | 'eip7702' | 'deposit'
| 'legacy'
| 'eip2930'
| 'eip1559'
| 'eip4844'
| 'eip7702'
| 'deposit'
| 'postExec'
>()
expectTypeOf(
transaction.type === 'deposit' && transaction.isSystemTx,
Expand All @@ -80,6 +86,12 @@ describe('smoke', () => {
expectTypeOf(
transaction.type === 'deposit' && transaction.mint,
).toEqualTypeOf<false | undefined | bigint>()
expectTypeOf(
transaction.type === 'postExec' && transaction.nonce,
).toEqualTypeOf<false | number | undefined>()
expectTypeOf(
transaction.type === 'postExec' && transaction.to,
).toEqualTypeOf<false | null>()
expectTypeOf(
transaction.type === 'eip1559' && transaction.isSystemTx,
).toEqualTypeOf<false | undefined>()
Expand Down
115 changes: 115 additions & 0 deletions src/op-stack/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ describe('block', () => {
value: '0x0',
yParity: '0x0',
},
{
blockHash:
'0x40b65fabd4bdda5bb716b1a39e761d76a968d5cc54d887d05b47ac6c2c298846',
blockNumber: '0x1',
from: '0x0000000000000000000000000000000000000000',
gas: '0x0',
hash: '0xe0d89f323eb3de298a53ceea9f829342c3137486664110c32a5677b77684c704',
input: '0xc60110c3c2012a',
transactionIndex: '0x2',
type: '0x7d',
value: '0x0',
},
],
uncles: [],
}),
Expand Down Expand Up @@ -176,6 +188,27 @@ describe('block', () => {
"value": 0n,
"yParity": 0,
},
{
"blockHash": "0x40b65fabd4bdda5bb716b1a39e761d76a968d5cc54d887d05b47ac6c2c298846",
"blockNumber": 1n,
"chainId": undefined,
"from": "0x0000000000000000000000000000000000000000",
"gas": 0n,
"gasPrice": undefined,
"hash": "0xe0d89f323eb3de298a53ceea9f829342c3137486664110c32a5677b77684c704",
"input": "0xc60110c3c2012a",
"maxFeePerBlobGas": undefined,
"maxFeePerGas": undefined,
"maxPriorityFeePerGas": undefined,
"nonce": undefined,
"to": null,
"transactionIndex": 2,
"type": "postExec",
"typeHex": "0x7d",
"v": undefined,
"value": 0n,
"yParity": undefined,
},
],
"transactionsRoot": "0xe41f41da49507646478023c060078f8afa962973ac38da1f7efd0355363b31d5",
"uncles": [],
Expand Down Expand Up @@ -492,6 +525,88 @@ describe('transaction (deposit)', () => {
})
})

describe('transaction (post-exec)', () => {
test('formatter', () => {
const { transaction } = optimism.formatters!

// The payload is `[1, 16, [[1, 42]]]`: a refund of 42 gas to the
// transaction at index 1 of block 16.
const postExec = {
blockHash:
'0xd981071322be9757dc3b5479a84d61b4f1f5dd2d44ecb66463be6cfc8246f574',
blockNumber: '0x10',
from: '0x0000000000000000000000000000000000000000',
gas: '0x0',
hash: '0xe0d89f323eb3de298a53ceea9f829342c3137486664110c32a5677b77684c704',
input: '0xc60110c3c2012a',
transactionIndex: '0x2',
type: '0x7d',
value: '0x0',
} as const

expect([
transaction.format(postExec),
// op-geth also reports the user transaction fields, as zero.
transaction.format({
...postExec,
gasPrice: '0x0',
nonce: '0x0',
r: '0x0',
s: '0x0',
to: null,
v: '0x0',
}),
]).toMatchInlineSnapshot(`
[
{
"blockHash": "0xd981071322be9757dc3b5479a84d61b4f1f5dd2d44ecb66463be6cfc8246f574",
"blockNumber": 16n,
"chainId": undefined,
"from": "0x0000000000000000000000000000000000000000",
"gas": 0n,
"gasPrice": undefined,
"hash": "0xe0d89f323eb3de298a53ceea9f829342c3137486664110c32a5677b77684c704",
"input": "0xc60110c3c2012a",
"maxFeePerBlobGas": undefined,
"maxFeePerGas": undefined,
"maxPriorityFeePerGas": undefined,
"nonce": undefined,
"to": null,
"transactionIndex": 2,
"type": "postExec",
"typeHex": "0x7d",
"v": undefined,
"value": 0n,
"yParity": undefined,
},
{
"blockHash": "0xd981071322be9757dc3b5479a84d61b4f1f5dd2d44ecb66463be6cfc8246f574",
"blockNumber": 16n,
"chainId": undefined,
"from": "0x0000000000000000000000000000000000000000",
"gas": 0n,
"gasPrice": 0n,
"hash": "0xe0d89f323eb3de298a53ceea9f829342c3137486664110c32a5677b77684c704",
"input": "0xc60110c3c2012a",
"maxFeePerBlobGas": undefined,
"maxFeePerGas": undefined,
"maxPriorityFeePerGas": undefined,
"nonce": 0,
"r": "0x0",
"s": "0x0",
"to": null,
"transactionIndex": 2,
"type": "postExec",
"typeHex": "0x7d",
"v": 0n,
"value": 0n,
"yParity": 0,
},
]
`)
})
})

describe('transaction receipt', async () => {
test('formatter', () => {
const { transactionReceipt } = optimism.formatters!
Expand Down
7 changes: 6 additions & 1 deletion src/op-stack/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ const block = {
for (let i = 0; transactions && i < transactions.length; i++) {
const formatted = transactions[i]!
const transaction = args.transactions![i] as OpStackRpcTransaction
if (typeof formatted === 'string' || formatted.typeHex !== '0x7e')
if (typeof formatted === 'string') continue
if (formatted.typeHex === '0x7d') {
formatted.type = 'postExec'
continue
}
if (formatted.typeHex !== '0x7e') continue
formatted.isSystemTx = transaction.isSystemTx
formatted.mint = transaction.mint
? hexToBigInt(transaction.mint)
Expand Down Expand Up @@ -65,6 +69,7 @@ export const formatters = {
transaction.sourceHash = args.sourceHash
transaction.type = 'deposit'
}
if (args.type === '0x7d') transaction.type = 'postExec'
return transaction
},
}),
Expand Down
2 changes: 2 additions & 0 deletions src/op-stack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ export type {
} from './types/block.js'
export type {
OpStackDepositTransaction,
OpStackPostExecTransaction,
OpStackRpcDepositTransaction,
OpStackRpcPostExecTransaction,
OpStackRpcTransaction,
OpStackRpcTransactionReceipt,
OpStackRpcTransactionReceiptOverrides,
Expand Down
30 changes: 30 additions & 0 deletions src/op-stack/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ describe('deposit', () => {
})
})

describe('postExec', () => {
test('default', () => {
// The payload is `[1, 16, [[1, 42]]]`: a refund of 42 gas to the
// transaction at index 1 of block 16.
const serialized = serializeTransaction({
data: '0xc60110c3c2012a',
type: 'postExec',
})

expect(serialized).toMatchInlineSnapshot(`"0x7dc60110c3c2012a"`)
expect(parseTransaction(serialized)).toMatchInlineSnapshot(`
{
"data": "0xc60110c3c2012a",
"type": "postExec",
}
`)
})

test('error: empty payload', () => {
expect(() => parseTransaction('0x7d')).toThrowErrorMatchingInlineSnapshot(`
[InvalidSerializedTransactionError: Invalid serialized transaction of type "postExec" was provided.

Serialized Transaction: "0x7d"
Missing Attributes: data

Version: viem@x.y.z]
`)
})
})

describe('eip1559', () => {
test('default', async () => {
const transaction = {
Expand Down
26 changes: 25 additions & 1 deletion src/op-stack/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { InvalidSerializedTransactionError } from '../errors/transaction.js'
import type { ErrorType } from '../errors/utils.js'
import { isHex } from '../utils/data/isHex.js'
import { size } from '../utils/data/size.js'
import { sliceHex } from '../utils/data/slice.js'
import { hexToBigInt, hexToBool } from '../utils/encoding/fromHex.js'
import type { GetSerializedTransactionType } from '../utils/transaction/getSerializedTransactionType.js'
Expand All @@ -15,7 +16,9 @@ import type {
OpStackTransactionSerialized,
OpStackTransactionType,
TransactionSerializableDeposit,
TransactionSerializablePostExec,
TransactionSerializedDeposit,
TransactionSerializedPostExec,
} from './types/transaction.js'

export type ParseTransactionReturnType<
Expand All @@ -25,7 +28,9 @@ export type ParseTransactionReturnType<
OpStackTransactionType = GetSerializedTransactionType<serialized>,
> = serialized extends TransactionSerializedDeposit
? TransactionSerializableDeposit
: ParseTransactionReturnType_<serialized, type>
: serialized extends TransactionSerializedPostExec
? TransactionSerializablePostExec
: ParseTransactionReturnType_<serialized, type>

export type ParseTransactionErrorType = ParseTransactionErrorType_ | ErrorType

Expand All @@ -39,6 +44,11 @@ export function parseTransaction<
serializedTransaction as TransactionSerializedDeposit,
) as ParseTransactionReturnType<serialized>

if (serializedType === '0x7d')
return parseTransactionPostExec(
serializedTransaction as TransactionSerializedPostExec,
) as ParseTransactionReturnType<serialized>

return parseTransaction_(
serializedTransaction,
) as ParseTransactionReturnType<serialized>
Expand Down Expand Up @@ -86,3 +96,17 @@ function parseTransactionDeposit(

return transaction
}

function parseTransactionPostExec(
serializedTransaction: TransactionSerializedPostExec,
): ParseTransactionReturnType<TransactionSerializedPostExec> {
if (size(serializedTransaction) === 1)
throw new InvalidSerializedTransactionError({
attributes: { data: undefined },
serializedTransaction,
type: 'postExec',
})

// The payload follows the type byte as is, with no outer RLP list.
return { data: sliceHex(serializedTransaction, 1), type: 'postExec' }
}
Loading
Loading