2

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.

  • 1
    Is the "PayableCalled" event actually logged? – TripleSpeeder May 16 '17 at 12:59
  • No, if PayableCalled works then it means function get called :) – furusiyya May 16 '17 at 17:16
  • do you have to declare amountRaised before using += ? I honestly don't know solidity at all but was first thing I thought of. – ruby_newbie May 17 '17 at 1:47
  • @ruby_newbie yes, please have a look at whole code, I have updated question – furusiyya May 17 '17 at 2:47
1

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.

  • thanks @Rob Hitchens, I have update my question with contract code so that may help you for knowing the certain reason. – furusiyya May 16 '17 at 21:22
0

The file you linked that you said the payable is failing on, https://gist.github.com/furusiyya/92c411824bf3ece02d6c80dad265353b has no fallback function in the code. I'm assuming your adding the code you showed in your post here to it, but just want to make sure -- if not, then that is clearly why.

The next thing I would consider is are the funds being sent to the contract from another contract, including a contract wallet, if its utilizing .send() then it will be rejected if the fallback function has anything more than an event long.

  • I copied code directly from editor so forget to undo changes I made after posting this question. It is fixed now. – furusiyya May 17 '17 at 2:35
  • Additionally, the current posted crowdfunding file is missing semicolons at the end of event related statements. I assume your live versions had this corrected, but it is worth noting that if you published it like that, I believe it would throw during execution when getting to call the event. – Dan Conway May 19 '17 at 21:09
  • You are right, My live version had this corrected. I will fix, thanks for point this :) – furusiyya May 20 '17 at 8:43

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.