Skip to content
On this page

serializeTransaction โ€‹

Serializes a transaction object. Supports EIP-1559, EIP-2930, and Legacy transactions.

Import โ€‹

ts
import { serializeTransaction } from 'viem'

Usage โ€‹

ts
import { serializeTransaction } from 'viem'

const serialized = serializeTransaction({
  chainId: 1,
  gas: 21001n,
  maxFeePerGas: parseGwei('20'),
  maxPriorityFeePerGas: parseGwei('2'),
  nonce: 69,
  to: "0x1234512345123451234512345123451234512345",
  value: parseEther('0.01'),
})

Returns โ€‹

Returns a template Hex value based on transaction type:

Parameters โ€‹

transaction โ€‹

  • Type: TransactionSerializable

The transaction object to serialize.

ts
const serialized = serializeTransaction({
  chainId: 1,
  gas: 21001n,
  maxFeePerGas: parseGwei('20'),
  maxPriorityFeePerGas: parseGwei('2'),
  nonce: 69,
  to: '0x1234512345123451234512345123451234512345',
  value: parseEther('0.01'),
})

signature โ€‹

  • Type: Hex

Optional signature to include.

ts
const serialized = serializeTransaction({
  chainId: 1,
  gas: 21001n,
  maxFeePerGas: parseGwei('20'),
  maxPriorityFeePerGas: parseGwei('2'),
  nonce: 69,
  to: '0x1234512345123451234512345123451234512345',
  value: parseEther('0.01'),
}, { 
  r: '0x123451234512345123451234512345123451234512345123451234512345',
  s: '0x123451234512345123451234512345123451234512345123451234512345',
  v: 28n
})

Released under the MIT License.