diff --git a/docs/contracts/functions.rst b/docs/contracts/functions.rst index 89a19bc79..f5413ee61 100644 --- a/docs/contracts/functions.rst +++ b/docs/contracts/functions.rst @@ -298,7 +298,8 @@ Below you can see an example of a Sink contract that uses function ``receive``. Fallback Function ================= -A contract can have at most one ``fallback`` function, declared using ``fallback () external [payable]``. +A contract can have at most one ``fallback`` function, declared using ``fallback () external [payable]`` +(without the ``function`` keyword). This function cannot have arguments, cannot return anything and must have ``external`` visibility. It is executed on a call to the contract if none of the other functions match the given function signature, or if no data was supplied at @@ -306,19 +307,31 @@ all and there is no :ref:`receive Ether function `. The fallback function always receives data, but in order to also receive Ether it must be marked ``payable``. -In the worst case, if a payable fallback function is also used in place of a receive function, it can only rely on 2300 gas being -available (see :ref:`receive Ether function ` for a brief description of the implications of this). +In the worst case, if a payable fallback function is also used in +place of a receive function, it can only rely on 2300 gas being +available (see :ref:`receive Ether function ` +for a brief description of the implications of this). -Like any function, the fallback function can execute complex operations as long as there is enough gas passed on to it. +Like any function, the fallback function can execute complex +operations as long as there is enough gas passed on to it. .. warning:: - A ``payable`` fallback function is also executed for plain Ether transfers, if no :ref:`receive Ether function ` - is present. It is recommended to always define a receive Ether function as well, if you define a payable fallback function + A ``payable`` fallback function is also executed for + plain Ether transfers, if no :ref:`receive Ether function ` + is present. It is recommended to always define a receive Ether + function as well, if you define a payable fallback function to distinguish Ether transfers from interface confusions. .. note:: Even though the fallback function cannot have arguments, one can still use ``msg.data`` to retrieve any payload supplied with the call. + After having checked the first four bytes of ``msg.data``, + you can use ``abi.decode`` together with the array slice syntax to + decode ABI-encoded data: + ``(c, d) = abi.decode(msg.data[4:], (uint256, uint256));`` + Note that this should only be used as a last resort and + proper functions should be used instead. + ::