0

I am trying to integrate web3.js but getting this error. Attaching the screenshots of my JS code

enter image description here

<script>
window.ethereum.enable( );
const provider = new ethers.providers.Web3Provider(web3.currentprovider, "goerli");
const MoodContractAddress = "0x65B1174cDBb463e0F9132c09aaEDc63b02986D7D";
const MoodContractABI = [
  {
      "inputs": [
          {
              "internalType": "string",
              "name": "_mood",
              "type": "string"
          } 
      ],
      "name": "setMood",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
  },
  {
      "inputs": [],
      "name": "getMood",
      "outputs": [
          {
              "internalType": "string",
              "name": "",
              "type": "string"
          }
      ],
      "stateMutability": "view",
      "type": "function"
  }
]

let MoodContract;
let signer;
provider.send("eth_requestAccounts", []).then(() => {
provider.listAccount().then(function(accounts) {
  signer = provider.getsigner(accounts[0]);
  MoodContract = new ethers.Contract(MoodContractAddress, MoodContractABI, signer);
});

});

async function getMood(){
    var getMoodPromise = MoodContract.getMood();
    var Mood = await getMoodPromise;
    console.log(Mood);
}

async function setMood(){
    var mood= getElementById("mood").value;
    var setMoodPromise = MoodContract.setMood(mood);
    await setMoodPromise;
} 

 </script>

I was expecting that the get mood and set mood buttons would work, but they did not work and instead I am getting this error.

Naren Murali
  • 19,250
  • 3
  • 27
  • 54

1 Answers1

0

Below are the things that were changed!

html

<button onclick="getMood()">
get mood
</button>
<button onclick="setMood()">
set mood
</button>

<input value="3" id="mood"/>

js

window.ethereum.enable( );
const provider = new ethers.providers.Web3Provider(web3.currentProvider, "goerli");
const MoodContractAddress = "0x65B1174cDBb463e0F9132c09aaEDc63b02986D7D"; 
const MoodContractABI = [
  {
      "inputs": [
          {
              "internalType": "string",
              "name": "_mood",
              "type": "string"
          } 
      ],
      "name": "setMood",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
  },
  {
      "inputs": [],
      "name": "getMood",
      "outputs": [
          {
              "internalType": "string",
              "name": "",
              "type": "string"
          }
      ],
      "stateMutability": "view",
      "type": "function"
  }
]

var MoodContract;
var signer;
console.log(provider);
provider.send("eth_requestAccounts", []).then(() => {
console.log(1);
provider.listAccounts().then(function(accounts) { // <- typo here 
console.log(accounts);
  signer = provider.getSigner(accounts[0]); // <- typo here 
  MoodContract = new ethers.Contract(MoodContractAddress, MoodContractABI, signer);
});

});

async function getMood(){
    var getMoodPromise = MoodContract.getMood();
    var Mood = await getMoodPromise;
    console.log(Mood);
}

async function setMood(){
    var mood= document.getElementById("mood").value;
    console.log(mood);
    var setMoodPromise = MoodContract.setMood(mood);
    await setMoodPromise;
} 

jsfiddle

Naren Murali
  • 19,250
  • 3
  • 27
  • 54