0

I'm trying to understand how can two contracts be deployed and interact with each other, given that I don't have their addresses before deployment. I ended in this question and I don't understand the answer.

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;
        }
}

I tried to execute this in remix to gather a better understanding, but it raised a bunch of errors I didn't knew how to fix. So I would like to understand how can contract A find contract B to call the g method if the address of contract B isn't stored? Same with contract B finding A to call method f.

1

It does not work because as you said A and B addresses are not stored in B and A.

The exemple you linked his about compilation and importing, and cannot works in test (infinite loop and no setup for a and b values). Something like that should do the trick for the setup but be aware that the infinite loop is still present

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;
        }

        function setB(address bAddress) {
                b = InterfaceOfB(bAddress);
        }
}

contract B is InterfaceOfB {
        InterfaceOfA a;
        function g(bool _input) returns (bool) {
                a.f(0);
                return false;
        }

        function setA(address aAddress) {
                a = InterfaceOfA(aAddress);
        }
}

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.