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?

1
  • As far as I understand there can't be circular imports. Nov 14, 2016 at 12:25

1 Answer 1

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
  • 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? Nov 15, 2016 at 16:30

Your Answer

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

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