Hi all!
1. I have a struct with nested mappings and 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?
Apr 24, 2022, 3:49 PM
I am probably doing something wrong with memory locations.
Apr 24, 2022, 3:50 PM
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.
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.
Here I added a verification system to your code💪🏽
Intended to be recorded for the same user but creating different instances of the vote since they're not state functions
Apr 24, 2022, 6:42 PM
voteId is equal to?)
You set 1, and tried to access 0
voteid = 0++ // voteId = 0
console.log(voteid) //1
console.log(voteid) //1
As far as I can judge
Apr 24, 2022, 7:03 PM
Yes exactly!
That's what I suggested here
That's what I suggested here
What's this ser
Apr 24, 2022, 7:05 PM
Where?
Apr 24, 2022, 7:05 PM
Console.log
Apr 24, 2022, 7:06 PM
import hardhat/console.sol
And u can use that method
Apr 24, 2022, 7:08 PM
Okay I think I can understand this.
''//'' is just you commenting that voteId will start at 0 but the event will be logged at 1 when voteId = 0++ is called right?
''//'' is just you commenting that voteId will start at 0 but the event will be logged at 1 when voteId = 0++ is called right?
Apr 24, 2022, 7:08 PM
But i wrote it down for myself
Reply to it in the morning
Apr 24, 2022, 7:08 PM
Hmm never really heard much about hardhat
Alright ser💪🏽
Apr 24, 2022, 7:09 PM
Thank you!
I'll send you the code when I finish this up.
I'll send you the code when I finish this up.
The logic which is desired here should be a bit different, but thank you for attention nevertheless.
Apr 24, 2022, 7:45 PM
VoteId = voteCount++ means very first voteID will be 1 right?
So vote[0] will always be skipped
Apr 24, 2022, 7:48 PM
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.
2. The main concern is that I don't see the mappings inside struct updated anywhere.
votes array of Votes structs doesn't reflect the changes to nested mappings.
I run vote() with a valid voteId and nomineeAddress.
I should have them in the votes[voteId], but the mappings don't get shown.
I should have them in the votes[voteId], but the mappings don't get shown.
Sorry if it looks vague.
If I won't come to solution, I'll come back with a much better formulated question.
If I won't come to solution, I'll come back with a much better formulated question.
Apr 24, 2022, 8:19 PM
Hello again. Yes I pointed out what could potentially be the problem here
Intended to be recorded for the same user but creating different instances of the vote since they're not state functions
Apr 24, 2022, 11:49 PM
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
2. To access those mappings, you hafta write a getter function for them. It's a like a two-dimensional array
function getNomineeByVoter(uint32 _voteId, address _voterAddr)
.
.
.
return votes[_voteId][_voterAddr]
.
.
.
return votes[_voteId][_voterAddr]
Apr 25, 2022, 4:19 AM
Regarding (2) — thank you very much, that was exactly what I was looking for.
The problem was that there were no implicit getters for nested mappings.
The problem was that there were no implicit getters for nested mappings.
Thank you too.
Apr 25, 2022, 10:10 AM