Get the balance of the currently executing contract
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:0x47Introduced: 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:
(none)
Stack Output:
balance (wei as u256)
Gas Cost: 5 (GasFastStep)Operation:
stack.push(balance(address(this)))
Hardfork: Available from Istanbul onwards
Behavior
SELFBALANCE pushes the current contract’s balance onto the stack as a 256-bit unsigned integer in wei:
contract Auction { uint256 public reservePrice; function finalize() external { require(address(this).balance >= reservePrice, "Reserve not met"); // Transfer to seller payable(seller).transfer(address(this).balance); }}
Security Considerations
Reentrancy and Balance Changes
Balance can change during execution:
contract ReentrancyAware { // VULNERABLE: Balance check before external call function vulnerable() external { uint256 balance = address(this).balance; // External call could send ETH back (reentrancy) externalContract.call{value: balance}(""); // balance value is now stale! } // SAFE: Check balance after operations function safe() external { externalContract.call(""); uint256 finalBalance = address(this).balance; // Use current balance }}
SELFDESTRUCT Interaction
Contracts can receive ETH from SELFDESTRUCT without receive function:
contract SelfdestructRecipient { function checkBalance() external view returns (uint256) { // Balance could be > 0 even without receive/fallback // if another contract selfdestructed to this address return address(this).balance; }}
Balance vs State Accounting
Don’t rely solely on balance for accounting:
contract BadAccounting { // VULNERABLE: No internal accounting function withdraw() external { uint256 share = address(this).balance / totalShares; // Balance could include unexpected ETH from SELFDESTRUCT payable(msg.sender).transfer(share); }}contract GoodAccounting { mapping(address => uint256) public balances; // SAFE: Track deposits explicitly function deposit() external payable { balances[msg.sender] += msg.value; } function withdraw() external { uint256 amount = balances[msg.sender]; balances[msg.sender] = 0; payable(msg.sender).transfer(amount); }}
Gas Cost Changes
Pre-Istanbul, querying self balance was expensive:
contract GasAware { // Pre-Istanbul: 2602+ gas // Post-Istanbul: 5 gas function getBalance() external view returns (uint256) { return address(this).balance; }}
Implementation
TypeScript
/** * SELFBALANCE opcode (0x47) - Get balance of executing contract * Available: Istanbul+ */export function selfbalance(frame: FrameType): EvmError | null { // Check hardfork availability if (frame.evm.hardfork.isBefore('ISTANBUL')) { return { type: "InvalidOpcode" }; } // Consume gas (GasFastStep = 5) frame.gasRemaining -= 5n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Get balance of current contract const balance = frame.evm.get_balance(frame.address); // Push to stack if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(balance); frame.pc += 1; return null;}