2

Is there a way to use modifiers from a different contract? My attempt did not work.

contract con1{

    modifier supermod(uint _value){
        require(_value == 1);
        _;
    }
}

contract con2{

    con1 public contract1;

    function setCon1Address(address _address)public{
        contract1 = con1(_address);
    }

    function test() contract1.supermod(uint _value){
        ..stuff..
    }


}
2

Modifiers have a similar scope to internal functions. The only way to access supermod from con2 is if con2 inherits from con1

1

Change to inheritance.

contract con2 is con1 {

I removed the parts you don't need and fiddled with the function to grab the input for the modifier.

contract con1{

    modifier supermod(uint _value){
        require(_value == 1);
        _;
    }
}

contract con2 is con1 {

    function test(uint value) supermod(value){
        // this will happen if value == 1
    }

}

Hope it helps.

2
  • The problem here is that i want to use a modifier from a contract that already exist. if i use inheritance i create a new con1 from my understanding. But thanks for the help.
    – Lomo
    Oct 13 '18 at 13:53
  • Different question. Then, no. Not possible. Oct 13 '18 at 18:57

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.