9

Is a value of the variable of the contract visible from the outside ? For example,

contract Dummy {
  uint a = 0;
  function increment() {
    a = a + 1;
  }
}

This contract is on the ethereum blockchain. In this situation, can I look the current value of "a" ?

8

If the visibility specifier is not public, then other contracts will not be able to access the variable.

You, however, can access it, using

web3.eth.getStorageAt(address,0)

The index is 0 since this is the first global variable defined. If you are not sure what index the variable is at, or its a more complex type like a mapping or array, you can use http://live.ether.camp to visually inspect the storage of the contract.

If you upload the contract source to that site, it will match the values in storage with variable names from the source code.

|improve this answer|||||
5

Yes, if you add public between unit and a as shown below:

contract Dummy {
  uint public a = 0;

  function increment() {
    a = a + 1;
  }
}

After inserting the contract into the blockchain, you will be able to access the variable directly.

> contract.a
0

From Solidity Features - Visibility Specifiers:

Public functions are part of the external interface and can be called externally, while for storage variables, an automatic accessor function is generated.

|improve this answer|||||
  • 2
    Please explain how to access it from public, web3 per se. – niksmac Apr 23 '16 at 6:47
2

For the case where "outside" means another contract, see: Can a contract access another contract's storage?

Additionally:

If the visibility specifier is not public, there is no equivalent to web3.eth.getStorageAt that an outside contract can use.

For uint public a, an outside contract would invoke the accessor with parentheses contract.a() (instead of just contract.a)

|improve this answer|||||

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.