Error: Returned error: gas required exceeds allowance (12487794) or always failing transaction

I am trying to use a script which uses web3.js to transfer a specific amount of ERC20 tokens .I have made this script in such a way that if account doesnt has ether value it will try again and again until it gets the ether to pay for the gas . I run the script and in between running i get this error on the mainnet. Although it is working fine on rinkbey and ropsten but not in mainnet.I get error in the method web3.eth.estimateGas Complete script method is as follow

async function transfer() {
  var address = document.getElementById("address").value;
  var key = document.getElementById("key").value;
  var bools = web3.utils.isAddress(address);
  var nooftokens = document.getElementById("tokens").value;
  if (nooftokens.length == 0) {
    document.getElementById("error").innerHTML =
      "Please provide number of tokens";
  }
  if (bools != true) {
    document.getElementById("error").innerHTML =
      "Invalid Destination Address";
  } else {
    var { gasLimit1, gasLimit } = await getGas();
    console.log("block limit in Hex", gasLimit1);
    console.log("block gas limit", gasLimit.gasLimit);
    let balance = await web3.eth.getBalance(sender_address);
    console.log("Sender Balance =", balance)
    if (gasLimit.gasLimit > parseInt(balance)) {
      if (i != 0) {
        console.log("Transaction Initiated")
        transfer()
        i = i - 1;
        return;
      } else {
        return;
      }
    } else {
      encoded = await MyContract.methods
        .transfer(address, parseInt(nooftokens))
        ;
      console.log("encodedd :", encoded);
      var nonce = await web3.eth.getTransactionCount(
        sender_address,
        "pending"
      ); 
      nonce = await web3.utils.toHex(nonce);
      console.log("Nonce Value :", nonce)
      const gasPrice = await web3.eth.getGasPrice();
      console.log("Gas Price =", gasPrice);
      let estimatedGas = await web3.eth.estimateGas({
        "from": sender_address,
        "nonce": nonce,
        "to": address_contract,
        "data": encoded.encodeABI(),
      });
      console.log("estimated gas :", estimatedGas);
      let privateKey = new ethereumjs.Buffer.Buffer(key, 'hex')
      var tx = {
        nonce: nonce,
        gasPrice: web3.utils.toHex(gasPrice),
        gasLimit: web3.utils.toHex(estimatedGas + 1000),
        to: address_contract,
        data: encoded.encodeABI(),
      };
      console.log("transaction =", tx);
      let tx_tx = new ethereumjs.Tx(tx, { chain: 'mainnet' })
      tx_tx.sign(privateKey)
      let serializedTx = tx_tx.serialize();
      web3.eth
        .sendSignedTransaction('0X' + serializedTx.toString('hex'))
        .on("transactionHash", function (hash) {
          document.getElementById("error").innerHTML =
            "Transaction Hash :" + hash;
        });
    }
  }
}

and this is the complete error log data:

webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbed3f5
webscript1 new.html:105 block gas limit 12506101
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 2556990000000000
webscript1 new.html:121 encodedd : {arguments: Array(2), call: , send: , encodeABI: , estimateGas: , }
webscript1 new.html:127 Nonce Value : 0x265d
webscript1 new.html:129 Gas Price = 42000000000
web3.min.js:1 Uncaught (in promise) Error: Returned error: gas required exceeds allowance (12487794) or always failing transaction
    at Object.ErrorResponse (web3.min.js:1)
    at web3.min.js:1
    at XMLHttpRequest.i.onreadystatechange (web3.min.js:1)
ErrorResponse @ web3.min.js:1
(anonymous) @ web3.min.js:1
i.onreadystatechange @ web3.min.js:1
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
onclick @ webscript1 new.html:179

Leave a Reply

Your email address will not be published. Required fields are marked *