Emit a event when hasRole() returns false

I want to emit a event when hasRole() return false ,how can i do ?

function hasRole(bytes32 role, address account) public view returns (bool) {
     return _roles[role].members.contains(account);
     ************************???************************
    emit Blacklisted(_account);
    }
1 Like

Hi @wangyuyue,

hasRole isn’t set to be overridden. Also it is a view function so can’t emit events.

Instead you can do an if statement and emit an event.

    function doStuff() public {
        if (hasRole(X_ROLE, msg.sender)) {
            emit HasRoleX();
        }
    }

Hi ~ I don’t know how to thank you enough, I am new at smart contract and didn’t know anything at the beginning. Under your guidance, I becoming more and more interested in it…I am trying to solve problems myself every day…
this is my code:()

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

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";

contract MyContract is Ownable,AccessControl {
    mapping (bytes32 => RoleData) private _roles;

    mapping(address => bool) public blacklist;

    event Blacklisted(address account);
    event Unblacklisted(address account);

    constructor() public {
    _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
    function isBlacklist(address _account)
        public
        view
        onlyOwner
        returns (bool)
    {
        return blacklist[_account];
    }

    function AddBlacklist(address _account) public onlyOwner {
        require(!isBlacklist(_account), "Blacklist: already listed");

        blacklist[_account] = true;
        emit Blacklisted(_account);
}

    function RemoveBlacklist(address _account) public onlyOwner {
        require(isBlacklist(_account), "Blacklist: not listed");

        delete blacklist[_account];
        emit Unblacklisted(_account);
    }

    function checkRole(bytes32 role, address _account) public  onlyOwner returns (bool) {
        require(!isBlacklist(_account), "This user is in the Blacklist");
        if( _roles[role].members.contains(_account)){
            return true;
        }else{
            return false;
            AddBlacklist(_account);            
    }
}
}

there is someing wrong in function checkRole()
first,I grantRole to a account,then i add him to blacklist, last,i call checkRole(),but the result did not return false,just show transaction sucessful.like below:

1 Like

Hi @wangyuyue,

When we read data externally from the blockchain (making a call) we can get return values.

When we write data externally to the blockchain (creating a transaction) we don’t get return values, we only get the transaction hash. To get information we can emit events.

If your checkRole function you first return false before calling AddBlacklist which code can never be reached as we already have called return.

    function checkRole(bytes32 role, address _account)
        public
        onlyOwner
        returns (bool)
    {
        require(!isBlacklist(_account), "This user is in the Blacklist");
        if (_roles[role].members.contains(_account)) {
            return true;
        } else {
            return false;
            AddBlacklist(_account);
        }
    }

Thank you for your patience, I gained a lot.

1 Like