5

I have deployed a smart contract on a public testnet and now I am trying to connect to it from the front end using ethers js. But when I try to fetch the value it gives the following errors in the console:

enter image description here

I am using Angular for the front end and here's the code I wrote:

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();
  }  
}

Vivek
  • 193
  • 3
  • 3
  • 9
  • 1
    Please edit the question and share the values of `addresses.electioncontract` and `Election.abi`. It's possible that you're either accessing an incorrect contract (e.g. on a different network or under a different address) or using an ABI that doesn't correspond with the function invoked from the JS code. – Petr Hejda Dec 07 '21 at 13:07
  • Yes you are correct, there was something wrong with the smart contract I deployed. Noticed it when tried to redeploy – Vivek Dec 10 '21 at 17:09
  • You are getting a "unpredictable gas limit" error here. This may happen if your solidity code runs into an infinite loop or recursive function call... you may also possibly be able to get around it by explicitly specifying a gas limit. – Jim Dec 13 '21 at 00:41

3 Answers3

6

I have encountered the same error. I had not changed the contract address once I deployed it to the testnet(Previously I deployed to localhost). Just check if the contract address you are using is of the contract you deployed to the testnet.

Karthik Pn
  • 88
  • 5
6

I met the same problem, check that:

  1. the contract address is correct
  2. the method you call is correct
  3. the parameter is correct

in my case, I called an non-exist method.

Siwei
  • 19,858
  • 7
  • 75
  • 95
  • How do we get the correct contract address? I'm using truffle, I use `truffle migrate --network rinkeby --reset` and the transfer() function is transferring from `msg.sender`. – KylianMbappe Jun 21 '22 at 18:02
  • `truffle migrate` output would give you the contract address. – Siwei Jun 22 '22 at 07:01
  • Yeah the address I've used is definitely correct, but I'm still getting this error. – KylianMbappe Jun 22 '22 at 08:10
  • try to switch to another network, e.g. ganache, or goerli , or fuji ... sometimes the network has bugs. – Siwei Jun 23 '22 at 03:37
0

I came across this problem and tried this solution and more but it didn't work. What solve it for me was making sure my contract address and abi file had the same version.

TG-EZE
  • 3
  • 4