【问题标题】:NodeJS web3: getting returned transaction hash, but it can't be located on block explorerNodeJS web3:获取返回的交易哈希,但无法在区块浏览器上找到
【发布时间】:2018-12-16 17:23:07
【问题描述】:

一直出现以下错误后:

资金不足。您尝试从中发送交易的帐户 没有足够的资金。需要 250000000000000 得到:0。

我从txData 中注释掉了gacPrice 如下:

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

})

【问题讨论】:

    标签: node.js transactions ethereum solidity web3


    【解决方案1】:

    从 web3 接收交易地址可能并不总是成功的交易。Web3 返回所有提交交易的交易哈希。 检查回调以识别交易状态和实际区块地址(如果成功)。

    【讨论】:

    • 你有一个代码示例如何通过回调检查状态和块地址?
    • 希望对您有所帮助 :::: contractInstance.SubmitTransaction(params1,....,function(error, result){ if(!error) { alert('success'); console.log( result); } else{ console.error(error); alert('添加失败'); } }); >!抱歉,无法正常使用
    猜你喜欢
    • 2021-05-09
    • 2021-10-08
    • 2021-12-18
    • 2022-07-19
    • 1970-01-01
    • 2022-07-22
    • 2023-01-14
    • 2013-11-27
    • 2021-11-18
    相关资源
    最近更新 更多