0
function approveUsers(address[] users) onlyOwner {
   for(uint i=0; i<users.length; i++) {
       approvedUsers[users[i]] = true; //Mapping {address: bool}
       ApproveUserForPreSale(users[i], approvedUsers[users[i]]); //Event
   }
 }

I'm getting a zero length array with this snippet. And if I take users as static array( address[10] users), I get the correct length but the contents are 0(0x0..).

edit This is working on Remix Studio as Expected, but not on Ethereum Wallet.

1 Answers1

1

There a re handful of things going on here.

The main problem

Your function doesn't return anything. It appears it sets a bit in a mapping to true and I would expect that probably works as far as it goes.

** Other problem #1 **

The use of a dynamic array in a function signature means this function will not be able to communicate with other contracts, at this time. Consider a fixed-length signature approach, e.g. ``` function approveUser(address user) onlyOwner returns(bool success) { approvedUsers[user] = true; // event ... return true; }

** Other problem #2 **

for i<0; i<unlimited ... is an anti-pattern that will cease to work when the gas cost exceeds the block gasLimit. The fixed-length approach that deals with one user at a time will resolve this.

The patterns over here may save you some time: Are there well-solved and simple storage patterns for Solidity?

Hope it helps.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
  • I need to approve many users at a time. One by one approving all users would be a lot of work and very costly. Any other way I can add multiple entries that would result in mapping. Why can't I even read what is passed. – Devashish Puri Sep 13 '17 at 03:57
  • It doesn't imply an increase in manual work or great increase in gas cost. It implies that the iteration will be performed by a smart client or server that will make multiple inexpensive calls, not by a single function that will perform a humongous job in one gulp. – Rob Hitchens Sep 13 '17 at 14:51