Skip to content

Eth.getStorageValues

Returns the values of several storage slots of several contracts in one request, in the order asked.

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 slot1 = "0x0000000000000000000000000000000000000000000000000000000000000001";
const CONTRACT = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const program = Effect.gen(function* () {
// a contract deployed on the local node, with 7 stored in slot 0
return yield* Eth.getStorageValues({ [CONTRACT]: [slot0, slot1] }, "latest");
});
const values = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// {
// "0x5fbdb2315678afecb367f032d93f642f64180aa3": [
// "0x0000000000000000000000000000000000000000000000000000000000000007",
// "0x0000000000000000000000000000000000000000000000000000000000000000"
// ]
// }

The call works over every transport. The examples below leave the transport out.

Give one entry per contract. The result has the same keys.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const slot0 = "0x0000000000000000000000000000000000000000000000000000000000000000";
const CONTRACT = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const ACCOUNT = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const values = Eth.getStorageValues(
{ [CONTRACT]: [slot0], [ACCOUNT]: [slot0] },
"latest",
);

An Address is lowercase, and so are the keys of the result: index with the address you asked with.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const slot0 = "0x0000000000000000000000000000000000000000000000000000000000000000";
const CONTRACT = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const stored = Effect.gen(function* () {
const values = yield* Eth.getStorageValues({ [CONTRACT]: [slot0] }, "latest");
const word = values[CONTRACT]?.[0];
return word === undefined ? null : BigInt(word); // 7n
});

Effect<Record<Address.Address, ReadonlyArray<Hex.Hex>>, Method.Error, Transport>

For each address asked, the values of its slots as 32-byte Hex words, in the order of the request.

  • Type: Record<Address.Address, ReadonlyArray<Hex.Hex>>

The slots to read, by contract. A slot may be written short: "0x1" is sent as the 32-byte word of slot 1.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const CONTRACT = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const values = Eth.getStorageValues({ [CONTRACT]: ["0x0", "0x1"] }, "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 { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const slot0 = "0x0000000000000000000000000000000000000000000000000000000000000000";
const CONTRACT = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const before = Eth.getStorageValues({ [CONTRACT]: [slot0] }, 1n);
  • No default block. Left out, the param is not sent and the node chooses the block: see Naming a block.
  • In the order asked. The value at position i of an address is the value of the slot at position i of the request.
  • Zero for what is not there. A slot that was never written holds zero, and so does every slot of an account without storage.
  • One slot, one account. Eth.getStorageAt reads a single slot.
  • Every key is an Address. A key that is not a lowercase address fails with a CodecError on the params, and nothing is sent.

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 request names no address, or 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 a key is not an Address: nothing is sent

eth_getStorageValues