首页 > 解决方案 > 如何解决 ParserError:预期的主表达式?

问题描述

完整来源:https ://github.com/laronlineworld/bettingMatch/blob/main/bettingMatch.sol这是一个投注智能合约,试图创建一个映射结构,用户/wallet_address 可以投注多个比赛。这个投注合约的问题是,每次user/wallet_address下注时,单个映射的数据都会被覆盖,如何创建一个值的映射,以便user/wallet_address可以投注不同的匹配。

错误:contracts/BettingTestingGame.sol:123:73:ParserError:预期的主要表达式。playerS.push(Player(msg.sender, _gameId, msg.value, _teamSelected,)-1; ^

投注功能:

function bet(uint16 _gameId, uint8 _teamSelected) public payable {
  Game storage game = gameInfo[_gameId];

  require(game.state == State.Created,"Game has not been created");
  require(matchBettingActive[_gameId] == true);
  
  
  //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
  Player storage playerS = playerInfo[msg.sender];
  playerS.push(Player(msg.sender, _gameId, msg.value, _teamSelected,)-1; 
  
  //add the mapping
  bytes32[] storage userBets = userToBets[msg.sender]; 
  userBets.push(_gameId);
// this only maps the playerinfo and every time player bets, it will overwrite  
//   playerInfo[playerS].amountBet = msg.value;
//   playerInfo[playerS].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(uint _gameId, uint16 teamWinner) public onlyOwner {
    
  Game storage game = gameInfo[_gameId];
  
  require(bettingActive == false);
  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


  //We loop through the player array to check who selected the winner team
  for(uint256 i = 0; i < players.length; i++){
     address 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 bets = playerInfo[add].amountBet;
        //Transfer the money to the user
        winners[j].transfer(    (bets*(10000+(LoserBet*devFee/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;
  
  game.state == State.Finished;
}

标签: mappingblockchainsoliditysmartcontracts

解决方案


推荐阅读