2

how the Smart contract decide how much token need to be send on every 1 Ethereum ? didn't found any which define such function, anyone ?

2

You need to specify the price of your token. Then when you are selling tokens in exchange for ether you divide the amount of ether by the price of each token. However, take into consideration that solidity does not have a representation of float numbers so 10/3 is 3, not 3.333333. To have a decimal representation you define how many decimal points you want and multiply your values by 10**decimal.

Here an example:

contract MyToken{
    uint public price;
    uint public decimals;
    uint mult_dec;
    mapping (address => uint) public balanceOf;

    function MyToken() public {
        price = 100000000000000000; //price in wei 1 Token = 0.1ETH
        decimals = 18;
        mult_dec = 10**decimals;
    }

    // people buy by sending ether to your contract
    function buyTokens() public payable{
        var amount = msg.value.mul(mult_dec).div(price);
        // do the other stuff here, i,e. update balances
    }
}

Remember that the balances are then multiplied by 10**decimals. If you use mist and you "watch" the contract defining decimals, mist will show the correct number of tokens.

I hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.