# Eth.sendRawTransactionSync

Sends a signed, encoded transaction, then waits for it to be mined and returns its receipt
(EIP-7966). Not every client serves it.

## 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 });
  const receipt = yield* Eth.sendRawTransactionSync(transaction);
  return { status: receipt.status, block: receipt.blockNumber };
});

const result = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// { status: "success", block: 1n }
```

One call sends the bytes and returns the [`Receipt`](https://ethect.dev/rpc/types/receipt/): there is
nothing to poll.

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.

### With a timeout

The second param is how long the node may wait, in milliseconds. When the time runs out the
node answers with an error whose reason is `TransactionNotMinedInTime`, and the transaction
can still be mined later.

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

// A signed, encoded transaction.
declare const transaction: Hex.Hex;

const outcome = Eth.sendRawTransactionSync(transaction, 2_000).pipe(
  Effect.map((receipt) => ({ mined: true as const, status: receipt.status })),
  Effect.catchTag("RpcError", (error) =>
    // not mined in time: `error.data` is the hash of the transaction
    error.reason._tag === "TransactionNotMinedInTime"
      ? Effect.succeed({ mined: false as const, hash: error.data })
      : Effect.fail(error),
  ),
);
```

## Returns

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

The [`Receipt`](https://ethect.dev/rpc/types/receipt/) of the mined transaction.

## Parameters

### transaction

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

The signed transaction, encoded, as for
[`Eth.sendRawTransaction`](https://ethect.dev/rpc/eth/send-raw-transaction/#transaction).

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

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

const receipt = Eth.sendRawTransactionSync(transaction);
```

### timeout (optional)

- **Type:** `number`

The longest the node may wait for the transaction to be mined, in milliseconds. It is a JSON
number on the wire, not a hex quantity.

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

declare const transaction: Hex.Hex;

const receipt = Eth.sendRawTransactionSync(transaction, 5_000);
```

Left out, the param is not sent and the node applies its own limit.

## Behavior

- **A revert is not a failure.** The receipt of a reverted transaction is a success of the
  call. Read `receipt.status`.
- **A timeout is not a rejection.** The transaction is in the pool and can still be mined.
  The error carries its hash: ask for the receipt later.
- **The timeout has a reason.** The code `4` is `TransactionNotMinedInTime`, and `error.data`
  is the hash of the transaction, typed `unknown`.
- **Not served by every client.** anvil serves it.

## 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` | `TransactionNotMinedInTime` | the transaction is not mined within the timeout (code `4`): `error.data` is its hash |
| `RpcError` | `InvalidParams` | the bytes are not an encoded transaction |
| `RpcError` | `TransactionRejected` | the nonce of the transaction was used already |

## JSON-RPC method

`eth_sendRawTransactionSync`
