Solidity Hardhat – Fix Error Calling Collaborator Contract

contract-invocationhardhatsolidity

I get the following error when a contract is invoking another injected contract:

Error: call revert exception [ See:
https://links.ethers.org/v5-errors-CALL_EXCEPTION ]
(method="helloWithDependency()", data="0x", errorArgs=null,
errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION,
version=abi/5.6.2)

I'm not sure if the collaborating contract is injected corectly. Looking at the test output it seems that could be the problem (cause of zero address in test output).
Anyway, this is the main contract:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;


import "hardhat/console.sol";
import "./TestDependency.sol";

contract TestContract
{
    TestDependency private testDep;

    function setDependency(address dependencyAddress) external
    {
        testDep = TestDependency(dependencyAddress);
    }

    function getDependency() external view returns (address)
    {
        return address(testDep);
    }

    function internalHello() public view returns (string memory) {
        console.log("internal hello function called");
        return "internal hello";
    }

    function helloWithDependency() public view returns (string memory) {
        console.log("helloWithDependency function called");
        return testDep.getConstant();
    }
}

Dependency contract:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

import "hardhat/console.sol";

contract TestDependency
{
    function getConstant() public view returns (string memory) {
        console.log("getConstant function called");
        return "test123";
    }
}

And a test:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Test", function () {
  it("Test dependency", async function () {

    const TestContract = await ethers.getContractFactory("TestContract");
    const testContract = await TestContract.deploy();
    await testContract.deployed();

    const TestDependency = await ethers.getContractFactory("TestDependency");
    const testDependency = await TestDependency.deploy();
    await testDependency.deployed();

    testContract.setDependency(testDependency);

    console.log("testContract.address:" + testContract.address);
    console.log("testDep.address:" + testDependency.address);    
    console.log("testDep injected dependency address:" + testContract.getDependency());    

    
    let internalHello = await testContract.internalHello();
    console.log("internalHello response:" + internalHello);

    let helloResp = await testContract.helloWithDependency();
    console.log("helloWithDependency response:" + helloResp);
  });
});

I'm getting the following output:

npx hardhat test test/test-dependency-test.js 


  Test
testContract.address:0x5FbDB2315678afecb367f032d93F642f64180aa3
testDep.address:0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
testDep injected dependency address:0x0000000000000000000000000000000000000000
internal hello function called
internalHello response:internal hello
helloWithDependency function called
    1) Test dependency


  0 passing (18s)
  1 failing

  1) Test
       Test dependency:
     **Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="helloWithDependency()", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.6.2)**
      at Logger.makeError (node_modules/@ethersproject/logger/src.ts/index.ts:261:28)
      at Logger.throwError (node_modules/@ethersproject/logger/src.ts/index.ts:273:20)
      at Interface.decodeFunctionResult (node_modules/@ethersproject/abi/src.ts/interface.ts:427:23)
      at Contract.<anonymous> (node_modules/@ethersproject/contracts/src.ts/index.ts:400:44)
      at step (node_modules/@ethersproject/contracts/lib/index.js:48:23)
      at Object.next (node_modules/@ethersproject/contracts/lib/index.js:29:53)
      at fulfilled (node_modules/@ethersproject/contracts/lib/index.js:20:58)

This is probably a newbie question, but I'm into solidity for a short time.

Thanks in advance,
Marko

Best Answer

you are not far from the answer, check your

testContract.setDependency(testDependency);

to be replaced with
await testContract.setDependency(testDependency.address);
Related Topic