# Eth.getStorageAt

Returns the value of one storage slot of a contract, as a 32-byte word.

## Usage

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

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.getStorageAt(ADDRESS, "0x0", "latest");
});

const word = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// "0x0000000000000000000000000000000000000000000000000000000000000007"
```

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

### As a number

The result is hex. `BigInt` reads a word that holds an unsigned integer.

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

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

const stored = Effect.gen(function* () {
  const word = yield* Eth.getStorageAt(ADDRESS, "0x0", "latest");
  return BigInt(word); // 7n
});
```

### At a past block

Pass a block number to read the value the slot had at that block.

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

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

const before = Eth.getStorageAt(ADDRESS, "0x0", 1n);
// "0x0000000000000000000000000000000000000000000000000000000000000000"
```

The slot was written in block 2, so at block 1 it still holds zero.

## Returns

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

The value of the slot: 32 bytes as a [`Hex`](https://ethect.dev/reference/primitives/hex/) string, with leading
zeros.

## Parameters

### address

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

The contract. 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("0x5FbDB2315678afecb367f032d93F642f64180aa3");

const word = Eth.getStorageAt(ADDRESS, "0x0", "latest");
```

### slot

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

The position of the slot, as hex. The short form and the 32-byte form name the same slot:
either way it is sent as 32 bytes.

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

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

const words = Effect.all([
  Eth.getStorageAt(ADDRESS, "0x0", "latest"),
  Eth.getStorageAt(
    ADDRESS,
    "0x0000000000000000000000000000000000000000000000000000000000000000",
    "latest",
  ),
]);
```

### block (optional)

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

The block to read at: 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("0x5FbDB2315678afecb367f032d93F642f64180aa3");

const words = Effect.all([
  Eth.getStorageAt(ADDRESS, "0x0", "finalized"),
  Eth.getStorageAt(ADDRESS, "0x0", 19_000_000n),
  Eth.getStorageAt(ADDRESS, "0x0", { 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).
- **Always 32 bytes.** A slot that was never written holds zero, and so does every slot of an
  account without storage. The result is never `null`.
- **A slot may be written short.** `"0x0"` is sent as the 32-byte word of slot 0, here as in
  every method that takes a slot.
- **The raw word.** ethect does not decode it. What it means depends on the storage layout of
  the contract.
- **Several slots at once.** [`Eth.getStorageValues`](https://ethect.dev/rpc/eth/get-storage-values/) reads
  many slots of many contracts in one request.

## 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"` | the slot is not hex or is longer than 32 bytes, or the block number is negative: nothing is sent |

## JSON-RPC method

`eth_getStorageAt`
