Deploy the First Contract

Remix Introduction

Remix is a web-side integrated development environment that can be used to create, run, and debug smart contracts in your browser. It is developed and maintained by the Ether Foundation. Everything you need to develop smart contracts with solidity is provided in Remix's web interface, so developers don't need to build their own development environment. Remix greatly simplifies the way contracts are deployed, making it easy to deploy contracts without using the command line interface.

Create and deploy the first contract

  • First go to Remix:https://remix.ethereum.org and create a new file

  • Add a new file in the upper left corner of the Remix screen and enter the desired file name.

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

// SPDX-License-Identifier: MIT
pragma solidity >=0.5.17;

contract Counter {

    // Public variable of type unsigned int to keep the number of counts
    uint256 public count = 0;

    // Function that increments our counter
    function increment() public {
        count += 1;
    }

    // Not necessary getter to get the count value
    function getCount() public view returns (uint256) {
        return count;
    }

}

If you have ever written a program, you should be able to easily guess what this program does. The following is explained by line:

  • Line 3: defines a contract namedCounter.

  • Line 6: Our contract stores an unsigned integer count, starting at 0.

  • Line 9: The first function will modify the state of the contract andincrement()the variable count.

  • Line 14, the second function is a getter function that can read the value of thecountvariable from outside the smart contract. Note that since we defined thecountvariable as public, this function is not necessary, but it can be shown as an example.

This is the end of the first simple smart contract. As you can see, it looks like a class in an object-oriented programming language like Java or C++. Now it's time to run our contract.

Deployment Contract

Once we have written our first smart contract, we can now deploy it in the blockchain and run it.

Deploying a smart contract on the blockchain effectively just sends a transaction containing compiled smart contract code and does not specify any recipient.

We first compile the contract by clicking on the compile icon on the left:

You can select the "Auto Compile" option so that the contract will always compile automatically when you save the content in the text editor.

Then switch to the Deploy and Run Transactions screen at:

On the Deploy and Run Transactions screen, double check the contract name displayed and click Deploy. At the top of the page you can see that the current environment is "JavaScript VM", which means that we are currently deploying and interacting with smart contracts on a local test blockchain, so that testing can be done faster and without any cost.

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

If you click on thecountorgetCountbutton,it will actually retrieve the contents of the contract'scountvariable and display it. Since we haven't called theincrementfunction yet, it should display 0.

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

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

In the next tutorial, we will discuss how to publish PRC-20 Token

Last updated