My smart contract code is here

// To specify what version of compiler this code will compile with
pragma solidity ^0.4.11;

contract tokensale {

  // Declaring the maximum number of tokens available for sale.
  uint public maxTokens = 10000;

  // The number of tokens you can buy for one wei (Solidity converts all unitless values from ethers to wei).
  // We will keep the default currency to be wei. It will get clearer.
  uint public tokenSwap = 10;

  // This variable, initially set to zero,  stores the number of tokens that have been bought, i.e it will
  // get incremented each time someone buys tokens.
  uint public supply = 0;

  // mapping is an equivalent to associative array. We store token balances corresponding
  // to each address that buys tokens in this mapping.
  mapping (address => uint) balance;

  // A modifier checks if a condition is met before executing a function.
  // This  modifer checks if there are tokens available before the buyTokens function is called.
  modifier isTokenAvailable () {
    require (msg.value*tokenSwap + supply <= maxTokens);     // require checks the condition and the function that
    _;                                                       // follows, denoted by the '_', is only run if the condition
  }                                                                      // is satisfied.

  // A function to check the ether balance of any address.

  // To check the token balance of any address. The external keyword allows the function
  // to be called from outside the contract. If no keyword is specified, all functions are external
  // by default..
  function balanceOf (address tokenHolder) external constant returns (uint) {
    return balance[tokenHolder];

  // A function to buy tokens accesible by any address
  // The payable keyword allows the contract to accept ethers
  // from the transactor. The ethers to be deposited is entered as msg.value
  // (which will get clearer when we will call the functions in browser-solidity)
  // and the corresponding tokens are stored in balance[msg.sender] mapping.
  // underflows and overflows are security consideration which are
  // not checked in the process. But lets not worry about them for now.

  function buyTokens () external
  isTokenAvailable {
    uint tokensAmount = msg.value * tokenSwap;    // At this point, msg.value is in wei.
    balance [msg.sender] += tokensAmount;
    supply += tokensAmount;

When I deploying this code it shows me the error

it seems this transaction will fail. If you submit it it may consume all the gas you send

I'm not sure what is wrong in this contract code. I have tried with different gas amount lower to higher but still same error.

Here is the output enter image description here

  • So does the transaction fail if you execute it? What is the value you are passing? Mar 8, 2018 at 12:02
  • @MuhammadAltabba No, Contract is deploying successfully but when I try to send some ETH on the contract address then the transaction getting failed
    – vikujangid
    Mar 8, 2018 at 12:07
  • And why it is showing the RED error on deployment time ?
    – vikujangid
    Mar 8, 2018 at 12:09
  • Can you share the deployment code (or how exactly do you make the deployment - is this Mist?) Mar 8, 2018 at 12:11
  • @MuhammadAltabba My code is already in my question. Yes I'm using MIST
    – vikujangid
    Mar 8, 2018 at 12:12

1 Answer 1

  1. Your contract is missing the constructor function.

  2. If you send along some ETH with the contract creation, your constructor needs to be marked payable.

  3. Use this code only for learning purpose, a good practice is using the SafeMath library for adding values to prevent overflows. In this case though this should not be that relevant.

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.