I have a smart contract that has only one payable function and one that pays, as follows:

contract DAO_Test{
// define something
   function buyToken() payable external returns(bool){        
    uint amount = (precision*msg.value)/exchange_Rate;
    token_balances[msg.sender] += amount/1000000000000000000;             
    ether_balances[msg.sender] += msg.value/1000000000000000000;
    Deposit(msg.sender, msg.value);
    return true;

   function send_funding (bytes2 proposal_id) internal  returns (bool){
    uint indx = find_proposal_indx (proposal_id);
    Proposal temp;
    temp = proposals[indx];
    if (final_result[indx]){ 

Question 1: Do I need to have a

  function() payable(){} 

in my contract?

Question 2: In general, why do we need to have a fallback function?

I'm aware that fall back functions are for the case where the function called does not match the functions defined in the contract. Or when the callers use "send" or "call" methods to pay the contract. I think, it's good not to have fallback function in our contract, because we force the caller to follow the rules (e.g. properly call the functions and use "transfer" to send money to the contract).

  • For Question 2 ,you have answered that's perfectly valid and that is the answer and for question 1 If you have only one function that is payable(buytoken),then you can have fallback function without payable. – Rangesh Sep 14 '17 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.