-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatthew.sol
90 lines (75 loc) · 2.89 KB
/
Matthew.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
pragma solidity ^0.4.6;
// ## Matthew - a contract for increasing "whaleth"
// README: https://github.com/rolandkofler/matthew
// MIT LICENSE 2016 Roland Kofler, thanks to Crul for testing
contract Matthew {
address owner;
address public whale;
uint256 public blockheight;
uint256 public stake;
uint256 period = 600; //180 blocks ~ 42 min, 300 blocks ~ 1h 10 min;
uint constant DELTA = 0.1 ether;
uint constant WINNERTAX_PRECENT = 10;
bool mustBeDestroyed = false;
uint newPeriod = period;
event MatthewWon(string msg, address winner, uint value, uint blocknumber);
event StakeIncreased(string msg, address staker, uint value, uint blocknumber);
function Matthew(){
owner = msg.sender;
setFacts();
}
function setFacts() private {
stake = this.balance;
period = newPeriod;
blockheight = block.number;
whale = msg.sender;
}
/// The rich get richer, the whale get whaler
function () payable{
if (block.number - period >= blockheight){ // time is over, Matthew won
bool isSuccess=false; //mutex against recursion attack
var nextStake = stake * WINNERTAX_PRECENT/100; // leave some money for the next round
if (isSuccess == false) //check against recursion attack
isSuccess = whale.send(stake - nextStake); // pay out the stake
MatthewWon("Matthew won", whale, stake - nextStake, block.number);
setFacts();//reset the game
if (mustBeDestroyed) selfdestruct(whale);
return;
}else{ // top the stake
if (msg.sender.balance < stake) throw; // proof of stake
if (msg.value < DELTA) throw; // you must rise the stake by Delta
bool isOtherSuccess = msg.sender.send(stake); // give back the old stake
setFacts(); //reset the game
StakeIncreased("stake increased", whale, stake, blockheight);
}
}
// better safe than sorry
function destroyWhenRoundOver() onlyOwner{
mustBeDestroyed = true;
}
// next round we set a new staking perioud
function setNewPeriod(uint _newPeriod) onlyOwner{
if (_newPeriod < 180) throw;
newPeriod = _newPeriod;
}
function getPeriod() constant returns (uint){
return period;
}
function getNewPeriod() constant returns (uint){
return newPeriod;
}
function getDestroyedWhenRoundOver() constant returns (bool){
return mustBeDestroyed;
}
//how long until a Matthew wins?
function getBlocksTillMatthew() public constant returns(uint){
if (blockheight + period > block.number)
return blockheight + period - block.number;
else
return 0;
}
modifier onlyOwner(){
if (msg.sender != owner) throw;
_;
}
}