0

Here's my sample contract code.

contract SimpleStore {

  uint public BetRound;
  uint public SelectedUser;

  struct Struct {
    uint _round;
    address _owner;
    uint _index;
    bool _active;
  }

  mapping(uint => Struct) public userIndexStruct;
  uint[] public userArray;

  function add(address _address) public {
    Struct storage accounts = userIndexStruct[userArray.length];
    accounts._round = BetRound+ 1;
    accounts._owner = _address;
    accounts._active = true;
    accounts._index = userArray.length;

    userArray.push(accounts._index);
  }

  function update(uint _index) public {
    userIndexStruct[_index]._round++;
  }

  function SetUsers() public returns (uint) {
      // If we reached the end of our array
      // it should mean we looped all users already
      // We need to increment the BetRound
      if(SelectedUser >= userArray.length ) {
          BetRound++;
          SelectedUser = 0;
      }

        for (uint i = SelectedUser; i < userArray.length; i++) {

          if (userIndexStruct[i]._round < BetRound) {
            if (userIndexStruct[i]._active) {
                // This is the user who is next to receive a reward.
                // Send rewards to the user, and then update his round.
                update(i);
                SelectedUser = i;
                // Return what user was selected
                return i;
            }
          }
        }

        // Our loop finished without result, we should move to the next round.
        BetRound++;
  }

}

Whenever i add a user to my struct, the first account index is 0.

If i verify this by entering index 0 in my userIndexStruct and the userArray, it returns the correct address.

However, every time my SetUsers loop hits 0, it returns address 0x0, and the next loop it returns the actual stored index[0] address.

Could someone please explain what's happening, and why it Always returns the 0x0 address when my loop begins with 0, instead of giving the correct address for my users stored in the struct with index 0?

Thank you!

Solved!

I simply forgotten to check array.length -1. Facepalm.

Thank you all for the time and effort!

  • Smart contract code that you have provided is not complete, kindly provide complete code. It will help to understand what you want to achieve. – Soham Lawar Sep 10 '18 at 8:40
  • The only missing function was the update function, i've edited and added it. – De ruige Sep 10 '18 at 10:27
  • I think Transfer function is still missing, solidity smart contract is throwing error in remix – Soham Lawar Sep 10 '18 at 10:41
  • Sorry, forgot to take that out! My bad, this contract should not use the Transfer function, it got in when playing around on fiddle. Editing now. – De ruige Sep 10 '18 at 10:47
1
  function add(address _address) public {
    Struct storage accounts = userIndexStruct[userArray.length];
    accounts._round = BetRound+ 1;
    accounts._owner = _address;
    accounts._active = true;
    accounts._index = userArray.length;

    userArray.push(accounts._index);
  }

Let us say I want to add 1'st user Length of userArray = 0. Now as there is not an entry in mapping userIndexStruct with key 0 i.e.userIndexStruct[0]. So remaining attributes of Struct of (round, owner, active and index) will be updated and index(0) will be inserted in userArray.

You are not using an array of structure anywhere in the above program. You are storing indexes in userArray array. You are storing addresses in userIndexStruct mapping and I am able to get proper addresses stored after executing SetUsers function.

You are not using mapping effectively as you are only storing array indexes as key. If the key of mapping is other than array indexes then you shall use mapping. In your case array of structures will satisfy requirements.

Refer this question to understand more about storage patterns in solidity.

  • Thank you for the reply. I tried to use the code used in the storage pattern post, but my results are the same. After every loop, i get address 0x0 as return value. I've created a fiddle with detailed information, together with the code i created from the above example. Please take a look ethfiddle.com/WabNnpZm24 – De ruige Sep 10 '18 at 12:18
  • 1
    Problem solved - I've forgotten when i loop to set the array length - 1. I feel stupid now. – De ruige Sep 10 '18 at 12:54
  • Glad to know that your problem is solved. – Soham Lawar Sep 10 '18 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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