8

I'm following this link - https://ethereum.org/token to learn Solidity. The example contract implementation has the following line which I don't understand.

enter image description here

Can someone please explain what is this function intended to do? When would it be called? Is it an internal function or external function call?

13

This is depreciated syntax that isn't required as of Solidity 0.4.0

function () is the 'default function' or 'fallback function'. If a transaction is sent to an address without transaction data, or if that data doesn't call a valid function on a contract, the default function is called instead.

throw is a depreciated keyword to halt the computation and revert any changed state.

The function as written will force all transactions to fail if they don't call a legitimate function.

In the later versions of Solidity (>= v0.4.0), a transaction will throw if the default function is not explicitly implemented and the transaction has not called a legitimate function.

If a contract is to accept trivial transactions, such as being sent ether, the default function must be implemented as payable. The amount of gas sent with a trivial send is not enough to change a state variable but is enough to log an event. So a typical implementation may be:

function () payable {
    Deposit(msg.sender, msg.value);
}

As regards throw, this keyword compiles to an illegal opcode which forces the EVM to halt the transaction which consumes all gas sent with the transaction. This has been replaced with the precompiled functions revert(), assert() and require() to test for exceptions and halt the transaction on a failure.

function () payable {
    // only owner can send
    require(msg.sender == owner);

    // Prevent overflow
    assert(balance[owner] + msg.value >= balance[owner]);

    // Accept only upto an amount
    if(balance[owner] + msg.value > maximum) revert();

    // Log the deposit
    Deposit(msg.sender, msg.value);

    // Careful, no guarantee there will be enough gas for this
    balance[owner] += msg.value;
}
  • +1 great answer - I assume that owner is the owner of the contract... – atomh33ls Aug 24 '17 at 9:34
  • realised I may be wrong - owner is probably msg.sender as per solidity.readthedocs.io/en/develop/… – atomh33ls Aug 24 '17 at 11:09
  • When we call function () could we provide msg.data as an argument into this anonymous function?. If yes, could you provide an example? Also after each throw would anonymous function will be called? @o0ragman0o – alper Dec 7 '17 at 6:59
  • The default function isn't called after a throw, only when a function signature in the data does not match anything in the contract. This is an example of a Forwarding contract which uses the fallback function to pass TX data through to a forwarding address. – o0ragman0o Dec 8 '17 at 7:48
  • Why do we need to be careful on the last line? If we are out of gas doesn't all the Tx would be reverted, so its safe? @o0ragman0o – alper Mar 7 '18 at 8:50
4

That function is called the "fallback" function. It is the function that is invoked when a transaction with no data field is made to a contract.

Notice that there is no "payable" modifier on that contract. When you label a function as "payable" it is a function that invokable with a non-zero Ether value in the "value" field of the transaction.

The reason for that empty fallback function is to say, if someone tries to send Ether to this contract, don't accept it, and throw an exception.

  • No - This is not actually a constructor (which is called once, when the contract is deployed, and specified as a function with the exact same name as the contract) but a Fallback function (which is called when eth is send to the contract address or the contract is called without a valid function). – Milney Aug 16 '17 at 8:30
  • This is the fallback function. It gets called when the first four bytes of the 'input' data field does not resolve to one of the other functions in the contract. The first four bytes are called the input field are the function's signature (properly encoded). If there's a match to an existing function that function is called, if not, the fallback or default function is called. – Thomas Jay Rush Aug 16 '17 at 15: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.