I am using Infura https endpoints to send a signed transaction and every time I am getting an error that transaction was not mined within 750 seconds. Using the transaction hash that is being returned, I looked up those transactions on testnet etherscan however, the transaction never shows up not even in pending state.
TestNets (and their corresponding etherscan) I tried on:
Rinkeby
Ropsten
Last parameters I used were:
gasPrice: 3000000000,
gasLimit: 30000,
value: 150000000000000
/*##########################
CONFIGURATION
##########################*/
// -- Step 1: Set up the appropriate configuration
const Web3 = require('web3');
const EthereumTransaction = require('ethereumjs-tx').Transaction;
const web3 = new Web3('https://rinkeby.infura.io/v3/<Project-ID>');
// -- Step 2: Set the sending and receiving addresses for the transaction.
const sendingAddress = '<Sending-Address>';
const receivingAddress = '<Recieving-Address>';
// -- Step 3: Check the balances of each address
web3.eth.getBalance(sendingAddress).then(console.log);
web3.eth.getBalance(receivingAddress).then(console.log)
/*##########################
CREATE A TRANSACTION
##########################*/
// -- Step 4: Set up the transaction using the transaction variables as shown
let rawTransaction = {
nonce: 0,
to: receivingAddress,
gasPrice: 3000000000,
gasLimit: 30000,
value: 150000000000000,
data: '0x546573742052756e',
chainId: 4
};
/*##########################
Sign the Transaction
##########################*/
// -- Step 7: Sign the transaction with the Hex value of the private key of the sender
const privateKeySender = '<Private-Key>';
let privateKeySenderHex = Buffer.from(privateKeySender, 'hex');
/*#########################################
Send the transaction to the network
#########################################*/
web3.eth.getTransactionCount(sendingAddress)
.then((nonce) => {
rawTransaction.nonce = nonce + 1048576;
return rawTransaction;
})
.then((rawTx) => {
console.log(rawTx);
let ethTx = new EthereumTransaction(rawTx, {'chain':'rinkeby'});
ethTx.sign(privateKeySenderHex);
return ethTx;
})
.then((ethTx) => {
let ethTxSerialized = ethTx.serialize();
ethTxSerialized = `0x${ethTxSerialized.toString('hex')}`;
console.log(`Is serialized tx in Hex: ${web3.utils.isHex(ethTxSerialized)}`);
console.log(`0x${ethTx.hash(true).toString('hex')}`);
console.log(`${ethTxSerialized.toString('hex')}`);
web3.eth.sendSignedTransaction(ethTxSerialized).on('transactionHash', console.log);
});
For Ropsten, I’m just replacing the Infura Endpoint, Chain ID and chain parameter for ethereum-js transaction constructor to match it with Ropsten’s.
First nonce on TestNet is 1048576 therefore you need to add that offset while calculating nonce on TestNet. If you don’t, you end up with an error “Error: returned error: already known”
Tx Hash on Rinkeby: 0x7988bd293880c5090b0e6033922df0eeb8840c7965d465c481aa92a9080d4334
When I manually broadcast this through https://rinkeby.etherscan.io/pushTx I see my transaction in pending state in https://rinkeby.etherscan.io/ but the same is not happening when I am doing it through my code using Infura. When I am using Infura transaction doesn’t show in etherscan at all while I get an error saying “Transaction was not mined within 750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined!”
Hey, I’m sorry I still miss why you need to increment the nonce. The tx is not getting mined because there is a nonce gap, this hash has nonce 1048578 but in fact your sender address should have nonce 2, you have two tx sent already (nonce 0 and 1) https://rinkeby.etherscan.io/address/0xa84a1dfb5cb5b47274f721eb60a7574c36f8fe38
Are you sure that you’re sending the same raw tx through etherscan and infura ? Thanks !
I did more research and realized incrementing nonce was a practice before “chain_id” was introduced. I removed the nonce increment and tried again and surprisingly, it went through this time. When I raised this ticket with Infura, the same code was throwing “already known” error even when no such transaction was in queue already which is why I added that nonce increment in the first place. I know for sure because someone else was also facing the exact same issue as I did. Regardless, the issue looks to be resolved now. Thanks a bunch for looking into it and providing your expert advice!