How to use the function in contract by web3.js 0.20.0v
var Web3 = require('web3'); var Tx = require('ethereumjs-tx'); var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); var abi =xxxxxxx; var myContractInstance = MyContract.at('0x5cB9e85d9610A183e80600dB21ED626e6ad673a5'); var txhash = myContractInstance.transfer.sendTransaction('0x264BE6314ad5e811b7997b78FAfb665c81F9b75d', 100, {from:'0xd30f94a694F8b432E5aC9a6f0e4F8a3C8d557267'});
Error: authentication needed: password or unlock
at Object.InvalidResponse (D:\blockchain\node\node_modules\web3\lib\web3\errors.js:38:16)
at RequestManager.send (D:\blockchain\node\node_modules\web3\lib\web3\requestmanager.js:61:22)
at Eth.send [as sendTransaction] (D:\blockchain\node\node_modules\web3\lib\web3\method.js:145:58)
at SolidityFunction.sendTransaction (D:\blockchain\node\node_modules\web3\lib\web3\function.js:170:26)
at Object.
I think this is just a matter of making sure you set a default account for the user. I'm using Ganache so the situation may be a bit different for Parity or Testnet (Parity has given me a prompt in the browser to enter my account password in the past). I did something like this on page load:
web3.eth.getAccounts((error, result) => {
const defaultAccount = web3.eth.defaultAccount;
if(!defaultAccount){
web3.eth.defaultAccount = result[0];
}
});
Good luck!