Solidity

智能合約在 Remix 上工作,但不是在 TestNet

  • March 2, 2019

這個問題以前有人問過。但是,這些答案對我沒有幫助。(當然,我現在找不到“答案”自動取款機)它說 Ropsten(我目前使用的)的礦工較少 - 或者在那個程度上,因此不是執行智能合約的最佳選擇。他們推薦了林克比。我試過了,但也沒有用 :( 目前,所有功能都適用於 Remix。

以下是關於我的 Rinkeby 和 Ropsten 的一些資訊:

我有兩個主要功能,第一個是PickYourNumbers()允許您選擇數字,將它們放入數組中,然後將它們顯示在合約上GetHash()。這個有效。

第二個是StartTheLotto()呼叫函式random()。此函式生成…一個隨機數。它呼叫此函式 6 次,將數組中的 6 個變數設置為不同的隨機數。最後,數組通過 顯示lottoWinners()

我猜是因為我讓合約/礦工做的太多了?你有什麼建議?

   pragma solidity ^0.4.24;

contract EthereumLottery {

   uint[6] array;
   uint[6] winners;
   string warning = "please be sure to bet below 67 for the first 5 balls and 34 for the 6th";
   address owner;    // current owner of the contract
   uint addr = address(this).balance;
   uint nonce = 1;
   uint startTime = now;

   constructor() public {
     owner = msg.sender;
   }

   function WARNING() public view returns (string memory) {
       return warning;
   }

   function PickYourNumbers(uint8 firstBall, uint8 secondBall, 
     uint8 thirdBall, uint8 fourthBall, 
     uint8 fithBall, uint8 powerballllll) public {

       if (firstBall <= 66) array[0] = firstBall;
       if (secondBall <= 66) array[1] = secondBall;
       if (thirdBall <= 66) array[2] = thirdBall;
       if (fourthBall <= 66) array[3] = fourthBall;
       if (fithBall <= 66) array[4] = fithBall;
       if (powerballllll <= 66) array[5] = powerballllll;

   }

   function GetHash() public view returns (uint[6] memory) {
       return array;
   }

   function StartTheLotto() public returns (uint[6] memory) {
       nonce++;
       winners[0] = random();
       nonce++;
       winners[1] = random();
       nonce++;
       winners[2] = random();
       nonce++;
       winners[3] = random();
       nonce++;
       winners[4] = random();
       nonce++;
       winners[5] = random();
   }


   function lottoWinners() public view returns (uint[6] memory) {
       return winners;
   }

   function random() private view returns(uint) {

       uint interval = now - startTime;

       bytes32 randomNum = keccak256(abi.encodePacked(interval + nonce + tx.gasprice + addr));

       uint number;
       for(uint i=0;i<randomNum.length;i++){
           number = number + uint(randomNum[i])*(2**(8*(randomNum.length-(i+1))));
       }
       return number % 67;
   }

}

編輯:

Rinkeby DAPP 地址:0x75AB85fB72Ba93c9a9dF23E6FC775b58Dc7f050B

Ropsten DAPP 地址:0x8bea60c97DC6BD955a614C58e5D0A0fBD12a8593

所以經過大量的重新編碼和嚇壞了……哈哈……我注意到了一些奇怪的東西。PickYourNumbers()當我在“最新交易”(霧錢包底部)中執行(訂單 66 公頃)時,它說Contract execution。但是當我執行StartTheLotto()它時說Transfer between accounts

然後我決定為第二個功能獲取使用者輸入,所以StartTheLotto()我沒有做StartTheLotto(int8 asdf)相應的調整。現在,由於某些奇怪的原因,該功能可以工作並顯示Contract execution. 如果有人能比我做得更好,我會很高興,這樣我就可以知道為什麼而不僅僅是如何。

新程式碼:

pragma solidity ^0.4.24;

contract EthereumLottery {

   uint[6] array;
   uint[6] winners;
   string warning = "please be sure to bet below 67 for the first 5 balls and 34 for the 6th";
   address owner;
   uint addr = address(this).balance;
   uint nonce = 1;
   uint startTime = now;
   int8 fdsa;              // ADDED THIS VAR TO USE ASDF USR INPUT

   constructor() public {
     owner = msg.sender;
   }

   function WARNING() public view returns (string memory) {
       return warning;
   }

   function PickYourNumbers(uint8 firstBall, uint8 secondBall, 
     uint8 thirdBall, uint8 fourthBall, 
     uint8 fithBall, uint8 powerballllll) public {
       if (firstBall <= 66) array[0] = firstBall;
       if (secondBall <= 66) array[1] = secondBall;
       if (thirdBall <= 66) array[2] = thirdBall;
       if (fourthBall <= 66) array[3] = fourthBall;
       if (fithBall <= 66) array[4] = fithBall;
       if (powerballllll <= 66) array[5] = powerballllll;
   }

   function GetHash() public view returns (uint[6] memory) {
       return array;
   }

                  //ADDED USR INPUT HERE
   function StartTheLotto(int8 asdf) public returns (uint[6] memory) {
       fdsa = asdf; // USESD IT SO WOULDN'T THROW ERROR
       nonce++;
       winners[0] = random();
       nonce++;
       winners[1] = random();
       nonce++;
       winners[2] = random();
       nonce++;
       winners[3] = random();
       nonce++;
       winners[4] = random();
       nonce++;
       winners[5] = random();
   }


   function lottoWinners() public view returns (uint[6] memory) {
       return winners;
   }

   function random() private view returns(uint) {

       uint interval = now - startTime;

       bytes32 randomNum = keccak256(abi.encodePacked(interval + nonce + tx.gasprice + addr));

       uint number;
       for(uint i=0;i<randomNum.length;i++){
           number = number + uint(randomNum[i])*(2**(8*(randomNum.length-(i+1))));
       }
       return number % 67;
   }

}

@克里斯,

我拿了你的 Solidity 程式碼並在 Rinkeyby 測試網路上嘗試了相同的程式碼,它對我來說就像預期的那樣完美。

請交叉檢查: - 你有足夠的餘額來執行每筆交易(函式呼叫),你可以從這裡獲得測試乙太幣在 rinkeby 測試網路上使用。

  • 允許礦工驗證每筆交易幾秒鐘,然後刷新函式呼叫,您應該會看到更改的狀態。

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