1

Let's consider this situation:

interface ITest
{
    function check()
        external;
}

contract Test is ITest
{
    address private admin;

    constructor()
        public
    {
        admin = msg.sender;
    }

    modifier onlyAdmin
    {
        assert(msg.sender == admin);
        _;
    }

    function check()
        external onlyAdmin
    {
        // work only with admin
    }
}

contract OtherTest
{
    ITest private test;

    constructor(
        address testAddress
    )
        public
    {
        test = ITest(testAddress);
        test.check();
    }
}

It doesn't give any error. Is this safe and is it working as expected? Will this function work only for admin? Interestingly, it seems to work on VM.

1 Answer 1

2

This looks fine, and yes it will only function for the admin as you have written it.

While modifiers live at the function declaration, their behavior is more like actual code. Thus a modifier behaves in the same way as when you declare the function represented in the interface. So, when you say:

function check() public onlyAdmin {
    ...    
}

You are really saying:

function check() public {
    require(msg.sender == admin);
    ...    
}

BTW, you really should be using require rather than assert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.