Appearance
交易小结
1. 手动签名
js
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. 自动签名
js
var transactionObject = {
from: '0xb588f29312fA735457C21436f12Ce247B6476fB6',
to: '0x9e91F5c51C69870f74106FC4A9589dad6dCAf4C6',
value: web3.utils.toWei('1', 'ether'),
data: web3.utils.toHex(234)
}
web3.eth.sendTransaction(transactionObject).then(console.log)
输出
js
{
transactionHash: '0x1e7b41544f9e71ccee670d8a3560376446c49743ee64f07c5e36a17be707877e',
transactionIndex: 0,
blockHash: '0x5ee052595201447637a17973e7493a8fef06e543022c389b80e904c272ae28b0',
blockNumber: 1,
from: '0xb588f29312fa735457c21436f12ce247b6476fb6',
to: '0x9e91f5c51c69870f74106fc4a9589dad6dcaf4c6',
gasUsed: 21016,
cumulativeGasUsed: 21016,
contractAddress: null,
logs: [],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
}
3. 查询交易信息
函数原型
js
web3.eth.getTransaction(transactionHash [, callback]) 返回具有指定哈希值的交易对象
- transactionHash - 交易哈希
示例
js
web3.eth.getTransaction('0x1e7b41544f9e71ccee670d8a3560376446c49743ee64f07c5e36a17be707877e').then(console.log)
输出
js
{
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. 查询交易收据
函数原型
js
web3.eth.getTransactionReceipt(hash [,callback])
//返回指定交易的收据对象,如果交易处于 pending 状态,则返回 null
示例
js
web3.eth.getTransactionReceipt('0x11e1559508147c7ea2d3e9ce0dfb65be29d42d7a9077f9cef3e80ad64d38b3b8').then(console.log)
输出
js
{
transactionHash: '0x1e7b41544f9e71ccee670d8a3560376446c49743ee64f07c5e36a17be707877e',
transactionIndex: 0,
blockHash: '0x5ee052595201447637a17973e7493a8fef06e543022c389b80e904c272ae28b0',
blockNumber: 1,
from: '0xb588f29312fa735457c21436f12ce247b6476fb6',
to: '0x9e91f5c51c69870f74106fc4a9589dad6dcaf4c6',
gasUsed: 21016,
cumulativeGasUsed: 21016,
contractAddress: null,
logs: [],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
}