<p>Note that this question has been automatically mapped to <a href="https://www.reddit.com/r/ethereum/comments/3a8oi0/is_there_a_way_to_access_ethereum_smart_contracts/" rel="noreferrer">reddit</a> and answered there.</p>

<p>If you declare your state variables as <code>public</code>, a function to read their values will be automatically created by the compiler ("<a href="https://github.com/ethereum/wiki/wiki/Solidity-Tutorial#accessor-functions" rel="noreferrer">accessor function</a>"). If you use the <a href="https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract" rel="noreferrer">rich interface provided by web3.js</a>, you can create a javascript contract object and just call <code>message()</code> on that to get the respective value.</p>

<p>For arrays and mappings, the compiler will generate a function that has additional arguments which correspond to the indexes. For example, for a variable <code>mapping(uint =&gt; uint[]) public data;</code> the function will be equivalent to <code>function data(uint x, uint y) constant returns (uint) { return data[x][y]; }</code>.</p>

CC BY-SA 3.0
2015-06-18T19:59:43.707Z
Hmm, I tried using the `public` function. I added the function to the contract descriptor, but now that I call it, the javascript thinks I want to send a transaction to the contract to invoke that function...
ThePiachu
CC BY-SA 3.0
2015-06-21T00:23:43.023Z
Which version of web3.js are you using? The most current one should decide between a local call and a transaction automatically, with older ones you had to be explicit.

Can you post the JSON for the ABI for the contract?
Rob Myers
CC BY-SA 3.0
2015-06-22T01:07:47.707Z
@RobMyers I'm trying to run it in AlethZero v0.8.2 . My current test code is available at https://github.com/ThePiachu/EtherTest .
ThePiachu
CC BY-SA 3.0
2015-06-22T10:06:36.710Z
You seem to use an outdated solidity compiler. `string32` has been replaced by `bytes32` and also your interface json looks incomplete.
chriseth
CC BY-SA 3.0
2015-06-23T19:28:05.797Z
@chriseth Hmm, interesting. I will have to give a "cutting edge" version a go then...
ThePiachu
CC BY-SA 3.0
2017-11-09T05:55:04.290Z
@chriseth Can you explain what you mean about the "accessor function"?  I went to that link, and it's been moved, and the phrase "accessor function" isn't anywhere on the new page.  What I want is to access a string that I coded into a contract.  Preferably I want to see it on the blockchain explorer.
stone.212
CC BY-SA 3.0
2017-11-13T17:29:03.850Z
@stone212 the term was renamed to "getter" in the meantime.
chriseth
CC BY-SA 4.0
2021-11-23T00:40:23.037Z
Who pays the cost of reading that data and where does the request go? Is it something that ETH nodes have to provide for free?
nanobar
CC BY-SA 3.0
2015-06-18T15:53:52.960Z
2015-06-18T15:53:52.960Z
chriseth
<p>Here is a JavaScript code for Node.JS to read a </p>

<ul>
<li><p>You need the contract Solidity source code to get ABI</p></li>
<li><p>With this ABI and web3 convenience methods you can read the contract data</p></li>
</ul>

<p>Example:</p>

<pre><code>var Web3 = require('web3');
var solc = require("solc");
var fs = require('fs');

// Connect to a geth server over JSON-RPC
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

console.log("Talking with a geth server", web3.version.api);

// Read standard token contract from https://www.ethereum.org/token
var sourceCode = fs.readFileSync('sol/token.sol','utf8')
var compiled = solc.compile(sourceCode);
var abiArray = compiled.contracts["MyToken"].interface;
abiArray = JSON.parse(abiArray);

// Create a proxy object to access the smart contract
var MyContract = web3.eth.contract(abiArray);

// instantiate by address
var address = "0x091cc7F4ACA751a6b8A4101715d6B07CD4232341";
var contractInstance = MyContract.at(address);

// All public variables have automatically generated getters
// http://bitcoin.stackexchange.com/a/38079/5464
var result = {
  "totalSupply": contractInstance.totalSupply(),
  "symbol": contractInstance.symbol(),
  "name": contractInstance.name(),
};
console.log(JSON.stringify(result));
</code></pre>

CC BY-SA 3.0
2018-02-05T07:56:04.487Z
yeah! thank you very much! save me a lot of time! save my day!
尤川豪
CC BY-SA 3.0
2016-05-09T13:43:39.400Z
2016-05-09T13:43:39.400Z
Mikko Ohtamaa
<p>You should check out <code>web3.eth.contract</code> - it should have exactly what you need. <a href="https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract" rel="nofollow">https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract</a></p>

CC BY-SA 3.0
2015-06-18T16:31:40.260Z
2015-06-18T16:31:40.260Z
Tim Coulter
<p>Say I have a simple smart contract:</p>

<pre><code>contract Simple {
    string32 message;

    function Simple() {
        message = "Hello world!";
    }
}
</code></pre>

<p>How can I easily access the <code>message</code> variable by name in JavaScript? It looks like <a href="https://dappsforbeginners.wordpress.com/tutorials/javascript-api-2/" rel="noreferrer">there is some way to map functions into JS</a>, but variables appear to only be accessible through <a href="https://dappsforbeginners.wordpress.com/tutorials/javascript-api-1/" rel="noreferrer">a complicated way</a>:</p>

<pre><code>var storageObject = web3.eth.storageAt(contractAddress);
document.getElementById('fullName').innerText = web3.toAscii(storageObject['0x']) + ' ' + web3.toAscii(storageObject['0x01']);
</code></pre>

<ethereum><javascript><smartcontracts>
Is there a way to access Ethereum Smart Contract's variables by name in JavaScript?
ThePiachu
# Is there a way to access Ethereum Smart Contract's variables by name in JavaScript?

(Asked by: username_0 on 2015-06-18)

Say I have a simple smart contract:
contract Simple {
    string32 message;

    function Simple() {
        message = "Hello world!";
    }
}

How can I easily access the message variable by name in JavaScript? It looks like there is some way to map functions into JS, but variables appear to only be accessible through a complicated way:
var storageObject = web3.eth.storageAt(contractAddress);
document.getElementById('fullName').innerText = web3.toAscii(storageObject['0x']) + ' ' + web3.toAscii(storageObject['0x01']);

**Tags:** ethereum, javascript, smartcontracts


---

## 3 answers

### Answer by username_1 on 2015-06-18. Score: 7

Note that this question has been automatically mapped to [reddit](https://www.reddit.com/r/ethereum/comments/3a8oi0/is_there_a_way_to_access_ethereum_smart_contracts/) and answered there.

If you declare your state variables as `public`, a function to read their values will be automatically created by the compiler ("[accessor function](https://github.com/ethereum/wiki/wiki/Solidity-Tutorial#accessor-functions)"). If you use the [rich interface provided by web3.js](https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract), you can create a javascript contract object and just call `message()` on that to get the respective value.

For arrays and mappings, the compiler will generate a function that has additional arguments which correspond to the indexes. For example, for a variable `mapping(uint => uint[]) public data;` the function will be equivalent to `function data(uint x, uint y) constant returns (uint) { return data[x][y]; }`.

> **Comments:**
> **username_0**: Hmm, I tried using the `public` function. I added the function to the contract descriptor, but now that I call it, the javascript thinks I want to send a transaction to the contract to invoke that function...  
> **username_2**: Which version of web3.js are you using? The most current one should decide between a local call and a transaction automatically, with older ones you had to be explicit.  Can you post the JSON for the ABI for the contract?  
> **username_0**: @RobMyers I'm trying to run it in AlethZero v0.8.2 . My current test code is available at https://github.com/username_0/EtherTest .  
> **username_1**: You seem to use an outdated solidity compiler. `string32` has been replaced by `bytes32` and also your interface json looks incomplete.  
> **username_0**: @username_1 Hmm, interesting. I will have to give a "cutting edge" version a go then...  
> **username_3**: @username_1 Can you explain what you mean about the "accessor function"?  I went to that link, and it's been moved, and the phrase "accessor function" isn't anywhere on the new page.  What I want is to access a string that I coded into a contract.  Preferably I want to see it on the blockchain explorer.  
> **username_1**: @stone212 the term was renamed to "getter" in the meantime.  
> **username_4**: Who pays the cost of reading that data and where does the request go? Is it something that ETH nodes have to provide for free?  

---

### Answer by username_5 on 2016-05-09. Score: 1

Here is a JavaScript code for Node.JS to read a 

You need the contract Solidity source code to get ABI
With this ABI and web3 convenience methods you can read the contract data

Example:
var Web3 = require('web3');
var solc = require("solc");
var fs = require('fs');

// Connect to a geth server over JSON-RPC
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

console.log("Talking with a geth server", web3.version.api);

// Read standard token contract from https://www.ethereum.org/token
var sourceCode = fs.readFileSync('sol/token.sol','utf8')
var compiled = solc.compile(sourceCode);
var abiArray = compiled.contracts["MyToken"].interface;
abiArray = JSON.parse(abiArray);

// Create a proxy object to access the smart contract
var MyContract = web3.eth.contract(abiArray);

// instantiate by address
var address = "0x091cc7F4ACA751a6b8A4101715d6B07CD4232341";
var contractInstance = MyContract.at(address);

// All public variables have automatically generated getters
// http://bitcoin.stackexchange.com/a/38079/5464
var result = {
  "totalSupply": contractInstance.totalSupply(),
  "symbol": contractInstance.symbol(),
  "name": contractInstance.name(),
};
console.log(JSON.stringify(result));

> **Comments:**
> **username_6**: yeah! thank you very much! save me a lot of time! save my day!  

---

### Answer by username_7 on 2015-06-18. Score: 1

You should check out `web3.eth.contract` - it should have exactly what you need. <https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract>


---

