Write smart contracts using Remix

I guess you're as excited as we are to deploy smart contracts on the HAH blockchain and interact with them. Don't worry, as our first smart contract, we will deploy it on a local test network, so you can deploy and run it freely without any cost.

Step 1: Create a smart contract file.

Access Remix and create a new file. In the top-left corner of the Remix interface, add a new file and enter the desired file name.

Step 2: Write the smart contract code

In this new file, we will paste the following code::

If you've ever written a program, you should be able to easily guess what this program does. Below is an explanation of each line::

Line 3: Defines a contract named Counter.

Line 6: Our contract stores an unsigned integer 'count', starting from 0.

Line 9: The first function modifies the state of the contract and increments the variable count.

Line 14: The second function is a getter function that can read the value of the count variable from outside of the smart contract. Note that this function is not necessary because we have defined the count variable as a public variable, but it serves as an example.

The first simple smart contract is now complete. As you may notice, it resembles a class in object-oriented programming languages such as Java or C++. Now, we can run our contract.

Step 3: Compile the Smart Contract

After writing the first smart contract, we can now deploy it to the blockchain and run it. Deploying a smart contract on the blockchain simply involves sending a transaction containing the compiled smart contract code and specifying no recipient. The contract needs to be compiled before deployment.

We first click on the compile icon on the left to compile the contract:

Then click on the "Compile" button:

If you select the "Auto compile" option, the contract will be compiled automatically when you save the contract code in the text editor.

Then switch to the "Deploy & Run Transactions" screen.

On the "Deploy & Run Transactions" screen, carefully check the displayed contract name and click "Deploy". As you can see at the top of the page, the current environment is the "JavaScript VM", which means we will deploy our smart contract on a local test blockchain and interact with it for faster testing without incurring any fees.

After clicking the "deploy" button, you can see the contract displayed at the bottom. Click on the arrow on the left to expand it and view the contents of the contract. Here, we have the variable counter, the function increment(), and the getter function getCounter().

If you click the count or getCount button, it will actually retrieve the content of the count variable of the contract and display it. Since we have not yet called the increment function, it should display 0.

When you click the button to call the increment() function, you can see the log of the transaction generated at the bottom of the window. When you click the button to retrieve data instead of the increment() button, you see a different log. This is because reading data from the blockchain does not require any transactions (writes) or fees, only modifying the state of the blockchain requires a transaction.

After clicking the increment button, a transaction will be generated to call our increment() function, and if we click the count or getCount button, it will read the latest state of our smart contract, with the count variable greater than 0.

Last updated