Counter Smart Contract
Remix is a powerful, open source tool that helps you write Solidity contracts straight from the browser. Remix also supports testing, debugging and deploying of smart contracts and much more.
It is recommended to use Remix to create and deploy smart contracts quickly.
In Remix, when opening a basic template you will see a folder called contracts. Right click on the folder and create a new contract called Counter.sol and begin typing the code below.
License
At the start of your smart contract, you can declare a license. In this case, we will leave this as UNLICENSED as we are not too concerned about what this code is used for.
// SPDX-License-Identifier: UNLICENSED
Mention Compiler Version
pragma solidity ^0.8.9;
compiler version
Now let's write out our contract:
contract Counter {
uint256 public count;
event updateCount(uint newCount);
function incrementCount() public returns(uint256) {
count +=1;
emit updateCount(count);
return count;
}
}
Here is a breakdown of what this contract is doing:
uint256 public count
: This line declares a public unsigned integer variable named count. Unsigned integers are non-negative numbers. Thepublic
keyword automatically generates a function that allows you to access the current value of thecount
state variable from outside the contract.event updateCount(uint newCount)
: This line declares an event namedupdateCount
. Events are a way for your contract to communicate that something happened on the blockchain to your app front-end, which can be 'listening' for certain events and take action when they happen. In this case, the event will emit the new value ofcount
.function incrementCount() public returns(uint256)
: This line declares a public function named incrementCount that doesn't take any parameters and returns an unsigned integer. Inside the incrementCount function:
count +=1
: This line increases thecount
variable by one.emit updateCount(count)
: This line triggers theupdateCount
event and sends the new value of the count variable to the listener.return count
: This line returns the new value ofcount
.
Now that we have our basic contract let's deploy this to the Polygon Amoy Test Network. You can follow the official Remix Documentation for Deployment to get this deployed and verified.