0

I'm trying to make a smart contract that can receive and send NFTs. I have something like this. I know this is wrong but what do I change? Also I'm trying to do this on the Polygon Network, not sure if that affects anything. Thanks

// SPDX-License-Identifier: MIT

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";

pragma solidity ^0.8.0;

contract Contract is ERC721 {
  
  uint num_of_nfts = 0;
  mapping (uint => ERC721) nft_holdings;

  function call_when_recieve_nft(nft) public {
    nft_holdings[num_of_nfts] = nft;
  }
}
1
  • ERC721 is a nft collection contract, not an individual nft. One ERC721 contract can issue almost unlimited nfts if it would like to. Also, I don't think you need to make your contract a ERC721, unless you want to issue your own nfts to people.
    – Bruce
    Commented Nov 30, 2022 at 0:14

2 Answers 2

1

A smart contract cannot block receiving NFTs. So the receiving part is already done when a smart contract is created. Sending NFTs can be done by calling the transferFrom function of the NFT smart contract.

Example:

interface IERC721 {
     function transferFrom(address from, address to, uint256 tokenId) external;
}

contract TransferNFT {
    function transferNFT(address NFTAddress, address _receiver, uint256 _NFTId) external {
        IERC721 NFT = IERC721(NFTAddress); // Create an instance of the NFT contract

        // Calling the NFT smart contract transferFrom function
        // From - receiver - NFT id
        NFT.transferFrom(address(this), _receiver, _NFTId);
    }
}

=-]

0

To receive NFTS on your smart contract you have to implement the following function after importing openzeppelin ERC721 contracts:


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

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.