0

I am writing a PreICO Smart contract. I got a code snippet which I am trying to understand. I am stuck in function buyXYZTokens function which is throwing error. Here is the code snippet:

Contract XYZ is StandardToken, Ownable{
string public name = "XYZ";
string public symbol = "X";
string public standard = "X v1.0";
uint256 public decimals = 18;

modifier inActivePeriod() {
    require((preSaleStart < now && now <= preSaleEnd));
    _;
}
 function XYZ( uint256 _initialsupply, address _escrow, uint _preSaleStart, uint _preSaleEnd) {
    require(_escrow != 0x0);
    require(_preSaleStart < _preSaleEnd);       
    escrow = _escrow;
    totalSupply_=_initialsupply;
    balances[escrow] = _initialsupply;
}
    function() inActivePeriod payable{
    buyXYZTokens(msg.sender);
}
function buyXYZTokens(address _sender) inActivePeriod payable{
    require(_sender!=0x0);
    require(msg.value>=minTransactionAmount);
    uint256 weiCount = msg.value;
    amountRaised = amountRaised.add(weiCount);
    uint256 tokens = weiCount.mul(rate);
    tokens += getBonus(tokens);
    tokenReserve(_sender, tokens);
    forwardFunds();
} 
  function getBonus(uint256 _tokens) constant returns (uint256 bonus) {
        require(_tokens != 0);

        if (preSaleStart <= now && now < preSaleStart + 1 days) {
              return _tokens.div(2);
        } else if (preSaleStart + 1 days <= now && now < preSaleStart + 2 days ) {
              return _tokens.div(4);
        } else if (preSaleStart + 2 days <= now && now < preSaleStart + 3 days ) {
              return _tokens.div(10);
        }

        return 0;
    }

    function tokenReserve(address _to, uint256 _value) internal returns (bool) {
        balances[_to] = balances[_to].add(_value);
        Transfer(msg.sender, _to, _value);
        return true;
    }
    function forwardFunds() internal {
        escrow.transfer(msg.value);
    }

I used these parameters to deploy contract: I got this error: I am not getting what this fallback function used for and why buyXYZTokens function throwing me error?

5
  • Please show all code, as well as, the error so it can be addressed. Thanks! 👍
    – foba
    Commented Nov 28, 2018 at 10:55
  • Is the error from contract deployment or when calling function buyXYZTokens ? Commented Nov 28, 2018 at 11:51
  • I got the error when I call the function buyXYZTokens.@Lauri Peltonen
    – crypto S.
    Commented Nov 28, 2018 at 11:53
  • Your code has this check: require(msg.value>=minTransactionAmount);. What is the minTransactionAmount and are you sending enough to pass that check? Commented Nov 28, 2018 at 11:59
  • I commented both the require statements of buyXYZTokens function but still I am getting the same error.
    – crypto S.
    Commented Nov 28, 2018 at 12:17

1 Answer 1

1

As your function call is reverting, it has to be caused by one of the require statements somewhere in the contracts.

If you are really deploying your contract with the parameters shown in the picture, the problem occurs (at least) in your inActivePeriod modifier which makes sure that the current block number is between the values _preSaleStart and _preSaleEnd.

After you fix that problem, you will probably face other similar problems if you do not fully understand what the code is doing (or what you are doing). One way to try to figure out this kind of reversion problems is by commenting out require statements until you find the one which causes the current problem - you can for example start by commenting them all out and enabling them one by one to see where you get issues. It's not a very fast method but it works.

2
  • When I removed the inActivePeriod modifier BuyXYZTokens function is working. But I want it to test it with` inActivePeriod` modifier what should I do for that.
    – crypto S.
    Commented Nov 29, 2018 at 4:41
  • Deploy the contract with correct _prepaleStart and preSaleEnd parameters. Please post a new question if you have troubles with some of the details. Commented Nov 29, 2018 at 6:09

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.