# Eth.sendTransaction

Asks the node or wallet to sign a transaction with an account it holds the key of, and to send
it. Returns the transaction 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 = Eth.sendTransaction({ from: FROM, to: TO, value: 10n ** 18n });

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 example sends one ether between two of anvil's development accounts, whose keys anvil
holds.

This method does not sign. The request goes to the node or the wallet, which holds the key,
and `from` must be an account it can sign for. To sign with a key your program holds, and to
wait for the receipt, use [`Tx.send`](https://ethect.dev/reference/tx/send/) with a
[`Signer`](https://ethect.dev/reference/signer/).

The examples below leave the transport out.

### Waiting for the receipt

The hash comes back when the node accepts the transaction, which can be before it is mined.
Ask for the receipt until there is one.

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

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

const receipt = Effect.gen(function* () {
  const hash = yield* Eth.sendTransaction({ from: FROM, to: TO, value: 1n });
  return yield* Eth.getTransactionReceipt(hash).pipe(
    Effect.repeat({
      until: (receipt) => receipt !== null,
      schedule: Schedule.spaced("1 second"),
    }),
  );
});
```

Where the node serves it, [`Eth.sendTransactionSync`](https://ethect.dev/rpc/eth/send-transaction-sync/)
does both in one call.

### Calling a contract

Put the contract in `to` and the calldata in `data`, as hex.
[`Call.encode`](https://ethect.dev/reference/call/encode/) builds both from a call of a contract.

```ts
import { Effect } from "effect";
import { Address, Call, Erc20 } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const RECIPIENT = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const hash = Effect.gen(function* () {
  // transfer(address,uint256): the selector, then one 32-byte word per argument
  const { data } = yield* Call.encode(Erc20.at(TOKEN).transfer(RECIPIENT, 10n ** 18n));
  return yield* Eth.sendTransaction({ from: FROM, to: TOKEN, data });
});
```

### Deploying a contract

Leave `to` out and put the creation code in `data`.

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

// The creation code of the contract, as your compiler outputs it.
declare const bytecode: Hex.Hex;

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

const hash = Eth.sendTransaction({ from: FROM, data: bytecode });
```

The address of the new contract is the `contractAddress` of the receipt.

### From a wallet

Over the [EIP-1193 transport](https://ethect.dev/reference/transport/eip1193/), the wallet asks its user to
approve. A refusal is an `RpcError` whose reason is `UserRejectedRequest`.

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

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

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

const sent = await Effect.gen(function* () {
  const [from] = yield* Eth.accounts();
  return yield* Eth.sendTransaction({ from, to: TO, value: 10n ** 16n }).pipe(
    Effect.map((hash) => ({ sent: true as const, hash })),
    Effect.catchReason("RpcError", "UserRejectedRequest", () =>
      Effect.succeed({ sent: false as const }),
    ),
  );
}).pipe(Effect.provide(Transport.eip1193(provider)), Effect.runPromise);
```

## 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

### request

- **Type:** [`TransactionRequest.TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/)

The transaction to send. Every field is optional: the node or wallet fills in what is missing.

| Field | Type | Description |
| --- | --- | --- |
| `from` | `Address` | the account that signs and pays |
| `to` | `Address` | the recipient or the contract, left out to create a contract |
| `value` | `bigint` | wei to send |
| `data` | `Hex` | the calldata, or the creation code of a contract |
| `gas` | `bigint` | the gas limit |
| `nonce` | `number` | the nonce, to replace a pending transaction or to order several |
| `maxFeePerGas`, `maxPriorityFeePerGas` | `bigint` | the fees of an EIP-1559 transaction, in wei |
| `gasPrice` | `bigint` | the gas price of a legacy transaction, in wei |

The other fields (an access list, blobs, authorizations) are on the
[`TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/) page.

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

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

const hash = Eth.sendTransaction({
  from: FROM,
  to: TO,
  value: 1n,
  maxFeePerGas: 30n * 10n ** 9n,
  maxPriorityFeePerGas: 10n ** 9n,
});
```

## Behavior

- **No signing here.** The request goes to the node or wallet unsigned. A node that holds no
  key for `from` refuses it: sign with a key of your own through
  [`Tx.send`](https://ethect.dev/reference/tx/send/), or sign elsewhere and use
  [`Eth.sendRawTransaction`](https://ethect.dev/rpc/eth/send-raw-transaction/).
- **Sent is not mined.** With automatic mining off on anvil, the hash comes back while the
  receipt is still `null`.
- **A revert still has a hash.** anvil mines a transaction that reverts: only its receipt, with
  the status `"reverted"`, says so.
- **The fees choose the type.** anvil sends an EIP-1559 transaction, and a legacy one when the
  request has a `gasPrice`.
- **Write the calldata as `data`**, the name wallets know. `input`, the name a node returns
  it by, is sent as well when a request has 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` | `InvalidParams` | the node holds no key for `from` |
| `RpcError` | `TransactionRejected` | `from` cannot pay the `value` and the gas |
| `RpcError` | `TransactionRejected` | the `nonce` was used already |
| `RpcError` | `InvalidInput` | `gas` is below the intrinsic gas of the transaction |

A wallet adds `UserRejectedRequest` when its user refuses, as defined by EIP-1193.

## JSON-RPC method

`eth_sendTransaction`
