var Web3 = require('web3');
var web3=new Web3(new Web3.providers.HttpProvider("http://1127.0.0.1:9545"));
// 1 返回以太坊js api版本
var version = web3.version.api;
// 2 返回客户端节点版本
version = web3.version.node;
// 3 返回网络协议版本
version = web3.version.network;
// 4 返回以太坊协议版本
version = web3.version.ethereum;
// 5 耳语协议版本(搞不懂,意思是说悄悄话的协议???)
// version = web3.version.whisper;
// console.log(version);
// 6
var hash = web3.sha3("Some string to be hashed");
var hashOfHash = web3.sha3(hash, {encoding: 'hex'});
// 7 转16进制
var str = web3.toHex('jiashiqiang');
// 8 根据交易hash查询交易信息
var transaction = web3.eth.getTransaction('0xa421f27668d34966d5a7660e17a99ec11566dbf399094d85b983dd8fb66238d7');
// { blockHash: '0x7d3a4da3199ffba4b741d038061f150577c620a9746274759978f0a03bad0eef',
// blockNumber: 6500000,
// chainId: '0x3d',
// condition: null,
// creates: null,
// from: '0x3c2f057f36422f99c52ba6da4a2576b88453bdf0',
// gas: 21000,
// gasPrice: BigNumber { s: 1, e: 4, c: [ 21000 ] },
// hash: '0xa421f27668d34966d5a7660e17a99ec11566dbf399094d85b983dd8fb66238d7',
// input: '0x',
// nonce: 4062,
// publicKey: '0x48b7c3da51cc64bd9d3e1ce01c46f44d89c3a5ad71f7d0e07d52175caaf04d3604cf0a3b311be7d94e2f39d9f89005f5fd4880c33da1573ee86dd872e6b90bb0',
// r: '0x366698126dc8b01806c0ce287a8ef87fe78afd811095187cc1674e235aae0851',
// raw: '0xf86c820fde825208825208942db3b38be92c7b1305f5b841c0d324fd251b88d188057c3e49561c700080819da0366698126dc8b01806c0ce287a8ef87fe78afd811095187cc1674e235aae0851a06d717745833f3dbf66bdc907813acf74fc93b0bbacec84b6627a12de198207a1',
// s: '0x6d717745833f3dbf66bdc907813acf74fc93b0bbacec84b6627a12de198207a1',
// standardV: '0x0',
// to: '0x2db3b38be92c7b1305f5b841c0d324fd251b88d1',
// transactionIndex: 0,
// v: '0x9d',
// value: BigNumber { s: 1, e: 17, c: [ 3952, 59352000000000 ] } }
// 9 检查web3是否已连接
var b = web3.isConnected();
// true
// 10 查看当前Provider 例如:http://127.0.0.1:9545
var provider = web3.currentProvider;
// HttpProvider {
// host: 'http://127.0.0.1:9545',
// timeout: 0,
// user: undefined,
// password: undefined,
// headers: undefined }
// 11 十六进制、Ascii互转
var str = web3.toAscii("0x657468657265756d000000000000000000000000000000000000000000000000");
// ethereum
str = web3.fromAscii('ethereum');
// 0x657468657265756d000000000000000000000000000000000000000000000000
// 12 十六进制、number互转
var number = web3.toDecimal('0x15');
// 21
var value = web3.fromDecimal('21');
// 0x15
// 13 wei和以下单位互转(
// kwei/ada
// mwei/babbage
// gwei/shannon
// szabo
// finney
// ether
// kether/grand/einstein
// mether
// gether
// tether
// )
var value = web3.fromWei('21000000000000', 'finney');
// 0.021
var value = web3.toWei('1', 'ether');
// 1000000000000000000
// 14 查看给定的字符串是不是一个地址
var isAddress = web3.isAddress("0x8888f1f195afa192cfee860698584c030f4c9db1");
// true
isAddress = web3.isAddress("sdkjfghisdfhs");
// false
// 15 检查当前这个节点是不是正在主动监听网络连接
var listening = web3.net.listening;
// true or false
// 16 显示当前正在连接的客户端数
var peerCount = web3.net.peerCount;
// 17 web3.eth里配置的默认地址,当然你也可以覆盖他自己指定
var defaultAccount = web3.eth.defaultAccount;
// undefined
web3.eth.defaultAccount = '0x3E319427fc42C85c949ba84A320BdFE0f85b14d3';
// 18 web3.eth里配置的默认块,当然你也可以覆盖他自己指定
var defaultBlock = web3.eth.defaultBlock;
// latest
web3.eth.defaultBlock='6499999';
// var transactionCount = web3.eth.getTransactionCount();
// 19
// var sync = web3.eth.syncing;
// 20
// web3.eth.isSyncing(function(error, sync){
// if(!error) {
// // stop all app activity
// if(sync === true) {
// // we use `true`, so it stops all filters, but not the web3.eth.syncing polling
// web3.reset(true);
// // show sync info
// } else if(sync) {
// console.log(sync.currentBlock);
// // re-gain app operation
// } else {
// // run your app init function...
// }
// }
// });
// 21
var coinbase = web3.eth.coinbase;
// 22 返回节点是否正在挖矿
var mining = web3.eth.mining;
// true or false
// 23
// var gasPrice = web3.eth.gasPrice;
// console.log(gasPrice.toString(10));
// 1000000000
// 24 返回由当前节点控制的所有账户的数组数据
var accounts = web3.eth.accounts;
// []
// 25 返回当前高度
var number = web3.eth.blockNumber;
// 26 这个方法0.20.7版本还没做
// web3.eth.register("0x407d73d8a49eeb85d32cf465507dd71d507100ca");
// 27 获取高度为6933756时账户'0x5D4057c5e2dE1CF089E21FD0e5dfD4d05c4DD2f4'的余额(单位是 wei)
var balance = web3.eth.getBalance("0x5D4057c5e2dE1CF089E21FD0e5dfD4d05c4DD2f4", 6933756);
// 91631559421210781
// console.log(web3.fromWei(balance.toString(10), 'ether'));
// 28 查询块信息,参数也可以是块hash
var info = web3.eth.getBlock(6933756);
var info = web3.eth.getBlock('0x8a04c5c6cb8538c030b791956c512f5d9f8e4ed838a2bb1c338a9d9f1725dc4d');
// { author: '0x1c0fa194a9d3b44313dcd849f3c6be6ad270a0a4',
// difficulty: BigNumber { s: 1, e: 14, c: [ 1, 75772127167507 ] },
// extraData: '0x66726131',
// gasLimit: 7955148,
// gasUsed: 249970,
// hash: '0x8a04c5c6cb8538c030b791956c512f5d9f8e4ed838a2bb1c338a9d9f1725dc4d',
// logsBloom: '0x00000000008000000000001000000000400000010000000000000000000080000000000000000000000000000018000000000000000000400000000000000000014000000600000080010008000000000000000002001000040000000000000000040000000000000000000000000000004400000002000000000010000000800000000100000001000000000000008000010000004000800000000000001000000000000000000000000000000000000000000000000020000010000000000000000002000000010000000000000000000000000000001000100000000000000000000001000000000000000000000400000080000000000000000400000000',
// miner: '0x1c0fa194a9d3b44313dcd849f3c6be6ad270a0a4',
// mixHash: '0x835f8e452ad8875f59905aa215169c7988e1f440666ce8d97cd1133c4595b947',
// nonce: '0x94a3da6011b4d665',
// number: 6933756,
// parentHash: '0xa9bb24a23fa4ed88bf3e2f9fdae0925ad7e7afe21a06f59fa3953fa9a4909678',
// receiptsRoot: '0xdbced9f649ca929299725ada10c118762fc4c6dc5216b8935641c62da246aa20',
// sealFields:
// [ '0xa0835f8e452ad8875f59905aa215169c7988e1f440666ce8d97cd1133c4595b947',
// '0x8894a3da6011b4d665' ],
// sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
// size: 1372,
// stateRoot: '0x56682e16b04303dae69aacf2280ec44de21b816891008d521b1328a90972b6ea',
// timestamp: 1542269098,
// totalDifficulty: BigNumber { s: 1, e: 20, c: [ 4964191, 2391526147883 ] },
// transactions:
// [ '0x03885a25d805d3efaecb841a4bfeb982fc03c54f9d4193f1bd9006e0b3047878',
// '0x9801edb5d972e0b8e467a20278dd369dc477392d98bd5a2ffebcb167dc872b17',
// '0x3a906b94f4a7efb6458d93c01b5fb6a01a92c4b603ff4b9f9cb3fdfbd14df492',
// '0xfe8d301408314a7ea9ef67ea4e66dd3a300f3ab3c5b0b9dde46927a1b5d37a2c',
// '0x67a2e0b0f5f788cc171c15689c638c2388d884e4033057e1ca8f590462314d2b',
// '0x33406aedf2781e9e393f8a3c50f3abc36e87abc7344d9a09484b7132efb6766c',
// '0x31542c6b763a9073bd141854c270d5ceb71969e084f9de32d2afb38dcee3caf2' ],
// transactionsRoot: '0xb5c2b48124f9e271d9ecac7a952ee005315ee1a2993bf46538892887f62af04b',
// uncles: [] }
// 29 查询这个块的交易条数,参数也可以是块hash
var number = web3.eth.getBlockTransactionCount(6933756);
var number = web3.eth.getBlockTransactionCount('0x8a04c5c6cb8538c030b791956c512f5d9f8e4ed838a2bb1c338a9d9f1725dc4d');
// 7
// 30 查询高度为6933756块的第三笔交易信息,第一个参数也可以是块hash
var transaction = web3.eth.getTransactionFromBlock(6933756, 2);
// { blockHash: '0x8a04c5c6cb8538c030b791956c512f5d9f8e4ed838a2bb1c338a9d9f1725dc4d',
// blockNumber: 6933756,
// chainId: '0x3d',
// condition: null,
// creates: null,
// from: '0xe592b0d8baa2cb677034389b76a71b0d1823e0d1',
// gas: 100000,
// gasPrice: BigNumber { s: 1, e: 10, c: [ 20000000000 ] },
// hash: '0x3a906b94f4a7efb6458d93c01b5fb6a01a92c4b603ff4b9f9cb3fdfbd14df492',
// input: '0x',
// nonce: 260999,
// publicKey: '0x081a232e1d820c16d560272666e895ec0dbd006eae8bc28a8409abe0d6e92bc4c48e1e2a1a4efc54dd688e850572a4acfa03a799117059a24a2fd3d82e5bdc25',
// r: '0xbc16d9af0f0a9aa96818fb101ac1c348aacc0d1f3d3721d18e69eb92abccdce6',
// raw: '0xf8718303fb878504a817c800830186a0940c951c715dffd9be3bdd7d0d1c52b5853347463b885320c1437c97000080819da0bc16d9af0f0a9aa96818fb101ac1c348aacc0d1f3d3721d18e69eb92abccdce6a02d6e211950d31a32d125cab6e564d732e8dba812dbf8c68a642e4927b3f1f732',
// s: '0x2d6e211950d31a32d125cab6e564d732e8dba812dbf8c68a642e4927b3f1f732',
// standardV: '0x0',
// to: '0x0c951c715dffd9be3bdd7d0d1c52b5853347463b',
// transactionIndex: 2,
// v: '0x9d',
// value: BigNumber { s: 1, e: 18, c: [ 59900 ] } }
// 31 返回交易收据,参数时交易hash
var receipt = web3.eth.getTransactionReceipt('0x3a906b94f4a7efb6458d93c01b5fb6a01a92c4b603ff4b9f9cb3fdfbd14df492');
// { blockHash: '0x8a04c5c6cb8538c030b791956c512f5d9f8e4ed838a2bb1c338a9d9f1725dc4d',
// blockNumber: 6933756,
// contractAddress: null,
// cumulativeGasUsed: 63080,
// gasUsed: 21000,
// logs: [],
// logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
// root: '0x1791dfe2d2ac82100c561e8578addd3dc72678cc023d3fc39479db9f98f6c787',
// status: null,
// transactionHash: '0x3a906b94f4a7efb6458d93c01b5fb6a01a92c4b603ff4b9f9cb3fdfbd14df492',
// transactionIndex: 2 }
// 32 查询这个账户总共有几笔支出交易, 后面时高度
var number = web3.eth.getTransactionCount("0x0953594a6E4020316AdD22BEf27473401F8E2E20", 6933755);
// 2
var number = web3.eth.getTransactionCount("0xe592b0d8baa2cb677034389b76a71b0d1823e0d1", 6933756);
// 261000
// 33 向本地leveldb数据库testDB中插入key-value数据 'key':'myString',成功返回true
web3.db.putString('testDB', 'key', 'myString');
// true
// 34 根据key查数据
var value = web3.db.getString('testDB', 'key');
// 35 插入value为十六进制的数据时调用此方法,成功返回true
web3.db.putHex('testDB', 'key', '0x4f554b443');
// true
// 36 根据key获取十六进制数据
var value = web3.db.getHex('testDB', 'key');
// true
console.log(b);