an additional mapping to work with its instances.
struct Vote {
uint256 startTimestamp;
bool isActive;
address winner;
mapping(address => address) voterToNominee;
mapping(address => uint32) nomineeToVoteCount;
}
mapping(uint32 => Vote) public votes;
2. I have a function called startVote(), which starts the vote:
function startVote() external onlyOwner {
uint32 voteId = voteCount++;
uint256 _startTimestamp = block.timestamp;
Vote storage _vote = votes[voteId];
_vote.startTimestamp = _startTimestamp;
_vote.isActive = true;
}
3. I also have a function vote() which allowsthe user to vote:
function vote(uint32 voteId, address nominee) external {
Vote storage _vote = votes[voteId];
_vote.voterToNominee[msg.sender] = nominee;
_vote.nomineeToVoteCount[nominee]++;
}
When I call startVote(), vote 0 starts.
When I call vote(0, %someAddress%), votes[0] doesn't reflect the changes.
Could anyone help me to understand what am I doing wrong, please?
Hi there! Looked at your code and I'd like to see the whole thing. Looks interesting. But when you call startVote(), I don't think vote 0 starts. This is because your startVote() function says Uint32 voteId = voteCount++ Which as you know, ups the count by one. So unless you had voteCount initialized to -1 (which you can't because 'unsigned intger' -- unit) somewhere else your voteId would most definitely start with 1. Now don't take my word for it. I'm still a newbie but just pointing that out.
What do you think about the code? I think there might be a little flaw in it
Thank you! I'll send you the code when I finish this up.
VoteId = voteCount++ means very first voteID will be 1 right?
1. It works with 0, but even if it didn't, that is not my main concern. 2. The main concern is that I don't see the mappings inside struct updated anywhere.
1. You don't update the winner 2. To access those mappings, you hafta write a getter function for them. It's a like a two dimensional array
Обсуждают сегодня