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