1

when I am trying to call payble function in embark it is showing gas limit exceeds. Please tell me where I am wrong?
///////////////// Index.js:
$('#buy button').click(function() { var buyAmount = $('#buy .amountBuy').val(); console.log(add); currentToken.buy({value:buyAmount}).then(function() { $('#buy .result').html('Done!'); });; }) ////////////////index.html: Buy Tokens

                <div class="custom-form">
                    <h2 class="custom-form-heading">Enter Details <a class="form-popup-close back-btn">Close</a></h2>

                    <div class="custom-form-group">
                        <input class="amountBuy" placeholder="enter amount to buy in wei" />
                    </div>
                    <div class="custom-form-group">
                        <button class="form-group-btn">Buy</button>
                    </div>
                    <div class="result"></div>
                </div>
            </div>
        </div>                                                                                                                                                                                                                             

//////////////////// solidity fnq: /// @notice Buy tokens from contract by sending ether function buy() payable public { require(msg.value!=0); require(buyPrice!=0); uint exceededEthers; uint amount = msg.value; // msg.value will be in wei.
(amount, exceededEthers) = calculateTokens(amount, buyPrice, decimals); require(amount!=0); _transfer(this, msg.sender, amount); // makes the transfers. msg.sender.transfer(exceededEthers);// sends exceeded ether to the seller.

   // addUsers(msg.sender);

    if (users.length>0){
            uint count=0;
        for (uint a=0;a<users.length;a++)
        {
        if (users[a]==msg.sender){
        count=count+1;
        }
        }
        if (count==0){
            users.push(msg.sender);
        }

    }
    else{
        users.push(msg.sender);
    }


}                                                                                                                                                                                       

1 Answer 1

0

You have to check the gaslimit of the tool you are working. Are you using testrpc or testnet?

And here is a tipo for you. Loop is always trick to use with Ethereum. You should avoid using it because if your array is too big, you will get out of gas anyway and your contract may work at first, but in the future you may get in trouble. So, avoid use of loops. Use loops only if you know for sure, 100% sure, that it has a maximun limit size that will not get you in "out of gas" exception.

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.