![](/img/trans.png)
[英]Metamask transaction is expected to fail on a Smart Contract function that batches ether transfer
[英]Hardhat metamask error - "Trying to send a raw transaction with an invalid chainId. The expected chainId is 31337"
我正在使用安全帽和 ethers js。 执行读取 function 时工作正常,但在使用写入功能时出现此错误-
MetaMask - RPC 错误:[ethjs-query] 格式化来自 RPC '{"value":{"code":-32603,"data":{"code":-32602,"message":"尝试发送具有无效链 ID 的原始交易。预期的链 ID 为 31337","data":{"message":"尝试发送具有无效链 ID 的原始交易。预期的链 ID 为 31337"}}}}'
这是我的代码——
应用程序.js
import './App.css'
import { ethers } from 'ethers'
import React, { useState, useEffect } from 'react'
function App() {
const [participantName, setParticipantName] = useState()
const [candidatesArray, setCandidatesArray] = useState([])
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner();
const contractAddress = ""
const abi = []
const contract = new ethers.Contract(contractAddress, abi, signer)
async function Connect() {
if (window.ethereum) {
try {
await window.ethereum.request({ method: 'eth_requestAccounts' })
console.log('connected')
} catch (error) {
console.log(error)
}
}
}
async function Participate() {
try {
await contract.participate(participantName);
console.log(participantName + " added successfully")
} catch (error) {
console.log(error)
}
}
async function noOfCandidates() {
const noCand = await contract.noOfCandidates();
console.log(noCand.toString())
}
return (
<div className="App">
<h1>Election Poll</h1>
<button onClick={Connect}>Connect</button>
<div className="participate">
<input type="text" onChange={(e)=>{setParticipantName(e.target.value)}}/>
<button onClick={Participate}>Participate</button>
</div>
<div className="noOfCand">
<button onClick={noOfCandidates}>Number of candidates running</button>
</div>
<div className="candArray">
<h4>{candidatesArray}</h4>
</div>
</div>
)
}
export default App
部署.js
const hre = require("hardhat");
async function main() {
const Election = await hre.ethers.getContractFactory("Election");
const election = await Election.deploy();
await election.deployed();
console.log("Election deployed to:", election.address);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.8",
};
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.