Index
Handling ether
Accounts Own Ether
Both kinds of Ethereum accounts (smart contracts and Externally-Owned Accounts) can own ether. Given an account’s address, its current ether balance can be accessed in Solidity as address.balance
https://programtheblockchain.com/posts/2017/12/15/writing-a-contract-that-handles-ether/
pragma solidity ^0.4.17;
contract CommunityChest {
function withdraw() public {
msg.sender.transfer(address(this).balance);
}
function deposit(uint256 amount) payable public {
require(msg.value == amount);
// nothing else to do!
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
contract CommunityChest {
function withdraw() public {
msg.sender.transfer(address(this).balance);
}
function deposit(uint256 amount) payable public {
require(msg.value == amount);
// nothing else to do!
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
Contracts can access their current ether balance with address(this).balance.