# Eth.createAccessList

Returns the access list a transaction would use, which is the addresses and storage slots it
touches, and the gas it uses when sent with that list.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Call, Erc20, Transport } 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 program = 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.createAccessList({ from: FROM, to: TOKEN, data }, "latest");
});

const { accessList, gasUsed } = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
```

The node runs the transaction and records what it reads and writes. `accessList` has one entry
per address, each with the storage keys touched there.

The examples below leave the transport out.

### Sending with the list

Add the list to the request you send. The transaction then declares what it touches
(EIP-2930).

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

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

const sent = Effect.gen(function* () {
  // a contract that stores the 32-byte word it is called with
  const request = { from: FROM, to: TO, data: `0x${"00".repeat(31)}05` as const };
  const { accessList } = yield* Eth.createAccessList(request, "latest");
  return yield* Eth.sendTransaction({ ...request, accessList });
});
```

### A transaction that would fail

The call still succeeds, and the result carries an `error`.

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

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

const checked = Effect.gen(function* () {
  // a contract that reverts when it gets no calldata
  const result = yield* Eth.createAccessList({ from: FROM, to: TO }, "latest");
  return result.error ?? "would succeed";
});
// "execution reverted"
```

## Returns

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

An [`AccessList.Result`](https://ethect.dev/rpc/types/access-list/):

| Field | Type | Description |
| --- | --- | --- |
| `accessList` | `AccessList` | the addresses touched, each with its storage keys |
| `gasUsed` | `bigint` | the gas the transaction uses when sent with that list |
| `error` | `string` (optional) | set when the transaction would fail |

A transfer of ether to an account without code gives `{ accessList: [], gasUsed: 21000n }`.

## Parameters

### request

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

The transaction to run. The fields are those of
[`Eth.estimateGas`](https://ethect.dev/rpc/eth/estimate-gas/#request).

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

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

const list = Eth.createAccessList({ from: FROM, to: TO, value: 1n }, "latest");
// { accessList: [], gasUsed: 21000n }
```

### block (optional)

- **Type:** [`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)

The block whose state the transaction runs on: a number, a tag, or an object that names the
block by hash.

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

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

const list = Eth.createAccessList({ from: FROM, to: TO, value: 1n }, "pending");
```

## Behavior

- **A revert is in the result.** A transaction that would revert does not fail the call. Test
  `result.error` before using the list.
- **Not always cheaper.** `gasUsed` is the gas with the list, and a list can cost more than it
  saves. Compare it with [`Eth.estimateGas`](https://ethect.dev/rpc/eth/estimate-gas/) of the same request.
- **No default block.** Left out, the param is not sent and the node chooses the block:
  see [Naming a block](https://ethect.dev/rpc/guides/calling-methods/#naming-a-block).
- **Lowercase addresses.** The addresses of the list are lowercase, like every address in a
  result.

## 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` | `TransactionRejected` | `from` does not hold the `value` |
| `RpcError` | `InvalidParams` | the block number is above the chain's head |

## JSON-RPC method

`eth_createAccessList`
