I would like to see an example of a function modified as payable in practice. I would like for it to accept payment and hold the ether.

I would like to read another function in the same contract that can send the held ether to a specific address.


A really basic example from https://programtheblockchain.com/posts/2017/12/15/writing-a-contract-that-handles-ether/:

pragma solidity ^0.4.17;

contract CommunityChest {
    function withdraw() public {

    function deposit(uint256 amount) payable public {
        require(msg.value == amount);
        // nothing else to do!

    function getBalance() public view returns (uint256) {
        return this.balance;
  • Where the money this.balance come from? – Kuelf Deez Mar 12 '18 at 3:23
  • 1
    It's the ether held by the contract. E.g. the ether sent via calls to deposit. – user19510 Mar 12 '18 at 3:24
  • What happens to money 'paid' to deposit() if nothing is done? What's this (here) exactly? – Kuelf Deez Mar 12 '18 at 3:25
  • 1
    I'm not sure I understand the question. If money is transferred in a call to deposit, then... that's it. It's been transferred. The account that sent the transaction now has less ether and the contract has more ether. this is a variable that refers to the contract. – user19510 Mar 12 '18 at 3:27
  • Alright. That answer my question. I didn't understand that a contract holds a single pot of all the money sent to it. – Kuelf Deez Mar 12 '18 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.