0

source code:https://github.com/laronlineworld/bettingMatch/blob/main/bettingMatch.sol

How to open and close bet per match

function bet(uint16 _matchSelected, uint16 _resultSelected) public payable {

  //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;
  }

}

This is the code for opening the betting

 /* Function to enable betting */
function beginVotingPeriod()  public onlyOwner returns(bool) {
    bettingActive = true;
    return true;
}

how about opening the bet per match?

also closing the bet per match

/* Function to close voting and handle payout. Can only be called by the owner. */
function closeVoting() public onlyOwner returns (bool) {
    // Close the betting period
    bettingActive = false;
    return true;
}

1 Answer 1

0

If you need to control voting per match, you'll need to use a mapping as follows:

mapping(uint16 => bool) matchBettingActive;
...
function beginVotingPeriodForMatch(uint16 _match)  public onlyOwner returns(bool) {
    matchBettingActive[_match] = true;
    return true;
}
function closeVotingForMatch(uint16 _match) public onlyOwner returns (bool) {
    // Close the betting period
    matchBettingActive[_match] = false;
    return true;
}

Update: Also, you want to make sure the betting is active at the beginning of your bet(...) function call as follows:

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);
  ...
}
8
  • How about checking the status of match_id before betting? Is it like this require(matchBettingActive)? Aug 24, 2021 at 9:24
  • @LloydRamos definitely, you want to make sure betting is active before you make your bet. I'll update the answer. Aug 24, 2021 at 9:29
  • Thank you for the help sir, realy appreciate, I have One last question about this betting? I can't bet on other match_id because everytime I bet --> //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; this works Aug 24, 2021 at 9:36
  • Is there a way that one address can bet on diffrent matches without changing the playersInfo? Aug 24, 2021 at 9:37
  • @LloydRamos you would need 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. But things would get exponentially more complex with that. Aug 24, 2021 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.