I am developing a contract where a predetermined address can change a contract variable.

The structure of my current contract is as follows:

pragma solidity ^0.5.0;

contract privacyPreferences {

 bool preference = false;                              

function changePreferences() public {
   if (msg.sender == ADDRESS  )
      preference = true;

 function preferenceStatus() public view returns (bool) {
    return preference;

In the changePreferences() function, I compare the user who is requesting access with the ADDRESS variable. The ADDRESS variable can have multiple values. In my application several addresses may request access to the contract. My question is: How do I make this validation in the contract considering that the variable ADDRESS can have more than one value?

mapping (address => bool) addresses;

constructor () public {
  addresses [ADDRESS1] = true;
  addresses [ADDRESS2] = true;

function changePreferences () public {
  if (addresses [msg.sender])
    preference = true;
  • What is the function of this line: mapping (address => bool) addresses; – Mutante Apr 20 '19 at 15:17
  • Updated the answer – Mikhail Vladimirov Apr 20 '19 at 15:19
  • this solution does not generate a high gas cost? – Mutante Apr 20 '19 at 15:24
  • Depends on how many addresses do you have. You may also consider using compressed list of addresses or bloom filter. – Mikhail Vladimirov Apr 20 '19 at 15:25
  • Inside [ADDRESS1] the value needs to be between " "? – Mutante Apr 20 '19 at 15:54

Solidity doesn't provide features to easily check whether a value is inside an array. Here are some more details: How to search string in array

So basically you have to either iterate over all the array's values every time, change it into a mapping (probably the best idea) or do something else creative. The mapping could be simply mapping(address => boolean) hasAccess; where each user with access has a value of true (false is the default for booleans so everyone else has false if checked).

Also you should put your access check in a modifier so it's easier to use. So something like this:

modifier canChangePreferences {

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.