3

How would you call a function with a modifier from another contract. Let the contracts be:

contract MyContract {
    address owner;
    modifier getModified (address owner) {
        if (msg.sender != owner) throw;
        _;
    }

    function func(address newOwnerAddress) getModified(owner) returns (address newOwner) {
        //....code....
    }
}

contract newContract {
    MyContract myContract = MyContract(0x08271...);

    function bid(uint amt, address _oldOwner, address _newOwner) returns(bool success) {
        if (amt > ...) {
            myContract.func(_newOwner,{getModified:_oldOwner})
        }
    }
}

I am getting an error Error: Undeclared identifier: getModified for getModified in Remix. I tried reading the docs but they also dont mention the funcition call with modifiers to other contracts. Thanks in advance.

  • You probably intended to use msg.sender as function func(address newOwnerAddress) getModified(msg.sender) .... Then in bid, myContract.func(_newOwner); should work. – eth Mar 21 '17 at 21:26
  • It's not entirely clear what you are trying to do and why you need a modifier to do it. The parameter getModified(owner) serves no purpose as it's always going to be the owner state variable which you already have in scope. But now it's unclear whether your intent for if (msg.sender != owner) refers to the owner state variable or owner parameter. Furthermore if (msg.sender != owner) will always be testing the calling contact against owner at it is the calling contract who is msg.sender. – o0ragman0o Mar 21 '17 at 22:09
  • Lastly, I've not seen myContract.func(_newOwner,{getModified:_oldOwner}) as a valid calling convention. func only takes one parameter. – o0ragman0o Mar 21 '17 at 22:11
  • @o0ragman0o, I have the test code here, the functions I am trying to interact is actually different. I have a function in some contract, and I want to call a specific function (which has a modifier), in that contract and answering your doubt, in if (msg.sender != owner) refers to the owner parameter. But @eth shouldn't the modifier be mentioned while making a function call for func. So I think the question still stands: how do you call a function with a modifier from another contract? – Rajat Mar 22 '17 at 4:10
  • 1
    You don't do anything different when calling a function with a modifier. There doesn't seem to be any reason for you to have a parameter to your modifier at all, but think about the modifier as being a macro that gets called at compile-time. The getModified(owner) modifier just substitutes the value of the global variable owner everwhere that the getModified definition uses the parameter owner – Tjaden Hess Mar 22 '17 at 23:04
1

The calling contract does nothing abnormal and it needs no knowledge of the modifier. modifier is just a compiler sweetener that helps roll up repetitive code.

There are useful comments here about correctly designing your modifier to perform its intended function including passing arguments into the modifier. What it does and how it does it is an internal matter; of no concern to contracts that call it.

Hope it helps.

0

So as @TjadenHess communicated in the comments and also from valuable inputs from others, this is the answer which can solve the problem. If you need to pass a value in the modifier to restrict the function from random calling then its better to put a check in the function rather than using a modifier.

contract MyContract{
    address owner = msg.sender;


function func(address newOwnerAddress) returns (address newOwner){
     if (msg.sender != owner) {throw;}
    //....Rest of the code remins same....
}

}

contract newContract{
    MyContract myContract = MyContract(0x08271...);

    function bid(uint amt, address _newOwner) returns(bool success){
        if (amt > ...){
        myContract.func(_newOwner)
    }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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