How to apply mapping structure on betting smart contract?

source code:bettingMatch/bettingMatch.sol at main · laronlineworld/bettingMatch · GitHub

how to change the data structure of playerInfo so that instead of one value for playerInfo[msg.sender].matchSelected, it would have a mapping of values. Also, you need to include resultSelected in the mapping so that the relation with matchSelected remains traceable. How can I apply the mapping of values and verify it on betting so that user can bet on different matches.

function bet(uint16 _matchSelected, uint16 _resultSelected) public payable {
       require(matchBettingActive[_matchSelected], "Betting: match voting is disabled");
      //Check if the player already exist
    //   require(!checkIfPlayerExists(msg.sender));
    
      //Check if the value sended by the player is higher than the min value
      require(msg.value >= minimumBet);
      
      //Set the player informations : amount of the bet, match and result selected
      playerInfo[msg.sender].amountBet = msg.value;
      playerInfo[msg.sender].matchSelected = _matchSelected;
      playerInfo[msg.sender].resultSelected = _resultSelected;
      
      //Add the address of the player to the players array
      players.push(msg.sender);
    
      //Finally increment the stakes of the team selected with the player bet
      if ( _resultSelected == 1){
          totalBetHome[_matchSelected] += msg.value;
      }
      else if( _resultSelected == 2){
          totalBetAway[_matchSelected] += msg.value;
      }
      else{
          totalBetDraw[_matchSelected] += msg.value;
      }
   }

Everytime the user bets, the playerInfo reset the data. The concept is to bets on multiple matches using 1 address only.

also the distribution of prize:

   function distributePrizes(uint16 matchFinished, uint16 teamWinner) public onlyOwner {
      address[1000] memory winners;
      //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 add;
      uint256 bets;
      address playerAddress;
    
      //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].matchSelected == matchFinished &&
            playerInfo[playerAddress].resultSelected == teamWinner){
            winners[count] = playerAddress;
            count++;
         }
       }
       //We define which bet sum is the Loser one and which one is the winner
       if ( teamWinner == 1){
         loserBet = totalBetAway[matchFinished] + totalBetDraw[matchFinished];
         winnerBet = totalBetHome[matchFinished];
       }
       else if ( teamWinner == 2){
         loserBet = totalBetHome[matchFinished] + totalBetDraw[matchFinished];
         winnerBet = totalBetAway[matchFinished];
       }
       else{
          loserBet = totalBetHome[matchFinished] + totalBetAway[matchFinished];
          winnerBet = totalBetDraw[matchFinished];
       }
      //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))
            add = winners[j];
            bets = playerInfo[add].amountBet;
            uint256 amountToPlayer = (bets * (10000+(loserBet*devFee/winnerBet))) / 10000;
            winners[j].transfer(amountToPlayer);
      }
      //Reset all variables
      delete playerInfo[playerAddress]; 
      players.length = 0; 
      loserBet = 0; 
      winnerBet = 0;
      //10 will be the number of matches (To improve this)
      for(uint256 k = 0; k < 10; k++){
         totalBetHome[k] = 0;
         totalBetAway[k] = 0;
         totalBetDraw[k] = 0;
      }
    }
1 Like

Welcome to the SCRF, @LAR – thank you so much for contributing this… can you tell us a little more about how you created this, and what it might be used for?

this smart contract is a betting contract, it has a multiple matches that user can select match to bet, but the problem is an user or wallet address can only bet on 1 matches due to structure of mapping of playerInfo, I’m here to ask on is there a help to construct the structure of this code? In order that user/wallet address can bet on multiple matches. Thank you

1 Like

Hi LAR, nice to see that more and more people are joining the development of smart contract, and blockchain applications. People like you are vital to the healthiness of the ecosystem.

I see that you also asked this question on ethereum stack exchange, and that is the correct place to ask.

As you can see, we called ourselves smart contract research forum - we focused on the research side, rather than the implementation side.

The best suggestion that I can give you is to trace other similar open source projects’ code, such as Augur, to see how your intended design can be implemented with solidity.

By following those projects, you can better understand the limitations and different design requirements on smart contracts (in comparison with regular web apps).

3 Likes