Search code examples
blockchainethereummetamask

Provider not set or invalid. How to interact with smart contract without wallet?


I am developing a Web3 App using Solidity, smart contract is working perfect, after deploying it to web server, The app is working flawless on my PC but I faced an issue on my browsers that don't have Metamask wallet.

Error:

Error: Provider not set or invalid
at Object.InvalidProvider (errors.js:39:16)
at f.send (index.js:145:32)
at s (index.js:624:42)
at a.n [as getId] (index.js:656:13)
at Web3Client.js:197:39
at f (regeneratorRuntime.js:86:17)
at Generator._invoke (regeneratorRuntime.js:66:24)
at Generator.next (regeneratorRuntime.js:117:21)
at Ne (asyncToGenerator.js:3:20)
at a (asyncToGenerator.js:25:9)

React Code:

export const init = async () => {
    let provider = window.ethereum;
    if (typeof provider !== 'undefined') {
    }
    const web3 = new Web3(provider);
    const networkId = await web3.eth.net.getId();
    erc20Contract = new web3.eth.Contract(
        erc20Abi,
        addr
    );
    isInitialized = true;
};

What is the best and minimal way to interact with Smartcontracts without Wallet?


Solution

  • You can create account on https://infura.io then you get api key to communicate with blockchain. Copy url from infura, in result you should get response from provider. const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-API-KEY ');