1

I have write a contract and deployed it through remix-ethereum IDE. That process goes alright. While accessing functions of contract through web3 in javascript I get following error Error: Provider not set or invalid"

It is noted that in the console the first address comes through following code console.log(web3.eth.defaultAccount)

Complete Code.

let web3 = new Web3(Web3.currentProvider)  
async function contract() {

    accounts = await ethereum.request({ method: 'eth_requestAccounts' });
    web3.eth.defaultAccount = accounts[0] 
    console.log(web3.eth.defaultAccount)
    var toAddress = "0x88c24A263802547F882A7F9cA0Ef58980Fe9f6f2"
    var accountAddress = '0x4a2499F46e00C25d6AFA7CC1cfa47cd69538aE61'
    var abi = [
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "a",
                    "type": "uint256"
                }
            ],
            "name": "getValue",
            "outputs": [],
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [],
            "name": "showValue",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        }
    ]
    var contractAddress ="0x3A4026a2613bAa8853e6fF29600f57cF5A9f9Ba1";
    var sampleContract = await new web3.eth.Contract(abi, contractAddress)
    console.log(sampleContract.options.address)
    sampleContract.methods.getValue(1).send({
        from: web3.eth.defaultAccount, // default from address
        gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case
    })
    sampleContract.methods.showValue().call(function(error,result) {
        console.log(error)
        console.log(result)
    })
}

contract()

Error

7
  • How are u connecting ur web3 to ur running blockchain ? Can u show the whole code
    – Majd TL
    Jul 1, 2021 at 18:23
  • Agreed with @MajdTL. If this is all the code, you are missing the part where you specify the provider (as the error explicitely says). Jul 2, 2021 at 14:24
  • let web3 = new Web3(Web3.currentProvider) Through this I connect with the metamask. I didnt understand how to specify provider. Kindly explain it. Jul 2, 2021 at 15:49
  • You need to connect web3 to a blockchain, remix run a small blockchain in memory for you. But when u leave remix u need to provide your own blockchain. U can use for testen something called Ganache which provide a simple simulated blockchain for u and then you can connect it with your web3
    – Majd TL
    Jul 3, 2021 at 10:05
  • it will look something like this -> Const web3 = new Web3.providers.HttpProvider(„127.0.0.1:<ganache-port>“);
    – Majd TL
    Jul 3, 2021 at 10:11

1 Answer 1

1

With the latest changes use window.ethereum as provider after the user has granted permission

async function contract() {
    const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
    const web3 = new Web3(window.ethereum);

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.