【问题标题】:MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'} while trying to connect to smart contractMetaMask - RPC 错误:尝试连接到智能合约时执行恢复 {code: -32000, message: 'execution reverted'}
【发布时间】:2021-12-07 09:41:23
【问题描述】:

我已经在公共测试网上部署了一个智能合约,现在我正在尝试使用 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();
  }  
}

【问题讨论】:

  • 请编辑问题并分享addresses.electioncontractElection.abi 的值。您可能正在访问不正确的合约(例如,在不同的网络上或在不同的地址下)或使用与从 JS 代码调用的函数不对应的 ABI。
  • 是的,你是对的,我部署的智能合约有问题。尝试重新部署时注意到它
  • 您在此处收到“不可预测的气体限制”错误。如果您的 Solidity 代码遇到无限循环或递归函数调用,则可能会发生这种情况......您也可以通过明确指定气体限制来绕过它。

标签: ethereum solidity ethers.js


【解决方案1】:

我遇到了同样的错误。一旦我将合约地址部署到测试网(之前我部署到本地主机),我就没有更改合约地址。 只需检查您使用的合约地址是否是您部署到测试网的合约。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,请检查:

    1. 合约地址正确
    2. 你调用的方法是正确的
    3. 参数正确

    在我的例子中,我调用了一个不存在的方法。

    【讨论】:

      猜你喜欢
      • 2023-02-26
      • 2023-01-21
      • 1970-01-01
      • 2021-12-16
      • 2022-12-21
      • 1970-01-01
      • 2018-11-30
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多