Solidität kann nicht abgelagert werden 1,1 (Float-Ether-Wert)

Hallo, das ist mein Vertrag

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
    }
}

Wenn ich eine Einzahlung 1 eth sende, um zurückzufallen, werden mir 100 Token zurückgegeben, aber wenn ich 1.1 einzahle, funktioniert Ether oder ein anderer Float-Wertvertrag nicht und zeigt keine Fehler an, was das Problem ist

Sie sollten das Solidity-Handbuch lesen: solidity.readthedocs.io/en/v0.4.24

Antworten (1)

Multiplizieren Sie zuerst, dividieren Sie zuletzt (oder Sie verlieren Informationen).

Ändere das:

msg.value/1000000000000000000*ethtstraito*10000

Dazu:

msg.value*ethtstraito*10000/1000000000000000000

Oder noch besser dazu:

msg.value*ethtstraito/100000000000000

Oder noch besser dazu:

mul(msg.value,ethtstraito)/100000000000000

Mit mulSein:

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;
}
Oder noch besser dazu: mul(msg.value,ethtstraito) * (uint256(10) ** decimals) / (uint256(10) ** 18)falls sich die Anzahl der Dezimalstellen später ändert.
@ JesseBusman: Das wird nicht kompiliert. Auch nachdem Sie die Kompilierungsfehler behoben haben, besteht bei diesem Code eine höhere Wahrscheinlichkeit eines arithmetischen Überlaufs (daher ein Fehlschlagen der Operation). Man könnte argumentieren, dass die Chancen nicht viel höher sind, aber unter Abwägung aller Faktoren würde ich es nicht als "besser" bezeichnen, nur für die Verwendung von ( decimalswenn die Wahrscheinlichkeit eines Überlaufs dadurch etwas höher ist).
Tnx viel Es ist Arbeit