【发布时间】: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.electioncontract和Election.abi的值。您可能正在访问不正确的合约(例如,在不同的网络上或在不同的地址下)或使用与从 JS 代码调用的函数不对应的 ABI。 -
是的,你是对的,我部署的智能合约有问题。尝试重新部署时注意到它
-
您在此处收到“不可预测的气体限制”错误。如果您的 Solidity 代码遇到无限循环或递归函数调用,则可能会发生这种情况......您也可以通过明确指定气体限制来绕过它。
标签: ethereum solidity ethers.js