Ethers.js – How to Obtain Return Value of Non-View Function

buidlercontract-invocationethers.js

In a Buidler test I have:

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

describe("SumOfTokens", function() {
  it("Checks correct transfers", async function() {
    const SumOfTokens = await ethers.getContractFactory("SumOfTokens");
    const sumOfTokens = await SumOfTokens.deploy();

    await sumOfTokens.deployed();

    console.log(await sumOfTokens.newToken());
  });
});

It prints some nonsense instead of the return value (that should be 1) of the external non-view function newToken.

How to obtain the return value of newToken after its call?

Best Answer

The return-value of a non-constant (neither pure nor view) function is available only when the function is called on-chain (i.e., from this contract or from another contract).

When you call such function from the off-chain (e.g., from an ethers.js script), you need to execute it within a transaction, and the return-value is the hash of that transaction.

This is because it is unknown when the transaction will be mined and added to the blockchain.

Moreover, even when the transaction is added to the blockchain, it can be removed from it later.

The longer it stays on the blockchain, the smaller the chances are that it will be removed from it later in the future.

It is custom to confirm 12 blocks before assuming that it will remain in the blockchain forever.

In order to obtain the return-value of a non-constant function when you call it from the off-chain, you can emit an event which contains the value which you are about to return.

The contents of this event will then be available to you within the transaction receipt, which you may obtain via the transaction hash.

Related Topic