Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make check if a key exists in mapping or not in Solidity when value is of type enum?

I have the following contract in solidity.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract PocketCrypto {
    enum Role{ GUARDIAN, WARD} 

    mapping(address => Role) public role;

    function setRole(Role _role) public {
        role[msg.sender] = _role;
    }
}

I want to check if for a given address a Role is set or not. But since default value for mapping will be 0, and for enum it will mean first Role, how do I accomplish this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can extend the enum to reflect the 0th index (default) value as none.

enum Role{ NONE, GUARDIAN, WARD} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading