0

Assume I have a contract A:

pragma solidity ^0.4.4;
contract A{
  struct St{
    uint val;
  }
  function double(uint a)external returns (uint res){
  res= 2*a;
  }
}

Let's call the above lines of code block 1.

I deploy contract A. Then I have a contract B that wants to call a function of contract A:

pragma solidity ^0.4.4;
contract B{
   uint res;
   function call_double(uint val) {
   A a=A(0x1);// 0x1 is the address of contract A after it's deployed.
   res = a.double(val);
   }
 }
 // how to include this contract abstract when deploying contract B?
 contract A{
    function double(uint a)external returns (uint res);
    struct St{
    uint val;
   }
 }

Let's call the above lines of code block 2.


Question: How can I include contract A when I'm deploying contract B? In other words, how can I deploy block2?

Please note that when I'm using remix, (after I deploy contract A) I put both contract B and abstract of contract A in one page and deploy B, and everything works fine. But it's not clear to me how it's done when Im manually deploying to a private blockchain using web3. Also, it's not clear which ABI I need when I want to interact with contract B.

2 Answers 2

1

You can create an instance of contract A in contract B and then point that instance to the address of contract A.

Deploy contract A:

contract A{
  struct St{
    uint val;
  }
  function double(uint a)external returns (uint res){
  res= 2*a;
  }
}

Now when creating contract B, you can pass the address of contract A. Constructor of B will initialise the instance of A:

contract A{
     function double(uint a)external returns (uint res);
     struct St{
     uint val;
    }
 }

contract B{
    uint res;
    A objOfA;
    function B(address _addressOfContractA){
      objOfA = A(_addressOfContractA);
    }
    function call_double(uint val) {
      res = objOfA.double(val);
    }
}
3
  • thanks for the answer, but I did the same! I don't see any differences. in Block2, A a=A(0x1) is an instance of contract A.
    – Aydin
    Commented Mar 26, 2018 at 16:51
  • my question is how to deploy the second part (i.e. contract B and abstract of contract A) together.
    – Aydin
    Commented Mar 26, 2018 at 16:52
  • You do not require to deploy the abstract of contract A. When deploying B, you just need the abstract of A to tell B how to interact with A. In your block, A was written after B, which would fail to compile. Also, you used var res but it required uint res
    – Ayushya
    Commented Mar 26, 2018 at 16:55
0

That's why the interface is needed.

Block2 must be look like below:

interface A{
     function double(uint a)external returns (uint res);
    }
 }

contract B{
    uint res;
    A objOfA;
    function B(address _addressOfContractA){
      objOfA = A(_addressOfContractA);
    }
    function call_double(uint val) {
      res = objOfA.double(val);
    }
}

Your Answer

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

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