3

Im building kind of like ICO app, where user can connect wallet, and send ETH to my smart contract and get some tokens in return.

But also in this application I wanna read some data from blockchain and display it to the user even before the wallet will be connected.

Unfortunately before wallet connection my app is all the time in loading state. After couple of minutes thought came in that I don't have any one to call this functions, I don't have the signer?

Is my thinking on good track? And if so how can I read data from blockchain in that case?

My function to get contract looks like this:

const getEthereumContract = () => {

  const provider = new ethers.providers.Web3Provider(ethereum);

  const signer = provider.getSigner();
  const icoContract = new ethers.Contract(contractIcoAddress, contractIcoABI, signer);

  return icoContract;
}
2
  • Contract take either signer or provider. Check the docs of ethers sadly cannot paste a link using my phone :)
    – Majd TL
    Commented Feb 4, 2022 at 20:23
  • @MajdTL Thanks. I guess you were pointing to this: docs.ethers.io/v5/getting-started/… And now that I pass there correct provider it work exactly as I want to
    – BlackH3art
    Commented Feb 5, 2022 at 12:46

3 Answers 3

3

As @MajdTL mentioned in the comment, all I had to do is to pass there provider instead of a signer.

So I created new function just to fetch the data regardless of whether the wallet is connected or not.

const getEtherumContractProvider = () => {

  const RPC = "https://... RPC provider e.g. Infura in my case";
  const provider = new ethers.providers.JsonRpcProvider(RPC);

  const icoContract = new ethers.Contract(contractIcoAddress, contractIcoABI, provider);

  return icoContract;
}

https://docs.ethers.io/v5/getting-started/#getting-started--connecting-rpc

1

You cannot use the ethereum provider for this job, you need to use some node provider. Moralis.io indeed is a good one. Then you will be able to make calls to the node. You still don't understand how ethers (or web3js) works. Look for the call() function in the documentation. To perform a call(), it is not necessary to sign anything. In web3js is something like ( contract.methods.[the method you want to call].call({from: account}).

0

Use a node like moralis.io and write a specific cloud function or use one of their built-in function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.