javascript 错误:返回错误:所需气体超过允许量(12487794)或始终无法进行交易

rqenqsqc  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(112)

我正在尝试使用一个脚本,它使用web3.js来传输特定数量的ERC20令牌。我用这样一种方式制作了这个脚本,如果帐户没有以太值,它会一次又一次地尝试,直到它得到以太来支付汽油。我运行脚本,在运行之间,我在mainnet上得到这个错误。虽然它在rinkbey和ropsten上工作正常,但在mainnet上不工作。我在方法web3.eth.estimateGas Complete脚本方法中得到错误,如下所示

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;
        });
    }
  }
}

这是完整的错误日志数据:

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 ```
5uzkadbs

5uzkadbs1#

它说交易总是失败,所以这意味着以太坊主网会拒绝你的交易,可能是因为智能合约代码不存在,地址错误,或者智能合约处于无效状态。

相关问题