Almost all of the docs out there refer to the 0.20 branch of web3.js which is the current stable branch. The version with promises as above is the 1.0.0-beta branch, which as of a few days ago npm installs by default.
As for sample code, the following code, almost identical to yours, is working correctly for me from the node console against my own ERC20 token running with testrpc locally.
The main point of difference I think is only the amount transferred. The toWei() method is not really applicable to ERC20 tokens and may confuse things.
Also, if you are on mainnet, it's going to take a while for the send() to get mined and the promise won't resolve until then. You may need to set the gasPrice option on the contract to a few GWei. On reflection, this is definitely an issue. (The call() methods are processed locally so there's no waiting and no gas cost.)
But then, if it's not asking you to unlock an account, I don't know... It may be well worth setting up testrpc locally (testrpc -d
is useful) and deploying an ERC20 contract locally to check everything out without the added complications of paying for gas etc.
Anyway, perhaps this sample is useful to you or to others.
> const Web3 = require('web3');
undefined
> const web3 = new Web3('http://localhost:8545');
undefined
> web3.version
'1.0.0-beta.11'
> fs = require('fs');
<blah>
> const abi = fs.readFileSync('erc20_abi.json', 'utf-8');
undefined
> const contract = new web3.eth.Contract(JSON.parse(abi), '0xafb7b8a4d90c2df4ce640338029d54a55bedcfc4', { from: '0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1', gas: 100000});
undefined
> contract.methods.balanceOf('0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1').call().then(console.log).catch(console.error);
Promise {...}
> 99997
> contract.methods.transfer('0xffcf8fdee72ac11b5c542428b35eef5769c409f0', 1).send().then(console.log).catch(console.error);
Promise {...}
> { transactionHash: '0xf2d621ba5029a086e212d87fab83be31c3fa41fe47198c378f55c252e5b25d5b',
transactionIndex: 0,
blockHash: '0x0e806bf3e88f9335ee9be903303a2393c940ab22f4a73c7e28ca8d9a212ffa4e',
blockNumber: 429,
gasUsed: 35206,
cumulativeGasUsed: 35206,
contractAddress: null,
events:
{ Transfer:
{ logIndex: 0,
transactionIndex: 0,
transactionHash: '0xf2d621ba5029a086e212d87fab83be31c3fa41fe47198c378f55c252e5b25d5b',
blockHash: '0x0e806bf3e88f9335ee9be903303a2393c940ab22f4a73c7e28ca8d9a212ffa4e',
blockNumber: 429,
address: '0xAFB7b8A4d90C2Df4ce640338029d54A55BEDcfC4',
type: 'mined',
id: 'log_bfd297b0',
returnValues: [Object],
event: 'Transfer',
signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
raw: [Object] } } }
> contract.methods.balanceOf('0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1').call().then(console.log).catch(console.error);
Promise {...}
> 99996