Web3js 06: 交易操作

前言

求其区块链团队出品

旗下网站


交易操作

1. 获取指定块中特定账户地址的余额

函数原型

web3.eth.getBalance(address [, defaultBlock] [, callback])
defaultBlock:表执行到指定的区块时的余额
区块号
区块的 hash 值
字符串 “earliest”、“latest”、“pending

示例

web3.eth.getBalance('0x956188a6bD41694BdB13AA5CE168543a03B74770', function(error, result) {
    if (error) {
        console.log("something error.")
    } else {
        var balance = result.toString()
        console.log(web3.utils.fromWei(balance, "ether"))
    }
})

输出

0

2. 查询平均 gas 价格

获取当前 gas 价格,该价格由最近的若干块的 gas 价格中值决定

web3.eth.getGasPrice().then(console.log)

输出

20000000000

3. 发送交易

var transactionObject = {
    from: '0xb588f29312fA735457C21436f12Ce247B6476fB6',
    to: '0x9e91F5c51C69870f74106FC4A9589dad6dCAf4C6',
    value: web3.utils.toWei('1', 'ether'),
    data: web3.utils.toHex(234)
}

web3.eth.sendTransaction(transactionObject).then(console.log)

输出

{
  transactionHash: '0x1e7b41544f9e71ccee670d8a3560376446c49743ee64f07c5e36a17be707877e',
  transactionIndex: 0,
  blockHash: '0x5ee052595201447637a17973e7493a8fef06e543022c389b80e904c272ae28b0',
  blockNumber: 1,
  from: '0xb588f29312fa735457c21436f12ce247b6476fb6',
  to: '0x9e91f5c51c69870f74106fc4a9589dad6dcaf4c6',
  gasUsed: 21016,
  cumulativeGasUsed: 21016,
  contractAddress: null,
  logs: [],
  status: true,
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'                                                                      
}

4. 查询交易信息

函数原型

web3.eth.getTransaction(transactionHash [, callback])	返回具有指定哈希值的交易对象
  • transactionHash - 交易哈希

示例

web3.eth.getTransaction('0x1e7b41544f9e71ccee670d8a3560376446c49743ee64f07c5e36a17be707877e').then(console.log)

输出

{
  hash: '0x1e7b41544f9e71ccee670d8a3560376446c49743ee64f07c5e36a17be707877e',
  nonce: 0,
  blockHash: '0x5ee052595201447637a17973e7493a8fef06e543022c389b80e904c272ae28b0',
  blockNumber: 1,
  transactionIndex: 0,
  from: '0xb588f29312fA735457C21436f12Ce247B6476fB6',
  to: '0x9e91F5c51C69870f74106FC4A9589dad6dCAf4C6',
  value: '1000000000000000000',
  gas: 90000,
  gasPrice: '20000000000',
  input: '0xea',
  v: '0x26',
  r: '0x9d02df03d72fe5099192e3348d74da0a6f5f00645c48a19bb4f99ac22583b2a',
  s: '0x51a7939ed360fbfa466c8431e5279e03f0642a45a7bcd8d63e32071053d89517'
}

5. 查询交易收据

函数原型

web3.eth.getTransactionReceipt(hash [,callback])
//返回指定交易的收据对象,如果交易处于 pending 状态,则返回 null

示例

web3.eth.getTransactionReceipt('0x11e1559508147c7ea2d3e9ce0dfb65be29d42d7a9077f9cef3e80ad64d38b3b8').then(console.log)

输出

{
  transactionHash: '0x1e7b41544f9e71ccee670d8a3560376446c49743ee64f07c5e36a17be707877e',
  transactionIndex: 0,
  blockHash: '0x5ee052595201447637a17973e7493a8fef06e543022c389b80e904c272ae28b0',
  blockNumber: 1,
  from: '0xb588f29312fa735457c21436f12ce247b6476fb6',
  to: '0x9e91f5c51c69870f74106fc4a9589dad6dcaf4c6',
  gasUsed: 21016,
  cumulativeGasUsed: 21016,
  contractAddress: null,
  logs: [],
  status: true,
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'                                                                      
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值