簡體   English   中英

MetaMask - RPC 錯誤:執行恢復 {code: -32000, message: 'execution reverted'} 在嘗試連接到智能合約時

[英]MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'} while trying to connect to smart contract

我已經在公共測試網上部署了一個智能合約,現在我正在嘗試使用 ethers js 從前端連接到它。 但是當我嘗試獲取該值時,它會在控制台中出現以下錯誤:

在此處輸入圖像描述

我在前端使用 Angular,這是我寫的代碼:

declare let window: any;
import { Component, OnInit } from '@angular/core';
import { ethers } from 'ethers';
import addresses from '../../environment/contract-address.json'
import Election from '../../blockchain/artifacts/blockchain/contracts/Election.sol/Election.json'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'angvote';
  public signer: any;
  public electionContract: any;
  public candidate : any;
  public candidatesList:string[] | undefined;
  constructor(){}

  async ngOnInit(){
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    window.ethereum.enable()
    
    provider.on("network",(newNetwork: any, oldNetwork: any)=>{
      if (oldNetwork){
        window.location.reload();
      }
    });

    this.signer = provider.getSigner();

    if(await this.signer.getChainId() !== 4){
      alert("Please change your network to Rinkeby!")
    }

    this.electionContract = new ethers.Contract(addresses.electioncontract,Election.abi,this.signer);
    this.candidate = await this.electionContract.candidatesCount();
  }  
}

我遇到了同樣的錯誤。 將合約地址部署到測試網后,我沒有更改合約地址(之前我部署到本地主機)。 只需檢查您使用的合約地址是否是您部署到測試網的合約。

我遇到了同樣的問題,請檢查:

  1. 合約地址正確
  2. 你調用的方法是正確的
  3. 參數正確

就我而言,我調用了一個不存在的方法。

我遇到了這個問題並嘗試了這個解決方案等等,但它沒有用。 為我解決的問題是確保我的合約地址和 abi 文件具有相同的版本。

暫無
暫無

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

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