Solidity

我的契約將用完 gas 並且不會被執行。為什麼?

  • May 28, 2017

對此非常非常新。

你向合約發送 1 個乙太幣:

  • 如果它不是一個乙太,它會拒絕它
  • 如果是,則將地址添加到映射中
  • 如果有 5 個映射,它會隨機選擇一個地址並發送 5 個乙太幣,並重置計數器

為什麼契約不執行?

謝謝

pragma solidity ^0.4.11;

contract BasicLottery {

       // A mapping to store ethereum addresses
           mapping(uint => address) public gamblers;


           uint public my_length; //*to keep track of the length.
           uint public random; //random number

           function BasicLottery(){
               my_length == 0;
           }
       // function when someone sends an ether
       // stores the address, and if there are 5 participants, 
       // chooses a winner and gives the money

       function bet() payable {

       // If the bet is not 1 ether, send the
       // money back.
       require(msg.value == 1);

       my_length +=1;

       gamblers[my_length] = msg.sender;

       if (my_length == 5) {
           // pick a random number between 1 and 5
           random = uint(block.blockhash(block.number-1))%5 + 1;
           gamblers[random].transfer(5);
           my_length == 0;

       }
   }
}

msg.value == 1表示發送 1 wei。您需要將其替換為msg.value == 1 ether.

require()假設您提供了合理數量的氣體,則 out of gas 錯誤可能是錯誤的結果。

您還需要更改my_length == 0;my_length = 0;

引用自:https://ethereum.stackexchange.com/questions/16717