测试nodejs chain3客户端 - coolcat2012/MoacDoc GitHub Wiki
安装nodejs
$ npm -v
6.1.0
但是出现错误:
$ npm install chain3 -g
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t https://github.com/frozeman/bignumber.js-nolookahead.git
npm ERR!
npm ERR! fatal: unable to access 'https://github.com/frozeman/bignumber.js-nolookahead.git/': The requested URL returned error: 504
npm ERR!
npm ERR! exited with error code: 128
好像是这个git连接已经不在了,运行了这个命令,再重试上面的命令就解决了
npm install -g ethereumjs-testrpc@beta
还是不行,好像是说需要指定版本号在本地的package.json文件,于是下载了一个:
git clone https://github.com/davekiss/web3-node-tutorial.git
然后把那个package.json修改那个web3,为:
"chain3": "latest",
然后在这个web3-node-tutorial目录下面,运行
npm install
就可以了,确认chain3已经安装成功了
ll node_modules
然后进入nodejs交互模式
Yuechaos-MacBook-Pro:web3-node-tutorial yuechaoma$ node
>
>
> var Chain3 = require('chain3');
undefined
> var chain3 = new Chain3();
undefined
> chain3.setProvider(new chain3.providers.HttpProvider('http://localhost:8545'));
undefined
> var networkId = chain3.version.network;
undefined
> console.log('network id:'+ networkId);
network id:102
undefined
> var listenState = chain3.net.listening;
undefined
> console.log(listenState);
true
undefined
> var nodeCoinbase = chain3.mc.coinbase;
undefined
> console.log(nodeCoinbase);
0x6f6595722544f892f0d1493a8883ce46ee371bd9
undefined
> var nodeAccounts = chain3.mc.accounts;
undefined
> console.log(nodeAccounts);
[ '0x6f6595722544f892f0d1493a8883ce46ee371bd9' ]
undefined
> var nowBlockNumber = chain3.mc.blockNumber;
undefined
> console.log(nowBlockNumber);
5202
undefined
> var balance = chain3.mc.getBalance("0x36eaa71d7383be53cb600743aad08a55222a4915");
undefined
> console.log("getBalance1" + balance); //instanceof BigNumber
getBalance10
undefined
> console.log("getBalance2" + balance.toString(10));
getBalance20
undefined
> var balance = chain3.mc.getBalance("0x6f6595722544f892f0d1493a8883ce46ee371bd9“)
var balance = chain3.mc.getBalance("0x6f6595722544f892f0d1493a8883ce46ee371bd9“)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid or unexpected token
> var balance = chain3.mc.getBalance("0x6f6595722544f892f0d1493a8883ce46ee371bd9")
undefined
> console.log(balance)
BigNumber { s: 1, e: 22, c: [ 102906689, 76624037165368 ] }
undefined
> console.log("b:" + balance)
b:1.0290668976624037165368e+22
undefined
> console.log("b:" + balance/10000000)
b:1029066897662403.6
undefined
> console.log("b:" + balance/1000000000000000000)
b:10290.668976624036
undefined
> var gasPrice = chain3.mc.gasPrice;//该值由最近几个块的gas价格的中值决定
undefined
> console.log(gasPrice.toString(10));
20000000000
undefined
> var getTheBlock = chain3.mc.getBlock(23)
undefined
> console.log('get the block :'+ JSON.stringify(getTheBlock ));
get the block :{"difficulty":"132416","extraData":"0xdc854d4f41432d85302e382e332d87676f312e392e328664617277696e","gasLimit":9000000,"gasUsed":0,"hash":"0x2765a7fdf74763496ac08e7256bdfd544dc9f1abd5c993eee16895393afa5535","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x6f6595722544f892f0d1493a8883ce46ee371bd9","mixHash":"0x95e88bd2736f091f8a808635d7f70a16d5d17596ca7755640a3dfc4edb469999","nonce":"0x11b3c49e4839e78e","number":23,"parentHash":"0x9aee460fad0cc606e7e8e9f6cc58b7f2561e55e33d1785d142772e86e912a735","receiptsRoot":"0x9287370eb27f11b0c2188431cbc58a23b685f02dbd851ed4d974f932bd780839","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":582,"stateRoot":"0x4beac739651769b05dd2208fda251a84e1e5d0c50b27480b23af3af27329faec","timestamp":1531168929,"totalDifficulty":"3029952","transactions":["0x03b167d213d5bdee0dc38c0ba52d24892dcecdd7006cd50c9d7120f24f292605"],"transactionsRoot":"0xc4949853cd96719c2e2179ea6fdfb2b38273c751e4c2437d55a3299689eff3a1","uncles":[]}
undefined
miner.start() web3不支持于是chain3也不支持。 可以在console里启动,也可以用moac命令运行。