【问题标题】:Solidity ParseError: Expected primary expressionSolidity ParseError:预期的主要表达式
【发布时间】:2021-11-09 04:11:35
【问题描述】:

当我用松露编译时,它给了我这个错误 添加应付账款后出现此错误 在此之前是

winners[j].transfer(betwa*(10000+(LoserBet*10000/WinnerBet))/10000);

我不得不添加它,因为我遇到了另一个错误 这是

TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".

我的完整合同:

pragma solidity ^0.5.16;


contract Betting {
   address public owner;
   uint256 public minimumBet;
   uint256 public totalBetsOne;
   uint256 public totalBetsTwo;
   address[] public players;
   struct Player {
      uint256 amountBet;
      uint16 teamSelected;
    }
// The address of the player and => the user info
   mapping(address => Player) public playerInfo;
   function() external payable {}
 constructor() public {
      owner = msg.sender;
      minimumBet = 100000000000000;
    }
function kill() public {
      if(msg.sender == owner) selfdestruct(msg.sender);
    }
function checkPlayerExists(address player) public view returns(bool){
      for(uint256 i = 0; i < players.length; i++){
         if(players[i] == player) return true;
      }
      return false;
    }


    function bet(uint8 _teamSelected) public payable {
      //The first require is used to check if the player already exist
      require(!checkPlayerExists(msg.sender));
      //The second one is used to see if the value sended by the player is
      //Higher than the minimum value
      require(msg.value >= minimumBet);

      //We set the player informations : amount of the bet and selected team
      playerInfo[msg.sender].amountBet = msg.value;
      playerInfo[msg.sender].teamSelected = _teamSelected;

      //then we add the address of the player to the players array
      players.push(msg.sender);

      //at the end, we increment the stakes of the team selected with the player bet
      if ( _teamSelected == 1){
          totalBetsOne += msg.value;
      }
      else{
          totalBetsTwo += msg.value;
      }
    }
    // Generates a number between 1 and 10 that will be the winner
    function distributePrizes(uint16 teamWinner) public {
      address[1000] memory winners;
      //We have to create a temporary in memory array with fixed size
      //Let's choose 1000
      uint256 count = 0; // This is the count for the array of winners
      uint256 LoserBet = 0; //This will take the value of all losers bet
      uint256 WinnerBet = 0; //This will take the value of all winners bet
        address  playerAddress;
      //We loop through the player array to check who selected the winner team
      for(uint256 i = 0; i < players.length; i++){
           playerAddress = players[i];

         //If the player selected the winner team
         //We add his address to the winners array
         if(playerInfo[playerAddress].teamSelected == teamWinner){
            winners[count] = playerAddress;
            count++;
         }
      }

      //We define which bet sum is the Loser one and which one is the winner
      if ( teamWinner == 1){
         LoserBet = totalBetsTwo;
         WinnerBet = totalBetsOne;
      }
      else{
          LoserBet = totalBetsOne;
          WinnerBet = totalBetsTwo;
      }


      //We loop through the array of winners, to give ethers to the winners
      for(uint256 j = 0; j < count; j++){
          // Check that the address in this fixed array is not empty
         if(winners[j] != address(0)){
            address add = winners[j];
            uint256 betwa = playerInfo[add].amountBet;
            //Transfer the money to the user
            payable(winners[j]).transfer( (betwa*(10000+(LoserBet*10000/WinnerBet)))/10000 );
            }
      }
      delete playerInfo[playerAddress]; // Delete all the players
      players.length = 0; // Delete all the players array
      LoserBet = 0; //reinitialize the bets
      WinnerBet = 0;
      totalBetsOne = 0;
      totalBetsTwo = 0;
    }

    function AmountOne() public view returns(uint256){
       return totalBetsOne;
    }

    function AmountTwo() public view returns(uint256){
       return totalBetsTwo;
    }
}

我尝试过在此函数中设置地址支付,但它不起作用我试图用支付替换内存,但它仍然不起作用 我的版本 松露 v5.4.18(核心:5.4.18) Solidity v0.5.16 (solc-js) 节点 v14.15.1 Web3.js v1.5.3

【问题讨论】:

    标签: solidity truffle ether


    【解决方案1】:

    Solidity 0.6 中引入了从 addressaddress payablepayable() 转换。

    来源:https://docs.soliditylang.org/en/latest/060-breaking-changes.html#new-features

    由于您的合同使用的是(已弃用)0.5.16 版,因此不允许进行此转换。所以你需要将winners数组定义为address payable

    // added `payable`
    address payable[1000] memory winners;
    

    然后你就可以使用address payable类型的.transfer()方法了:

    // `winners[j]` is payable
    winners[j].transfer(...);
    

    【讨论】:

    • Hey Petr,感谢您抽出宝贵的时间,因为我已经解决了,但是我在这个问题上被困了几天,我也发布了一个关于这个的问题。如果你有时间也许,看看这个ethereum.stackexchange.com/questions/113174/…
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多