# TxEnvelopeEip1559

Utility functions for working with [EIP-1559 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-1559)

## Examples

Below are some examples demonstrating common usages of the `TxEnvelopeEip1559` module:

* [Instantiating](#instantiating)

* [Signing](#signing)

* [Serializing](#serializing)

* [Sending](#sending)

* [Computing Hashes](#computing-hashes)

### Instantiating

Transaction Envelopes can be instantiated using [`TxEnvelopeEip1559.from`](/api/TxEnvelopeEip1559/from):

```ts twoslash
import { TxEnvelopeEip1559, Value } from 'ox'

const envelope = TxEnvelopeEip1559.from({
  chainId: 1,
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1')
})
// @log: {
// @log:   chainId: 1,
// @log:   maxFeePerGas: 10000000000n,
// @log:   maxPriorityFeePerGas: 1000000000n,
// @log:   to: '0x0000000000000000000000000000000000000000',
// @log:   type: 'eip1559',
// @log:   value: 1000000000000000000n,
// @log: }
```

### Signing

Transaction Envelopes can be signed using [`TxEnvelopeEip1559.getSignPayload`](/api/TxEnvelopeEip1559/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign):

```ts twoslash
import { Secp256k1, TxEnvelopeEip1559 } from 'ox'

const envelope = TxEnvelopeEip1559.from({
  chainId: 1,
  nonce: 0n,
  gasPrice: 1000000000n,
  gas: 21000n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n
})

const signature = Secp256k1.sign({
  // [!code focus]
  payload: TxEnvelopeEip1559.getSignPayload(envelope), // [!code focus]
  privateKey: '0x...' // [!code focus]
}) // [!code focus]

const envelope_signed = TxEnvelopeEip1559.from(envelope, {
  signature
})
```

### Serializing

Transaction Envelopes can be serialized using [`TxEnvelopeEip1559.serialize`](/api/TxEnvelopeEip1559/serialize):

```ts twoslash
import { TxEnvelopeEip1559, Value } from 'ox'

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

const serialized = TxEnvelopeEip1559.serialize(envelope) // [!code focus]
```

### 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`](/api/RpcTransport/fromHttp) to broadcast a `eth_sendRawTransaction` request over HTTP JSON-RPC.

```ts twoslash
import {
  RpcTransport,
  TxEnvelopeEip1559,
  Secp256k1,
  Value
} from 'ox'

// Construct the Envelope.
const envelope = TxEnvelopeEip1559.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: TxEnvelopeEip1559.getSignPayload(envelope),
  privateKey: '0x...'
})

// Serialize the Envelope with the Signature. // [!code focus]
const serialized = TxEnvelopeEip1559.serialize(envelope, {
  // [!code focus]
  signature // [!code focus]
}) // [!code focus]

// Broadcast the Envelope with `eth_sendRawTransaction`. // [!code focus]
const transport = RpcTransport.fromHttp(
  'https://1.rpc.thirdweb.com'
) // [!code focus]
const hash = await transport.request({
  // [!code focus]
  method: 'eth_sendRawTransaction', // [!code focus]
  params: [serialized] // [!code focus]
}) // [!code focus]
```

If you are interfacing with an RPC that supports `eth_sendTransaction`, you can also use
[`TxEnvelopeEip1559.toRpc`](/api/TxEnvelopeEip1559/toRpc) to convert an Envelope to an RPC-compatible format.
This means you can skip the ceremony of manually filling & signing the Transaction.

```ts twoslash
import 'ox/window'
import { Provider, TxEnvelopeEip1559, Value } from 'ox'

const envelope = TxEnvelopeEip1559.from({
  chainId: 1,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: Value.fromEther('1.5')
})

const envelope_rpc = TxEnvelopeEip1559.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 [`TxEnvelopeEip1559.hash`](/api/TxEnvelopeEip1559/hash):

```ts twoslash
import { Secp256k1, TxEnvelopeEip1559, Value } from 'ox'

const envelope = TxEnvelopeEip1559.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: TxEnvelopeEip1559.getSignPayload(envelope),
  privateKey: '0x...'
})

const envelope_signed = TxEnvelopeEip1559.from(envelope, {
  signature
})

const hash = TxEnvelopeEip1559.hash(envelope_signed) // [!code focus]
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TxEnvelopeEip1559.assert`](/api/TxEnvelopeEip1559/assert) | Asserts a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) is valid. |
| [`TxEnvelopeEip1559.deserialize`](/api/TxEnvelopeEip1559/deserialize) | Deserializes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) from its serialized form. |
| [`TxEnvelopeEip1559.from`](/api/TxEnvelopeEip1559/from) | Converts an arbitrary transaction object into an EIP-1559 Transaction Envelope. |
| [`TxEnvelopeEip1559.getSignPayload`](/api/TxEnvelopeEip1559/getSignPayload) | Returns the payload to sign for a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). |
| [`TxEnvelopeEip1559.hash`](/api/TxEnvelopeEip1559/hash) | Hashes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). This is the "transaction hash". |
| [`TxEnvelopeEip1559.serialize`](/api/TxEnvelopeEip1559/serialize) | Serializes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). |
| [`TxEnvelopeEip1559.toRpc`](/api/TxEnvelopeEip1559/toRpc) | Converts an [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) to an [`TxEnvelopeEip1559.Rpc`](/api/TxEnvelopeEip1559/types#rpc). |
| [`TxEnvelopeEip1559.validate`](/api/TxEnvelopeEip1559/validate) | Validates a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). Returns `true` if the envelope is valid, `false` otherwise. |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TxEnvelopeEip1559.Rpc`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559rpc) |  |
| [`TxEnvelopeEip1559.Serialized`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559serialized) |  |
| [`TxEnvelopeEip1559.SerializedType`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559serializedtype) |  |
| [`TxEnvelopeEip1559.Signed`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559signed) |  |
| [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559txenvelopeeip1559) |  |
| [`TxEnvelopeEip1559.Type`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559type) |  |
