如何实现onERC721Received函数

mar*_*tty 6 solidity

我正在尝试将 NFT 发送到智能合约以进行托管,但在实现 onERC721Received 的重写函数时遇到了困难,因为我收到了有关未使用变量的错误 - 这是我收到的错误吗?

Warning: Unused function parameter. Remove or comment out the variable name to silence this warning.
--> contracts/NftEscrow.sol:11:79:
|
11 | function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes memory _data) public override returns(bytes4) {
| ^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

这就是智能合约

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";

    
    contract nftescrow is IERC721Receiver {
        
        enum ProjectState {newEscrow, nftDeposited, cancelNFT, ethDeposited, canceledBeforeDelivery, deliveryInitiated, delivered}
        
        address payable public sellerAddress;
        address payable public buyerAddress;
        address public nftAddress;
        uint256 tokenID;
        bool buyerCancel = false;
        bool sellerCancel = false;
        ProjectState public projectState;
    
        constructor()
        {
            sellerAddress = payable(msg.sender);
            projectState = ProjectState.newEscrow;
        }
        
        function onERC721Received( address operator, address from, uint256 tokenId, bytes calldata data ) public override returns (bytes4) {
            return this.onERC721Received.selector;
        }
        
        function depositNFT(address _NFTAddress, uint256 _TokenID)
            public
            inProjectState(ProjectState.newEscrow)
            onlySeller
        {
            nftAddress = _NFTAddress;
            tokenID = _TokenID;
            ERC721(nftAddress).safeTransferFrom(msg.sender, address(this), tokenID);
            projectState = ProjectState.nftDeposited;
        }
    } 
    
    }
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?

Sve*_*enM 10

您可以像这样使警告消失:

    function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
        return IERC721Receiver.onERC721Received.selector;
    }
Run Code Online (Sandbox Code Playgroud)


Pet*_*jda 0

这“只是”一个警告——而不是一个错误——所以即使有警告,合约也会编译。

编译器告诉您正在定义参数operatortokenIdfromdata,但您没有使用它们。但是,ERC721 标准要求onERC721Received()函数接受所有这些参数,即使您可能不使用它们,因为函数选择器是根据它们确定的。

因此您可以安全地忽略此警告。