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 {
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;
}
}
deposit
.
Mar 12, 2018 at 3:24
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.
Mar 12, 2018 at 3:27