Crowd-Sale Smart Contract – Troubleshooting Non-Functioning Buy Token Function

contract-debuggingcontract-developmentcrowdsaleicosolidity

I am new to ethereum block-chain. I created a crowd-sale and ERC20 smart contract and deployed on rinkeby test network. The ERC20 token transfer function working fine.

But when I check functionality of buy token function. So it's run successfully. But when I check it's output on https://rinkeby.etherscan.io/. So it's shows 0 token transfer. And it's don't show token price on wallet.

Crowd Sale smart contract code like this:-

pragma solidity ^0.4.2;

import "./DappToken.sol";

contract DappTokenSale {
    address admin;
    DappToken public tokenContract;
    uint256 public tokenPrice;
    uint256 public tokensSold;
    uint256 public decimals;


    event Sell(address _buyer, uint256 _amount);

    function DappTokenSale(DappToken _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(address _receiver, uint256 _amount) public payable {
        _amount = msg.value;
        require(_receiver != address(0));
        require(_amount > 0);
        uint256 tokensToBuy = multiply(_amount, (10 ** decimals)) / 1 ether * tokenPrice;
        require(tokenContract.transfer(msg.sender, tokensToBuy));
        tokensSold += _amount;

        emit Sell(msg.sender, tokensToBuy);
    }

  // Ending Token DappTokenSale
    function endSale() public {
            // Require admin
            require (msg.sender == admin);

             // Transfer remaing DappToken to admin
            require(tokenContract.transfer(admin,tokenContract.balanceOf(this)));


            // Destroy Contract
            selfdestruct(admin);
    }
}

DappToken Solidity code:-

pragma solidity ^0.4.24;

/**
 * The contractName contract does this and that...
 */

contract DappToken {
    // Name
    string public name = "DappToken";
    // Symbol
    string public symbol = 'DAPP';
    //standard
    string public standard = 'DApp Token v1.0';
    //Decimals
    uint256 public decimals = 18;
    //token price
    uint256 public tokenPrice = 2000; //1 eth = 2000 tokens

    uint256 public totalSupply;

    event Transfer(
        address indexed _form,
        address indexed _to,
        uint256 _value
        );

    event Approval(
            address indexed _owner,
            address indexed _spender,
            uint256 _value
        );


    mapping(address => uint256) public balanceOf;
    mapping(address => mapping (address => uint256)) public allowance;


//  function DappToken (uint256 _intialSupply) public {
//      balanceOf[msg.sender] = _intialSupply;
//      totalSupply = _intialSupply;
//      //allocate the intial supply
//
//  }
    constructor(uint256 _intialSupply) public
            {
                balanceOf[msg.sender] = _intialSupply;
                totalSupply = _intialSupply;
            }

    //Transfar
    function transfer(address _to,uint256 _value) public returns (bool success){
    // Exception if account does not enough
    require(balanceOf[msg.sender] >= _value);
    // Transfar the balance
    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value; 

    // Transfar Event
    emit Transfer(msg.sender,_to,_value);

    // Return a boolean
    return true;
    } 

    // approve
    function approve(address _spender,uint256 _value) public returns (bool success){
        //allowence
        allowance[msg.sender][_spender] = _value;

        // Approve event
        emit Approval(msg.sender,_spender,_value);
             return true;

    }
    // transfer form
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_value <= balanceOf[_from]);
        require(_value <= allowance[_from][msg.sender]);
        // change the balance
        balanceOf[_from] -= _value;

        //update the balance
       balanceOf[_to] += _value; 

       allowance[_from][msg.sender] -= _value;

       emit Transfer(_from,_to,_value);


        return true;
    }
}

Actually I want buy token functionality like this:-

  1. User input token amount(3 ethers).
  2. I set token price 1 ether= 2000 tokens.
  3. So when user input 3 ethers So 6000 tokens transfer in his account.

So my query is:-

Query-1 :-Why Buy token function does not send 6000 tokens in receiver account??

Query-1:-Where I am doing wrong in smart contract code?

Best Answer

I think this should be your function:

function buyTokens(address _receiver) public payable { 
uint256 _amount = msg.value; 
require(_receiver != address(0)); require(_amount > 0); 
uint256 tokensToBuy = multiply(_amount, (10 * decimals)) / 1 ether tokenPrice;
require(tokenContract.transfer(_receiver, tokensToBuy)); 
tokensSold += _amount; 

emit Sell(msg.sender, tokensToBuy); }

Edit: You can check the amount of tokens received by the end-user via Metamask.

Related Topic