web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

How to use the function in contract by web3.js 0.20.0v

Open theWangs opened this issue 8 years ago • 1 comments

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. (D:\blockchain\node\HelloWord.js:20:42) at Module._compile (module.js:643:30) at Object.Module._extensions..js (module.js:654:10) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3)

theWangs avatar Mar 01 '18 06:03 theWangs

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!

harterc3 avatar Mar 04 '18 13:03 harterc3