Skip to content

Eth.getBalance

Returns the balance of an account in wei, at the latest block or at a block you name.

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("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Eth.getBalance(ADDRESS, "latest");
const balance = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// 10000000000000000000000n

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

Pass a block number to read the balance the account had at that block.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const before = Eth.getBalance(ADDRESS, 19_000_000n);

A node keeps old state for a limited number of blocks unless it is an archive node.

The result is a bigint of wei. ethect has no unit helpers: Effect’s BigDecimal turns wei into text, as Amounts and decimals shows. By hand, it is a division.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const inEther = Effect.gen(function* () {
const wei = yield* Eth.getBalance(ADDRESS, "latest");
const whole = wei / 10n ** 18n; // 10000n
const rest = wei % 10n ** 18n; // 0n
return `${whole}.${rest.toString().padStart(18, "0")}`;
});

Effect<bigint, Method.Error, Transport>

The balance in wei. An address that was never used has a balance of 0n.

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 balance = Eth.getBalance(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.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const balances = Effect.all([
Eth.getBalance(ADDRESS, "finalized"),
Eth.getBalance(ADDRESS, 19_000_000n),
Eth.getBalance(ADDRESS, { 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.
  • Wei as bigint. The node answers "0x21e19e0c9bab2400000" and you get 10000000000000000000000n. See values on the wire.
  • EIP-55 address. The address is sent in its EIP-55 form, whatever case you wrote it in. A node reads either.

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 block number is negative: nothing is sent

eth_getBalance