3

I am trying to figure out why the OpenZeppelin ERC721 contracts always refer to ERC721.ownerOf(tokenId) instead of just ownerOf(tokenId). This is the only function referred to in this way. When I try to execute the following:

function mint() public {
    require(msg.sender == ownerOf(0),"You don't own the right NFT");
    _safeMint(msg.sender,1);
}

I get the error"

transact to test.mint errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

But when I use ERC721.ownerOf(tokenId) instead, the code:

function mint() public {
    require(msg.sender == ERC721.ownerOf(0),"You don't own the right NFT");
    _safeMint(msg.sender,1);
}

executes as expected.

Thanks!

1
  • 1
    it is probably due to the difference between ownerOf of the IERC721 interface and the one of the ERC721 they have the same name, but one is external and the other is virtual so they should serve different purposes. Jun 6 at 16:45
1

There is a difference if ownerOf is overridden by a contract that inherits from ERC721.

This code will call the overridden version

require(msg.sender == ownerOf(0),"You don't own the right NFT");

The other code will call the function from ERC721 and skip the overridden one

require(msg.sender == ERC721.ownerOf(0),"You don't own the right NFT");

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.