Skip to main content

Overview

AccountState represents the four fields of an Ethereum account as defined in the Yellow Paper (section 4.1). Every account has a nonce, balance, storage root, and code hash.
import * as AccountState from '@voltaire/primitives/AccountState'

const state = AccountState.from({
  nonce: Nonce.from(5n),
  balance: Wei.from(1000000000000000000n), // 1 ETH
  storageRoot: StateRoot.from("0x56e81f171bcc55a6ff8345e692c0f86e5b47e5b60e2d8c5ab6c7c9fa0e32d3c5"),
  codeHash: Hash.from("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"),
})

Type Definition

type AccountStateType = {
  readonly nonce: NonceType
  readonly balance: WeiType
  readonly storageRoot: StateRootType
  readonly codeHash: HashType
}
Fields:
  • nonce - Transaction count (EOA) or contract creations (contract)
  • balance - Account balance in Wei
  • storageRoot - Merkle Patricia Trie root of storage slots
  • codeHash - Keccak256 hash of EVM bytecode

Creating AccountState

from

Universal constructor from object with required fields.
import * as AccountState from '@voltaire/primitives/AccountState'
import * as Nonce from '@voltaire/primitives/Nonce'
import * as Wei from '@voltaire/primitives/Denomination/Wei'
import * as StateRoot from '@voltaire/primitives/StateRoot'
import * as Hash from '@voltaire/primitives/Hash'

const state = AccountState.from({
  nonce: Nonce.from(0n),
  balance: Wei.from(0n),
  storageRoot: StateRoot.from(AccountState.EMPTY_TRIE_HASH),
  codeHash: Hash.from(AccountState.EMPTY_CODE_HASH),
})

createEmpty

Creates an empty EOA (Externally Owned Account) with zero balance and nonce.
const emptyAccount = AccountState.createEmpty()
// { nonce: 0n, balance: 0n, storageRoot: EMPTY_TRIE_HASH, codeHash: EMPTY_CODE_HASH }

Account Type Detection

isEOA

Checks if account is an Externally Owned Account (has no bytecode).
if (AccountState.isEOA(state)) {
  console.log('This is a regular wallet account')
}

isContract

Checks if account is a contract (has bytecode).
if (AccountState.isContract(state)) {
  console.log('This is a smart contract')
}

Comparing AccountStates

equals

Compares all four fields for equality.
const a = AccountState.createEmpty()
const b = AccountState.createEmpty()

if (AccountState.equals(a, b)) {
  console.log('Account states are identical')
}

Constants

// Keccak256 of empty string - used for EOA code hash
AccountState.EMPTY_CODE_HASH
// "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"

// Keccak256 of RLP([]) - used for EOA storage root
AccountState.EMPTY_TRIE_HASH
// "0x56e81f171bcc55a6ff8345e692c0f86e5b47e5b60e2d8c5ab6c7c9fa0e32d3c5"

EOA vs Contract Accounts

FieldEOAContract
nonceTransaction countContract creations
balanceETH ownedETH owned
storageRootEMPTY_TRIE_HASHStorage trie root
codeHashEMPTY_CODE_HASHBytecode hash

See Also