0

Say I have this contract:

contract Foo {

    uint256 public foo;

    function setFoo(uint256 _foo) public {
        foo = _foo;
    }

    function setFooToZero() public {
        foo = 0;
    }
}

And I want to inherit from it like so:

contract Bar is Foo {

    function setFooToZero() public {}
}

This way, setFooToZero gets updated to a function that does nothing, but is there a way of disabling it? Of not having it in the bytecode of the contract at all?

  • 1
    Just delete the function from Foo (seriously). Why are you inheriting from a contract that has code in it you don't actually want? See also diligence.consensys.net/posts/2019/06/… for a more general rant about inheritance. – user19510 Sep 23 at 19:20
  • Thanks @smarx. I'll do that. I was importing from OpenZeppelin, but indeed it'd be better to just copy their code and remove the function. – Paul Razvan Berg Sep 23 at 20:21
  • 1
    If it's a known contract like that, my advice would be to comment out the function and add a comment explaining why. This will make it easier if, e.g., you get an audit and don't want the auditors to have to puzzle over where the code came from. – user19510 Sep 23 at 23:50
1

Child contract can't "erase" functionality from the parent contract. It can only override it.

So the only things you can do is set some boolean flag to disable functionality in parent function or simply override the functionality with your own implementation. Of course nothing prevents someone from using the parent contract directly without your tricks so they'd get the full parent functionality in use - unless these functions are marked as internal.

1

No, but sort of.

A contract that inherits (Bar) is committed to implementing all functions defined in the parent (Foo), so you can't make the function disappear.

Functions can be overridden, as you have discovered. You can also give the function private visibility which makes it inaccessible to child contracts.

Your empty function will do nothing. It will also return "success" and allow the caller to continue as normal. That is either desirable or undesirable depending on the purpose of the function. In most cases, the desirable thing to do would be to revert. It's an abstract example, so abstract guidance. ;-)

function setFooToZero() public {
    revert("disabled");
}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.