Ray
Ray

Reputation: 373

Why Openzeppelin ERC721 defines two 'safeTransferFrom'?

Source code:

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory data
    ) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not token owner or approved");
        _safeTransfer(from, to, tokenId, data);
    }

Hi everyone.

While reading the Openzeppelin ERC-721 source code, I found that it defined two safeTransferFrom method with different implementations.

I am curious why it's made in this way. Could anyone help me with it?

Many thanks.

Upvotes: 1

Views: 595

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43561

It follows the ERC-721 standard that also defines two functions with the same name - but with different input params. Generally in OOP, this is called function overloading.

As you can see in the OpenZeppelin implementation, when you call the function without the data param, it passes an empty value.

I can't speak for the authors of the standard, but to me it seems like a more developer friendly approach compared to having to explicitly pass the empty value, since Solidity doesn't allow specifying a default param value.

Upvotes: 1

Related Questions