How to write your first Solidity smart contract?
Tags: solidity, remixide
Published: 2022-11-03 20:06:34

<!-- wp:paragraph -->
<p>Hello folks! Maybe, as you might know, I am learning blockchain development. My current focus is Solidity smart contract development. I decided to make a simple article to show you how to write your first Solidity smart contract. I don't think you will learn a lot. But I want to show how it is simple to start. Maybe that will motivate you to continue to do some interesting things. Who knows? Let's start. </p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading -->
<h2>What is Remix IDE?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Starting to develop Solidity smart contracts is very simple. There is an online IDE for that called Remix IDE. You can open it in your browser and start it! You don't need to set up everything on your computer. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Maybe some serious developers use the local environment to build robust smart contracts. However, I don't know a simpler way for beginners than Remix IDE. So, don't waste your time and click on <a href="https://remix-project.org/" target="_blank" rel="noreferrer noopener">https://remix-project.org/</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Then scroll down and click on that block.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2775,"width":493,"height":457,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image.png" alt="remix ide link" class="wp-image-2775" width="493" height="457"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>If you even use VSCode, then you will like Remix IDE UI. They are very similar. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2776,"width":768,"height":440,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-1-1024x586.png" alt="remix ide home" class="wp-image-2776" width="768" height="440"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>My goal is to show you the simplicity of writing a new contract. So, let's go. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Writing smart contract code</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Left-click on the contracts folder on the left and choose a new file. Name it MyContract.sol.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2777,"width":354,"height":274,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-2.png" alt="create new file in remix ide" class="wp-image-2777" width="354" height="274"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Cool, now we should write code. But before we write the first Solidity smart contract, let's check some examples of open contracts which are already in the contracts folder. You could open, for example, 1_Storage.sol. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2778,"width":485,"height":512,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-3-970x1024.png" alt="example of solidity smart contract" class="wp-image-2778" width="485" height="512"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>There aren't a lot of code lines. It's pretty simple. What could we learn from it? </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Each Solidity contract should have a license comment and a Solidity version at the top. </li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Each contract is very similar to a class in other languages. </li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>The contract could have methods inside. </li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Let's write our contract now. First copy license and pragma version. Then add an empty contract.</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 &lt;0.9.0;

contract MyContract {

}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>Looks great, but it's empty. Let's add some logic. </p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">contract MyContract {
    string name;

    function setName(string memory _name) public {
        name = _name;
    }

    function whatIsName() public view returns(string memory) {
        return name;
    }
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>I will not jump into detail today. The general idea of MyContract is similar to 1_Storage.sol. However, it stores strings instead of integers. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Compiling and Deployment</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Press Ctrl + S or CMD + S to compile the contract. Another way is to click that icon and press the button.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2779,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-4.png" alt="remix ide compiler icon" class="wp-image-2779"/></figure>
<!-- /wp:image -->

<!-- wp:image {"align":"center","id":2780,"width":512,"height":372,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-5-1024x744.png" alt="remix ide compile smart contract" class="wp-image-2780" width="512" height="372"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p> If everything is good, you will see the green mark. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Testing</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>How could we test the contract? In the real world, developers deploy contracts on the blockchain. However, it costs money. Remix IDE provides an environment to deploy contracts and test them for free. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Go to that tab.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2781,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-6.png" alt="remix ide deployment section icon" class="wp-image-2781"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>There are many things, but you don't need to know each one to test your smart contract. Just press the Deploy button and see the magic. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2782,"width":358,"height":512,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-7-715x1024.png" alt="remix ide deployment section" class="wp-image-2782" width="358" height="512"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":2783,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-8-1024x167.png" alt="remix ide transactions" class="wp-image-2783"/></figure>
<!-- /wp:image -->

<!-- wp:image {"align":"center","id":2784,"width":472,"height":203,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-9.png" alt="remix ide deployed contract" class="wp-image-2784" width="472" height="203"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>As you can see contract is deployed. Let's test it. Press the arrow on the left and expand the contract details. You can see our methods/functions from our contract.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2785,"width":403,"height":263,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-10.png" alt="remix ide interacting with solidity smart contract" class="wp-image-2785" width="403" height="263"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Remix IDE provides UI for interacting with our contract. Write something in the input text field and press the "setName" button. You will see some transaction appears in the bottom window. It is similar to transactions when we deploy contracts.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2786,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-11-1024x152.png" alt="remix ide transactions" class="wp-image-2786"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now press the "whatIsName" button. THE MAGIC WILL APPEAR! </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2787,"width":315,"height":194,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full is-resized"><img src="https://jakeroid.com/wp-content/uploads/2022/11/image-12.png" alt="remix ide interacting with solidity smart contract" class="wp-image-2787" width="315" height="194"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Is it pretty simple? Absolutely! Of course, to develop some complex smart contracts as real developers do, you should learn more about blockchains, crypto, and Solidity language. However, I hope this small note in my blog will inspire you to try and go deeper. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Stay posted if you are interested in more articles about blockchains, crypto, and Solidity. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By the way, did you check my article about <a href="https://jakeroid.com/blog/make-a-great-upwork-profile-to-get-clients/">how to make a great Upwork profile</a>? </p>
<!-- /wp:paragraph -->