-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12Mapping.sol
55 lines (32 loc) · 1.46 KB
/
12Mapping.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract Mapping{
//Mapping is declaring a variable as a value for a key of specific data type
//address => uint256 balance shows that a relationship is set for balances with the address
//balances remain as uint data type , but can be used where there is a relation btw address and balances
mapping (address => uint256) public balances;
function Deposit() public payable {
//Here , there is a relation btw the fn caller and his balance
balances[msg.sender] = msg.value;
}
function getBalance(address user) public view returns (uint){
//user argument is whatever the address given by fn caller
return balances[user];
}
//Nested mapping
//This is the status of the voter wether he/she voted or not
mapping (address => mapping (uint => bool)) public voted;
//Vote count is number of votes gained by the candidate
mapping (uint => uint) public voteCount;
function voteStatus(address voter, uint idNo) public view returns (bool) {
return voted[voter][idNo];
}
function CastVote(address _voter, uint8 _CandidateidNo) public {
require(!voted[_voter][_CandidateidNo],"Already voted for this candidate");
voted[_voter][_CandidateidNo] = true;
voteCount[_CandidateidNo]++;
}
function Votes(uint8 CandidateID) public view returns (uint){
return voteCount[CandidateID];
}
}