1

I am attempting to deploy a contract via an Infura node, and I am the point where a txhash is generated from the sendSignedTransaction() web3 function. However, after the txhash is generated nothing happens. It just hangs forever, and etherscan never recognizes that txhash. I have tried Ropsten and Rinkeby, and I have sufficient funds in all accounts that are sending the tx.

Why would the transaction not be broadcasted by Infura? The gas price and limit both seem to be correct and within a reasonable range. Increasing the gas price doesn't change the issue, I've tried that a few times. All of the relevant code below:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/PROJECT_ID"));
const config = require('./contractInterface')

function deployContract() {
    var privateKey = 'PRIVATE_KEY_HERE'
    var address = 'ADDRESS_HERE'
    var abi = config.contractInterface.abi;
    var bytecode = config.contractInterface.bytecode

    // Construct the raw transaction
    let gasPriceHex = web3.utils.toHex(1700000000);
    let gasLimitHex = web3.utils.toHex(200000);
    let block = web3.eth.getBlock("latest");
    let nonce = web3.eth.getTransactionCount(address);
    let nonceHex = web3.utils.toHex(nonce);

    // Contract object
    const contract = new web3.eth.Contract(abi);

    // Get contract data
    const contractData = contract.deploy({
        data: bytecode,      
    }).encodeABI();

    //estimate gas and log to console
    contract.deploy({
        data: bytecode,
    }).estimateGas().then(data => console.log("estimated gas: ",data));

    const rawTx = {
        nonce: nonceHex,
        gasPrice: gasPriceHex,
        gasLimit: gasLimitHex,
        data: contractData,
        from: address,
        chainId:web3.utils.toHex(3)
    };

    console.log(rawTx);

    web3.eth.accounts.signTransaction(rawTx, privateKey, function (error, signedTx) {
        if (error) {
            //do stuff
        } else {
            web3.eth.sendSignedTransaction(signedTx.rawTransaction)
                .on('transactionHash', function (hash) {
                    console.log("txhash: ", hash);
                })
                .on('receipt', function (receipt) {
                    console.log("receipt: ", receipt);
                })
                .on('confirmation', function (number) {
                    console.log("block number: ", number);
                })  
                .on('error',console.log)
        }
    })
}

deployContract();

EDIT: As per request below, auto-generated the address from the private key and checked balance programmatically to ensure funds, however, issue still exists.

var privateKey = '0xREDACTED';
var account = web3.eth.accounts.privateKeyToAccount(privateKey);
var address = account.address;

web3.eth.getBalance(address).then(data => console.log("balance: ",data))

Result:

balance: 35493192886000000000

  • Can you add few lines to generate address from private key and log balance for that address. This would rule out integration issues. – Shamit Verma Dec 28 '18 at 3:12
  • Hi @ShamitVerma, I just added an edit to my post with the result of generating the address from the private key and then logging the balance. The account has a positive balance of around 35 ETH, but the broadcasting issue persists – offthewall1066 Dec 28 '18 at 4:23
  • Do you see thxHash in pending transactions ( txHash in pending or queued transactions) – Shamit Verma Dec 28 '18 at 11:16
  • I'm not exactly sure how to differentiate that, but using the web3.eth.getTransaction(txhash) function, the returned value has a blockHash of all 0's and a null blockNumber. – offthewall1066 Dec 28 '18 at 15:32
  • Is bytecode prefixed with '0x'? The gas price of 1.7 gwei seems a little low for ropsten. Make sure the nonce is correct, you are not awaiting for the value. – Ismael Dec 28 '18 at 15:45
1

The issue was resolved based on @Ismael's comment on this post. I wasn't waiting for the promise to resolve for the function to get the nonce - web3.eth.getTransactionCount(address) - and as such the nonce was a nonsense hex value.

After updating to async / await and ensuring the nonce is there before proceeding everything functioned as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.