Solidity Issue – Fallback Function Not Getting Called

go-ethereumsolidity

I have defined a fallback function to accept ethers, so that anybody can send ether's simply with send. But this is not working, if anybody send ether's to my contract, Transaction goes successful but account balance remains same, no deduction of gas fee. So contract does not receive any ether's.
Code of fallback Function:

function () payable {
        PayableCalled(true)   //event registered to ensure that function was called
        if (crowdsaleClosed) throw;
        uint amount = msg.value;
        balanceOf[msg.sender] = amount;
        amountRaised += amount;
        Transaction(msg.sender,msg.value,amountRaised)
        tokenReward.transfer(msg.sender, amount / price);
        FundTransfer(msg.sender, amount, true);
    }

while trying to debug this issue, I have made sure that there is only one fallback function, I also increased gas. I have also checked there are no pendingTransactions. In transaction data field is also nil. How can I solve this issue? why fallback is not accessible?
Network:
Private network, geth v 1.6

Updated:
Contract:
First of all I deployed contract for custom token and saved its address for sharing with crowdfunding contract in future. Next I deployed contract for crowdfunding. Whose payable is not working. I have also added contract address to custom token allowance list. Custom token contract is working.

Best Answer

There are several possible explanations. It's not possible to be certain because the rest of the contract isn't posted.

  1. Possible the contract didn't compile and/or deploy successfully. Be certain there is a valid contract at the address you're sending the transaction.
  2. Possible the contract throws instead of successfully executing. For example, this line: if (crowdsaleClosed) throw;. If that is true, then there will be no event emitted.

The commented lines (below) could be suspect.

I'm not certain this is on point, but it might be helpful in this case. In most languages, one can record messages during debugging and the log will indicate things that happened before a crash. In Ethereum, if anything should cause the execution to throw then the state will revert to the pre-transaction state (with gas destroyed).

The effect of an error is that Ether is returned and no events are emitted, as if the transaction never happened. It may be helpful to think of transactions as either 100% successful or 100% failed. We don't get events to help understand failures. That would be a semi-successful transaction which is not allowed.

In practice, this means we can't add events to a problematic function to gain insight into what's going wrong. If something goes wrong at any stage, it means no event will be emitted regardless of the order of steps in the function. It also means we shouldn't assume failure happened early merely because event emission came first. The error might occur further on in processing, but since errors cancel events, we don't get a hint about the step that caused the error.

Below seems to work.

contract Fallback {

    event PayableCalled(bool called);
    bool crowdsaleClosed;

    function () payable {
        PayableCalled(true);   //event registered to ensure that function was called
        if (crowdsaleClosed) throw;
        uint amount = msg.value;
        // balanceOf[msg.sender] = amount; // consider +=
        // amountRaised += amount;
        // Transaction(msg.sender,msg.value,amountRaised);
        // tokenReward.transfer(msg.sender, amount / price);
        // FundTransfer(msg.sender, amount, true);
    }  
}

In Remix to show it working.

enter image description here

Hope it helps.

Related Topic