【问题标题】:Metamask returns RPC Error: Error: [ethjs-rpc] rpc error with payload {"id":5715960965837,"jsonrpc":"2.0","params":Metamask 返回 RPC 错误:错误:[ethjs-rpc] rpc 错误,有效负载 {"id":5715960965837,"jsonrpc":"2.0","params":
【发布时间】:2020-07-08 18:23:11
【问题描述】:

我有这段代码可以让我使用 web3 购买代币,显然如果交易经过验证,但当我点击确认时,会出现以下错误:

inpage.js:1 MetaMask - RPC错误:错误:[ethjs-RPC] RPC错误与有效载荷{ “ID”:5715960965839 “jsonrpc”: “2.0”, “PARAMS”:[ “0xf8ac178504a817c8008301022794cfeb869f69431e42cdb54a4f4f105c19c080a60180b844095ea7b3000000000000000000000000c89ce4735882c9f0f0fe26686c53074e09b0d55000000000000000000000000000000000000000000000000000000000000f4240820557a07ff18aba5ab0a8080f4a4b29e8736c4ded84e09cec8f66eba186995a6b22261fa041fd2e3351985a3b0a4e1ec9ef1be79c3513170810569b78ba9f238d36e781fa”],”方法":"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
          });
      });
  }
};

【问题讨论】:

    标签: javascript html ethereum solidity metamask


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

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 2016-02-24
      • 2017-06-29
      相关资源
      最近更新 更多