# Eth.getProof

Returns the Merkle proof of an account and of the storage slots you name, as defined by
EIP-1186.

## Usage

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

const slot0 = "0x0000000000000000000000000000000000000000000000000000000000000000";

const ADDRESS = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");

const program = Effect.gen(function* () {
  // a contract deployed on the local node, with 7 stored in slot 0
  return yield* Eth.getProof(ADDRESS, [slot0], "latest");
});

const proof = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// proof.nonce: 1
// proof.balance: 0n
// proof.storageProof[0].value: 7n
```

The call works over every [transport](https://ethect.dev/guides/transports/). The examples below leave the
transport out.

### The account alone

Pass no slot to prove the account without any of its storage.

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

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

const account = Effect.gen(function* () {
  const proof = yield* Eth.getProof(ADDRESS, [], "latest");
  return { balance: proof.balance, nodes: proof.accountProof.length };
});
```

`storageProof` is then an empty array.

### Against a block you trust

A proof is checked against the state root of a block. Name that block by hash, so that the
proof and the root belong together.

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

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

const proven = Effect.gen(function* () {
  const block = yield* Eth.getBlockByNumber("finalized", false);
  if (block === null || block.hash === null) return null;
  const proof = yield* Eth.getProof(ADDRESS, [], { blockHash: block.hash });
  return { stateRoot: block.stateRoot, proof };
});
```

## Returns

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

A [`Proof`](https://ethect.dev/rpc/types/proof/): the account's balance, nonce, code hash and storage
root, the trie nodes that prove them, and one storage proof per slot asked.

## Parameters

### address

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

The account to prove. A plain string is not accepted: build it with
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/address/#makeunsafe), or with `Address.make` when it
comes from outside.

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

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const proof = Eth.getProof(ADDRESS, [], "latest");
```

### slots

- **Type:** `ReadonlyArray<Hex.Hex>`

The storage slots to prove, as [`Hex`](https://ethect.dev/reference/primitives/hex/). A slot may be written
short: `"0x1"` is sent as the 32-byte word of slot 1.

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

const ADDRESS = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");

const proof = Eth.getProof(ADDRESS, ["0x0", "0x1"], "latest");
```

### block (optional)

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

The block to prove against: a number, a tag (`"latest"`, `"earliest"`, `"pending"`, `"safe"`,
`"finalized"`), or an object that names the block by hash.

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

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const proofs = Effect.all([
  Eth.getProof(ADDRESS, [], "finalized"),
  Eth.getProof(ADDRESS, [], 19_000_000n),
  Eth.getProof(ADDRESS, [], { blockHash: `0x${"ab".repeat(32)}` }),
]);
```

## Behavior

- **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).
- **One storage proof per slot, in the order asked.** Each has the slot as `key`, its `value`
  as a `bigint`, and the trie nodes that prove it.
- **An account that does not exist has a proof too.** Its balance is `0n`, its nonce `0`, and
  each slot asked has the value `0n`. The proof shows the account is absent from the state.
- **Not verified.** ethect returns the proof as the node sent it. Nothing in the library checks
  it against a state root yet.

## 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 block number is above the chain's head |
| `RpcError` | `ResourceNotFound` | no block has the given `blockHash` |
| `CodecError` | `on: "params"` | a slot is not hex or is longer than 32 bytes, or the block number is negative: nothing is sent |

## JSON-RPC method

`eth_getProof`
