# What is (fallback) method in a contract?

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 {

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

function getBalance() constant returns (uint) {
}

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?

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

function() payable {

}


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.

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