0

Contracts can be explicitly converted to and from the address type. In solidity docs, it's mentioned that the contracts can be converted into an address type. But when I try to do so the compiler gives me the following error. Explicit type conversion not allowed from "type(contract)" to "address"

What I'm exactly trying to do is the following

contract A {
    // some logic 
}

contract B {
    //some logic
}

contract C{
    address a = address(A);
    address b = address(B);
}

The error I'm getting:-

Explicit type conversion not allowed from "type(contract A)" to "address". address a = address(A);

Explicit type conversion not allowed from "type(contract B)" to "address". address b = address(B);

This is the link for the doc where I saw this could be done.

0

You have to first create these contracts in order to get their address, change this:

contract C {
    address a = address(A);
    address b = address(B);
}

To this:

contract C {
    address a = address(new A());
    address b = address(new B());
}

Then you can read them like this:

function getContractAddresses() view public returns(address, address) {
    return (a, b);
}
2
  • Thanks a lot for your help. If I'm not wrong the address a would not be equal to the address of contract A? I want the address of contract A to be stored in the variable address a. – Manas Nanivadekar Feb 20 at 10:54
  • You're wrong, just debug what I've sent you. By just leaving multiple contracts in one .sol file this does not mean that you are deploying them. In my example when you're deploying contract C ( manually ) then once it is deployed, that particular contract is also deploying contracts A and B. – Miroslav Nedelchev Feb 20 at 16:01

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.