skip to Main Content

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

2

Answers


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

    Login or Signup to reply.
  2. 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
    );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search