0

I have 2 contracts that can interact with each other:

contract ContractFoo {
  address private _contractBarAddress;

  constructor() {

  }

  function setContractBarAddress(address _address) public onlyOwner {
    _contractBarAddress = _address;
  }

  function doSomething() public {
    console.log("Bar is called");
    Bar(_contractBarAddress).helloWorld();
  }
}

contract Bar is Ownlable {

  constructor() {}

  function helloWorld() external {
    // Possible check if it is the Foo calling it
    console.log("Hello, world!");
  }
}

Right now, after each deployment, I have to call setContractBarAddress to interact with Bar. Is there a way one could set it automatically directly in solidity? Right now the only way to automate is to write a custom function in e.g. Hardhat scripts to call such function.

1 Answer 1

2

yes. deploy bar from ContractFoo and you have an object for the deployed contract.

something like

pragma solidity ^0.8.14;

//if bar in another file
import "./Bar.sol"

contract ContractFoo {

    Bar contractBar;

        constructor () {
            contractBar = new Bar;
        }

    contractBar.doNothing();
}


contract Bar {
    function doNothing() public pure {
            
    }
}

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.