2

When it comes time to do the actual minting, it seems that every ERC721 contract out there calls the 2-argument version of the _safeMint() function, which in turn then proceeds to immediately internally call the 3-argument version of _safeMint().

Here's what I mean - this is the code directly from OpenZeppelin's official ERC721.sol contract:

// The 2-argument version:
function _safeMint(address to, uint256 quantity) internal {
   // Immediately call the 3-argument version:
   _safeMint(to, quantity, "");
}



// And here's that 3-argument version:
function _safeMint(address to, uint256 quantity, bytes memory _data) internal {
   // minting logic/code, usually eventually calling:
   _mint();
}

So like I said, I've seen this pattern again and again and again, in pretty much every minting contract out there.

So I'm wondering if there's any reason why we can't just directly call the 3-argument version from our contract and skip that 2-argument version - since it all it seems to do is call the 3-argument version anyway - and be done with it?

Especially if we know we'll never have any real value for the data argument that has to get passed in, and can therefore just happily pass "" for it ourselves?

1 Answer 1

2

You could call the 3 argument method immediately.

The two-argument version is provided with the default value for convenience during development. This way you don't need to figure out what default values need to apply to _data when it's not being used.

The reason the 2 argument version calls the three argument version is also for maintainability. If the 3 argument version is overridden, you still expect the 2 argument version to call the 3 argument version with the default value for _data.

The pattern is called overloading and is common in OOP languages like Java. https://www.mygreatlearning.com/blog/method-overloading-in-java

4
  • Understood the 1st pt. of you answer - but not the 2nd. part. Can you give an example? (Who/Where would the 3-arg. version be overridden? And if it is overridden, wouldn't it be for the purpose of just calling it directly - and again skipping the 2-arg version anyway?)
    – Mark55
    Aug 30, 2022 at 0:23
  • When working with libraries (like OpenZeppelin) and complex contracts you might find a situation where you need to change the underlying implementation. In that case, you override the 3 arg version so that any methods using your 2 arg version are calling your updated code.
    – Milk
    Aug 30, 2022 at 2:56
  • You could also call the 3 arg version directly, supplying your own value for the extra field. This multi-method pattern is called overloading and it's very common in OOP languages like Java
    – Milk
    Aug 30, 2022 at 2:58
  • What's the purpose of that 3rd argument? I am using it so I can attach a string to each mint but I don't know how to access. Is it possible to access that data field somehow? Oct 30, 2023 at 8:13

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.