Fehler beim Bereitstellen eines einfachen Vertrags: Diese Transaktion scheint fehlzuschlagen. Wenn Sie es einreichen, kann es das gesamte von Ihnen gesendete Gas verbrauchen

Mein Smart-Contract-Code ist hier

// To specify what version of compiler this code will compile with
pragma solidity ^0.4.11;

contract tokensale {

  // Declaring the maximum number of tokens available for sale.
  uint public maxTokens = 10000;

  // The number of tokens you can buy for one wei (Solidity converts all unitless values from ethers to wei).
  // We will keep the default currency to be wei. It will get clearer.
  uint public tokenSwap = 10;

  // This variable, initially set to zero,  stores the number of tokens that have been bought, i.e it will
  // get incremented each time someone buys tokens.
  uint public supply = 0;

  // mapping is an equivalent to associative array. We store token balances corresponding
  // to each address that buys tokens in this mapping.
  mapping (address => uint) balance;


  // A modifier checks if a condition is met before executing a function.
  // This  modifer checks if there are tokens available before the buyTokens function is called.
  modifier isTokenAvailable () {
    require (msg.value*tokenSwap + supply <= maxTokens);     // require checks the condition and the function that
    _;                                                       // follows, denoted by the '_', is only run if the condition
  }                                                                      // is satisfied.


  // A function to check the ether balance of any address.



  // To check the token balance of any address. The external keyword allows the function
  // to be called from outside the contract. If no keyword is specified, all functions are external
  // by default..
  function balanceOf (address tokenHolder) external constant returns (uint) {
    return balance[tokenHolder];
  }

  // A function to buy tokens accesible by any address
  // The payable keyword allows the contract to accept ethers
  // from the transactor. The ethers to be deposited is entered as msg.value
  // (which will get clearer when we will call the functions in browser-solidity)
  // and the corresponding tokens are stored in balance[msg.sender] mapping.
  // underflows and overflows are security consideration which are
  // not checked in the process. But lets not worry about them for now.

  function buyTokens () external
  payable 
  isTokenAvailable {
    uint tokensAmount = msg.value * tokenSwap;    // At this point, msg.value is in wei.
    balance [msg.sender] += tokensAmount;
    supply += tokensAmount;
  }
}

Wenn ich diesen Code einsetze, wird mir der Fehler angezeigt

Es scheint, dass diese Transaktion fehlschlagen wird. Wenn Sie es einreichen, kann es das gesamte von Ihnen gesendete Gas verbrauchen

Ich bin mir nicht sicher, was in diesem Vertragscode falsch ist. Ich habe es mit unterschiedlichen Gasmengen von niedriger bis höher versucht, aber immer noch der gleiche Fehler.

Hier ist die AusgabeGeben Sie hier die Bildbeschreibung ein

Schlägt die Transaktion also fehl, wenn Sie sie ausführen? Was ist der Wert, den Sie übergeben?
@MuhammadAltabba Nein, der Vertrag wird erfolgreich bereitgestellt, aber wenn ich versuche, etwas ETH an die Vertragsadresse zu senden, schlägt die Transaktion fehl
Und warum zeigt es den ROTEN Fehler zur Bereitstellungszeit an?
Können Sie den Bereitstellungscode teilen (oder wie genau machen Sie die Bereitstellung - ist das Nebel?)
@MuhammadAltabba Mein Code ist bereits in meiner Frage. Ja, ich benutze MIST
Ich frage nach dem "Bereitstellungscode" (nicht Smart Contract-Code), falls es einen gibt? Ich verstehe jetzt, dass Sie die Benutzeroberfläche verwenden, um die Bereitstellung ohne Bereitstellungscode vorzunehmen. OK
Tut mir leid, ich weiß nicht, wie ich den "Bereitstellungscode" überprüfen kann. @MuhammadAltabba Haben Sie einen funktionierenden UI-Code?

Antworten (1)

  1. Ihrem Vertrag fehlt die Konstruktorfunktion .

  2. Wenn Sie bei der Vertragserstellung etwas ETH mitsenden, muss Ihr Konstrukteur als zahlbar gekennzeichnet werden.

  3. Verwenden Sie diesen Code nur zu Lernzwecken. Eine bewährte Methode ist die Verwendung der SafeMath-Bibliothek zum Hinzufügen von Werten, um Überläufe zu verhindern. In diesem Fall sollte dies jedoch nicht so relevant sein.