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