Viv*_*vek 5 ethereum solidity ethers.js
我已经在公共测试网上部署了一个智能合约,现在我尝试使用 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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23247 次 |
| 最近记录: |