Suppose In a banking system many people are enrolled (managed through mapping )
mapping (address => bool) enrolled;
And only those can deposit who are enrolled in the bank.
Can I check if the msg.sender exists as a key in the above mapping?
Suppose In a banking system many people are enrolled (managed through mapping )
mapping (address => bool) enrolled;
And only those can deposit who are enrolled in the bank.
Can I check if the msg.sender exists as a key in the above mapping?
The default value of a bool in a mapping is false. If you also use false as a part of your tracking system, then no, you will not be able to tell whether a value has been set to false or has not been set at all.
For that, you may need a second mapping which tracks whether the value has been set before.
mapping (address => bool) enrolledSet;
Here is a minimal way to cover random access, counting uniques and iterating over the list.
pragma solidity 0.5.0;
contract InterableMapping {
address[] public addressList;
mapping(address => bool) public isAddress;
function insertAddress(address customer) public {
require(!isAddress[customer]);
addressList.push(customer);
isAddress[customer] = true;
}
function getAddressCount() public view returns(uint count) {
return addressList.length;
}
}
You could add a soft delete by setting the bool to false.
function disableCustomer(address customer) public {
require(isAddress[customer]);
isAddress[customer] = false;
}
Have a look over here for different patterns: Are there well-solved and simple storage patterns for Solidity?
Hope it helps.
Yes, with your example, you can just do the following:
require(enrolled[msg.sender]);