web3.eth 是 web3.js 库中用于以太坊区块链交互的主要对象,它提供了一系列的方法来获取区块链数据,发送交易,操作智能合约等。
web3.eth.getAccounts()
Promise 返回一个包含账户地址的数组。web3.eth.getAccounts().then(console.log);
web3.eth.getBlockNumber()
Promise 返回当前的区块号。web3.eth.getBlockNumber().then(console.log);
web3.eth.getBlock(blockHashOrBlockNumber)
blockHashOrBlockNumber (String|Number): 区块号或区块哈希。Promise 返回区块的详细信息。web3.eth.getBlock('latest').then(console.log);
web3.eth.sendTransaction(transactionObject)
transactionObject (Object): 交易对象,包含如 from, to, value, data 等字段。Promise 返回交易的收据。web3.eth.sendTransaction({from: '0x...', to: '0x...', value: '1000000000000000'}).then(console.log);
web3.eth.call(callObject, blockNumber)
callObject (Object): 调用对象,包含 to, data 等字段。blockNumber (Number|String): 可选,指定从哪个区块的状态读取数据。Promise 返回调用结果。web3.eth.call({to: '0x...', data: '0x...'}).then(console.log);
web3.eth.subscribe('newBlockHeaders')
web3.eth.subscribe('newBlockHeaders', function(error, result) {
if (!error) {
console.log(result);
}
})
.on("data", function(blockHeader){
console.log(blockHeader);
});
声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开发者社区:孟斯特