geth常用命令记录

【geth常用命令记录】geth常用命令记录
mygeth.exe --datadir "%cd%\chain" –rpc –rpcaddr "127.0.0.1" –rpcport "8545" --rpcapi "web3,eth,personal"
参数解释:

  • -rpc -rpcaddr “127.0.0.1”,设置rpc请求地址
  • -rpc -rpcport “8545”,设置rpc的端口
  • --rpcapi “web3,eth,personal”,启动web3,eth,personal,提供rpc服务,让web3.js可以在控制台中调用上述几个命令。
  • --nodiscover 私有链地址,不会被网上看到
#!/usr/bin/env node #使用node.js连接geth,通过代码调用geth的命令 var Web3 = require('web3'); var web3 = new Web3(); web3.setProvider(new web3.providers.HttpProvider('http://127.0.0.1:8545')); console.log("web3 info is :" + web3) var accounts = web3.eth.accounts; var arrsAccount = accounts.toString().split(",") arrsAccount.forEach(function (item, index) { console.log(item + "---" + index) }) console.log("账户集合:" + accounts) var coinbase = web3.eth.accounts[0]; // unlock account web3.personal.unlockAccount(coinbase, "aaa111") console.log(coinbase); var balance = web3.eth.getBalance(coinbase); // println balance console.log("余额是:" + balance.toString(10)); //web3.eth.sendTransaction("from:0xf183833eabae0658cabb46e7d6f45f4de654471d", "0xa525a870e87b804edba7cc0906380a4d514e48c3", 10) var result = web3.eth.sendTransaction({ from: "0xf183833eabae0658cabb46e7d6f45f4de654471d", to: "0xa525a870e87b804edba7cc0906380a4d514e48c3", value: 10 }) console.log("交易结果:" + result)

    推荐阅读