-
Notifications
You must be signed in to change notification settings - Fork 0
/
lottery2.sol
101 lines (86 loc) · 2.43 KB
/
lottery2.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
91
92
93
94
95
96
97
98
99
100
101
/*
Lottery Contract
*/
pragma solidity ^0.4.19;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract Lottery is usingOraclize {
string public betNumber;
string public result;
address public house;
address public better;
enum State { Created, Betted, Paidout, Inactive }
State public state;
event newOraclizeQuery(string description);
event Aborted();
event Betted();
event Released();
function Lottery() public payable {
house = msg.sender;
state = State.Created;
}
modifier condition(bool _condition) {
require(_condition);
_;
}
modifier onlyBetter() {
require(msg.sender == better);
_;
}
modifier onlyHouse() {
require(msg.sender == house);
_;
}
modifier inState(State _state) {
require(state == _state);
_;
}
//source: https://ethereum.stackexchange.com/questions/30912/how-to-compare-strings-in-solidity
function compareStrings (string a, string b) view returns (bool){
return keccak256(a) == keccak256(b);
}
function bet(string _betNumber)
public
inState(State.Created)
condition(msg.value == (1 ether))
payable
{
Betted();
better = msg.sender;
betNumber = _betNumber;
state = State.Betted;
}
function abort()
public
onlyHouse
inState(State.Created)
{
Aborted();
state = State.Inactive;
house.transfer(address(this).balance);
}
function release()
payable
public
onlyHouse
inState(State.Betted)
{
if (oraclize_getPrice("URL") > this.balance) {
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
} else {
newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query("URL", "json(https://jacksonng.org/codetest/random.php/).random");
}
}
function __callback(bytes32 myid, string res) {
if (msg.sender != oraclize_cbAddress()) throw;
result = res;
if (compareStrings(result,betNumber)) {
better.transfer(address(this).balance);
}
else {
house.transfer(address(this).balance);
}
Released();
state = State.Paidout;
}
}