# `Cartouche.Transaction`
[🔗](https://github.com/zenhive/cartouche/blob/main/lib/cartouche/transaction.ex#L1)

A module to help build, sign and encode Ethereum transactions.

# `build_signed_trx`

```elixir
@spec build_signed_trx(
  &lt;&lt;_::160&gt;&gt;,
  integer(),
  binary() | {String.t(), [term()]},
  integer() | {integer(), :wei | :gwei} | nil,
  integer(),
  integer() | {integer(), :wei | :gwei},
  Keyword.t()
) :: {:ok, Cartouche.Transaction.V1.t()} | {:error, String.t()}
```

Builds and signs a transaction, to be ready to be passed to JSON-RPC.

Optionally takes a callback to modify the transaction before it is signed.

## Examples

    iex> signer_proc = Cartouche.Test.Signer.start_signer()
    iex> {:ok, signed_trx} = Cartouche.Transaction.build_signed_trx(<<1::160>>, 5, {"baz(uint,address)", [50, :binary.decode_unsigned(<<1::160>>)]}, {50, :gwei}, 100_000, 0, signer: signer_proc, chain_id: :goerli)
    iex> {:ok, signer} = Cartouche.Transaction.V1.recover_signer(signed_trx, 5)
    iex> Cartouche.Hex.to_address(signer)
    "0x63Cc7c25e0cdb121aBb0fE477a6b9901889F99A7"

# `build_signed_trx_v2`

```elixir
@spec build_signed_trx_v2(
  &lt;&lt;_::160&gt;&gt;,
  integer(),
  binary() | {String.t(), [term()]},
  integer() | {integer(), :wei | :gwei} | nil,
  integer() | {integer(), :wei | :gwei} | nil,
  integer(),
  integer() | {integer(), :wei | :gwei},
  list(),
  Keyword.t()
) :: {:ok, Cartouche.Transaction.V2.t()} | {:error, String.t()}
```

Builds and signs a V2 transaction, to be ready to be passed to JSON-RPC.

Optionally takes a callback to modify the transaction before it is signed.

## Examples

    iex> signer_proc = Cartouche.Test.Signer.start_signer()
    iex> {:ok, signed_trx} = Cartouche.Transaction.build_signed_trx_v2(<<1::160>>, 5, {"baz(uint,address)", [50, :binary.decode_unsigned(<<1::160>>)]}, {50, :gwei}, {10, :gwei}, 100_000, 0, [], signer: signer_proc, chain_id: :goerli)
    iex> {:ok, signer} = Cartouche.Transaction.V2.recover_signer(signed_trx)
    iex> Cartouche.Hex.to_address(signer)
    "0x63Cc7c25e0cdb121aBb0fE477a6b9901889F99A7"

# `build_trx`

```elixir
@spec build_trx(
  &lt;&lt;_::160&gt;&gt;,
  integer(),
  binary() | {String.t(), [term()]},
  integer() | {integer(), :wei | :gwei} | nil,
  integer(),
  integer() | {integer(), :wei | :gwei},
  atom() | integer() | nil
) :: Cartouche.Transaction.V1.t()
```

Builds a v1-style call to a given contract

## Examples

    iex> use Cartouche.Hex
    iex> Cartouche.Transaction.build_trx(<<1::160>>, 5, {"baz(uint,address)", [50, :binary.decode_unsigned(<<1::160>>)]}, {50, :gwei}, 100_000, 0, 5)
    %Cartouche.Transaction.V1{
      nonce: 5,
      gas_price: 50000000000,
      gas_limit: 100000,
      to: <<1::160>>,
      value: 0,
      data: ~h[0xA291ADD600000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000001],
      v: 5,
      r: 0,
      s: 0
    }

    iex> use Cartouche.Hex
    iex> call_data = ~h[0xA291ADD600000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000001]
    ...> Cartouche.Transaction.build_trx(<<1::160>>, 5, call_data, {50, :gwei}, 100_000, 0, 5)
    %Cartouche.Transaction.V1{
      nonce: 5,
      gas_price: 50000000000,
      gas_limit: 100000,
      to: <<1::160>>,
      value: 0,
      data: ~h[0xA291ADD600000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000001],
      v: 5,
      r: 0,
      s: 0
    }

# `build_trx_v2`

```elixir
@spec build_trx_v2(
  &lt;&lt;_::160&gt;&gt;,
  integer(),
  binary() | {String.t(), [term()]},
  integer() | {integer(), :wei | :gwei} | nil,
  integer() | {integer(), :wei | :gwei} | nil,
  integer(),
  integer() | {integer(), :wei | :gwei},
  list(),
  atom() | integer() | nil
) :: Cartouche.Transaction.V2.t()
```

Builds a v2 (eip-1559)-style call to a given contract

## Examples

    iex> use Cartouche.Hex
    iex> Cartouche.Transaction.build_trx_v2(<<1::160>>, 6, {"baz(uint,address)", [50, :binary.decode_unsigned(<<1::160>>)]}, {50, :gwei}, {10, :gwei}, 100_000, 0, [<<1::160>>], :goerli)
    %Cartouche.Transaction.V2{
      chain_id: 5,
      nonce: 6,
      max_priority_fee_per_gas: 50000000000,
      max_fee_per_gas: 10000000000,
      gas_limit: 100000,
      destination: <<1::160>>,
      amount: 0,
      data: ~h[0xA291ADD600000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000001],
      access_list: [<<1::160>>],
      signature_y_parity: nil,
      signature_r: nil,
      signature_s: nil
    }

    iex> use Cartouche.Hex
    iex> call_data = ~h[0xA291ADD600000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000001]
    ...> Cartouche.Transaction.build_trx_v2(<<1::160>>, 5, call_data, {50, :gwei}, {10, :gwei}, 100_000, 0, [<<1::160>>], :goerli)
    %Cartouche.Transaction.V2{
      chain_id: 5,
      nonce: 5,
      max_priority_fee_per_gas: 50000000000,
      max_fee_per_gas: 10000000000,
      gas_limit: 100000,
      destination: <<1::160>>,
      amount: 0,
      data: ~h[0xA291ADD600000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000001],
      access_list: [<<1::160>>],
      signature_y_parity: nil,
      signature_r: nil,
      signature_s: nil
    }

---

*Consult [api-reference.md](api-reference.md) for complete listing*
