0

I have a problem to type an abi key "5777" in typescript. When i get a netwotkId and then set in networks key, the linter show me the next error

enter image description here

i need to type "networkId" and that it is not a always a fixed value as like "5777"

let networkId = await web3.eth.net.getId();
// type network = "5777";

let networkId = await web3.eth.net.getId();

DeluxerContract = new web3.eth.Contract(
      artifact.abi,
      artifact.networks[networkId].address
);

This is not a viable solution because the networkId is variable.

    type network = "5777";

    let networkId: network = await web3.eth.net.getId();

    DeluxerContract = new web3.eth.Contract(
      artifact.abi,
      artifact.networks[networkId].address
    );
TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

0

Try this: artifact.networks[networkId as network].address

Cognia
  • 437
  • 3
  • 10
0

I think that is because web3.eth.net.getId() return value might be undefined (add logic based on return value) . You should add a guard

let networkId=await web3.eth.net.getId()
// continue if networkId exists
if (networkId){
  DeluxerContract = new web3.eth.Contract(
      artifact.abi,
      artifact.networks[networkId].address
);
}
Yilmaz
  • 35,338
  • 10
  • 157
  • 202