# Eth.sendRawTransaction

Sends a transaction that is already signed and encoded. Returns its hash.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const program = Effect.gen(function* () {
  // signed by the node, which holds the key of this account
  const transaction = yield* Eth.signTransaction({ from: FROM, to: TO, value: 10n ** 18n });
  return yield* Eth.sendRawTransaction(transaction);
});

const hash = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// "0x…": the hash of the transaction, 32 bytes
```

The node only forwards the bytes: it needs no key, so this works on a public node.

The bytes are a signed transaction.
[`Signer.signTransaction`](https://ethect.dev/reference/signer/sign-transaction/) gives them for a key your
program holds, and [`Eth.signTransaction`](https://ethect.dev/rpc/eth/sign-transaction/) for a key the node
holds. [`Tx.send`](https://ethect.dev/reference/tx/send/) signs and sends in one step.

The examples below leave the transport out.

### Signed elsewhere

The param is the signed transaction as hex, whatever produced it.

```ts
import { type Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

// A signed, encoded transaction: from a hardware wallet, a signing service,
// or another library.
declare const transaction: Hex.Hex;

const hash = Eth.sendRawTransaction(transaction);
```

### The hash before sending

The hash of a transaction is the Keccak-256 of its signed bytes, so you can know it without
the node.

```ts
import { Effect } from "effect";
import { Hash, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const send = (transaction: Hex.Hex) =>
  Effect.gen(function* () {
    const expected = Hash.keccak256(yield* Hex.toBytes(transaction));
    const hash = yield* Eth.sendRawTransaction(transaction);
    return hash === expected; // true
  });
```

## Returns

[`Effect<Hash, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The [hash](https://ethect.dev/reference/primitives/hash/) of the transaction. It says the node accepted the
transaction, not that it is mined or that it succeeded.

## Parameters

### transaction

- **Type:** [`Hex`](https://ethect.dev/reference/primitives/hex/)

The signed transaction, encoded: the type byte followed by the RLP of its fields, or the RLP
alone for a legacy transaction. An EIP-1559 transaction starts with `0x02`.

```ts
import { type Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

declare const transaction: Hex.Hex; // "0x02f86c…"

const hash = Eth.sendRawTransaction(transaction);
```

## Behavior

- **Sent is not mined.** The hash comes back when the node accepts the transaction. Ask for the
  receipt with [`Eth.getTransactionReceipt`](https://ethect.dev/rpc/eth/get-transaction-receipt/) until
  there is one, or use
  [`Eth.sendRawTransactionSync`](https://ethect.dev/rpc/eth/send-raw-transaction-sync/).
- **Nothing is filled in.** The nonce, the gas and the fees are inside the signed bytes. The
  node cannot change them.
- **Once only.** Sending the same bytes again after the transaction is mined fails: its nonce
  is used.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `InvalidParams` | the bytes are not an encoded transaction |
| `RpcError` | `TransactionRejected` | the nonce of the transaction was used already |

## JSON-RPC method

`eth_sendRawTransaction`
