i have a function that receive a number, autentic evaluates if it is already in an array, if it is it should return false, if it is not, return true. Then onlyOneVote will add the number to the array if true and not if false. The problem is the function never adds anything to the array.

uint32[100] public people;
uint8 public counter;

function onlyOneVote(uint32 ida) public returns(bool) {
  bool a = autentic(ida);
  if (a == true) {
    people[counter] = ida;
    counter = counter + 1;
  return a;

function autentic(uint32 idb) public returns(bool) {
  bool b;
  for(uint i = 0; i< people.length; i++) {
    if (people[i] == idb) {
      b = false;
    } else {
      b = true;
  return b;

I think is safe to say that your code can be reduced to this:

uint32[100] public people;
uint8 public counter;

mapping (uint32 => bool) public alreadyVoted;

function vote(uint32 id) public returns(bool) {
  if (alreadyVoted[id]) return(false);
  alreadyVoted[id] = true;
  people[counter] = id;
  counter = counter + 1;

Call it with web3 like this:

contractInstance.vote.sendTransaction(id, {from: _from});

where _from is your account address (web3.eth.accounts[0] maybe).

  • still the same problem. when testing with IDs, no new id are in the array, still empty after the test: > contractInstance.vote.call(1) true > contractInstance.vote.call(1) true – bhalgalix May 18 '18 at 17:58
  • In Ethereum, you can call functions in contracts using two different methods: call() or sendTransaction(). When the function changes the state of the blockchain (and that's the case for this function), you have to use sendTransaction(). – bordalix May 18 '18 at 18:19
  • Thank you. I have one last question, what "account address" do you refer to? dont get that part.. Sorry for so many questions XD – bhalgalix May 19 '18 at 3:11
  • The account address the transaction origins from. You can use accounts[0] – bordalix May 19 '18 at 6:32
  • it sends me "Reference Error: accounts is nit defined – bhalgalix May 21 '18 at 15:46

This code will do it, it's tested and here you have the address to interact with it on Kovan: 0xc807caebda01eaffd7998ab7fd9fcc4a2cf5730c

pragma solidity ^0.4.18; 

contract Test {

uint32[100] public people;
uint256 public counter; // Is uint256 because the on the array[100], 100 is a uint256 variable.
mapping(uint32 => bool) public voteVerifier; //mapping saves you so much gas at checking if it's in the array

function onlyOneVote(uint32 ida) public returns(bool) {
  require(!voteVerifier[ida]); //If false, enter to add the uint32 to array.

    people[counter] = ida; //Add the ida to the array
    counter = counter + 1;

    voteVerifier[ida] = true;//Set on the mapping that this uint32 is now on the array.
  return true;


A few things to comment on:

  • It's so much better to use a mapping to check if a number is on the array because it's not a time-dependent search. Find params on arrays makes you operate much more as greater the array becomes. But with mappings, the search lasts and costs the same even if millions of values were included on the array.

Here you have some info: http://solidity.readthedocs.io/en/v0.4.21/types.html#mappings

This code: people[counter] = ida; //Add the ida to the array counter = counter + 1;

Can be replaced to be more accurate and cheaper on gas spending: people.push(ida)//Puts on the last array's position the item ida;

Hope it helps!!

  • You cannot push to an array with its size defined. people.push(id) will not compile. – bordalix May 18 '18 at 18:25
  • I know, that's why it's not on the solution code, it's just an advice :) – CPereez19 May 18 '18 at 18:27
  • Thank you for the useful advices about the mapping to use a lot less gas. i could use 'not size' defined array or mapping because the infinite loop problem and in this case i know the amount of voters, i started using push but didnt compile. – bhalgalix May 19 '18 at 3:18
  • As bordalix said, push can only be used with dynamic arrays, not with fixed ones. So to limit the number of votes if you know is 100, make a require on the function that adds numbers to the array controlling it. – CPereez19 May 19 '18 at 7:25

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.