In Mist you can call functions of contracts in two ways:
- On the left panel, to call functions that read from a contract (the result is immediate).
- On the right panel, to call functions that write into a contract (for this a new transaction is made, so it asks you to unlock the account, and some gas is spent).
I want to know how to do the equivalent with nodejs' web3.
In the first case, I already succeeded, with something like this, e.g. when the function I want to call is called foo()
, receives an account(string) and returns a uint256:
var Web3 = require('./node_modules/web3/lib/web3');
var fs = require('fs');
// dont override global variable
if (typeof window !== 'undefined' && typeof window.Web3 === 'undefined') {
window.Web3 = Web3;
}
module.exports = Web3;
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var theAddressWhereMyContractLives = "0xbb9bc244d798123fde783fcc1c72d3bb8c189413";
var abiContractContent = fs.readFileSync('./bin/Token.abi', 'utf8' );
var abiJsonContract = JSON.parse(abiContractContent);
var daoTokenContract = web3.eth.contract(abiJsonContract);
var contractInstance = daoTokenContract.at(theAddressWhereMyContractLives);
var accounts = web3.eth.accounts;
accounts.forEach(function(account) {
var result = contractInstance.foo(account);
}
However, I don't know how to do this to write into a contract. Because, if I do it in the same way (let's say the non-readonly function is called bar
and receives an address and a number):
contractInstance.bar(account, 1);
I get this error:
/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/formatters.js:271
throw new Error('invalid address');
^
Error: invalid address
at inputAddressFormatter (/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/formatters.js:271:11)
at inputTransactionFormatter (/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/formatters.js:97:20)
at /home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/method.js:89:28
at Array.map (native)
at Method.formatInput (/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/method.js:88:32)
at Method.toPayload (/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/method.js:114:23)
at Eth.send [as sendTransaction] (/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/method.js:139:30)
at SolidityFunction.sendTransaction (/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/function.js:133:26)
at SolidityFunction.execute (/home/myuser/Documents/Code/ethContractPlayground/node_modules/web3/lib/web3/function.js:219:37)
at Object.<anonymous> (/home/myuser/Documents/Code/ethContractPlayground/approver_test.js:57:18)
And I guess this is normal, because I didn't send a transaction. I guess I need to figure out the way to call bar(account,1)
inside the inputData payload of a transaction. How to do it with nodejs+web3? Thanks