Web3js 09: 交易小结

前言

求其区块链团队出品

旗下网站


交易小结

1. 手动签名

let Web3 = require("web3")
let web3 = new Web3(new Web3.providers.HttpProvider("HTTP://127.0.0.1:8545"))

// 发送者地址
const fromAddress = '0x123...';
// 接收者地址
const toAddress = '0x456...';
// 发送的金额
const etherAmount = web3.utils.toWei('1', 'ether');
// gas 配置
const gasPrice = web3.utils.toWei('10', 'gwei');
const gasLimit = 21000;

const transactionObject = {
   from: fromAddress,
   to: toAddress,
   value: etherAmount,
   gasPrice: gasPrice,
   gas: gasLimit
};

// 使用私钥签名
const privateKey = '0xabc...';

const signedTransaction = web3.eth.accounts.signTransaction(transactionObject, privateKey)
.then((signedTx) => {
    console.log(signedTx.rawTransaction);
})
.catch((error) => {
    console.log(error);
});

web3.eth.sendSignedTransaction(signedTransaction.rawTransaction)
.on('transactionHash', (txHash) => {
   console.log(`Transaction Hash: ${txHash}`);
})
.on('receipt', (receipt) => {
   console.log(`Transaction Receipt: ${receipt}`);
})
.on('error', console.error);

2. 自动签名

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'                                                                       
}

3. 查询交易信息

函数原型

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'
}

4. 查询交易收据

函数原型

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'                                                                       
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值