0

I'm writing an NFT staking contract and I want my contract to accept NFTs only from 3 specific contracts. I have implemented IERC721Receiver and the onERC721Received function successfully fires. What is the best way to get the contract address of a received token?

1 Answer 1

0

The token that is being transferred will call the IERC721Receiver methods. Therefore you can use msg.sender to detect the address of the token that is being transfered.

The ERC721 smart contract calls this function on the recipient after a transfer. This function MAY throw to revert and reject the transfer. Return of other than the magic value MUST result in the transaction being reverted.

Taken from https://eips.ethereum.org/EIPS/eip-721

Be aware that for ERC-721 tokens this only applies to the safeTransferFrom methods. It is still possible to transfer the tokens to your contracts by using the transferFrom method.

2
  • Thank you so much it definitely works. Regarding the last part of your answer, I'm handling the staking logic on the onERC721Received function and I'm calling the safeTransferFrom function from the ERC-721 contract on the website so... Is there anything extra that I can do except warn users not to manually transfer tokens to the staking address?
    – Accretence
    Jan 21 at 22:01
  • Require that msg.sender is equal to one of your 3 addresses? Otherwise it will revert the transaction with an error message of your choice. Jan 21 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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