Web3 Api學習
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);