4

I have two contracts with variables and functions that each should need to reference/call. I would prefer to separate the contracts, as different user groups would be accessing each. Can two contracts import each other? Or is there an order of operations -so to speak- wherein only one contract can be dependent on another?

  • As far as I understand there can't be circular imports. – Mikko Ohtamaa Nov 14 '16 at 12:25
3

Yes, you can compile two contracts that call each other. For example,

contract InterfaceOfA {
        function f(uint) returns (uint);
}

contract InterfaceOfB {
        function g(bool) returns (bool);
}

contract A is InterfaceOfA {
        InterfaceOfB b;
        function f(uint _input) returns (uint) {
                b.g(true);
                return 3;
        }
}

contract B is InterfaceOfB {
        InterfaceOfA a;
        function g(bool _input) returns (bool) {
                a.f(0);
                return false;
        }
}
  • 1
    Functionally, is there any difference if the two contracts modify each other in the same .sol file vs. importing and modifying each other from separate .sol files? – CBobRobison Nov 15 '16 at 16:30

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.