i'm building a sample application which require voting.
I think it needs to be split into 2 contracts where the owner of Ballot
will create Proposal
contracts.
Splitting simply because the logic is got too complex to be clearly understandable in a single contract.
Question: what is the proper way to let only the contributors
from Ballot
contract vote for proposals.
My implementation - simply a call to Bellot
to check if the address is in contributors
. I've read that there are a lot of security flaws in interacting between 2 contracts(since i'm new to solidity might not know all details), so i want to verify if this is correct solution.
Ballot
contract Ballot {
address public manager;
mapping (address => bool) public contributors;
Proposal[] proposals;
mapping(address => Proposal[]) proposalsByAddress;
constructor(address _creator) public {
manager = _creator;
}
function createProposal() public {
Proposal memory newProposal = new Proposal();
proposalsByAddress[msg.sender].push(newProposal);
//to be able to returl all porposals later
proposals.push(newProposal);
}
function checkIfContributor(uint address _address) public view returns (bool) {
require(proposalsByAddress[msg.sender]);
if (contributors[_address]) {
return true;
} else {
return false;
}
}
}
Proposal:
contract Proposal {
Ballot ballot;
bool complete;
uint approveCount;
uint rejectCount;
mapping (address => bool) voters;
constructor() {
ballot = new Ballot(msg.sender);
}
function vote(bool _vote) {
require(ballot.checkIfContributor(msg.sender));
require(!voters[msg.sender]);
voters[msg.sender] = true;
if (_vote) {
approveCount++;
} else {
rejectCount++;
}
}
}