0

I have a situation where i'd like to fulfill array with elements that are not duplicates ( set alike ), so I wrote simple function in order to compare addresses before insertion.

Function is quite simple, but it doesn't work, so I have to ask for help...thanks in advance.

// function to prevent insertion of duplicates in array
function isDuplicate() public view returns (bool){
    for(uint i = 0; i < addressIndices.length; i++){
        if(addressIndices[i] == msg.sender) return true;
        else return false;
  }
}

In order to achieve completness of question, I''ll provide you more information:

First: Main structures:

struct Match{
    bytes32 teamHome;
    bytes32 teamAway;
    uint teamHomeGoals;
    uint teamAwayGoals;
    uint  oddsTeamHome;
    uint  oddsTeamAway;
    uint  oddsDraw;
    uint outcome; // outcome of a match ( possible values: '1', 'X', '2', mapped to, because of uint, as: '1', '2', '3');
}
struct Bet{
    address bettor; // address a of creator of a bet;
    bytes32 name; // name in a relation with ^ a provided address;
    uint amount; // deposit on bet;
    uint bet; // match index being bet on;
    uint outcome; // bet placed on a outcome; defined as: '1', 'X', '2', mapped to, because of uint, as: '1', '2', '3';

Second: Function to place a bet.

function placeBet(bytes32 name, uint _outcome, uint desiredMatchIndex, uint _amount) public payable{
// find a way to store a bid in order to be easily searchable, in order to easily send money to winners;
    //   require(!roundEnd, "Interactions with contract are locked, be careful next time!");
    //   require(state == State.Active, "Betting is over, game have already started!");
      require(msg.value > 0, "It isn't possible to place a bet without a money ");
    if(!isDuplicate()){
        addressIndices.push(msg.sender);
    }
      existingBets[msg.sender].push(Bet({
          bettor: msg.sender,
          name: name,
          amount: _amount,
          bet: desiredMatchIndex,
          outcome: _outcome
      }));
    // emit event, finally;
}

The problem is really weird: After I make 3 bets with 3 different addresses, length of my array that i am trying to use as a set, is 7. If function worked, it should be 3, if not it should be 9.

Any idea?

Edit: isDuplicate() function works only for first inserted address

4
  • The length of which array is 7? You append to existingBets no matter what isDuplicate() returns, so the length of that array should be the number of times placeBet() was called. The length of addressIndices should be the number of unique addresses that placed a bet.
    – user94559
    Jun 29, 2018 at 19:27
  • Much easier would be a mapping: mapping(address => bool) used; and then require(!used[msg.sender]); used[msg.sender] = true; at the top of placeBet(). (This is assuming you don't actually want to add to existingBets when msg.sender is a duplicate.)
    – user94559
    Jun 29, 2018 at 19:28
  • @smarx First of all, thanks for effort, I really appreciate it! Yes, the length of addressIndices should be the number of unique addresses that placed a bet. Thanks for pointing out to an easier solution, but, at the end I'd need to keep track of length of unique addresses, in order to send payout to all of winners ( not sure if that's most optimal scenario, but I have to start with something).
    – polux
    Jul 2, 2018 at 7:46
  • So, basically, your approach makes sense, but I would need to keep track of length of suggested mapping as well?
    – polux
    Jul 2, 2018 at 7:49

1 Answer 1

0

The current function never looks beyond the first element of the array. It returns either true or false immediately. Try this instead, but as I noted in my comment, a mapping would let you avoid the iteration:

// function to prevent insertion of duplicates in array
function isDuplicate() public view returns (bool) {
    for(uint i = 0; i < addressIndices.length; i++){
        if(addressIndices[i] == msg.sender) return true;
    }

    return false;
}
1
  • (This is assuming you don't actually want to add to existingBets when msg.sender is a duplicate.) I gave it a thought, and realized that it's not my use case - I'll add it in existingBets anyways ( no matter is it duplicate address or not ), but won't add it in addressIndices because i have to use that array in order to keep track of addresses count. @smarx It's my fault, however, because I haven't provided enough information. ( mapping(address => Bet[]) public existingBets; ) Much gratitude for your assistance! : )
    – polux
    Jul 2, 2018 at 13:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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