0

I have a simple counter and it charges a fee everytime:

But it throws an error:

Error: processing response error (body="{"jsonrpc":"2.0","id":48,"error":{"code":-32603,"message":"Error: Transaction reverted without a reason string"}}", error={"code":-32603}, requestBody="{"method":"eth_estimateGas","params":[{"from":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","to":"0x5fbdb2315678afecb367f032d93f642f64180aa3","data":"0xd09de08a"}],"id":48,"jsonrpc":"2.0"}", requestMethod="POST", url="http://localhost:8545", code=SERVER_ERROR, version=web/5.4.0)

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;

contract Counter {
    uint256 public count;

    address public Owner;

    event Increment(uint256 value);
    event Decrement(uint256 value);

    constructor() public {
        count = 0;
    }

    function getCount() public view returns (uint256 count) {
        return count;
    }

    function increment() public payable {
        require(msg.value > 10);
        count += 1;
        emit Increment(count);
    }

    function decrement() public payable {
        require(msg.value > 100);
        count -= 1;
        emit Decrement(count);
    }
}

Front End:

const [count, setCount] = useState()

  useEffect(() => {
    loadCount()
    setInterval(() => { loadCount() }, 2000)
  }, [])

  async function loadCount() {
    const provider = new ethers.providers.JsonRpcProvider()
    const counterContract = new ethers.Contract("0x5FbDB2315678afecb367f032d93F642f64180aa3", Counter.abi, provider)

    const count = await counterContract.getCount()

    console.log(await count.toString())
    setCount(await count.toString())
  }

  async function increment() {
    const provider = new ethers.providers.JsonRpcProvider()
    const signer = provider.getSigner()
    const counterContract = new ethers.Contract("0x5FbDB2315678afecb367f032d93F642f64180aa3", Counter.abi, signer)

    await counterContract.increment()

    loadCount()
  }

  async function decrement() {
    const provider = new ethers.providers.JsonRpcProvider()
    const signer = provider.getSigner()
    const counterContract = new ethers.Contract("0x5FbDB2315678afecb367f032d93F642f64180aa3", Counter.abi, signer)

    await counterContract.decrement()

    loadCount()
  }

  return (
    <div className="flex flex-col items-center justify-center">
      <Head>
        <title>Create Next App</title>
        <meta name="description" content="Counter" />
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <h2 className="">{count}</h2>
      <button onClick={() => increment()}>INCREMENT</button>
      <button onClick={() => decrement()}>DECREMENT</button>
    </div>
  )
14
  • How are u calling those functions ? Using remix!?
    – Majd TL
    Oct 2, 2021 at 17:12
  • No I have a frontend
    – robinnlmn
    Oct 2, 2021 at 17:13
  • Are you sending value with the transaction? You can try it on remix and if it works then ur Frontend call is the error
    – Majd TL
    Oct 2, 2021 at 17:15
  • maybe show frontend code cause the contract look correct
    – Majd TL
    Oct 2, 2021 at 17:15
  • Ok updated it. I will try it on remix
    – robinnlmn
    Oct 2, 2021 at 17:19

2 Answers 2

0

The problem is probably from the variable count being use in a return statement and a global variable.

uint256 public count;


function getCount() public view returns (uint256 count) {
        return count;
    }

First you don't need this getCount function, By declaring count as public, solidity create a getter function for count. You can use count() and it will return the value.

Just remove that function.

and in the front end call for count()

const count = await counterContract.count()

Same result, Should not trow an error, and you just saved some gas on deployment.

0
0

require(msg.value > 10);

You have condition for value to be send while calling the function increament as well as for decrement and you are not sending any eth while calling the function. At the same time you had not added revert message. This might be the reason for failing the contract function call.

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.