[Ethereum] see an example of a payable function in practical use

addressesetherpayableremixsolidity

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.

Best Answer

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 {
        msg.sender.transfer(this.balance);
    }

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

    function getBalance() public view returns (uint256) {
        return this.balance;
    } 
}