Skip to content

TransactionEnvelopeEip1559

Utility functions for working with EIP-1559 Typed Transaction Envelopes

Examples

Below are some examples demonstrating common usages of the TransactionEnvelopeEip1559 module:

Instantiating

Transaction Envelopes can be instantiated using TransactionEnvelopeEip1559.from:

import { TransactionEnvelopeEip1559, Value } from 'ox'
 
const envelope = TransactionEnvelopeEip1559.from({
  chainId: 1,
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1'),
})
{
chainId: 1,
maxFeePerGas: 10000000000n,
maxPriorityFeePerGas: 1000000000n,
to: '0x0000000000000000000000000000000000000000',
type: 'eip1559',
value: 1000000000000000000n,
}

Signing

Transaction Envelopes can be signed using TransactionEnvelopeEip1559.getSignPayload and a signing function such as Secp256k1.sign or P256.sign:

import { Secp256k1, TransactionEnvelopeEip1559 } from 'ox'
 
const envelope = TransactionEnvelopeEip1559.from({
  chainId: 1,
  nonce: 0n,
  gasPrice: 1000000000n,
  gas: 21000n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n,
})
 
const signature = Secp256k1.sign({ 
  payload: TransactionEnvelopeEip1559.getSignPayload(envelope), 
  privateKey: '0x...'
})
 
const envelope_signed = TransactionEnvelopeEip1559.from(envelope, { signature })

Serializing

Transaction Envelopes can be serialized using TransactionEnvelopeEip1559.serialize:

import { TransactionEnvelopeEip1559, Value } from 'ox'
 
const envelope = TransactionEnvelopeEip1559.from({
  chainId: 1,
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1'),
})
 
const serialized = TransactionEnvelopeEip1559.serialize(envelope)

Sending

We can send a Transaction Envelope to the network by serializing the signed envelope with .serialize, and then broadcasting it over JSON-RPC with eth_sendRawTransaction.

In this example, we will use RpcTransport.fromHttp to broadcast a eth_sendRawTransaction request over HTTP JSON-RPC.

import { RpcTransport, TransactionEnvelopeEip1559, Secp256k1, Value } from 'ox'
 
// Construct the Envelope.
const envelope = TransactionEnvelopeEip1559.from({
  chainId: 1,
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  nonce: 69n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: Value.fromEther('1.5'),
})
 
// Sign over the Envelope.
const signature = Secp256k1.sign({
  payload: TransactionEnvelopeEip1559.getSignPayload(envelope),
  privateKey: '0x...',
})
 
// Serialize the Envelope with the Signature.
const serialized = TransactionEnvelopeEip1559.serialize(envelope, { 
  signature
})
 
// Broadcast the Envelope with `eth_sendRawTransaction`.
const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com')
const hash = await transport.request({ 
  method: 'eth_sendRawTransaction', 
  params: [serialized], 
})

If you are interfacing with an RPC that supports eth_sendTransaction, you can also use TransactionEnvelopeEip1559.toRpc to convert an Envelope to an RPC-compatible format. This means you can skip the ceremony of manually filling & signing the Transaction.

import 'ox/window'
import { Provider, TransactionEnvelopeEip1559, Value } from 'ox'
 
const envelope = TransactionEnvelopeEip1559.from({
  chainId: 1,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: Value.fromEther('1.5'),
})
 
const envelope_rpc = TransactionEnvelopeEip1559.toRpc(envelope)
 
const provider = Provider.from(window.ethereum)
const hash = await provider.request({
  method: 'eth_sendTransaction',
  params: [envelope_rpc],
})

Computing Hashes

Transaction Hashes can be computed using TransactionEnvelopeEip1559.hash:

import { Secp256k1, TransactionEnvelopeEip1559, Value } from 'ox'
 
const envelope = TransactionEnvelopeEip1559.from({
  chainId: 1,
  nonce: 0n,
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  gas: 21000n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n,
  data: '0x',
})
 
const signature = Secp256k1.sign({
  payload: TransactionEnvelopeEip1559.getSignPayload(envelope),
  privateKey: '0x...'
})
 
const envelope_signed = TransactionEnvelopeEip1559.from(envelope, { signature })
 
const hash = TransactionEnvelopeEip1559.hash(envelope_signed)

Functions

NameDescription
TransactionEnvelopeEip1559.assertAsserts a TransactionEnvelopeEip1559.TransactionEnvelopeEip1559 is valid.
TransactionEnvelopeEip1559.deserializeDeserializes a TransactionEnvelopeEip1559.TransactionEnvelopeEip1559 from its serialized form.
TransactionEnvelopeEip1559.fromConverts an arbitrary transaction object into an EIP-1559 Transaction Envelope.
TransactionEnvelopeEip1559.getSignPayloadReturns the payload to sign for a TransactionEnvelopeEip1559.TransactionEnvelopeEip1559.
TransactionEnvelopeEip1559.hashHashes a TransactionEnvelopeEip1559.TransactionEnvelopeEip1559. This is the "transaction hash".
TransactionEnvelopeEip1559.serializeSerializes a TransactionEnvelopeEip1559.TransactionEnvelopeEip1559.
TransactionEnvelopeEip1559.toRpcConverts an TransactionEnvelopeEip1559.TransactionEnvelopeEip1559 to an TransactionEnvelopeEip1559.Rpc.
TransactionEnvelopeEip1559.validateValidates a TransactionEnvelopeEip1559.TransactionEnvelopeEip1559. Returns true if the envelope is valid, false otherwise.

Types

NameDescription
TransactionEnvelopeEip1559.Rpc
TransactionEnvelopeEip1559.Serialized
TransactionEnvelopeEip1559.SerializedType
TransactionEnvelopeEip1559.Signed
TransactionEnvelopeEip1559.TransactionEnvelopeEip1559
TransactionEnvelopeEip1559.Type