1

my solidity code compiled using solcjs

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.4;
contract FirstContract {
  int count = 0;
  function incr() public{
    count = 15;
  }
  function decr() public{
    count = 10;
  }
  function display() public view returns (int){
    return count;
  }
}

Compiled the aboove code in counter.js using node counter.js

const fs = require('fs');
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
const bytecode = fs.readFileSync('build/counter_sol_FirstContract.bin');
const abi = JSON.parse(fs.readFileSync('build/counter_sol_FirstContract.abi'));

(async function () {
  const ganacheAccounts = await web3.eth.getAccounts();
  const myWalletAddress = ganacheAccounts[0];

  const myContract = new web3.eth.Contract(abi);

  myContract.deploy({
    data: bytecode.toString()
  }).send({
    from: myWalletAddress,
    gas: 5000000
  }).then((deployment) => {
    console.log('voting was successfully deployed!');
    console.log('voting can be interfaced with at this address:');
    console.log(deployment.options.address);
  }).catch((err) => {
    console.error(err);
  });
})();

And finally my html

<!DOCTYPE html>
<html>

<head>
  <title>First DApp</title>
</head>

<body>

  Increment
  <button id="incr">+</button>
  <br>
  Decrement
  <button id="decr">-</button>
  <br />
  <label id="myLabel"></label>

</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
<script>

  const web3 = new Web3('http://localhost:8545');

  const myContractAddress = '0xEE22eF9B315E396933231Cd86a069d1ea6763DD7';
  const myAbi = [{"inputs":[],"name":"decr","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"display","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"incr","outputs":[],"stateMutability":"nonpayable","type":"function"}];
  const myContract = new web3.eth.Contract(myAbi, myContractAddress);

  const htmlLabel = document.getElementById('myLabel');
  myContract.methods.display().call().then((jsonRpcResult) => {
      htmlLabel.innerHTML = jsonRpcResult;
      console.log(jsonRpcResult);
    });
  $('#incr').click(function () {
    myContract.methods.incr().call();
  });
  $('#decr').click(function () {
    myContract.methods.decr().call();
  });


</script>

</html>

Basically trying to run a counter using solidity but it is not updating for some reason

2 Answers 2

0

methods.xxx().call() is a read-only function. It cannot update the blockchain state. Thus, your incr().call() just returts the result of incr function without updating anything.

To update a state you need to you us methods.xxx().send().

1
  • I tried using send() but issue i faced there was it did not incremented the counter value if i write the below function function incr() public{ count ++; }, It returned the initial value which is 0
    – oizryx
    Commented Apr 4, 2021 at 18:07
0

Your incr and decr functions should be used by: .send() because you updating your count state variable on a blockchain, after this you should call your display function after updating your variable. it looks you want to get count in a incr function, but this function does not return anything, it just update your variable.

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.