第4章 开始使用web3.js
- web3.js目前已经到1.0.0-beta18
- 接上一章交互控制台继续试验
console.log(web3.eth.gasPrice.toString());
因为本机刚开始挖矿,gas价格是1
- 查看余额(因示例的45区块太大,修改第二个参数“区块”为2)
console.log(web3.eth.getBalance("0x1628cb0fE5e3fFE930C4Fb13Ba3C0d032DD6cadE",2).toString());
第一个参数是地址
- 查看交易细节(无法打印对象,但可以直接输出)
web3.eth.getTransactionReceipt("0x3fc10b7326cc5f4211ac6c665c3370b2d00a1b433659cfb1e48da8d0b884ae01");
输出类似如下形式
{
blockHash: "0x47c72a27d214838de126ca4057e5760ae45ab5e285104eddde2b9b958646938a",
blockNumber: 1,
contractAddress: "0xf095b1932ee9e6424b328bdf518d617503c7c0f7",
cumulativeGasUsed: 574693,
from: "0x1628cb0fe5e3ffe930c4fb13ba3c0d032dd6cade",
gasUsed: 574693,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
status: "0x1",
to: null,
transactionHash: "0x3fc10b7326cc5f4211ac6c665c3370b2d00a1b433659cfb1e48da8d0b884ae01",
transactionIndex: 0
}
参数为交易号,而非帐户地址。
- 章节4.1.9的程序代码,感谢简述的Solidity高亮
var event = proof.logFileAddedStatus(null, {
fromBlock: 0,
toBlock:"latest"
});
event.get(function(error, result){
if(!error)
{
console.log(result);
}
else
{
console.log(error);
}
});
event.watch(function(error, result){
if(!error)
{
console.log(result.args.status);
}
else
{
console.log(error);
}
});
setTimeout(function(){
event.stopWatching();
}, 60000);
var events = proof.allEvents({
fromBlock: 0,
toBlock: "latest"
});
events.get(function(error, result){
if(!error)
{
console.log(result);
}
else
{
console.log(error);
}
});
event.watch(function(error, result){
if(!error)
{
console.log(result.args.status);
}
else
{
console.log(error);
}
});
setTimeout(function(){
event.stopWatching();
}, 60000);