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
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.