1

I need help clearing somethings up when it comes down to redeclaration and implementation of inherited functions. I have an example below. I am building an nft erc1155 marketplace and inherited the safeBatchTransferFrom function. How would I redeclare this function within my main contract? And do I need to add more functionality to inherited functions or do I just redeclare them? Thank you!

    contract SpiderVerse is ERC1155{ 

    //SpiderVerse is my main contract and I am inheriting from ERC1155 which inherits 
    from IERC1155.



    function safeBatchTransferFrom(
        address _from,
        address _to,
        uint256[] calldata _ids,
        uint256[] calldata _values,
        bytes calldata _data
    ) external;

    //I am confused as to how I initialize this function as with other functions that 
    are inherited. I know there are rules and requirements as well.

}

1 Answer 1

0

I believe function overriding is what you are looking for.

It is the process of declaring a function that was already declared in a parent contract.

Take:

Contract A {

   function foo(uint256 amount) public virtual pure returns (uint256){
    return amount;
   }

}

If you have a contract B that inherits from Contract A and that wants to redeclare foo() (to either adding functionnality or changing totally it's inner workings) you can redeclare the function foo() in Contract B by using the same function header and adding the override modifier as such:

import {A} from "./A.sol";

Contract B is A {

   function foo(uint256 amount) public override pure returns(uint256) 
   {
       return amount * 5;
   }

Note that to be able to override a function the parent function MUST contain the virtual modifier


  • Function overriding can often be used in conjunction with super that allows to call a function from the direct parent contract (only one level higher in the inheritance line):

enter image description here

That pattern is generally used to only adding functionality without changing the parent function:

function foo(uint256 amount) public override pure returns(uint256){
   require(amount > 5, "amount should GT 5");
   return super.foo(amount);
}
7
  • ah ok. I think I understand it more. So basically just rewrite the function, add the functionality as you see fit, and then use ContractName(the name of the function you are inheriting?).functionName(name of the function you are inheriting?) or super.functionName()?
    – EGstacking
    Aug 25, 2023 at 13:40
  • No ContractName.functionName() is different than super.functionName(). the modifier super tells the compiler to check the function in one of the direct parent. The ContractName.functionName() is when you have declared an instance of a new contract (generally with the ERC1155 erc1155Token = new ERC1155() . You can also declare an instance of an already existing/deployed contract with ERC1155 erc1155Token = ERC1155(ADDRESS_OF_THE_DEPLOYED_CONTRACT). In that case you cannot override and modify those functions.
    – Torof
    Aug 26, 2023 at 3:52
  • To modify an existing function you need to inherit from the contract and use the override modifier. Otherwise if you are using a function from an already deployed contract you are not inheriting from you are actually making an external call.
    – Torof
    Aug 26, 2023 at 3:52
  • hello again. I understand the overriding aspect but what if I am just trying to use the existing function from another contract without any modification? Kind of just like copying and pasting?
    – EGstacking
    Aug 29, 2023 at 17:04
  • If you like my answer and think it's the right answer to the question you asked I invite you to upvote my post and maybe validate my answer as the right one. As for your question: I think you might be misguided. Since you are inheriting the previous contract that defines the function, if you do not want to change it in any way, you do not have to override or redeclare it you can just use it as is. Override is just if you want to change things about a function previously declared in parent contracts.
    – Torof
    Aug 30, 2023 at 3:49

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.