3

The tutorial crowdfunding app has the following function:

/* checks if the goal or time limit has been reached and ends the campaign */
function checkGoalReached() afterDeadline {
    if (amountRaised >= fundingGoal){
        beneficiary.send(amountRaised);
        FundTransfer(beneficiary, amountRaised, false);
    } else {
        for (uint i = 0; i < funders.length; ++i) {
          funders[i].addr.send(funders[i].amount);
          FundTransfer(funders[i].addr, funders[i].amount, false);
        }
    }

    beneficiary.send(this.balance); // send any remaining balance to beneficiary anyway
    crowdsaleClosed = true;
}

If in the 'else' statement all the funders get the amount they funded back, how there can be any left over balance to send to the beneficiary?

  • I suppose if the contract has a balance, i.e. someone had sent funds to the contract, then this would clear remaining funds from the contract. – Laurence Jun 19 '16 at 14:38
2

This tutorial may be updated because there are 2 glaring improvements that should be made to that code snippet.

First, the return value of all .send should be checked and handled.

Second, state should generally be updated first, before sending funds. So swap these lines:

beneficiary.send(this.balance); // send any remaining balance to beneficiary anyway
crowdsaleClosed = true;

To the question, generally contracts can receive funds by sending ETH to it directly, so there may be a code path where the balance can be higher than what funders have provided.

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.