在testrpc环境下学习web3.js的命令
环境
ubuntu 16.04, 64位
testrpc的安装过程可以参考以前的文章。
【在testrpc环境下学习web3.js的命令】直接启动testrpc
pony@pony-virtual-machine:~$ testrpc
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)Available Accounts
==================
(0) 0x20aa72706554abd905de23b86a90985e21c7358d
(1) 0x308efa51779f899ff946b83d1e7ecf16b801c0b3
(2) 0x34389038cd130004f1f580819b0c82df6c6266e7
(3) 0xb4e9e45ff3161c0f7263ceef6ea340d85391980c
(4) 0x036529b97a340a4f60a6017063317c98e2c1e1fd
(5) 0x46bb5ff2dfe6fc43652351cad501eed6766bb92c
(6) 0xab49534cfb489642b0c384f44063a76ea8aa60aa
(7) 0xff2c41eeb85444ced3c279c898ce45b941401119
(8) 0x0aad45abf4efd8108eaf546372ade58941288b60
(9) 0x80ac11a1e69dc37a28e4cefd4310db8e303609e4Private Keys
==================
(0) ddacdbe2dcc3ebeb89e77193ef4dedbd009d4977151e2f7d39efcdbc420d2d40
(1) 44953cfb0592764e459342f1512a03cd6b8263e8b9c0ac2c3246bb550d953ebc
(2) e8c6467d2af56a9d35793f36a6f6bc48b93d5aa6209c36708b0945e1165cee38
(3) c4b5dc3392b204070598ad78d562a4fc4b8697a3f4d01410d120896cbe229530
(4) f3f4a33807a5653809d056db824f193f9645094bef6328a8b0c3f5638406571b
(5) a2582972fd2f27bb8ed20024b896a67b021c8801081fbd98a13f02058cb74ac8
(6) 411ae9da8a9958e60008cc8fa21b72b1bf820276fd6103e480f2ceb3a88cd4f3
(7) 661687407667b796047ab89156de31b1b10bde43f675f5442bf62f4be2f46042
(8) 81800b1afa6e31c5f0a6483ab0a40be40238e235823555f2b516ce1ecdf72aa7
(9) 54b3412a5c09d659eb81ee0b97e828960ce49b6529d2514089f52f3aa69c44cdHD Wallet
==================
Mnemonic:coach fluid lobster stereo enrich rural invest end viable title anxiety priority
Base HD Path:m/44'/60'/0'/0/{account_index}Listening on localhost:8545
默认启动10个账户,我们接下来的部分命令会用到这些账户。
另外开启一个终端,进入到一个truffle的目录(如何初始化并配置请参考我以前的文章),输入truffle console,启动truffle控制台。
wei和其它以太坊货币单位之间的转换
先看下这些货币单位之间的汇率关系,
单位 | 价值 | Wei |
---|---|---|
wei | 1 | 1 wei |
Kwei(babbage) | 1e3 wei | 1,000 |
Mwei (lovelace) | 1e6 wei | 1,000,000 |
Gwei (shannon) | 1e9 wei | 1,000,000,000 |
microether (szabo) | 1e12 wei | 1,000,000,000,000 |
milliether (finney) | 1e15 wei | 1,000,000,000,000,000 |
ether | 1e18 wei | 1,000,000,000,000,000,000 |
truffle(development)> web3.fromWei('21000000000000', 'finney');
'0.021'
truffle(development)> web3.fromWei('21000000000000', 'gwei');
'21000'
truffle(development)> web3.fromWei('21000000000000', 'kwei');
'21000000000'
truffle(development)> web3.fromWei('21000000000000', 'ether');
'0.000021'
反过来,把其它以太单位转换成wei
truffle(development)> web3.toWei('1', 'ether');
'1000000000000000000'
truffle(development)> web3.toWei('1', 'kwei');
'1000'
truffle(development)> web3.toWei('1', 'gwei');
'1000000000'
truffle(development)> web3.toWei('1', 'finney');
'1000000000000000'
查看账户列表和余额
truffle(development)> var address0=web3.eth.accounts[0];
undefined
truffle(development)> console.log(address0.toString(10));
0x20aa72706554abd905de23b86a90985e21c7358d
undefined
truffle(development)> var balance = web3.eth.getBalance(address0);
undefined
truffle(development)> console.log(balance)
BigNumber { s: 1, e: 20, c: [ 1000000 ] }
truffle(development)> console.log(balance.toNumber());
100000000000000000000
这里查询第一个账户的余额,console.log(balance)返回的是nodejs里大数的表示法,理论上我们不需要关心s,e,c表示什么意思。我们可以用toNumber方法转换成可读的表示方法。
这里查询到余额是100 ETH(事实上缺省下所有账户都是这个余额)。
在两个账户之间进行以太币转账
truffle(development)> acc0 = web3.eth.accounts[0]
'0x20aa72706554abd905de23b86a90985e21c7358d'
truffle(development)> acc1 = web3.eth.accounts[1]
'0x308efa51779f899ff946b83d1e7ecf16b801c0b3'
truffle(development)> amount = web3.toWei(0.01,'ether')
'10000000000000000'
truffle(development)> web3.eth.sendTransaction({from:acc0,to:acc1,value:amount});
'0x066162686579156223f05120724a9bde9c8d8987b69c50bcd7eaad2009cfec6c'
最后返回的是交易的hash值。
转账完成后看下两个账户的余额,
账户0
truffle(development)> var balance = web3.eth.getBalance(acc0);
undefined
truffle(development)> console.log(balance.toNumber());
99989999999999980000
undefined
truffle(development)> web3.fromWei('99989999999999980000','ether')
'99.98999999999998'
账户1
truffle(development)> var balance = web3.eth.getBalance(acc1);
undefined
truffle(development)> console.log(balance.toNumber());
100010000000000000000
undefined
truffle(development)> web3.fromWei('100010000000000000000','ether')
'100.01'
truffle(development)>
账户0因为有些gas消耗,所以会比少一些。
更多命令参考:
https://github.com/ethereum/wiki/wiki/JavaScript-API