How to feed data of mapping in solidity?

How to get the data from mapping and display it, or create a getter for mapping to check if data populate from mapping. Full source:

  address public owner;

  struct Bet {
     uint256 amountBet;
     uint16 matchSelected;
     uint16 resultSelected;
   //   address[] players;

  struct Player {
     uint256 amountBet;
     uint16 matchSelected;
     uint16 resultSelected;
     uint numFunders;
     mapping (uint => Bet) Bets;
  mapping(uint16 => bool) matchBettingActive;

  mapping(address => Player) public playerInfo;

place bet function:

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

  Player storage c = playerInfo[_matchSelected];

  require(matchBettingActive[_matchSelected], "Betting: match voting is disabled");
  //Check if the player already exist
  require(!checkIfPlayerExists(msg.sender, _matchSelected));

  //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;
  c.Bets[c.numFunders++] = Bet({ amountBet:msg.value,matchSelected: _matchSelected,resultSelected: _resultSelected});

  //Add the address of the player to the players array

  //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;
      totalBetDraw[_matchSelected] += msg.value;


I didn't understand very well but let me try to answer.
If you want to know playerInfo for an address you can do something like this:

function getPlayerInfo(address account) public view returns (uint256, uint16, uint16, uint, uin256, uint16, uint16){
  return (playerInfo[account].amountBet, playerInfo[account].matchSelected, playerInfo[account].resultSelected..)

I wrote it here, so it isn't tested.

copy sir, Thank you. I'll try to test it.

hi @FreezyEx , this function works,

function getPlayerInfo(address account) public view returns (uint256, uint16, uint16, uint){
  return (playerInfo[account].amountBet, playerInfo[account].matchSelected, playerInfo[account].resultSelected, playerInfo[account].numFunders);

but only get the current value of the address, how about getting the value per address and match?

You may want to try the EnumerableMap data structure.

1 Like