-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathballot.sol
76 lines (60 loc) · 2.17 KB
/
ballot.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
pragma solidity ^0.4.4;
import "./Ownable.sol";
import "./CopaCoin.sol";
import "./Utils.sol";
contract RecuperarClase is Ownable {
using Utils for Utils;
struct Proposal {
bytes32 name;
uint8 votesCount;
}
Proposal[] public proposals;
CopaCoin copaCoinContract;
function RecuperarClase() public {
copaCoinContract = new CopaCoin();
}
modifier proposalNotExists(bytes32 _proposal) {
for (uint256 i = 0; i < proposals.length ; i++) {
require(_proposal != proposals[i].name);
}
// Este guion bajo se reemplaza por el cuerpo de la funcion
// que esta aplicando el modifier
_;
}
function addProposal(bytes32 _proposal) public onlyOwner proposalNotExists(_proposal) {
proposals.push(Proposal({name: _proposal, votesCount: 0}));
}
function bytes32ToString(bytes32 x) public pure returns (string) {
bytes memory bytesString = new bytes(32);
uint charCount = 0;
for (uint j = 0; j < 32; j++) {
byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
if (char != 0) {
bytesString[charCount] = char;
charCount++;
}
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (j = 0; j < charCount; j++) {
bytesStringTrimmed[j] = bytesString[j];
}
return string(bytesStringTrimmed);
}
function getProposal(uint256 _index) public constant returns (string proposal) {
return bytes32ToString(proposals[_index].name);
}
function getProposalVotesCount(uint256 _index) public constant returns (uint8 proposal) {
return proposals[_index].votesCount;
}
function getProposalsCount() public constant returns (uint256) {
return proposals.length;
}
modifier hasCoins {
require(copaCoinContract.balanceOf(msg.sender) > 0);
_;
}
function voteProposal(uint256 _index) public {
//require(copaCoinContract.spend(1));
proposals[_index].votesCount = proposals[_index].votesCount + 1;
}
}