0

我们开发了前端来买卖币安链代币。与智能合约交互时,它在 Web Metamask 扩展上运行良好。我们正在使用 infura API 为移动设备上的其他钱包获取 connectwallet 接口。它连接成功,但交易未得到确认。当我按下确认按钮时显示此错误,收到内部 JSON-RPC 错误

我们收到了这个问题

这是我们在确认时运行的函数:

async function change_transfer() {
  if (globalObj.typeTxn == "a") {
    var receiver = payableAmountObjectt.address_to;
  } else {
    var receiver = receiverCoin;
  }

  console.log("receiver", receiver);

  // check address is valid or if not then catch
  await web3Global.utils.toChecksumAddress(receiver);

  if (selectedAccount.toLowerCase() == receiver.toLowerCase()) {
    swal("You cannot transfer to same address").then((value) => {});
    toastr.error("You cannot transfer to same address");
    return false;
  }

  // const balanceOf_ = await web3Global.eth.getBalance(selectedAccount);
  const balanceOf_ = await getBnbBalance(selectedAccount);
  console.log("balanceOf_", balanceOf_);
  var balance_ = 0;
  if (balanceOf_ > 0) {
    balance_ = balanceOf_ / 1e18;
  }
  // console.log(balance_);

  var minBal = 0.001;
  console.log("balance : ", balance_);
  console.log("min balance : ", minBal);

  if (balance_ < minBal) {
    swal(
      "Balance Low",
      "You must have BNB " + minBal + " for transaction."
    ).then((value) => {});
    return false;
  }

  // Get connected chain id from Ethereum node
  const chainId = await web3Global.eth.getChainId();
  // Load chain information over an HTTP API
  const chainData = evmChains.getChain(chainId);
  console.log("chainData = ", chainData.name);
  console.log("chainData = ", chainData);

  var network = chainData.networkId;
  console.log(network);
  payableAmountObjectt.network = network;
  if (checkNetwork(network) == false) {
    return false;
  }

  const contract = new web3Global.eth.Contract(abi, contractAddress);

  // var weiAmount = payableAmountObjectt.coin_amount * 1e9;
  var weiAmount = payableAmountObjectt.coin_amount * decimalVal;  var weiAmount2 = weiAmount;
  var value = weiAmount2;
  value = toFixed(value);

  payableAmountObjectt.coin_amount_raw = value;
  var from_account = selectedAccount;

  const balanceOf = await contract.methods.balanceOf(from_account).call();
  console.log("value", value);
  console.log("balanceOf", balanceOf);
  if (value > balanceOf) {
    swal("Balance Low", "Your Token balance is low.").then((value) => {});
    toastr.error("Your Token balance is low.");
    return false;
  }

  payableAmountObjectt.from_address = from_account;
  payableAmountObjectt.to_address = receiver;

  console.log("from_account", from_account);
  console.log("receiver", receiver);
  console.log("value", value);
  $(".payBtn").hide();
  $(".payBtn").attr("disabled", true);
  $("#showLoaderAjaxWeb3Modal").show();

  var gasPrice_gwei = "10";

  var estimateGasRaw = await contract.methods
    .transfer(receiver, value.toString())
    .estimateGas({
      from: from_account,
      gasPrice: await web3Global.utils.toHex(
        web3Global.utils.toWei(gasPrice_gwei, "gwei")
      ),
    });

  var estimateGas = estimateGasRaw;
  console.log("estimateGas", estimateGas);

  const transfer = await contract.methods
    .transfer(receiver, value.toString())
    .send({
      from: from_account,
      gas: await web3Global.utils.toHex(estimateGas), // Raise the gas limit to a much higher amount
      gasLimit: await web3Global.utils.toHex(800000), // Raise the gas limit to a much higher amount
      gasPrice: await web3Global.utils.toHex(
        web3Global.utils.toWei(gasPrice_gwei, "gwei")
      ),

      value: "0x0",
    })
    .on("transactionHash", function (hash) {
      console.log("transactionHash", hash);
      $("#showLoaderAjaxWeb3Modal").hide();
      toastr.success(hash);
      payableAmountObjectt.hash = hash;
      if (payableAmountObjectt.type == "admin") {
        transferTokenUpdate(hash);
      } else {
        transferTokenUpdate_purchase(hash);
      }
    })
    .on("receipt", function (receipt) {
      console.log("receipt", receipt);
    })
    .on("confirmation", function (confirmationNumber, receipt) {
      console.log("confirmation", confirmationNumber, receipt);
    })
    .on("error", function (error, receipt) {
      console.log("error", error, receipt);
      console.log("error.message", error.message);
      console_(error.message, "status");
      toastr.info(error.message);

      if (
        error.message == "User rejected the transaction" ||
        error.message ==
          "MetaMask Tx Signature: User denied transaction signature."
      ) {
        $(".payBtn").show();
        $(".payBtn").attr("disabled", false);
        $("#showLoaderAjaxWeb3Modal").hide();
      }
    });
}
4

1 回答 1

0

我遇到了同样的问题,您应该查看您尝试发送的交易是否有效,因为在我的情况下,Metamask 总是恢复我的交易,因为它无效(合同所有权问题)并抛出“JSON-RPC " 错误 在您的情况下,您应该查看是否有发现,或者合同上是否有某些东西可以恢复您的 tx,因此 Metamask 会抛出错误

于 2021-09-10T22:59:43.947 回答