Not able to get response of sendSignedTransaction method

Team,

I have integrated your endpoint in my node js project for ETH, OMG, USDT, USDC coins. Everything seems working on my development server but when I deployed the same on production some of the APIs are not responding.

This is the URL I am using https://mainnet.infura.io/v3/XXXXXXXXXXX4e4326b3fb4eec83f88640

  1. Gebalance - getBalance() (This method is working perfect and I can get the response back from the server)

  2. sendSignedTransaction() (This method does not respond back but when I check etherscan explorer I can the transaction is done)
    Please find the code sample. I debugged and it goes to the success part but still, no response comes from the server.

get_payment: async (req, res) => {
if (!req.body.privateKey || !req.body.fromAddr || !req.body.toAddr || !req.body.value) {
return res.send({ code: 400, message: “Parameters Missing!!” })
}
getBalance(req.body.fromAddr, async (err, result) => {
if (err) {
return res.send({ code: 500, message: “Internal server error” })
}
else if (result < 0.01) {
res.send({ code: 500, message: “Min. 0.01eth is required” })
}
else if (result) {
privateKey = (req.body.privateKey).split(‘0x’)
privateKey = privateKey[1]
var privateKey = new Buffer(privateKey, ‘hex’);
var amount = new BigNumber(req.body.value).multipliedBy(new BigNumber(Math.pow(10, 18)));
signTxn(req.body.toAddr, req.body.fromAddr, amount, privateKey, async function (hash) {
if (hash) {
var receiptT = await web3.eth.sendSignedTransaction(hash)
if (receiptT) {
var transactionFee = (5 1e9) receiptT.gasUsed;
var fee_data = new BigNumber(transactionFee).dividedBy(new BigNumber(Math.pow(10, 18)));
return res.send({ code: 200, txid: receiptT.transactionHash, fee: fee_data })
}
}
else {
res.send({ code: 500, message: “Insufficient balance” })
}
})
} else {
return res.send({ code: 500, message: “Internal server error” })
}
})
}

Hello @Mobiloitte1, this looks very similar to an issue we sometimes see with the web3 version, stackexchange thread here . What version of web3 are you using?

Another potential fix is described in a web3.js issue here where adding a hardfork: 'istanbul' parameter to Tx() resolves the receipt.

Thanks @Sean_Infura , Still not able to get the response however I get the data in the console. When sending JSON response back(return res.send({ code: 200, txid: receiptT.transactionHash, fee: fee_data })), it does respond to anything. Please find the updated code

=================Console Print===========================
{ blockHash:
‘0x41b96486c1c806370a78d4f8bac612043c868dd57121cd431fc08303a131d3d3’,
blockNumber: 9682293,
contractAddress: null,
cumulativeGasUsed: 8651216,
from: ‘0x06a39beee2f0b34c7d62704b8597780aca5f507e’,
gasUsed: 21000,
logs: [],
logsBloom:
‘0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000’,
status: true,
to: ‘0xd94f5910d71ecef244f3d8bfc68c97ffc434e636’,
transactionHash:
‘0x9c824c884abad44cedc681f420f8ad50679cec884dae3452845b94ab16ad86d3’,
transactionIndex: 118 }
BigNumber { s: 1, e: -4, c: [ 10500000000 ] }

====================Code===================================
var signTxn = (toAddr, fromAddr, value, key, cb) => {
estGas(toAddr, fromAddr, value, (estmdGas) => {
getCurrentGasPrice((currentGasPrice) => {
getTxnCountForNonce(fromAddr, (hardCount) => {
let rawTx = {
nonce: web3.utils.toHex(hardCount),
from: web3.utils.toHex(fromAddr),
gasPrice: 40000000000,
gas: web3.utils.toHex(estmdGas),
to: web3.utils.toHex(toAddr),
value: web3.utils.toHex(value)
}
var tx = new Tx(rawTx, { chain: ‘ropsten’, hardfork: ‘istanbul’ })
tx.sign(key)
;
let serializedTx = tx.serialize();
let cbData = ‘0x’ + serializedTx.toString(‘hex’)
cb(cbData)
})
})
})
}

get_payment: async (req, res) => {
if (!req.body.privateKey || !req.body.fromAddr || !req.body.toAddr || !req.body.value) {
return res.send({ code: 400, message: “Parameters Missing!!” })
}
getBalance(req.body.fromAddr, async (err, result) => {
if (err) {
return res.send({ code: 500, message: “Internal server error” })
}
else if (result < 0.01) {
res.send({ code: 500, message: “Min. 0.01eth is required” })
}
else if (result) {
privateKey = (req.body.privateKey).split(‘0x’)
privateKey = privateKey[1]
var privateKey = new Buffer(privateKey, ‘hex’);
var amount = new BigNumber(req.body.value).multipliedBy(new BigNumber(Math.pow(10, 18)));
signTxn(req.body.toAddr, req.body.fromAddr, amount, privateKey, async function (hash) {
if (hash) {
var receiptT = await web3.eth.sendSignedTransaction(hash)
console.log(receiptT)
if (receiptT) {
var transactionFee = (5 1e9) receiptT.gasUsed;
var fee_data = new BigNumber(transactionFee).dividedBy(new BigNumber(Math.pow(10, 18)));
return res.send({ code: 200, txid: receiptT.transactionHash, fee: fee_data })
}
}
else {
res.send({ code: 500, message: “Insufficient balance” })
}
})
} else {
return res.send({ code: 500, message: “Internal server error” })
}
})
}

==================================================================

One thing more I noticed randomly I get the response once and then again I hit it get stopped.Is there any time problem that we cannot hit the API continuously?

Hey @Mobiloitte1 , a couple things. I’d reccomend testing against the ropsten.infura.io or rinkeby.infura.io endpoint, in order to save yourself real Eth when testing.

So you’re seeing the console.log(receiptT) , but not getting the return res.send({ code: 200, txid: receiptT.transactionHash, fee: fee_data }) ? How is the code configured to handle the res.send?

Also, in the next code block, is there supposed to be a ‘return’ in front of the res.send for insufficient balance?

Hello @Sean_Infura
Sorry for missing return statement in ‘res.send({ code: 500, message: “Insufficient balance” })’. I am doing it on mainnet .I get other responses such as “Internal server error” ,“Min. 0.01eth is required” ,but only don’t receive the success response " return res.send({ code: 200, txid: receiptT.transactionHash, fee: fee_data })".

What is this line doing? var transactionFee = (5 1e9) receiptT.gasUsed;

Can you console log the transactionFee and receiptT.transactionHash to make sure they are what you expect?

Hello @Sean_Infura,
" var transactionFee = (5 1e9) receiptT.gasUsed;** " -This is the transaction fee that I get when the transaction gets completed in the console , and then it is converted into hex and then to big number to show it in the response as fee_data .

console gives me the full receiptT from which I get the transactionHash
Here is the console -

{ blockHash:

‘0x41b96486c1c806370a78d4f8bac612043c868dd57121cd431fc08303a131d3d3’,
blockNumber: 9682293,
contractAddress: null,
cumulativeGasUsed: 8651216,
from: ‘0x06a39beee2f0b34c7d62704b8597780aca5f507e’,
gasUsed: 21000,
logs: [],
logsBloom:
‘0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000’,
status: true,
to: ‘0xd94f5910d71ecef244f3d8bfc68c97ffc434e636’,
transactionHash:
‘0x9c824c884abad44cedc681f420f8ad50679cec884dae3452845b94ab16ad86d3’,
transactionIndex: 118 }
BigNumber { s: 1, e: -4, c: [ 10500000000 ] }

Hello @Sean_Infura ,
console for - receiptT.transactionHash and transactionFee is -

transactionFee 105000000000000
receiptT.transactionHash 0xb1264653ed454e8174bb309d8bad2d7e2f54404423475c413fe0155b0283c6c2