9

Can you call the fallback function of the base contract that the current contract is inherited from?

contract LittleA {
  function() {
    doLittleStuff();

    // how do I call the base contract's fallback function?
  }
}

contract A {
  function() {
    doStuff();
  }
}
4

You can't call things like super() or A() so you have no way to call the unnamed fallback function.

However, if you can modify the parent contract, you can move all the logic of your fallback function to another named function that would be called from the callback in A and that would be callable from LittleA.

Here is an exemple with two contracts named Parent and Child. The fallback() function can be called from Child using super.fallback().

contract Parent{
  function fallback() internal {
    log0("Parent fallback function");
  }

  function (){
     fallback();
  }
}

contract Child is Parent {
  function (){
    log0("child fallback function");
    super.fallback();
  }
}

Sending a basic transaction to Child contract will call its fallback function which calls Parent.fallback() using. The result is :

LOG (Child): child fallback function
LOG (Child): Parent fallback function 

I admit that this not the most beautiful design but as you set the visibility of fallback() to internal, it's not visible into the ABI so you're good.

  • Also note that you could simply call the parent contract using the call() function. But you would have to know contract address. Also note that having too much logic in the fallback functions seems to be discouraged because a tx that reach fallback may not have enough gas for complex operations, most of the time it's a regular ether send transaction : github.com/ConsenSys/Ethereum-Development-Best-Practices/wiki/… – Nicolas Massart Jun 6 '16 at 21:11
  • 1
    Thanks! Thats a good answer. One question though: isn't the contract address just this? – Raine Revere Jun 6 '16 at 21:22
  • I'm not sure that I understand what you want to do with "this". This is a self reference, how would you want to use it there? – Nicolas Massart Jun 7 '16 at 8:52
  • this is explicitly convertible to an address type. It is the address of the contract. solidity.readthedocs.io/en/latest/… – Raine Revere Jun 7 '16 at 12:20
  • @Raine do you mean using inheritance to get the parent address using this to then call the fallback using this address? – Nicolas Massart Jun 7 '16 at 14:28

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.