0

for example,

contract A{   

}  
contract B{

} 

but here contract B will call contract A, I use solidity to compile and get two source code, how can I deployed it? deploy two times on myetherwallet since i have two codes? if I deploy two times, there will be no way to tell B where A is so B cannot find A?

4 Answers 4

0

I think that you can do like this

  1. deploy contract A
  2. modify code of contract B to pass the address of contract A in the constructor of contract B
  3. deploy the contract B

then you can call contract A from contract B

0

I updated the answer.

Here is an example I just did for you in Remix IDE

I call them Parent and Child for B and A;

My parent contract deploys the child and then I can access its functions and internal variables.

enter image description here

1
  • 1
    This is different. In OP's example, contract A and contract B do not inherit from each other. Therefore, OP has to deploy both contract A and contract B like Ha ĐANG described
    – Henk
    Aug 28, 2018 at 9:54
0

You don't need to deploy both contract one at a time. You can deploy contract B and deploy contract A by executing contract code. Here is sample:

contract A {
    constructor(){}
  //your code here  
}

contract B{
    address public address_of_contract_A;
    constructor (){
    A addr_of_a = new A();
    address_of_contract_A = addr_of_a;
    }
    //your code here
}   

here addr_of_a holds the address of just deployed contract, contract address of contract A.

0

Here is a code example :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract A {
    function functionInsideA() public {}
}

contract B {
    A contractA;

    constructor(A _addressOfA) {
        contractA = _addressOfA;
    }

    function callFunctionInsideA() public {
        contractA.functionInsideA();
    }
}

It will require you to give the address where you deployed A when deploying B. Then you will be able to call visible functions from A directely in B.

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.