I can't understand why am getting: ProviderError:Provider does not have a request or send method to use

Problem: Provider does not have a request or send method to use.

  • SDK Version: 6.1.4
  • Platform: Windows 11
  • Framework: Next.js (No app directory)
  • Web3 Library: Web3.js
  • Browser Console Screenshots:

Please provide the Web3Auth initialization and login code snippet below:

import { useState, useEffect } from 'react';
import { motion } from 'framer-motion';
import { Web3Auth } from "@web3auth/modal";
import { CHAIN_NAMESPACES } from "@web3auth/base";
import RPC from "./web3RPC";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import { OpenloginAdapter } from "@web3auth/openlogin-adapter";



const Auth = () => {
    const [error, setError] = useState('');
    const [address, setAddress] = useState(null);
    const [web3auth, setWeb3auth] = useState(null);
    const [provider, setProvider] = useState(null);
    //const walletButton = document.getElementById('walletButton')
    const clientId = "";
    

    

    useEffect(() => {
        const init = async () => {
            try {
                
                const chainConfig = {
                    chainNamespace: CHAIN_NAMESPACES.EIP155,
                    chainId: "0x13881",
                    rpcTarget: "https://polygon-mumbai.infura.io/v3/", // This is the public RPC we have added, please pass on your own endpoint while creating an app
                }
                
                const web3auth = new Web3Auth({
                    clientId, 
                    web3AuthNetwork: "testnet", // mainnet, aqua,  cyan or testnet
                    chainConfig,
                });
    
                setWeb3auth(web3auth);
                await web3auth.initModal();
                
        
                
                setProvider(web3auth.provider);
                
            } catch (err) {
                console.log(err.message);
            }   
        }
        init(); 
    }, []);


    const getUserInfo = async () => {
        if (!web3auth) {
            console.log("web3auth not initialized yet");
            return;
        }
        const user = await web3auth.getUserInfo();
        console.log(user);
    };

    const getAccounts = async () => {
        if (!provider) {
            console.log("provider not initialized yet");
            return;
        }
        console.log(provider);
        const rpc =  new RPC(provider);
        const address = await rpc.getAccounts();
        console.log(address);
    };

    const connectWalletHandler = async () => {
        if(!web3auth) {
            console.log("Web3Auth not initialized");
            return;
        }
        const web3authProvider = await web3auth.connect();
        setProvider(web3authProvider);
        await getUserInfo();
        await getAccounts();
    };

    return (
        <motion.button
            onClick={connectWalletHandler}
            whileHover={{ scale: 1.1 }}
            whileTap={{ scale: 0.9 }}
            id="walletButton"
            className="bg-cblue hover:bg-gold font-bold py-2 px-4 rounded-lg"
        >
            Web3Auth
        </motion.button>
    );
};

export default Auth;

So am getting the error “ProviderError: Provider does not have a request or send method to use” while trying to get a wallet address from the account. These is a code that simply connects with web3auth and should return an account so i can interact with the blockchain. Am coding in Js not Ts so am not sure if this could be the problem i have modified the provided exemple in the doc to make it a js file (web3RPC.js):

import { SafeEventEmitterProvider } from '@web3auth/base';
import Web3 from 'web3';

export default class EthereumRpc {
    constructor(provider) {
        this.provider = provider;
    }

    async getChainId() {
        try {
            const web3 = new Web3(this.provider);

            // Get the connected Chain's ID
            const chainId = await web3.eth.getChainId();

            return chainId.toString();
        } catch (error) {
            return error;
        }
    }

    async getAccounts() {
        try {
            const web3 = new Web3(this.provider);

            // Get user's Ethereum public address
            const address = (await web3.eth.getAccounts())[0];

            return address;
        } catch (error) {
            return error;
        }
    }

    async getBalance() {
        try {
            const web3 = new Web3(this.provider);

            // Get user's Ethereum public address
            const address = (await web3.eth.getAccounts())[0];

            // Get user's balance in ether
            const balance = web3.utils.fromWei(
                await web3.eth.getBalance(address) // Balance is in wei
            );

            return balance;
        } catch (error) {
            return error;
        }
    }

    async sendTransaction() {
        try {
            const web3 = new Web3(this.provider);

            // Get user's Ethereum public address
            const fromAddress = (await web3.eth.getAccounts())[0];

            const destination = fromAddress;

            const amount = web3.utils.toWei('0.01'); // Convert 1 ether to wei

            // Submit transaction to the blockchain and wait for it to be mined
            const receipt = await web3.eth.sendTransaction({
                from: fromAddress,
                to: destination,
                value: amount,
                maxPriorityFeePerGas: '5000000000', // Max priority fee per gas
                maxFeePerGas: '6000000000000', // Max fee per gas
            });

            return receipt;
        } catch (error) {
            return error;
        }
    }

    async signMessage() {
        try {
            const web3 = new Web3(this.provider);

            // Get user's Ethereum public address
            const fromAddress = (await web3.eth.getAccounts())[0];

            const originalMessage = 'YOUR_MESSAGE';

            // Sign the message
            const signedMessage = await web3.eth.personal.sign(
                originalMessage,
                fromAddress,
                'test password!' // configure your own password here.
            );

            return signedMessage;
        } catch (error) {
            return error;
        }
    }

    async getPrivateKey() {
        try {
            const privateKey = await this.provider.request({
                method: 'eth_private_key',
            });

            return privateKey;
        } catch (error) {
            return error;
        }
    }
}

I hope some one could help me with these :slight_smile:

Hey @lucasch2011, I have seen this error before, could you please check if the web3.js module is v1.8.0 and not v4?

2 Likes

It seems tha my web3.js version is 4.0.2

image

"dependencies": {
    "@openzeppelin/contracts": "^4.9.2",
    "@truffle/hdwallet-provider": "^2.1.12",
    "@web3auth/ethereum-provider": "^6.1.4",
    "@web3auth/modal": "^6.1.4",
    "@web3auth/openlogin-adapter": "^6.1.4",
    "autoprefixer": "10.4.14",
    "dotenv": "^16.3.1",
    "eslint": "8.44.0",
    "eslint-config-next": "13.4.8",
    "framer-motion": "^10.12.18",
    "next": "13.4.8",
    "postcss": "8.4.24",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "tailwindcss": "3.3.2",
    "truffle": "^5.10.2",
    "truffle-plugin-verify": "^0.5.32",
    "web3": "^4.0.2"
  }

I will try to downgrade it

By downgrading web3js i was capable of making it work, tks a lot! Lastly, for my personal understanding, why it was happening? I know that it will probably be a version compatibility, but what is the newest version of web3js that i could use?

@lucasch2011 Web3.js v4 has some breaking changes. We’ll be updating the examples soon. It’s next on our list. I have seen examples work well with v1.8. So any version equal or below that is safe. Although, I’ll keep you posted on this thread once I update the examples.

1 Like

Did you downgrade it to 1.8?

1 Like

Yes, please downgrade it to 1.8 and it’ll work fine. @zkPlaty

1 Like

I was facing the same issue, nearly drove me crazy. Thanks!

2 Likes

Aaaand back to the same issue.

Now wallet connects, but can’t get user balance or wallet address. Did you also face same issue?

UPDATE: My bad. I was assigning RPC to already assigned RPC provider

2 Likes