简体   繁体   中英

NodeJS web3: getting returned transaction hash, but it can't be located on block explorer

After getting the follwowing error all the time:

Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 250000000000000 and got: 0.

I commented out the gacPrice from my txData as follows:

const txData = {
        nonce: web3.utils.toHex(txCount),
        gasLimit: web3.utils.toHex(40000),
        //gasPrice: web3.utils.toHex(10e9),
        to: addressTo,
        from: addressFrom,
        data: functionAbi
}

And now I am getting a tx hash returned as a result, but this tx hash can't be located via etherscan or any other blockexplorer. What else can I do to execute successfully a transaction on Kovan ? This is my smart contract:

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

And here is how I execute it via web3.js (full code):

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

})

Receiving transaction address from the web3 may not always conclude to a successful transaction.Web3 returns transaction hash for all submitted transactions. Check the callback to identify the status of the transaction and actual block address(if succeeded).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM