I'm trying to create a crowd sale contract and have run into two issues that I can't resolve/find the answers to in the forums or documentation despite searching for the past two days. I imagine its simply because I don't know what to look for rather than it not being there.

Problem 1: My crowd sale sends the wrong amount of tokens in response to the ether contribution. Instead of providing 10000 tokens per ether it is providing the inverse to the contributors, 0.00001 tokens. I am using scientific notation so that the price is a portion of ether 1e-4 to be precise.

Problem 2: When the crowd sale ends the contract is funded with the ether contributed but it is not sent to the beneficiary address. How do I get the ether out of the contract so that it can be used by the beneficiary (DAO or otherwise).

My code below based on the Ethereum guidance on the main webpage:

pragma solidity ^0.4.16;

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

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,
    address addressOfTokenUsedAsReward
) public{
    beneficiary = ifSuccessfulSendTo;
    fundingGoal = fundingGoalInEthers * 1 ether;
    deadline = now + durationInMinutes * 1 minutes;
    price = 1e-4 * 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 public{
    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 public{
    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 public{
    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;
  • Rather than reinventing the wheel i would suggest you base your Crowdsale on the Open Zeppelin contracts, all you need is already there and properly tested. – Julien Jan 15 '18 at 0:16
  • Thanks Julien. This is really helpful but has now raised another question of mine. Where is the my ether wallet compiler storing the contract file because when I try to import related code from the Zeppelin library it cannot locate the directory. – Richard Hurley Jan 26 '18 at 21:46
  • you need to install all the Zeppelin files with NPM by doing npm install zeppelin-solidity, it will create a 'zeppelin-solidity' folder with them inside the node_modules folder. – Julien Jan 31 '18 at 1:51

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.