> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# AccountState

> Ethereum account state representation per Yellow Paper

## 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.

```typescript theme={null}
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

```typescript theme={null}
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.

```typescript theme={null}
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.

```typescript theme={null}
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).

```typescript theme={null}
if (AccountState.isEOA(state)) {
  console.log('This is a regular wallet account')
}
```

### isContract

Checks if account is a contract (has bytecode).

```typescript theme={null}
if (AccountState.isContract(state)) {
  console.log('This is a smart contract')
}
```

## Comparing AccountStates

### equals

Compares all four fields for equality.

```typescript theme={null}
const a = AccountState.createEmpty()
const b = AccountState.createEmpty()

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

## Constants

```typescript theme={null}
// 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

| Field       | EOA               | Contract           |
| ----------- | ----------------- | ------------------ |
| nonce       | Transaction count | Contract creations |
| balance     | ETH owned         | ETH owned          |
| storageRoot | EMPTY\_TRIE\_HASH | Storage trie root  |
| codeHash    | EMPTY\_CODE\_HASH | Bytecode hash      |

## See Also

* [State](/primitives/state) - Global state management
* [Hash](/primitives/hash) - Keccak256 hashing
* [Denomination](/primitives/denomination) - Wei/Ether conversion
