0

GOAL: Allow anyone to send an NFT to my smart contract and create a new Loan struct with the received data.

Here is my code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

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


contract Borrow is ERC721, IERC721Receiver {
    
    mapping (uint256 => Loan) public loans;

    uint256 count;

    struct Loan {
        ERC721 nftContract;
        uint256 tokenId;
        address borrower;
        bool repayable;
    }

    
    

    function onERC721Received(
        address,
        uint256 _tokenId,
        address _borrower,
        bytes calldata
    )
        external
        virtual
        returns(bytes4)
    {
        uint256 loanId = uint256(keccak256(abi.encode(address(msg.sender), _tokenId)));
        loans[loanId] = Loan({
            nftContract: ERC721(msg.sender),
            tokenId: _tokenId,
            borrower: _borrower,
            repayable: false
        });


        return 0x150b7a02;
    }
}

However, I keep receiving the following errors:

TypeError: Contract "Borrow" should be marked as abstract.

and

  | ^ (Relevant source part starts here and spans across multiple lines).
Note: Missing implementation: 
  --> @openzeppelin/contracts/token/ERC721/ERC721.sol:44:5:

1 Answer 1

1

even though you inherit from ERC721 and IERC721Receiver, you never end up initialising nor implementing any of them in your contract.

if you remove the inheritance from the contract declaration, and you should see no errors.

contract Borrow is ERC721, IERC721Receiver {...}

to

contract Borrow {...} should do the trick

UPDATE: Alternatively,

since you want your contract to receive NFTs(and thus be ERC721 compliant) you need to initialise the ERC721 contract you inherit.

keep your code as before contract Borrow is ERC721, IERC721Receiver {...} adding the constructor to initialise it like:

contract Borrow is ERC721, IERC721Receiver { 

constructor() public ERC721("MyNFT", "MFT") {}
//...(the rest of your contract)
}
2
  • When I do this, the error goes away. However, when I try to send an ERC721 token to it, I get the following error: Reason provided by the contract: "ERC721: transfer to non ERC721Receiver implementer". Commented Dec 7, 2021 at 16:38
  • updated answer based on your last 2 comments
    – 0xninja
    Commented Dec 7, 2021 at 17:09

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.