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: 0x47 Introduced: Istanbul (EIP-1884) SELFBALANCE retrieves the balance of the currently executing contract in wei. This is a gas-efficient alternative to ADDRESS followed by BALANCE for querying the executing contract’s own balance.

Specification

Stack Input:
Stack Output:
Gas Cost: 5 (GasFastStep) Operation:
Hardfork: Available from Istanbul onwards

Behavior

SELFBALANCE pushes the current contract’s balance onto the stack as a 256-bit unsigned integer in wei:
This is significantly cheaper than the equivalent sequence:
  • ADDRESS (2 gas) + BALANCE (cold: 2600 gas, warm: 100 gas) = 2602+ gas
  • SELFBALANCE: 5 gas

Examples

Basic Usage

Pre-Istanbul Error

Balance Checks

Gas Cost

Cost: 5 gas (GasFastStep) SELFBALANCE is dramatically cheaper than the alternative: Comparison:
  • SELFBALANCE: 5 gas
  • ADDRESS + BALANCE (cold): 2 + 2600 = 2602 gas (520x more expensive!)
  • ADDRESS + BALANCE (warm): 2 + 100 = 102 gas (20x more expensive)
This makes SELFBALANCE one of the most cost-effective operations introduced in Istanbul.

Common Usage

Minimum Balance Guard

Balance Tracking

Payment Verification

Withdrawal Logic

Fee Collection

Auction Reserve Check

Security Considerations

Reentrancy and Balance Changes

Balance can change during execution:

SELFDESTRUCT Interaction

Contracts can receive ETH from SELFDESTRUCT without receive function:

Balance vs State Accounting

Don’t rely solely on balance for accounting:

Gas Cost Changes

Pre-Istanbul, querying self balance was expensive:

Implementation

Edge Cases

Pre-Istanbul Execution

Zero Balance

Maximum Balance

During ETH Transfer

Practical Patterns

Balance-Based State Machine

Efficient Balance Queries

Benchmarks

Performance:
  • Balance lookup: O(1) from state
  • Stack push: O(1)
Gas efficiency:
  • 5 gas per query
  • ~200,000 queries per million gas
  • 520x cheaper than ADDRESS + BALANCE (cold)

References