1

I am building my own application for an erc1155 dapp but have some questions about interfaces and inheritance. How do you re-declare functionality that you inherit from another contract? Also, how do you even implement interfaces in your code? I understand importing files and the "is" keyword in your contract header, but how do you actually USE an interface? As in are you supposed to rewrite functions that you inherit from interfaces or other imported contracts? Or just leave them alone after declaring you inherited? Sorry if this is such a silly question I am just very excited yet overwhelmed on now starting to make my own projects unguided haha. Thank you!

Edit:

function safeTransferFrom(address _IERC115) external 
{ 
    IERC1155(_SpiderVerseNFT) .safeTransferFrom(); 
}

function safeTransferFrom(address _IERC1155) external view returns(address)
{ 
    return IERC1155(_SpiderVerseNFT).safeTransferFrom(); 
} //Functions redecalred in my base contract.

function safeTransferFrom(address _IERC1155) external 
{ 
    IERC1155(_SpiderVerseNFT).safeTransferFrom();
}

function safeTransferFrom(address _IERC1155) external view returns(address) 
{
    IERC1155(_SpiderVerseNFT).safeTransferFrom();
}

1 Answer 1

1

No question is a silly question.

Interfaces are used to interact with different contracts.

Inside interfaces, you only declare the functionality of the function you wish to implement so that you can re-use it in different contracts.
It's used for light-weight importing code, instead of importing the whole contract.

Here's an example from Solidity by Example

First, you define the contract containing the basic functionality:

contract Counter {
    uint public count;

    function increment() external {
        count += 1;
    }
}

Now, to use this function, we declare an interface, containing only the basic structure of the function:

interface ICounter {
    function count() external view returns (uint);

    function increment() external;
}

Now, to use these functions stated in the base contract and re-declared in the interface, we simply call the interface with the address of the contract containing the code and return it with the interface call, as follows:

contract MyContract {
    // "address _counter" here is the address of 
    // the first declared contract, called "Counter".
    function incrementCounter(address _counter) external {
        // In the next line, we call the interface with 
        // the base contract address, and call the function we wish to use.
        ICounter(_counter).increment();
    }

    function getCount(address _counter) external view returns (uint) {
        return ICounter(_counter).count();
    }
}

The owner of Solidity by Example also has a YouTube channel I recommend, here's a link of him explaining about Interfaces.

Please let me know if I've explained it properly or if you have further questions. This is indeed a concept that takes some time to grasp. I suggest you try to implement it in your code to understand better how it works.

7
  • Thank you so much for replying. I actually do understnad the concept of what interfaces are and how inheritance works however its the actual implementation that is a bit confusing to me(as in how to actually use it in your code). I am trying to use an erc1155 interface and inheritance but again am a bit confused with the syntax and "how to". When I redeclare functions I redecalre them in my base function correct? So I basically inly use the functions from the interface that I want to use and not the whole thing?
    – EGstacking
    Aug 14, 2023 at 15:45
  • Paste some code, so I can help you more specifically. Generally, you have the first contract with the code you wish to re-use. Then, in your interface you copy the base of the function, and lastly, you call the interface name, with the address of the contract, and finish with the function itself. Aug 14, 2023 at 21:35
  • Ah ok I see. I think I may have done the opposite actually. I will paste the code to my smart contract here. Hope it reads well. function safeTransferFrom(address _IERC115) external { IERC1155(_SpiderVerseNFT) .safeTransferFrom(); } function safeTransferFrom(address _IERC1155) external view returns(address){ return IERC1155(_SpiderVerseNFT).safeTransferFrom(); } //Functions redecalred in my base contract.
    – EGstacking
    Aug 14, 2023 at 23:30
  • Kind of hard to read it from a comment, edit your original post with block code formatting. Aug 15, 2023 at 13:10
  • function safeTransferFrom(address _IERC1155) external { IERC1155(_SpiderVerseNFT).safeTransferFrom() }; function safeTransferFrom(address _IERC1155) external view returns(address) { IERC1155(_SpiderVerseNFT).safeTransferFrom(){; I inherited these functions from the open zepellin IERC1155 and declared them in my base contract named SpiderVerseNFT.
    – EGstacking
    Aug 15, 2023 at 14:38

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.