0

hi this is my contract

pragma solidity ^0.4.2;


contract TrusteeFinTestToken {
    /* Public variables of the token */
    string public standard = 'Token 0.1';
    string public name;
    string public symbol;
    uint8 public decimals;
    uint256 ethtstraito = 100;
    uint256 public totalSupply;

    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

    /* This generates a public event on the blockchain that will notify clients */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function TrusteeFinTestToken(
        uint256 initialSupply,
        string tokenName,
        uint8 decimalUnits,
        string tokenSymbol
        ) {

        initialSupply = 10000000000 ;
        tokenName ="TrustiFintraTest";
        tokenSymbol="TFINT";
        decimalUnits = 4;
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
        totalSupply = initialSupply;                        // Update total supply
        name = tokenName;                                   // Set the name for display purposes
        symbol = tokenSymbol;                               // Set the symbol for display purposes
        decimals = decimalUnits;                            // Amount of decimals for display purposes
        transferFromUser(this,initialSupply);

        }

    function setETHTokenTokenRaito(uint256 newRaito){
        ethtstraito = newRaito;
    }

    function() public payable{

        transferFromContract(msg.sender,msg.value/1000000000000000000*ethtstraito*10000);
    }

    function contractETHbalance() constant returns(uint256) {
        return this.balance;
        }

    /* Send coins */
    function transferFromUser(address _to, uint256 _value) {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
        Transfer(msg.sender, _to, _value);                   // Notify anyone listening that this transfer took place
    }
        function transferFromContract(address _to, uint256 _value) {
        if (balanceOf[this] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[this] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
        Transfer(this, _to, _value);                   // Notify anyone listening that this transfer took place
    }
}

when i send deposit 1 eth to fallback its return me 100 Tokens but when i deposit 1.1 ehter or other float value contract doesn't work and show no errors what is the problem

0

Multiply first, divide last (or you lose information).

Change this:

msg.value/1000000000000000000*ethtstraito*10000

To this:

msg.value*ethtstraito*10000/1000000000000000000

Or better yet, to this:

msg.value*ethtstraito/100000000000000

Or even better yet, to this:

mul(msg.value,ethtstraito)/100000000000000

With mul being:

function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
        return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
}
  • Or better even better yet, to this: mul(msg.value,ethtstraito) * (uint256(10) ** decimals) / (uint256(10) ** 18) in case the amount of decimals changes later on. – Jesse Busman May 18 '18 at 19:02
  • @JesseBusman: That will not compile. Also, even after you fix the compilation errors, with this code there's a higher chance of arithmetic overflow (hence failure of the operation). You could argue that the chances aren't that much higher, still, weighing all the factors, I wouldn't call it "better" just for the use of decimals (when the chances of an overflow are slightly higher due to that). – goodvibration May 18 '18 at 19:05
  • Tnx A lot It's Work – Danial Qsk May 19 '18 at 9:28

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.