1

By safe withdrawal, I mean if a crowdsale doesn't reach the target, what area of code sends the tokens back to investors. I'm really stumped.

pragma solidity ^0.4.16;

interface token {
    function transfer(address receiver, uint amount);
}

contract Crowdsale {
    address public beneficiary;
    uint public fundingGoal;
    uint public amountRaised;
    uint public deadline;
    uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;
    bool fundingGoalReached = false;
    bool crowdsaleClosed = false;

    event GoalReached(address recipient, uint totalAmountRaised);
    event FundTransfer(address backer, uint amount, bool isContribution);

    /**
     * Constrctor function
     *
     * Setup the owner
     */
    function Crowdsale(
        address ifSuccessfulSendTo,
        uint fundingGoalInEthers,
        uint durationInMinutes,
        uint etherCostOfEachToken,
        address addressOfTokenUsedAsReward
    ) {
        beneficiary = ifSuccessfulSendTo;
        fundingGoal = fundingGoalInEthers * 1 ether;
        deadline = now + durationInMinutes * 1 minutes;
        price = etherCostOfEachToken * 1 ether;
        tokenReward = token(addressOfTokenUsedAsReward);
    }

    /**
     * Fallback function
     *
     * The function without name is the default function that is called     whenever anyone sends funds to a contract
 */
function () payable {
    require(!crowdsaleClosed);
    uint amount = msg.value;
    balanceOf[msg.sender] += amount;
    amountRaised += amount;
    tokenReward.transfer(msg.sender, amount / price);
    FundTransfer(msg.sender, amount, true);
}

modifier afterDeadline() { if (now >= deadline) _; }

/**
 * Check if goal was reached
 *
 * Checks if the goal or time limit has been reached and ends the campaign
 */
function checkGoalReached() afterDeadline {
    if (amountRaised >= fundingGoal){
        fundingGoalReached = true;
        GoalReached(beneficiary, amountRaised);
    }
    crowdsaleClosed = true;
}


/**
 * Withdraw the funds
 *
 * Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
 * sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw
 * the amount they contributed.
 */
function safeWithdrawal() afterDeadline {
    if (!fundingGoalReached) {
        uint amount = balanceOf[msg.sender];
        balanceOf[msg.sender] = 0;
            if (amount > 0) {
                if (msg.sender.send(amount)) {
                    FundTransfer(msg.sender, amount, false);
                } else {
                    balanceOf[msg.sender] = amount;
                }
            }
        }

        if (fundingGoalReached && beneficiary == msg.sender) {
            if (beneficiary.send(amountRaised)) {
                FundTransfer(beneficiary, amountRaised, false);
            } else {
                //If we fail to send the funds to beneficiary, unlock funders balance
                fundingGoalReached = false;
            }
        }
    }
}
  • It's a withdraw pattern (vs send pattern), so the function is called by investors to get their ether back. – libertylocked Nov 14 '17 at 17:15
  • What I don't understand is what part of code is actually the part that allows investors to call their ether back though. I see the function safeWithdrawal, but don't understand the two parts of it. – N.Hyatt Nov 14 '17 at 17:23
  • One part lets beneficiary withdraw ether, one part lets investors withdraw ether. msg.sender.send is where the funds are sent. – libertylocked Nov 14 '17 at 17:35
  • Ah, yeah I see the difference between the two chunks of code now. Thanks! – N.Hyatt Nov 14 '17 at 18:21
2

These are the relevant parts:

modifier afterDeadline() { if (now >= deadline) _; }

and

function safeWithdrawal() afterDeadline {
    if (!fundingGoalReached) {
        uint amount = balanceOf[msg.sender];
        balanceOf[msg.sender] = 0;
        if (amount > 0) {
            if (msg.sender.send(amount)) {
                FundTransfer(msg.sender, amount, false);
            } else {
                balanceOf[msg.sender] = amount;
            }
        }
    }

    if (fundingGoalReached && beneficiary == msg.sender) {
        if (beneficiary.send(amountRaised)) {
            FundTransfer(beneficiary, amountRaised, false);
        } else {
            //If we fail to send the funds to beneficiary, unlock funders balance
            fundingGoalReached = false;
        }
    }
}

The modifier checks if the crowdsale is over. If it's not over, then safeWithdrawal exits without doing anything. safeWithdrawal then checks if the funding goal was reached. If it wasn't reached, then it checks if the sending address deposited anything. If sender did deposit, then the balance is returned and balance in the contract is set to 0.

The second half of safeWithdrawal is regarding the group that put on the ICO. If the funding goal was reached and the sending address is the beneficiary (the group getting the funds), then it'll send the funds. Then if sending fails, it unlocks the funds for the contributors.

| improve this answer | |
  • Ah, that makes alot of sense. You really cleared it up for me, thanks! Another question, is there a way to create a minimum goal; from what I see the fundingGoalReached is a maximum goal - the crowdsale ends if it hits it. Is it possible to have a minimum (and possibly a maximum too)? – N.Hyatt Nov 14 '17 at 18:20
  • No problem! So the fundingGoalReached is actually a minimum, and there is no maximum in the current system. You could create a maximum by modifying the payable function to not accept contributions if amountRaised is over a certain amount. – flygoing Nov 14 '17 at 20:01

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.