Created
January 31, 2019 08:41
-
-
Save lyhistory/f1c92d06c42c65ede2c90c5f6d40c6b4 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Sharer { | |
function sendHalf(address payable addr) public payable returns (uint balance){ | |
require(msg.value % 2 == 0, "Even value reqired."); | |
uint balanceBeforeTransfer = address(this).balance; | |
addr.transfer(msg.value / 2); | |
// Since transfer throws an exception on failure and | |
// cannot call back here, there should be no way for us to | |
// still have half of the money. | |
assert(address(this).balance == balanceBeforeTransfer - msg.value/2); | |
return address(this).balance; | |
} | |
function getBalance() public view returns(uint){ | |
return address(this).balance; | |
} | |
} | |
contract VendingMachine { | |
function buy(uint amount) public payable { | |
if(amount > msg.value / 1 ether) | |
revert("Not enough Ether provided"); | |
// Alternative way to do it: | |
require( | |
amount <= msg.value / 1 ether, | |
"Not enough Ether provided." | |
); | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment