Hardhat/Ethersjs – Retrieve Non-Constant Transaction’s Return Data

dataethers.jshardhatreturnsolidity

I'm trying to figure out if it's possible to retrieve the return value of a non-view function. Specifically, I want to see the return value of a newly created contract

I have created a simple setup to illustrate the problem:

pragma solidity ^0.8.0;

contract TestA {

    function test() public returns (TestB) {
        return new TestB();
    }
}

contract TestB {
    uint256 public somedata = 123;
}

Is there any way to query the blockchain for the transactions return value when calling TestA::test()?

Best Answer

Ok, I was hoping to be able to read the output data from the transaction after it had been mined, but it seems like .callStatic is the closest to what I was looking for:

    const TestB = await ethers.getContractFactory('TestB');
    const TestA = await ethers.getContractFactory('TestA');
    const testA = await TestA.deploy();

    const addressTestB = await testA.callStatic.test();
    const testb = TestB.attach(addressTestB);
    await testA.test();
    console.log(await testb.somedata());
Related Topic