Learning how to Smart Contracts pt.2

Learning how to Smart Contracts pt.2

Writing a Smart Contract on Remix IDE

Today I'm learning Solidity language, which is the language that we use to write Smart Contracts for the Ethereum Virtual Machine.

There is a Integrated Development Environment, IDE for short, called Remix And we will be using it to compile our contracts, test them in a local blockchain, and once ready deploy to a testNet called Rinkeby ( which is a ethereum copy testnet, thank you people on Rinkeby for running this for free! )

First we need to tell the code the license and the compiler version.

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

This will state, that this code is under Free use MIT LICENSE. And it will be compiled from versions 0.7.0 up to but not including version 0.9.0 ( compiled means built to machine language ), or we could do ^0.8.1 which means any version above that.

Now we are ready to define our contract

// this is a comment in the code.

contract Counter{
    uint public count; 
// this states that a new variable called count will be a integer of 256 bytes that cannot be negative

// Function to get the current count.
    function get() public view returns (uint) {
        return count;
    // public means everyone can use this function, view means we will read only
    // and returns(uint) guarantees we will return an unsigned integer data type.
    }

    // Function to increment count by 1.
    function inc() public {
        count += 1;
    }

    // Function to decrement count by 1.
    function dec() public {
        // This function will fail if count = 0
        count -= 1;
    }
}

Done! We wrote a very simple contract that act as a Public Counter !

Basically people can run 3 functions in this contract, they can get the counter number, they can increase or decrease the counter by 1. Every time they use a function on this contract it will cost some gas.

Deploying

So far we have been playing with this contract in the local environment to make sure it has no compiling problems. Now let's try deploying it to Rinkeby testnet!

As you can see in the image below, we have the green check mark at the compiler status. In the Deploy tab we will select the Injected web3 instead of the default JavaScript so we can use our MetaMask wallet to approve and pay for the transaction.

Screenshot from 2022-06-22 12-37-59.png

We will see how much gas this will cost us, and we will confirm the transaction to create this Contract.

Screenshot from 2022-06-22 12-41-49.png

Once it's deployed and no errors occurred, we will see a green check mark on the Command Line:

Screenshot from 2022-06-22 12-43-57.png

We can see that it was in fact created and all the Block Details of the transaction on Rinkeby's Etherscan

Screenshot from 2022-06-22 12-44-36.png

And if we copy and paste the contract address into the search bar we will get the contract information and all it's transactions, being contract creation the first one.

Screenshot from 2022-06-22 12-47-53.png

Done! We have a working, running contract on the Blockchain! 🤩

Alright Catch you guys on the next ~