1

I was trying to do a simple NFT locking mechanism when I came across this error (It transfers over the NFT from owner to a locking address): TypeError: Contract "locker" should be marked as abstract.

Here is the code.

contract locker is IERC721
{
    function lockThisToken(address nftAddress, uint tokenID) public
    {
        IERC721(nftAddress).safeTransferFrom(msg.sender, address(this), tokenID);
        
    }
}

What I understand is I must implement every method in the interface, this is quite fascinating - why would this be a strict rule from a program design perspective?

Also, when this user writes a similar code but for ERC20s, it does not return an error - any reasons why?

ref link: https://solidity-by-example.org/defi/vault

2
  • 2
    you are inheriting from an interface that has functions but those functions need to be implemented by you. I think you don't need "is IERC721". importing it just at the start of the contract is enough
    – Majd TL
    Commented May 31, 2022 at 6:30
  • @Majd TL has given the answer.
    – Abdulhakim
    Commented Dec 26, 2022 at 10:58

1 Answer 1

2

You should understand the difference between an abstract contract, an interface and an implementation.

  • Interface: contains no function implementations, only function signatures.
  • Implementation: contains only function implementations. Can't include functions without an implementation (so the function has to have at least an empty implementation: { })
  • Abstract contract: Can contain both implementations and only signatures. This form is not used very often

Since you are implementing an interface (denoted by is IERC721) you have to explicitly include each function. Typically you have the function implementation code (although it can be empty) also, but abstract contracts allow you to only list the signature with no functionality (basically the same way as is written in the interface).

In your ERC20 example, the user is writing an interface and then using the interface (but not with is IERC20). If the Vault was written as Vault is IERC20 then it would have to include all of the interface functions.

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.