[Ethereum] Solidity: How to specify a (hard-coded) address as a literal

addressessolidity

I am defining a contract and I want to ensure a particular member variable can only be modified by me. So, specifically I want to ensure that if someone else deploys one of my contracts, then they will not be able to modify this variable.

To do this I tried to hard-code my address, and test against msg.sender in the setter

contract Sample {
  address constant public myAddress = 0xe0f5206bbd039e7b0592d8918820024e2a7437b9;
  uint public vari;
  function setVari(uint a) {
    if(msg.sender == myAddress) {
      vari = a;
    }
  }  
}

When I compile this, I get the following error

Warning: This looks like an address but has an invalid checksum. If this is not used as an address, please prepend '00'.
      address constant public myAddress = 0xe0f5206bbd039e7b0592d8918820024e2a7437b9;
                                          ^----------------------------------------^

And when I try to modify this variable within geth using

myInstance.setVari.sendTransaction(22, {from: eth.accounts[0]})

the value of "vari" is not updated. (Where eth.accounts[0] is the hardcoded address in the source.)

However, if I remove the protection within setVari(), everything works as expected.

This implies to me that everything I'm doing is correct, except the hard-coding of my address.

Can anyone help? How do I hard code my account into a contract?

Thanks!


Note: the address included is one I'm using on a private test network, so you won't be able to find it in the public network. However, I got the same compiler warning when I tried hard-coding one of my real accounts addresses.

Best Answer

Enter your address in https://etherscan.io/ and copy the uppercase/lowercase checksummed version and paste it into your solidity code.

contract Sample {
  address constant public myAddress = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9;
  uint public vari;
  function setVari(uint a) {
    if(msg.sender == myAddress) {
      vari = a;
    }
  }  
}

And the warning message will disappear. The compiler checks the checksummed address for validity.

But I just checked your code with the checksummed address in Remix and I get a new warning message:

Untitled4:4:39: Warning: Initial value for constant variable has to be compile-time constant. This will fail to compile with the next breaking version change.
  address constant public myAddress = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9;
                                      ^----------------------------------------^

This second error will disappear if you remove the constant modifier.

Related Topic