forked from christine2515/blockchain-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VotingGame.sol
71 lines (63 loc) · 1.88 KB
/
VotingGame.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
pragma solidity 0.8.11;
contract VotingGame{
address public owner;
uint poolA;
uint poolB;
address payable[] a;
address payable[] b;
uint[] private valuesA;
uint[] private valuesB;
constructor(){
owner=msg.sender;
poolA=0;
poolB=0;
}
function getBalance() public view returns (uint){
return address(this).balance;
}
function getMoneyInPoolA() public view returns(uint){
return poolA;
}
function getMoneyInPoolB() public view returns(uint){
return poolB;
}
//decrease the user's money by amount and increase ours.
//can't use private?
//I wrote it like what I found online, but I don't how how the user was able to enter the value
function putInA() public payable{
require(msg.sender!=owner);
require(msg.value>.01 ether, "You must put in at least .01 ether to enter.");
poolA+=msg.value;
a.push(payable(msg.sender));
valuesA.push(msg.value);
}
//same function for B
function putInB() public payable{
require(msg.sender!=owner);
require(msg.value>.01 ether, "You must put in at least .01 ether to enter.");
poolB+=msg.value;
b.push(payable(msg.sender));
valuesB.push(msg.value);
}
//gives the winner back their money doubled
function giveBack() public {
require(msg.sender==owner);
if (getMoneyInPoolA()>getMoneyInPoolB()){
for (uint i=0;i<b.length;i++){
b[i].transfer(2*valuesB[i]);
}
}
else{
for (uint i=0;i<a.length;i++){
a[i].transfer(2*valuesA[i]);
}
}
//renew for new round
a=new address payable[](0);
b=new address payable[](0);
valuesA=new uint[](0);
valuesA=new uint[](0);
poolA=0;
poolB=0;
}
}