簡體   English   中英

安全帽元掩碼錯誤 - “嘗試使用無效鏈 ID 發送原始交易。預期鏈 ID 為 31337”

[英]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",
  };

謝謝

將元掩碼配置中的 chainId 更改為本地網絡的 31337(例如 http://localhost:8545)

元掩碼網絡配置

或者在您的安全帽配置中手動設置chainId:

  networks: {
    hardhat: {
      url: process.env.RPC_URL,
      chainId: 1337,
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM