Skip to main content
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.

Overview

Opcode: 0x31 Introduced: Frontier (EVM genesis) BALANCE retrieves the balance (in wei) of any account on the blockchain. It pops an address from the stack and pushes the balance of that address.

Specification

Stack Input:
Stack Output:
Gas Cost: Variable (hardfork-dependent)
  • Frontier - Homestead: 20 gas
  • Tangerine Whistle (EIP-150): 400 gas
  • Istanbul (EIP-1884): 700 gas
  • Berlin (EIP-2929): 2600 gas (cold) / 100 gas (warm)
Operation:

Behavior

BALANCE accesses the blockchain state to retrieve an account’s balance. The address is popped from the stack as a uint256, with only the lower 160 bits used. Key characteristics:
  • Returns balance in wei (1 ether = 10^18 wei)
  • Returns 0 for non-existent accounts
  • Gas cost depends on warm/cold access (Berlin+)
  • Does not distinguish between EOA and contract accounts

Examples

Basic Balance Check

Contract Balance Check

Payment Validation

Gas Cost

Historical evolution: Cold vs Warm Access (Berlin+):
  • Cold: First access to an address in transaction (2600 gas)
  • Warm: Subsequent accesses to same address (100 gas)
Access List (EIP-2930):

Common Usage

Minimum Balance Requirement

Balance Tracking

Withdrawal Pattern

Payment Routing

Security

Balance Checks Are Not Atomic

Safe pattern:

Self-Destruct Race Condition

Attack:
Safe pattern:

Integer Overflow in Balance Calculations

Safe pattern (0.8.0+):

Implementation

Edge Cases

Zero Address Balance

Non-Existent Account

Maximum Balance

Stack Underflow

References