Created
September 27, 2017 18:30
-
-
Save anonymous/194627e18b23a8119201a27d11538541 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.17+commit.bdeb9e52.js&optimize=undefined&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.4.0; | |
contract GroupPayment { | |
enum Status { Open, Canceled, Finished } | |
address owner; | |
uint target; | |
Status status; | |
mapping(address => uint) investments; | |
event Invested( | |
address sender, | |
uint value | |
); | |
event Finished(); | |
event Canceled(); | |
function GroupPayment(uint _target) public { | |
require(_target > 0); | |
owner = msg.sender; | |
target = _target * 1 ether; | |
status = Status.Open; | |
} | |
modifier onlyWhenOpen() { | |
require(status == Status.Open); | |
_; | |
} | |
modifier onlyWhenOpenOrCanceled() { | |
require(status == Status.Open || status == Status.Canceled); | |
_; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
function getTotal() public view returns (uint) { | |
return this.balance; | |
} | |
function getTarget() public view returns (uint) { | |
return target; | |
} | |
function cancel() public onlyOwner { | |
status = Status.Canceled; | |
Canceled(); | |
} | |
function finish() internal { | |
status = Status.Finished; | |
Finished(); | |
} | |
function invest() public payable onlyWhenOpen { | |
investments[msg.sender] += msg.value; | |
if (this.balance >= target) { | |
finish(); | |
if (this.balance > target) { | |
uint remain = this.balance - target; | |
investments[msg.sender] -= remain; | |
msg.sender.transfer(remain); | |
} | |
} | |
} | |
function takeBack() public onlyWhenOpenOrCanceled { | |
var value = investments[msg.sender]; | |
investments[msg.sender] = 0; | |
msg.sender.transfer(value); | |
} | |
function withdraw() public onlyOwner { | |
require(status == Status.Finished); | |
msg.sender.transfer(this.balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment