1

I have a simple contract for getting a greeting and changing the greeting message. I am getting this error when calling the getGreeting function.

error:  Error: call revert exception (method="getGreeting()", errorSignature=null, errorArgs=[null], reason=null, code=CALL_EXCEPTION, version=abi/5.0.0-beta.156)

Web:

import "./App.css";
import { useState } from "react";
import { ethers } from "ethers";

import Greeting from "./artifacts/contracts/Greeting.sol/Greeting.json";

const greetingAddress = "0x0165878a594ca255338adfa4d48449f69242eb8f";

function App() {
  const [message, setMessage] = useState("");

  // helper functions helps us to interact with contract
  async function requestAccounts() {
    await window.ethereum.request({ method: "eth_requestAccounts" });
  }


  async function getGreeting() {
    if (typeof window.ethereum !== "undefined") {
      const provider = new ethers.providers.Web3Provider(window.ethereum);
      const contract = new ethers.Contract(
        greetingAddress,
        Greeting.abi,
        provider
      );
      console.log("New contract : ", contract);
      try {
        const data = await contract.getGreeting();
        console.log("data : ", data);
        const tx = await contract.getGreeting();
        const receipt = await tx.wait();
        console.log(receipt);

      } catch (error) {
        console.log("error: ", error);
      }
    }
  }


  async function changeGreeting() {
    if (!message) return;

    //check if metamask exists
    if (typeof window.ethereum !== "undefined") {
      await requestAccounts();

      const provider = new ethers.providers.Web3Provider(window.ethereum);
      const signer = provider.getSigner();

      //create contract with signer
      const contract = new ethers.Contract(
        greetingAddress,
        Greeting.abi,
        signer
      );
      const transaction = await contract.changeGreeting(message);

      setMessage("");
      await transaction.wait();
      getGreeting();
    }
  }

  return (
    <div className="App">
      <h1>Hello</h1>
      <button onClick={getGreeting}>Get Greeting</button>
      <button onClick={changeGreeting}>change Greeting</button>
      <input
        type="textarea"
        onChange={(e) => setMessage(e.target.value)}
        value={message}
        placeholder="Set greeting message . ."
      />
    </div>
  );
}

export default App;

Solidity contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Greeting {
    // State variable to store the greeting message
    string private greeting;

    // Event emitted when the greeting is changed
    event GreetingChanged(string newGreeting);

    // Constructor to set the initial greeting
    constructor(string memory initialGreeting) {
        greeting = initialGreeting;
    }

    // Function to get the current greeting
    function getGreeting() public view returns (string memory) {
        return greeting;
    }

    // Function to change the greeting
    function changeGreeting(string memory newGreeting) public {
        greeting = newGreeting;
        emit GreetingChanged(newGreeting);
    }
}
2
  • I would love to help, but the formatting for this is all over the place. Can you share the steps you took, then the error, and then what you expect to occur?
    – red-swan
    Dec 17, 2023 at 6:58
  • The function getGreeting() is a view that just returns a string so it should cause much trouble when executing. Some posible problems: 1/ the contract's addrdess isn't correct, 2/ compiler generates opcodes that aren't valid in the blockchain (which network are you using?, which solc version are you using?).
    – Ismael
    Dec 23, 2023 at 3:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.