2

As per the ERC721 requirements, I have to implement onERC721Recieved. This is how I did it:

function onERC721Received(
    address _operator, 
    address _from, 
    uint256 _tokenId, 
    bytes calldata _data
)external returns(bytes4) {
    return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
} 

This seems to be in line with what is required of the function. However, this raises two questions:

  1. If this does not actually use any of the parameters, why pass the parameters to the function at all? It is now throwing errors every time I test the contract.
  2. What is the reason behind this function?
user51347
  • 107
  • 9
  • Check at least https://ethereum.stackexchange.com/questions/52258/what-are-good-specific-reasons-the-onerc721received-function-returns-a-magic-val and https://ethereum.stackexchange.com/questions/48796/whats-the-point-of-erc721receiver-sol-and-erc721holder-sol-in-openzeppelins-im – Lauri Peltonen Mar 17 '19 at 20:24

1 Answers1

1

You can remove the errors with this implementation:

function onERC721Received(
    address, 
    address, 
    uint256, 
    bytes calldata
)external returns(bytes4) {
    return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
} 

The parameters are there for implementations that want to use them. Perhaps for doing an accounting of incoming assets. Or perhaps for denying receipt of certain assets.

William Entriken
  • 4,690
  • 15
  • 42
  • The gas for deployment and usage can be optimized by relying on a constant that pre-calculates the return - eg https://github.com/makerdao/nft-adapter/blob/68d035589757169be963ca1ade5b4a3b1e3050cc/src/test/openzeppelin-solidity/token/ERC721/ERC721.sol#L17-L19 – Fabiano Soriani Jul 28 '22 at 07:13