0

I thought that when you mint an ERC721 Token in the Constructor of your smart-contract it will not emit the Transfer event.

I'm thinking this cause of what's written in the official ERC721 EIP documentation:

    ///  Exception: during contract creation, any number of NFTs
    ///  may be created and assigned without emitting Transfer

(You can read the full docs here: https://eips.ethereum.org/EIPS/eip-721)

I supposed the key is the wording "may be created and assigned without emitting Transfer"... Meaning it's not automatic, but it can be made to happen.

So how do we get this "may be" to actually BE? Cause when I do my minting in the constructor the Transfer event is being emitted every single time (or 100 times in my case.)

Here's my constructor code:

constructor() ERC721("myNFT", "mNFT") {
  
  // Mint 100 NFTs in a FOR loop:
  for(uint8 NFTCounter = 0; NFTCounter < 100; NFTCounter ++) {
     _safeMint(msg.sender, NFTCounter, "");
  }
}

So why is Transfer being emitted - and how can I prevent it from being emitted?

(And ere's the full blurb from the docs:)

interface ERC721 /* is ERC165 */ {
    /// @dev This emits when ownership of any NFT changes by any mechanism.
    ///  This event emits when NFTs are created (`from` == 0) and destroyed
    ///  (`to` == 0). Exception: during contract creation, any number of NFTs
    ///  may be created and assigned without emitting Transfer. At the time of
    ///  any transfer, the approved address for that NFT (if any) is reset to none.

    event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);

1 Answer 1

0

The reason I think because what you read and what you use are not the same. I assume you're using latest EIP721 from openzeppelin which is referred here: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L241

You can take a look in this code: _safeMint -> _mint -> _update -> emit event

So whenever _safeMint is called and success, one event will be emitted, don't have any condition.

and how can I prevent it from being emitted? => Override it by yourself

1
  • ok but HOW do I override the _mint method? It doesn't have override in it's signature - like say the supportsInterface() method does, meaning I can't rewrite/override it in my own parent-contract like I could with supportsInterface(). And if instead I rewrite it in the original ERC721 contract - by just removing the emit Transfer line, would it still be compliant? (And actually I shouldn't remove emit Transfer cause I do still need to emit Transfer on all upcoming regular mints that will happen outside of my constructor. So...how do you propose to override?
    – Mark55
    Commented Mar 6 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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