1

I have been testing the code and for some reason I am having a block. The error is described as "the constructor should be payable if you send value". The debugger points to " uint256 weiAmount = msg.value; ". I've tried putting constructor info in the function, I've tried to change the validation, etc. Is there something stupid I am missing. Thank you, Pete

function buyTokens(address beneficiary) public payable {

  //tokens not to be sent to 0x0
  require(beneficiary != 0x0);

  if(hasEnded() && !isHardCapReached)
  {
      if (!isSoftCapReached)
        refundToBuyers = true;
      burnRemainingTokens();
      beneficiary.transfer(msg.value);
  }
  else
  {
    //the purchase should be within duration and non zero
    require(validPurchase());

    // amount sent by the user
   uint256 weiAmount = msg.value;

    // calculate token amount to be sold
    uint256 tokens = weiAmount.mul(rate);

    require (tokens>=500 * 10 ** 18);

    //Determine bonus
    uint bonus = determineBonus(tokens);
    tokens = tokens.add(bonus);

    //can't sale tokens more than 200000000 tokens
    require(tokens_sold + tokens <= maxTokensForSale * 10 ** 18);

    //30% of the tokens being sold are being accumulated for the Cloudexchange team
    updateTokensForCloudexchangeTeam(tokens);

    weiRaised = weiRaised.add(weiAmount);


    if (weiRaised >= softCap * 10 ** 18 && !isSoftCapReached)
    {
      isSoftCapReached = true;
    }

    if (weiRaised >= hardCap * 10 ** 18 && !isHardCapReached)
      isHardCapReached = true;

    token.mint(wallet, beneficiary, tokens);

    uint olderAmount = usersThatBoughtCXT[beneficiary];
    usersThatBoughtCXT[beneficiary] = weiAmount + olderAmount;

    emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

    tokens_sold = tokens_sold.add(tokens);
    forwardFunds();
  }
 }

  // send ether to the fund collection wallet
  function forwardFunds() internal {
    wallet.transfer(msg.value);
  }

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

  // @return true if crowdsale event has ended
  function hasEnded() public constant returns (bool) {
    return now > endTime;
  }
  • 1
    What is the code for the constructor? – Raghav Sood Sep 2 '18 at 16:18
  • Hi Rag, there was no constructor for uint256 weiAmount = msg.value; it says that it expects one. I've looked at hundreds of contracts and never seen any one add one for weiAmount. – pete maccormack Sep 2 '18 at 17:05
  • In that case, it sounds like you are using a default constructor, and sending ether while deploying the contract. Simply deploy it with a 0-value tx – Raghav Sood Sep 2 '18 at 17:43
  • Did you mean "fallback function"? All functions calling buyTokens() should also be made payable. – Ismael Sep 4 '18 at 20:56

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.