首页 > 解决方案 > Metamask 返回 RPC 错误:错误:[ethjs-rpc] rpc 错误,有效负载 {"id":5715960965837,"jsonrpc":"2.0","params":

问题描述

我有这段代码允许我使用 web3 购买令牌,显然如果交易得到验证,但是当我单击确认时,会出现以下错误:

inpage.js:1 MetaMask - RPC Error: Error: [ethjs-rpc] rpc error with payload {"id":5715960965839,"jsonrpc":"2.0","params":["0xf8ac178504a817c8008301022794cfeb869f69431e42cdb54a4f4f105c19c080a60180b844095ea7b3000000000000000000000000c89ce4735882c9f0f0fe26686c53074e09b0d55000000000000000000000000000000000000000000000000000000000000f4240820557a07ff18aba5ab0a8080f4a4b29e8736c4ded84e09cec8f66eba186995a6b22261fa041fd2e3351985a3b0a4e1ec9ef1be79c3513170810569b78ba9f238d36e781fa"],"method": “eth_sendRawTransaction”} [对象对象]

尝试卸载扩展程序并按照其他地方所述重新安装它,但没有得到任何结果

这是我的代码

/* 'metamask': */
const web3 = new Web3(window["ethereum"]);

const USDT_ADDRESS = "XxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const ORION_SALE_ADDRESS = "XxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const usdtContract = new web3.eth.Contract(IRC20_JSON.abi, USDT_ADDRESS);

const saleContract = new web3.eth.Contract(
  ORION_SALE_JSON.abi,
  ORION_SALE_ADDRESS
);
let userWallet;

const getAddress = async () => {
  try {
    const accounts = await window.ethereum.enable();
    userWallet = accounts[0];
    
    console.log(accounts, ORION_SALE_JSON);
    if (!userWallet) {
      document.getElementById("msg").innerHTML =
        "Oops, we can't seem to find the address of your wallet. Please, log in into your metamask account using the chrome extension and then refresh the page";
    } else {
      document.getElementById("msg").innerHTML =
        "Your wallet have been successfully added";
    }
  } catch (error) {
    console.log(error);
  }
};

getAddress();
//buyTokenFunction
const myfunction = async () => {
  let inputVal = document.getElementById("myInput").value;
  if (inputVal !== "") {
    usdtContract.methods
      .approve(ORION_SALE_ADDRESS, (inputVal * 1e6).toFixed(0))
      .send({ from: userWallet })
      .once("transactionHash", (hash) => {
        saleContract.methods
          .buyTokens(userWallet, USDT_ADDRESS, (inputVal * 1e6).toFixed(0))
          .send({ from: userWallet, gas: 10000000 }, (_) => {
            // Waiting for the transaction to be mined
          })
          .then((_) => {
            // Success
          });
      });
  }
};

标签: javascripthtmlethereumsoliditymetamask

解决方案


  1. 跟踪inputVal(或输出到控制台)。甚至更好的是整个表达式:
(inputVal * 1e6).toFixed(0)
  1. 确定发生错误的方法:approvebuyToken

推荐阅读