Skip to content

Eth.getAccount

Returns the nonce, the balance, the storage root and the code hash of an account in one call. Not served by every client.

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 = Eth.getAccount(ADDRESS, "latest");
const account = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// {
// nonce: 1,
// balance: 0n,
// storageRoot: "0xf258b1c6d5ee7f6f3549117fb0ac79118d5ad19b9c027f9b8e1471ca519b3b6c",
// codeHash: "0x56a1235d0e575670832f9fb685f7146d7d9d6d7e4cbc28bfe94e39beb717a7cf"
// }

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

One request replaces Eth.getTransactionCount and Eth.getBalance.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const sender = Effect.gen(function* () {
const account = yield* Eth.getAccount(ADDRESS, "latest");
if (account === null) return { nonce: 0, balance: 0n };
return { nonce: account.nonce, balance: account.balance };
});

An account without code has the hash of empty code. Hash.keccak256 of no bytes gives it.

import { Effect } from "effect";
import { Address, Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const emptyCodeHash = Hash.keccak256(new Uint8Array());
const ADDRESS = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const isContract = Effect.gen(function* () {
const account = yield* Eth.getAccount(ADDRESS, "latest");
return account !== null && account.codeHash !== emptyCodeHash; // true
});

A node without the method answers with an RpcError whose reason is MethodNotFound. Fall back to the two calls every node serves.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const sender = Eth.getAccount(ADDRESS, "latest").pipe(
Effect.map((account) => ({ nonce: account?.nonce, balance: account?.balance })),
Effect.catchReason("RpcError", "MethodNotFound", () =>
Effect.all({
nonce: Eth.getTransactionCount(ADDRESS, "latest"),
balance: Eth.getBalance(ADDRESS, "latest"),
}),
),
);

Effect<Account.Account | null, Method.Error, Transport>

An Account: the four fields an account has in the state.

The account. 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("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const account = Eth.getAccount(ADDRESS, "latest");

The block to read at: a number, a tag ("latest", "earliest", "pending", "safe", "finalized"), or an object that names the block by hash. Unlike the other state methods, it is required.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const accounts = Effect.all([
Eth.getAccount(ADDRESS, "finalized"),
Eth.getAccount(ADDRESS, 19_000_000n),
Eth.getAccount(ADDRESS, { blockHash: `0x${"ab".repeat(32)}` }),
]);
  • An unused address is an empty account on anvil. anvil 1.9 answers a nonce of 0, a balance of 0n, and the hashes of empty storage and empty code. The result type also allows null: handle it.
  • The same values as a proof. nonce, balance and codeHash are the ones Eth.getProof returns, without the trie nodes.
  • No code, no storage. The account holds two hashes. Read the code with Eth.getCode and a slot with Eth.getStorageAt.

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

eth_getAccount