0

Given the following function:

    function checkLock() external view returns (bool) {
        require(
            address(this) == IERC721(_nftRegistryAddress).ownerOf(_tokenId),
            "[checkLock] Contract is not owner of tokenId"
        );
        return true;
    }

The correct state is false - the contract does not own the NFT.

If I call this function with Metamask's web3, i.e. 0.20.7, it correctly returns false.

If I call this function with web3 1.2.6, it incorrectly returns true!

I'm aware that relying on a require to return state is nonstandard, but would be interested in understanding the discrepancy.

  • I'm aware that relying on a require to return state is nonstandard - it's not returning anything. The line below it returns true. And BTW, you may as well get rid of that line (as well as the returns (bool) in the function declaration), because it's never going to return anything else. Either the function-call completes successfully, or it reverts (so you can simply rely on that in order to determine the status). – goodvibration Mar 27 at 12:33
  • If I call this function with Metamask's web3, i.e. 0.2.7, it correctly returns false - this function does not return anything other than true! – goodvibration Mar 27 at 12:50
  • @goodvibration ok, so let me reformulate: why does it appear to revert with 0.2.7 and not with 1.2.6? – Joël Mar 28 at 0:59
  • If I call this function with web3 1.2.6 - how do you call this function? – goodvibration Mar 28 at 4:44
  • @goodvibration I instantiate the contract and use .call() for both versions. – Joël Mar 28 at 4:53
1

This is a bug in either web3.js OR the node that you're communicating with.

It happens only if the require statement includes an error-message.

It happens when the node is Infura, but not when the node is Ganache.

Note that your function shouldn't be returning false in either case.

Either it returns true, or the function-call reverts.

| improve this answer | |
  • Thanks for helping debug! – Joël Mar 29 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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