繁体   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