Web3 module to interact with the Ethereum blockchain and smart contracts.
67
The account operations functionality provides comprehensive access to Ethereum account information including balances, transaction counts, contract code, storage inspection, and cryptographic proof generation.
Retrieves the Ether balance of an account at a specific block.
getBalance(address: Address, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Numbers>;Parameters:
address: The account address to queryblockNumber: Block number or tag (defaults to "latest")returnFormat: Output format configurationUsage Example:
// Get current balance
const balance = await eth.getBalance("0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E");
console.log(`Balance: ${balance} wei`);
// Get historical balance
const historicalBalance = await eth.getBalance(
"0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E",
15000000
);
// Convert to ETH for display
import { fromWei } from "web3-utils";
const balanceInEth = fromWei(balance, "ether");
console.log(`Balance: ${balanceInEth} ETH`);Returns the number of transactions sent from an address (account nonce).
getTransactionCount(address: Address, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Numbers>;Usage Example:
// Get current nonce (for next transaction)
const nonce = await eth.getTransactionCount("0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E");
// Get pending nonce (including pending transactions)
const pendingNonce = await eth.getTransactionCount(
"0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E",
"pending"
);Retrieves the bytecode of a smart contract at a given address.
getCode(address: Address, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Bytes>;Usage Example:
// Check if address is a contract
const code = await eth.getCode("0x1234567890123456789012345678901234567890");
if (code !== "0x") {
console.log("Address is a smart contract");
console.log("Bytecode:", code);
} else {
console.log("Address is an externally owned account (EOA)");
}Reads a value from a smart contract's storage at a specific slot.
getStorageAt(address: Address, storageSlot: Numbers, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Bytes>;Parameters:
address: Contract addressstorageSlot: Storage slot index (0-based)blockNumber: Block number or tagreturnFormat: Output format configurationUsage Example:
// Read storage slot 0 (often contains important state)
const storageValue = await eth.getStorageAt(
"0x1234567890123456789012345678901234567890", // contract address
0 // storage slot
);
console.log("Storage slot 0:", storageValue);
// Read multiple storage slots
const slots = [0, 1, 2, 3];
const values = await Promise.all(
slots.map(slot =>
eth.getStorageAt("0x1234567890123456789012345678901234567890", slot)
)
);
console.log("Storage values:", values);Returns a list of accounts controlled by the connected wallet/provider.
getAccounts(): Promise<Address[]>;Usage Example:
const accounts = await eth.getAccounts();
console.log("Available accounts:", accounts);
if (accounts.length > 0) {
console.log("Default account:", accounts[0]);
}Requests access to accounts from the user (e.g., MetaMask popup).
requestAccounts(): Promise<Address[]>;Usage Example:
try {
const accounts = await eth.requestAccounts();
console.log("User granted access to accounts:", accounts);
} catch (error) {
console.log("User denied account access");
}Generates a Merkle proof for an account and its storage, useful for state verification.
getProof(address: Address, storageKeys?: HexString32Bytes[], blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<AccountObject>;Parameters:
address: Account address to provestorageKeys: Array of storage slot keys to include in proofblockNumber: Block number for proof generationreturnFormat: Output format configurationUsage Example:
// Get proof for account and specific storage slots
const proof = await eth.getProof(
"0x1234567890123456789012345678901234567890",
[
"0x0000000000000000000000000000000000000000000000000000000000000000", // slot 0
"0x0000000000000000000000000000000000000000000000000000000000000001" // slot 1
],
"latest"
);
console.log("Account proof:", {
address: proof.address,
balance: proof.balance,
codeHash: proof.codeHash,
nonce: proof.nonce,
storageProof: proof.storageProof
});
// Verify the proof contains expected storage values
proof.storageProof.forEach((storage, index) => {
console.log(`Storage slot ${index}:`, {
key: storage.key,
value: storage.value,
proof: storage.proof
});
});// Check if address is a contract
async function isContract(address: Address): Promise<boolean> {
const code = await eth.getCode(address);
return code !== "0x";
}
// Get comprehensive account info
async function getAccountInfo(address: Address) {
const [balance, nonce, code] = await Promise.all([
eth.getBalance(address),
eth.getTransactionCount(address),
eth.getCode(address)
]);
return {
address,
balance,
nonce,
isContract: code !== "0x",
codeSize: code === "0x" ? 0 : (code.length - 2) / 2 // bytes
};
}interface AccountObject {
address: Address;
balance: Numbers;
codeHash: HexString32Bytes;
nonce: Numbers;
storageHash: HexString32Bytes;
accountProof: HexString[];
storageProof: StorageProof[];
}
interface StorageProof {
key: HexString32Bytes;
value: Numbers;
proof: HexString[];
}
type Address = HexString20Bytes;
type Numbers = HexString | number | bigint;
type Bytes = HexString;
type BlockNumberOrTag = Numbers | "latest" | "earliest" | "pending" | "safe" | "finalized";
interface DataFormat {
number: NumberFormat;
bytes: BytesFormat;
}
type NumberFormat = "NUMBER_HEX" | "NUMBER_NUMBER" | "NUMBER_BIGINT";
type BytesFormat = "BYTES_HEX" | "BYTES_UINT8ARRAY";Install with Tessl CLI
npx tessl i tessl/npm-web3-ethdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10