0

I have two contracts one for defining token and other for crowdsale. I am using remix ide.

pragma solidity >=0.4.22 <0.6.0;

contract owned {
  address public owner;
  constructor () public {
    owner=msg.sender;
  }

  modifier onlyOwner {
    require (msg.sender == owner);
    _;
  }
}

contract EdCoin is owned {
  string public name;
  string public symbol;
  uint8 public decimals = 18;
  uint256 public totalSupply;

  mapping (address => bool) public frozenAccount;
  mapping (address => uint256) public balanceOf;

  event Transfer (address indexed from, address indexed to, uint256 value);
  event FrozenFunds (address target, bool frozen);
  event Burn(address indexed from, uint256 value);

  constructor(
    uint256 initialSupply,
    string memory tokenName,
    string memory tokenSymbol
  ) public {
    totalSupply = initialSupply * 10 ** uint256(decimals);  
    balanceOf[msg.sender] = totalSupply;                
    name = tokenName;                                   
    symbol = tokenSymbol;                               
  }

  function _transfer (address _from, address _to, uint _value) internal {
    require(_to != address(0x0));
    require(!frozenAccount[_to]);
    require(!frozenAccount[_from]);
    require(balanceOf[_from] >= _value);
    require(balanceOf[_to] + _value >= balanceOf[_to]);
    uint previousBalances = balanceOf[_from] + balanceOf[_to];
    balanceOf[_from] -= _value;
    balanceOf[_to] += _value;
    emit Transfer(_from, _to, _value);
    assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
  }

  function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
    require (_to != address(0x0));                          
    require (balanceOf[_from] >= _value);
    require (balanceOf[_to] + _value >= balanceOf[_to]);
    require(!frozenAccount[_from]);
    require(!frozenAccount[_to]);                           
    _transfer(_from, _to, _value);
    return true;
  }

  function transferN (address _to, uint256 _value) public returns (bool success) {
    require (_to != address(0x0));  
    require(balanceOf[msg.sender] >= _value);

    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value;

    emit Transfer(msg.sender, _to, _value);

    return true;
  }

  function burn(uint256 _value) public returns (bool success) {
    require(balanceOf[msg.sender] >= _value);   
    balanceOf[msg.sender] -= _value;            
    totalSupply -= _value;                      
    emit Burn(msg.sender, _value);
    return true;
  }

  function burnFrom(address _from, uint256 _value) public returns (bool success) {
    require(balanceOf[_from] >= _value);                
    balanceOf[_from] -= _value;                         
    totalSupply -= _value;                              
    emit Burn(_from, _value);
    return true;
  }

  function freezeAccount(address target, bool freeze) public {
    frozenAccount[target] = freeze;
    emit FrozenFunds(target, freeze);
  }

  function distributeToken(address[] memory addresses, uint256[] memory _value) public returns (bool success) {
    for( uint256 i=1;i<addresses.length;i++){
      _transfer(addresses[0], addresses[i], _value[i]);
    }
    return true;
  }

  function mintToken(address target, uint256 mintedAmount) onlyOwner public {
    balanceOf[target] += mintedAmount;
    totalSupply += mintedAmount;
    emit Transfer(address(0), address(this), mintedAmount);
    emit Transfer(address(this), target, mintedAmount);
  }
} 

CrowdSale contract:

pragma solidity >=0.4.22 <0.6.0;

import "./Ed.sol";

contract DappTokenSale {
  address admin;
  EdCoin public tokenContract;
  uint256 public tokenPrice;
  uint256 public tokensSold;

  event Sell(address _buyer, uint256 _amount);

  constructor(EdCoin _tokenContract, uint256 _tokenPrice) public {
    admin = msg.sender;
    tokenContract = _tokenContract;
    tokenPrice = _tokenPrice;
  }

  function multiply(uint x, uint y) internal pure returns (uint z) {
    require(y == 0 || (z = x * y) / y == x);
  }

  function buyTokens(uint256 _numberOfTokens) public payable {
    require(msg.value == multiply(_numberOfTokens, tokenPrice));
    require(tokenContract.balanceOf(address(this)) >= _numberOfTokens);
    require(tokenContract.transferN(msg.sender, _numberOfTokens));

    tokensSold += _numberOfTokens;

    emit Sell(msg.sender, _numberOfTokens);
  }
}

Both contracts have been successfully deployed but while trying to buy tokens it throws error below

Gas estimation failed
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
gas required exceeds allowance or always failing transaction

I am using RopstenTest network for testing purposes.

4
  • Your contract seems working on Remix VM. Are you sure that you transferred the tokens to DappTokenSale contract? As an alternative, you can directly deploy contract in your TokenSale contract using tokenContract = new Edcoin(params) Apr 18, 2019 at 7:22
  • No i haven't transferred tokens to Sale contract. How to do it? I deployed Token contract, supplied its address to sale contract while deploying it.
    – Saeedi
    Apr 18, 2019 at 7:33
  • make following call to Token Contract : >>>>>>transferN(tokenSaleAddress, totalSupply) from the account that deployed token contract. Apr 19, 2019 at 11:06
  • Transferred it but the error is still persisting
    – Saeedi
    Apr 22, 2019 at 5:22

2 Answers 2

0

There are several require statements in your code and it seems that some of them fail. Try adding error messages to your require statements to known which one actually fails:

function multiply(uint x, uint y) internal pure returns (uint z) {
  require(y == 0 || (z = x * y) / y == x, "multiply");
}

function buyTokens(uint256 _numberOfTokens) public payable {
  require(msg.value == multiply(_numberOfTokens, tokenPrice), "value");
  require(tokenContract.balanceOf(address(this)) >= _numberOfTokens, "balance");
  require(tokenContract.transferN(msg.sender, _numberOfTokens), "transfer");

  tokensSold += _numberOfTokens;

  emit Sell(msg.sender, _numberOfTokens);
}
0

your contracts are perfectly written but i think you do not understand basic token operations.

First of all your token contract have no tokens to give some one as you transferred total supply to your Owner in token contract.

Second: the statement in TransferN function

require(balanceOf[msg.sender] >= _value);

in this case msg.sender is the person who is trying to buy how is balance can be more than the value requested?

If you want to sell your tokens this way you should remove above mentioned require statement and call _transfer from within your transferN. Its not a good approach but it will work.Learn how token work.

function transferN (address _to, uint256 _value) public returns (bool success) {
    require (_to != address(0x0));  

    _transfer(owner, _to, _value);

    emit Transfer(owmer, _to, _value);

    return true;
}

Again it is not right approach.

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.