I have ERC20 controller contract which deploys ERC20 contract.

contract ERC20Controller {
  ERC20 ercContract; 
  constructor {
    ercContract = new ERC20(); 

Now I'm testing this contract in hardhat and want to call balanceOf function. I've expected that I can just call it this way: controller.ercContract().balanceOf() but it fails?
Is the only option there to create function inside controller which calls balanceOf on child contract?

1 Answer 1


First, you may want to define the ercContract variable as public. You can read this thread to learn more about visibility for storage variables: Solidity: private vs public variables.

Second, you would have to create a custom function in the ERC20Controller contract if you want to call ercContract.balanceOf.

The erc20Contract variable is stored as an address in the ERC20Controller cotract. When you call ercContract() from outside the blockchain, you're not getting a contract instance back, but rather a simple address.


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.