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:Behavior
SELFBALANCE pushes the current contract’s balance onto the stack as a 256-bit unsigned integer in wei:ADDRESS(2 gas) +BALANCE(cold: 2600 gas, warm: 100 gas) = 2602+ gasSELFBALANCE: 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 gasADDRESS+BALANCE(cold): 2 + 2600 = 2602 gas (520x more expensive!)ADDRESS+BALANCE(warm): 2 + 100 = 102 gas (20x more expensive)
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
- TypeScript
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)
- 5 gas per query
- ~200,000 queries per million gas
- 520x cheaper than
ADDRESS+BALANCE(cold)
Related Instructions
- ADDRESS (0x30) - Get contract address
- BALANCE (0x31) - Get balance of any address
- CALLVALUE (0x34) - Get ETH sent with call
References
- EIP-1884 - Repricing and SELFBALANCE
- Yellow Paper - Section 9.3
- EVM Codes - SELFBALANCE
- Solidity Docs - address.balance

