I'm new to Solidity programming and I don't know what is (fallback) function generated when I deploy a DAO contract. It is not defined in the contract code.

Sample code I used:

pragma solidity ^0.4.24;

contract Fundraiser {
    mapping(address=>uint) balances;

    function withdrawCoins(){ // vulnerability inside
      uint withdrawAmount = balances[msg.sender];
      Wallet wallet = Wallet(msg.sender);
      balances[msg.sender] = 0;

    function getBalance() constant returns (uint) {
        return address(this).balance;

    function contribute() payable {
        balances[msg.sender] += msg.value;

    function() payable {


This method is used as Adding a fund to that contract, and by this, tokens are added to that DAO, and contributing or withdrawing is enabled.

Does this mean I have to put ETH into a DAO contract to get token of that DAO contract? How can I define that (fallback) method, and where can I get the documentation or reference for how to use it?

Thank you in advance for your answer.


The fallback function is the last one in the contract you posted:

function() payable {


From https://solidity.readthedocs.io/en/v0.5.10/contracts.html?#fallback-function:

A contract can have exactly one unnamed function. This function cannot have arguments, cannot return anything and has to have external visibility. It is executed on a call to the contract if none of the other functions match the given function identifier (or if no data was supplied at all). Furthermore, this function is executed whenever the contract receives plain Ether (without data).

Note that in your example the fallback function is only there so that the contract can receive ETH directly, without the evocation of any of its functions. It's quite useful for receiving donations, for instance. So, if you want to just send ETH to that contract, you can simply transfer ETH to its address as you would if it was a personal account.

In the contract you posted you should not do that, though, because the contract will not know how much your balance is. That accounting is done in the contribute() function:

balances[msg.sender] += msg.value;

Thus, you should use the function contribute() to send funds, instead of sending directly.

Does this mean I have to put ETH into a DAO contract to get token of that DAO contract?

I haven't seen the code, but most probably, yes. That's why the contract keeps track of how much you sent - so that it can convert to another currency when you call withdrawCoins().

It's worth mentioning that this contract is vulnerable to a reentrancy attack and should not be used (as is) in the mainnet.

| improve this answer | |
  • Thx for the detailed explanation. – cadenzah Jul 31 '19 at 8:11
  • You're welcome! – Daniel Portugal Jul 31 '19 at 15:11

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.