0

Before adding this function below, the contract works well. But after adding the function, when I trying to deploy the contract, the remix IDE keeps showing the error message of "Gas estimation failed". Did something wrong with the function?

 //return the user name by mapping the address
    function find_user_name(address _address) public view returns(string){
      uint256 i;
      string account_name;
      for(i=0; i < userlist.length; i++){//find the mapping user account name
            if(_address == userlist[i].useraddress)account_name = userlist[i].account;
      }

      return account_name;
    }
1

There is nothing wrong in the code. It worked fine for me. Please provide more context and code. If this is the problem when calling the function then you should check validity of the address parameter. But this problem is on deployment so, more context is needed.

You are using array for userlist. Try to change it to mapping with address as key and value be your user struct.

mapping(address => User) userlist;

This has the advantage also you do not have to loop, you can directly get user with user address.

5
  • A problem with iterating an array is if it grows too large it might cause an out of gas error. As you say using an mapping is a solution to that issue. – Ismael May 27 '18 at 20:02
  • What I am trying to do with this find_user_name function is to map the correct address with the user's name. The userlist is an instance of struct user, as shown below – 2341047123413331 May 28 '18 at 2:03
  • struct user{ string account; string password; address useraddress; } – 2341047123413331 May 28 '18 at 2:06
  • user[] public userlist; – 2341047123413331 May 28 '18 at 2:06
  • you should not be saving passwords in the network, all the information is visible even if you set it to private. unless you are saving the password encrypted. – Jaime May 28 '18 at 11:43

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.