2

Here's the function that's supposed to return a uint256

function initializeToken(string memory name, string memory symbol) public returns (uint256) {

   // _creatorIds is a @openzeppelin counter
   address creator = msg.sender;
   _creatorIds.increment();
   newId = _creatorIds.current();

   Token token = new Token(symbol, name, creator);
   _tokens[newId] = address(token);
   return newId;
}

I followed the testing instructions given here

The when I console.log() the result in a test file and run it with hardhat it returns a

{ Object (hash, blockHash, ...) }

instead of a

{ BigNumber (_hex, _isBigNumber) }

So I can't do any equality tests.
There's probably a really easy answer to this, but I've tried recompiling multiple times, and it still doesn't work.

edit:
Here's the test code as requested:

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

describe("TokenDistributor contract", function() {
    it("Deployment should return a Token with name Test Token and symbol TTN", async function() {
      const [distributor] = await ethers.getSigners();
  
      const TokenDistributor = await ethers.getContractFactory("TokenDistributor");
  
      const instance = await TokenDistributor.deploy();
      await instance.deployed();

      var tokenId = await instance.initializeToken("Test Token","TTN");
      console.log(tokenId); //This is where I did the console.log to see what was wrong
      
      expect(tokenId).to.equal(1); //this is where the test fails
    });
  });
2
  • can we see the test code?
    – haxerl
    Commented Mar 19, 2021 at 2:09
  • @haxerl sure. I've edited the question body.
    – Sloth Man
    Commented Mar 19, 2021 at 2:25

1 Answer 1

8

Okay, so after fiddling around, I've realized what the problem was. Return value of a non-view/pure functions inaccessible outside of the chain (i.e. other smart-contracts). Hence, to retrieve the return value while returning a function, one must use events instead.

Related question/answer is here for future reference and for anyone who stumbles upon the same problem.

1
  • OMG thank you! I would never have worked this out on my own... Commented Oct 18, 2023 at 4:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.