[英]NodeJS web3: getting returned transaction hash, but it can't be located on block explorer
一直出现以下错误之后:
不充足的资金。 您尝试从其发送交易的帐户资金不足。 需要250000000000000并得到:0。
我注释掉gacPrice
从我txData
如下:
const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(40000),
//gasPrice: web3.utils.toHex(10e9),
to: addressTo,
from: addressFrom,
data: functionAbi
}
现在,我得到一个tx hash
作为结果返回,但这个tx hash
无法通过etherscan或任何其他blockexplorer定位。 我还可以做些什么才能在Kovan
上成功执行交易? 这是我的智能合约:
pragma solidity ^0.4.24;
contract Test2 {
address public bank;
struct Box {
uint size;
}
Box public box;
constructor() public {
box.size = 3;
bank = 0xa2079636...;
}
function changeBox(uint _change) public {
box.size = _change;
}
function getBox() public returns (uint) {
return box.size;
}
}
这是我通过web3.js
(完整代码)执行它的方式:
const Web3 = require('web3')
const Tx = require('ethereumjs-tx')
const web3 = new Web3(new Web3.providers.HttpProvider('https://kovan.infura.io/apikey'))
const addressFrom = '0x002D18...'
const privKey = '240462...'
const addressTo = '0x36075430619b21Fff798454e2D5C81E9C18DEe81'
var contractABI = new web3.eth.Contract(
[...abi...], addressTo);
const contractFunction = contractABI.methods.changeBox(5);
const functionAbi = contractFunction.encodeABI();
function sendSigned(txData, callback) {
//const privateKey = new Buffer(config.privKey, 'hex')
const privateKey = Buffer.from(privKey, 'hex');
const transaction = new Tx(txData)
transaction.sign(privateKey)
const serializedTx = transaction.serialize().toString('hex')
web3.eth.sendSignedTransaction('0x' + serializedTx, callback)
}
web3.eth.getTransactionCount(addressFrom).then(txCount => {
// construct the transaction data
const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(40000),
//gasPrice: web3.utils.toHex(10e9), // 10 Gwei
to: addressTo,
from: addressFrom,
data: functionAbi
//value: web3.utils.toHex(web3.utils.toWei(123, 'wei'))
}
sendSigned(txData, function(err, result) {
if (err) return console.log('error', err)
console.log('sent', result)
})
})
从Web3接收交易地址可能并不总是成功完成交易。Web3返回所有已提交交易的交易哈希。 检查回调以识别事务处理的状态和实际的块地址(如果成功)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.