Solidity

Solidity Fallback Functions

Solidity Fallback Functions Main Tips

  • In Solidity, a contract may have precisely one unnamed function, which cannot have arguments, nor return anything.
  • Fallback functions are executed if a contract is called and no other function matches the specified function identifier, or if no data is supplied.
  • These functions are also executed whenever a contract would receive plain Ether, without any data.

Solidity Fallback Functions

A Solidity contract may have a single unnamed function, no more no less. This functions cannot have any arguments, nor return anything.

That is what we refer to as a fallback function.

Fallback functions are executed if a contract is called and no other function matches the specified function identifier, or if no data is supplied.

These functions are also executed whenever a contract would receive plain Ether, without any data.

In addition to that, to enable receiving Ether, you have to mark the fallback function payable. When no function like that is present, the contract is unable to receive Ether cia regular transactions.

In context like that, you normally would have minimal amounts of gas available for the function call (2300 gas exactly), so you must make fallback functions as cheap as they can be.

Keep in mind that what gas is required by the transaction (differently than with an internal type call) which will invoke the fallback function is a lot higher, since every transaction will an additional amount of than 21000 gas or even greater amounts for things such as signature checking.

Particularly, the operations in the following list are going to consume more gas than the stipend provides to a fallback function (therefore you should avoid including them in a fallback function):

  • Contract creation
  • Sending Ether
  • Writing to storage
  • External function call requiring large amounts of gas

Make sure to test your fallback functions carefully in order to ensure that the execution cost is no more than 2300 gas before the contract is deployed.

A contract is not capable of reacting to such Ether transfers, therefore, being unable to reject them. This choice regarding the design belongs to the EVM, meaning that Solidity has no way of working around it.

Additionally, it means that this.balance may be greater than the sum of some manual accounting implemented in a contract (i.e. having a counter updated in the fallback function).

Example

pragma solidity ^0.4.0;

contract TestContract {
    // The function below will be called for each message
    // that is sent to this contract (as there is no other function to call).
    // However, if Ether is sent to this contract, an exception will occur.
    // That is because this contract does not have the "payable" modifier.
    function() { a = 1; }
    uint a;
}


// this is a contract, which keeps all Ether to it with not way of 
// retrieving it.
contract SinkContract {
    function() payable { }
}

contract CallerContract {
    function testCall(TestContract test) {
        test.call(0xabcdef01); // hash is non-existent
        // will result in test.a becoming == 1.

        // The following statement is not going to compile.
        // But if ether is sent to this contract, the
        // transaction would fail and the Ether would be rejected
        //test.send(2 ether);
    }
}

 

Try on Remix Try live on Hosting

Note: Despite the fallback function being unable to have arguments, msg.data can still be used to retrieve any payload that has been supplied with the call.

Warning: Contracts which get Ether directly (instead of via a function call, i.e. using transfer or send) but happen to lack a fallback function, will throw an exception, then send the Ether back (the process went differently before Solidity v0.4.0). For this reason, if you would like your contract to be able to receive Ether transaction, implementing a fallback function is a must.

Warning: A contract that does not have a payable fallback function may receive Ether as a recipient of a coinbase transaction (miner block reward) or if it is set as the destination of a selfdestruct statement.

Read previous post:
Solidity Pure Functions

Solidity Pure Functions Main Tips In Solidity pure functions are functions, that are promised not to modify or read the state....

Close