Here is a JavaScript code for Node.JS to read a
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));
Hmm, I tried using the
publicfunction. 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 – 2015-06-18T19:59:43.707Which 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 – 2015-06-21T00:23:43.023
@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 – 2015-06-22T01:07:47.707You seem to use an outdated solidity compiler.
string32has been replaced bybytes32and also your interface json looks incomplete. – chriseth – 2015-06-22T10:06:36.710@chriseth Hmm, interesting. I will have to give a "cutting edge" version a go then... – ThePiachu – 2015-06-23T19:28:05.797
@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 – 2017-11-09T05:55:04.290
@stone212 the term was renamed to "getter" in the meantime. – chriseth – 2017-11-13T17:29:03.850