0

I have a code below and when I am transferring 1,695,000,000 to a particular address, so actually 0.000000001695 are being transferred. Why is it so? Does that mean to transfer 1.695 billion tokens I need to put 18 leading 0s. Here's the code:

function transfer(address _to, uint256 _amount) public returns (bool success) {
    require(_to != address(0));
    require(balances[msg.sender] >= _amount && _amount > 0
        && balances[_to].add(_amount) > balances[_to]);

    // SafeMath.sub will throw if there is not enough balance.
    balances[msg.sender] = balances[msg.sender].sub(_amount);
    balances[_to] = balances[_to].add(_amount);
    Transfer(msg.sender, _to, _amount);
    return true;
  }


contract SomeToken is MintableToken, BurnableToken {
     string public name ;
     string public symbol ;
     uint8 public decimals = 18 ;

     /**
     *@dev users sending ether to this contract will be reverted. Any ether sent to the contract will be sent back to the caller
     */
     function ()public payable {
         revert();
     }

     /**
     * @dev Constructor function to initialize the initial supply of token to the creator of the contract
     * @param initialSupply The initial supply of tokens which will be fixed through out
     * @param tokenName The name of the token
     * @param tokenSymbol The symbol of the token
     */
     function SomeToken(
            uint256 initialSupply,
            string tokenName,
            string tokenSymbol
         ) public {
         totalSupply = initialSupply.mul( 10 ** uint256(decimals)); //Update total supply with the decimal amount
         name = tokenName;
         symbol = tokenSymbol;
         balances[msg.sender] = totalSupply;

         //Emitting transfer event since assigning all tokens to the creator also corresponds to the transfer of tokens to the creator
         Transfer(address(0), msg.sender, totalSupply);
     }
}
  • But that is throwing an error. Suppose my total supply is 5,000,000,000 and when I am trying to transfer to 2 billion with 18 leading 0's, its showing an error in the remix "SomeToken.transfer errored: Error encoding arguments: Error: Assertion failed ". – Prateek Singhania Feb 27 '18 at 12:09
  • you have to add this line tokenCount ** uint256(decimals) – Crissi Mariam Robert Feb 27 '18 at 12:11
  • Instead of adding leading zeros,update your contract with above line for the tokenCount – Crissi Mariam Robert Feb 27 '18 at 12:12
  • Can you please brief a little more where this line will be added. – Prateek Singhania Feb 27 '18 at 12:15
  • Update the code with transfer function in your contract – Crissi Mariam Robert Feb 27 '18 at 12:26
0

Your tokens are divided in X decimal places (18 in your case). By default (and by convention) transfers are based on the lowest amount possible so that there wouldn't be decimal numbers in transfers. Therefore transferring an a mount of 1 means trasferring 0.000000000000000001 (17 zeros) tokens.

I'd imagine this is mostly so that we don't need to deal with fractional numbers which would require more storage and are more difficult for programs to understand. Ether works the same way.

0

When you call the function to transfer the coins (transfer), you need to multiply the parameter 'amount' by 10**uint(decimals). That will do.

This is tricky as this is necessary only if you call the transfer function directly. If you do a transaction from your wallet to any other address you do not need to do this because the wallet will 'know' that you are using a particular number of decimals in your contract. The function 'transfer' alone does not know this.

Note: the transfer function should be inside the contract's body.

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.