There is a code, the essence is this: I need to check the account balance for erc20 tokens, and if the balance is not zero, I need to send eth from another account to this one, after eth reaches the account, I need to send erc20 tokens to another account . How then can I get the status of a transaction? And if the status is successful, then do the token transaction?
const balance = contractInstance.methods.balanceOf(walletAddress).call(function(err, result) {
//console.log(err)
if (result>0) {
console.log("success")
web3.eth.getTransactionCount(fromAddress)
.then((count) => {
let rawTransaction = {
'from': fromAddress,
'gasPrice': web3.utils.toHex(10 * 1e9),
'gasLimit': web3.utils.toHex(21000),
'to': walletAddress,
'value': web3.utils.toHex(web3.utils.toWei("0.0002")),
'nonce': web3.utils.toHex(count)
}
let transaction = new Tx(rawTransaction)
transaction.sign(privateKey)
web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
.on('transactionHash', console.log)
})
web3.eth.getTransactionCount(fromAddress)
.then((count) => {
let rawTransaction = {
'from': walletAddress,
'gasPrice': web3.utils.toHex(20 * 1e9),
'gasLimit': web3.utils.toHex(41000),
'to': tokenAddress,
'value': '0x' + Number(result)/1000000000000000000,
'data': contract.methods.transfer(fromAddress, amount).encodeABI(),
'nonce': web3.utils.toHex(count)
}
let transaction = new Tx(rawTransaction)
transaction.sign(privateKey1)
web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
.on('transactionHash', console.log)
})
}
})
.on('receipt', (txReceipt) => console.log(txReceipt.status))
web3js.readthedocs.io/en/v1.2.0/web3-eth.html#id67 – Gabriel G. Dec 5 '19 at 14:03