0

Error: The contract code couldn't be stored, please check your gas amount. (I also tried to add gas in truffle.js but didn't get any success)

pragma solidity ^0.4.21;


contract Ownable {
  address public owner;
  event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  constructor() public {
    owner = msg.sender;
  }
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
  function transferOwnership(address newOwner) public onlyOwner {
    require(newOwner != address(0));
    emit OwnershipTransferred(owner, newOwner);
    owner = newOwner;
  }
}

// ######################################################################

library SafeMath {
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
      return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
  }
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a / b;
    return c;
  }
  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }
  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
  }
}

// ######################################################################

contract ERC20Basic {
  function totalSupply() public view returns (uint256);
  function balanceOf(address who) public view returns (uint256);
  function transfer(address to, uint256 value) public returns (bool);
  event Transfer(address indexed from, address indexed to, uint256 value);
}

// ######################################################################

contract BasicToken is ERC20Basic {
  using SafeMath for uint256;

  mapping(address => uint256) balances;

  uint256 totalSupply_;

  function totalSupply() public view returns (uint256) {
    return totalSupply_;
  }

  function transfer(address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[msg.sender]);

    // SafeMath.sub will throw if there is not enough balance.
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    emit Transfer(msg.sender, _to, _value);
    return true;
  }

  function balanceOf(address _owner) public view returns (uint256 balance) {
    return balances[_owner];
  }

}

// ######################################################################

contract ERC20 is ERC20Basic {
  function allowance(address owner, address spender) public view returns (uint256);
  function transferFrom(address from, address to, uint256 value) public returns (bool);
  function approve(address spender, uint256 value) public returns (bool);
  event Approval(address indexed owner, address indexed spender, uint256 value);
}

// ######################################################################

contract StandardToken is ERC20, BasicToken {

  mapping (address => mapping (address => uint256)) internal allowed;

  function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[_from]);
    require(_value <= allowed[_from][msg.sender]);

    balances[_from] = balances[_from].sub(_value);
    balances[_to] = balances[_to].add(_value);
    allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
    emit Transfer(_from, _to, _value);
    return true;
  }

  function approve(address _spender, uint256 _value) public returns (bool) {
    allowed[msg.sender][_spender] = _value;
    emit Approval(msg.sender, _spender, _value);
    return true;
  }

  function allowance(address _owner, address _spender) public view returns (uint256) {
    return allowed[_owner][_spender];
  }

  function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
    allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
    emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

  function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
    uint oldValue = allowed[msg.sender][_spender];
    if (_subtractedValue > oldValue) {
      allowed[msg.sender][_spender] = 0;
    } else {
      allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
    }
    emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

}

// ######################################################################

contract DeveloperToken is StandardToken {
  string public constant name = "DeveloperToken";
  string public constant symbol = "DEV";
  uint256 public constant decimals = 18;
}

// ######################################################################

contract DeveloperCrowdsale is Ownable {
  using SafeMath for uint256;

  // ============= Token Distribution ================
  uint256 public maxTokens = 100000000 *(10 ** 18);
  uint256 public tokensForEcosystem = 80000000 *(10 ** 18);
  uint256 public tokensForBounty = 4000000 *(10 ** 18);
  uint256 public totalTokensForSale = 15000000 *(10 ** 18);
  uint256 public tokenForAirdrop = 1000000 *(10 ** 18);

  ERC20 public token;

  address public wallet;

  uint256 public rate;

  uint256 public weiRaised;

  uint256 public cap;
  uint256 public goal;

  // start and end timestamps where investments are allowed (both inclusive)
  uint256 public startTime;
  uint256 public endTime;

  bool public mintingFinished = false;

  mapping(address => uint256) balances;

  event Mint(address indexed to, uint256 amount);
  event MintFinished();
  event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);

  constructor() public {
    rate = 1000;
    wallet = msg.sender;
    token = new DeveloperToken();
    startTime = 1527186660000;  // new Date('05/25/2018 00:01').getTime();
    endTime = 1529951340000; //  new Date('06/25/2018 23:59').getTime();
    cap = 42500 *(10 ** 18);
    goal = 7500 *(10 ** 18);
  }

  function () external payable {
    uint256 tokensThatWillBeMintedAfterPurchase = msg.value.mul(rate);
    require(token.totalSupply() + tokensThatWillBeMintedAfterPurchase < totalTokensForSale);
    buyTokens(msg.sender);
  }

  // ====================== Crowdsale Price Management =================
  function setCrowdsalePrice() public onlyOwner {
      if (weiRaised <= 2500000 *(10 ** 18)) {
        setCurrentRate(1000);
      } else if (weiRaised <= 5000000 *(10 ** 18)) {
        setCurrentRate(500);
      } else if (weiRaised <= 10000000 *(10 ** 18)) {
        setCurrentRate(333);
      } else if (weiRaised <= 15000000 *(10 ** 18)) {
        setCurrentRate(250);
      }
  }

  // Change the current rate
  function setCurrentRate(uint256 _rate) private {
      rate = _rate;
  }

  function buyTokens(address _beneficiary) public payable {
    require(validPurchase());

    uint256 weiAmount = msg.value;

    // calculate token amount to be created
    uint256 tokens = _getTokenAmount(weiAmount);

    // update state
    weiRaised = weiRaised.add(weiAmount);

    _processPurchase(_beneficiary, tokens);
    emit TokenPurchase(msg.sender, _beneficiary, weiAmount, tokens);

    _forwardFunds();
  }

  function _deliverTokens(address _beneficiary, uint256 _tokenAmount) internal {
    token.transfer(_beneficiary, _tokenAmount);

  }

  function _processPurchase(address _beneficiary, uint256 _tokenAmount) internal {
    _deliverTokens(_beneficiary, _tokenAmount);
  }

  function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
    return _weiAmount.mul(rate);
  }

  function _forwardFunds() internal {
    token.totalSupply() + msg.value.mul(rate);
    wallet.transfer(msg.value);
  }

  // Finish: Mint Extra Tokens as needed before finalizing the Crowdsale.
  function finish(address _airdrop, address _ecosystemFund, address _bountyFund) public onlyOwner {
    require(!mintingFinished);
    require(hasEnded());
    uint256 alreadyMinted = token.totalSupply();
    require(alreadyMinted < maxTokens);

    uint256 unsoldTokens = totalTokensForSale - alreadyMinted;
    if (unsoldTokens > 0) {
      tokensForEcosystem = tokensForEcosystem + unsoldTokens;
    }

    token.totalSupply().add(tokensForEcosystem);
    token.totalSupply().add(tokensForBounty);
    token.totalSupply().add(tokenForAirdrop);

    balances[_airdrop].add(tokenForAirdrop);
    balances[_ecosystemFund].add(tokensForEcosystem);
    balances[_bountyFund].add(tokensForBounty);
    mintingFinished = true ;
  }

  // @return true if the transaction can buy tokens
  function validPurchase() internal view returns (bool) {
    bool withinCap = weiRaised.add(msg.value) <= cap;
    bool withinPeriod = now >= startTime && now <= endTime;
    bool nonZeroPurchase = msg.value != 0;
    return withinPeriod && nonZeroPurchase && withinCap;
  }

  // @return true if crowdsale event has ended
  function hasEnded() public view returns (bool) {
    bool capReached = weiRaised >= cap;
    return now > endTime || capReached;
  }

}

This is my migration file :

var SafeMath = artifacts.require("./SafeMath.sol");
var DeveloperCrowdsale =  artifacts.require("./DeveloperCrowdsale.sol");


module.exports = function(deployer) {
  deployer.deploy(SafeMath);
  deployer.link(SafeMath, DeveloperCrowdsale);
  deployer.deploy(DeveloperCrowdsale);
};

};

This is my truffle.js :

module.exports = {
    networks: {
         development: {
              host: "localhost",
              port: 8545,
              network_id: "*" // Match any network id
            }
       }
};
15
  • 1
    If you are using ganache-cli ( saw your problem on Gitter ) try increasing the gas limit to see if it is actually a gas problem. run ganache-cli with -l 9999999999 option for example. Commented Apr 19, 2018 at 13:44
  • 1
    Why are you launching individual contracts for contracts you want to inherit from? Why not just import them?
    – ReyHaynes
    Commented Apr 19, 2018 at 13:47
  • 1
    @chiragmaliwal Web3 0.x version which is used by truffle uses the gas problem as a generic error, technically It can mean anything. my advice is try to be sure of the variables you pass to your contract's constructors and try to isolate the contract deployment generating the error origin error. If you can make a repo on github of your truffle project and send the link i'll see it myself. Commented Apr 19, 2018 at 14:07
  • 1
    @chiragmaliwal You only need to deploy DeveloperCrowdsale within your migration script. deployer.deploy(DeveloperCrowdsale); Commented Apr 19, 2018 at 19:28
  • 2
    @chiragmaliwal since you import them, they will be included in the DeveloperCrowdsale deployment (along with any other solidity file you inherit from) Commented Apr 19, 2018 at 20:23

2 Answers 2

1

You do not need to deploy all the contracts, in your case only SafeMath library and the DeveloperCrowdsale contract.

module.exports = function(deployer) {
  deployer.deploy(SafeMath);
  deployer.link(SafeMath, DeveloperCrowdsale);
  deployer.deploy(DeveloperCrowdsale);
};
2
  • Okay ! Got your point. contract deployed successfully , but when I tried to add tokens on metamask, I am not getting the token details so looks like something wrong in deployment. please have a look : ethereum.stackexchange.com/questions/47208/… Commented May 1, 2018 at 18:22
  • Mention not, I am very close to my answer. I will accept your answer. Commented May 1, 2018 at 18:28
-1

edit: Learned in the newer versions it should be called constructor. In your case it's misspelled

2
  • This is just typo and I updated the code. Commented May 1, 2018 at 17:25
  • this is not the reason of error. Commented May 1, 2018 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.