Skip to content

Eth.getStorageAt

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

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. The examples below leave the transport out.

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

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
});

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

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.

Effect<Hex.Hex, Method.Error, Transport>

The value of the slot: 32 bytes as a Hex string, with leading zeros.

The contract. A plain string is not accepted: build it with Address.makeUnsafe, or with Address.make when it comes from outside.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const word = Eth.getStorageAt(ADDRESS, "0x0", "latest");

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.

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",
),
]);

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

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)}` }),
]);
  • No default block. Left out, the param is not sent and the node chooses the block: see 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 reads many slots of many contracts in one request.

The three 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

eth_getStorageAt