javascript - 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
});
});
}
};
解决方案
- 跟踪
inputVal
(或输出到控制台)。甚至更好的是整个表达式:
(inputVal * 1e6).toFixed(0)
- 确定发生错误的方法:
approve
或buyToken
。
推荐阅读
- scala - SBT:如何在 IntegrationTest 中获取所有定义?
- excel - 是否可以在 Excel 数据输入电子表格(VBA?)中不允许特定的空值?
- chart.js - ChartJS:xAxis 标签的完整日期?
- python - 如何在 python 中找到 bokeh.core.property.containers.PropertyValueColumnData 对象的第 n 个元素?
- python-3.x - 我想在特定的父元素(如“列支敦士登”)中查找朋友子元素是否存在
- azure - Azure DevOps REST API:如何获取发布阶段的测试结果?
- javascript - 在 Angular 5 的 post API 中上传表单数据,但它给出了这样的主体 [object FormData]
- php - open_basedir 似乎没有设置基本路径
- python - 无法在 python 脚本中执行 sudo 命令
- python - 如何使用 Python Pandas 创建具有相同数据库的多个数据框