paint-brush
How to Write & Compile Your Solidity Smart Contract Code in Remix: dApp Development Basicsby@lumoslabshq
2,171 reads
2,171 reads

How to Write & Compile Your Solidity Smart Contract Code in Remix: dApp Development Basics

by Lumos LabsMarch 23rd, 2023
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

This week’s blog post introduces the topic of smart contracts but in a language that most web developers might find easy to transition to: we're talking about Solidity. Most smart contract developers begin with Solidity and for which, the Remix Integrated Development Environment is the first tool that they generally begin their journey with.
featured image - How to Write & Compile Your Solidity Smart Contract Code in Remix: dApp Development Basics
Lumos Labs HackerNoon profile picture

Even though the notion of a smart contract dates back to the 90s, Ethereum was the first platform to realize its potential and eliminate the need for a third-party by virtue of its implementation.


With Ethereum taking center stage, the smart contract market has grown from strength to strength and is estimated to grow to a market size worth US$ 8.3 billion by 2030. Obviously, with smart contracts finding favor in projects across several industries, it should come as no surprise that smart contract developers get paid well, given this increase in demand.


Speaking of which, this week’s blog post introduces the topic of smart contracts but in a language that most web developers might find easy to transition to: we're talking about Solidity.

The Remix Integrated Development Environment

If you want to learn smart contract development, the ideal choice involves selecting a language that is popular, simple to learn and offers an easily accessible development environment.


For this reason, most smart contract developers begin with Solidity and for which, the Remix Integrated Development Environment is the first tool that they generally begin their web3 development journey with. For most, this will be the first time they use a development environment but this is made effortless since the Remix IDE can be set up in any of the popular browsers.


The Remix IDE - Default Screen


At a basic level, it’s important to note that each smart contract is a .sol file by virtue of the language that it is written in: Solidity. Also, compiling the contract requires you to select which version you wish to use. Both these tasks of writing and compiling your smart contract must be performed prior to deploying your smart contract to a Testnet or a Mainnet.


Speaking of which, it is vital that you test your smart contract on a Testnet prior to deploying it on the Mainnet. When using Remix, you can use either the Goerli or Sepolia testnet. However, for this, you will need to connect your Web3 wallet to Remix in the first place - preferably, using the Metamask wallet.


The Metamask Wallet


If you have no idea what a Testnet is, think of it as an environment where you get to run your contract using fake funds while evaluating whether your smart contract is ready for the real world through testing. In short, it’s a simulation of how the smart contract will work prior to its deployment on the Mainnet.


Lastly, you need to understand that every smart contract, once deployed, is identified by a unique address. Of course, you can move funds in and out of your smart contract by means of dummy user accounts that are available in Remix. Also, you must also learn everything you can about paying gas fees and how it gets deducted from your account as you write out and run your first smart contract too.

Your First Steps to Using the Remix IDE

Now, if you want to write and compile your first smart contract using the Remix IDE, there are four icons in the icon panel that you must learn to use:


The Icon Panel


Each of these four icons represent different functions on how to use the workspace:

  1. File Explorer
  2. Search in Files
  3. Solidity Compiler
  4. Deploy & Run Transactions


Selecting File Explorer takes you to the default workspace, as shown below:

The default workspace


You should be able to create, modify and delete folders and Solidity files here. Uploading files to your workspace as well as adding it to a Github gist are options that are available too.


The second icon in the panel is labeled Search in Files, and which allows you to do just that. In this case, we’re looking for the search term ‘vending machine’, and for which, the results appear below.


Search in Files


As for selecting the Solidity compiler option, we are taken to a screen, as shown below:

Solidity Compiler


Here you can select the compiler version and the contract you want to compile before hitting the blue button, as shown above.


As for the final icon in the panel namely Deploy & Run Transactions, we will cover how to use that icon in the next blog post of this series.

Working With Your First Solidity Contract

Now, a number of popular smart contract languages have their syntax similar to JavaScript, Python and Rust - languages that are already popular among experienced developers.


Yet the one that stands out is Solidity as the Ethereum platform continues to hire developers at an even clip, so it is best to begin our journey with an implementation of a vending machine smart contract using this language. You can copy the vending machine smart contract code from this Ethereum webpage.


Familiar with vending machines?


It must be pointed out that the working of a vending machine is very similar to how smart contracts function. In fact, the two methods ‘refill’ and ‘purchase’, as you can see in the Vending Machine smart contract, replicate the actions that the owner of a vending machine and its buyers perform in real life.


Now, navigate to your workspace by selecting the ‘File Explorer’ icon. Open a new .sol file using the Create New File icon, as shown below:


The Create New File Icon


Now, paste the Vending Machine smart contract code into that file along with the SPDX-License-Identifier, as shown below:

The 'Vending Machine' Smart Contract


After this, it’s time to compile your first smart contract in Remix based on the compiler version selected in the “pragma” statement. Of course, debugging for errors will be a part of this process too.

Compiling Your First Solidity Smart Contract in Remix

Now, once we’ve added our smart contract code to a .sol file in our default workspace, the next step is to debug your code. As a result of zero errors, you will compile the smart contract prior to deploying it to the Testnet.


Before we begin, the Ctrl + S keyboard shortcut will compile your smart contract. Now, if the Solidity Compiler option in the icon panel has a green check mark, your code is ready for deployment!


Successful compilation (with green tickmark)


Of course, not everything goes to plan. In most cases, there will be errors that you will have to sort out prior to succeeding with your attempt to compile the code you have written. The Solidity Compiler icon will look something like this, where ‘1’ indicates the number of errors in your code:


Unsuccessful compilation (with 1 error)


Speaking of common errors, beginners tend to fail to select the right compiler version that matches the one chosen in the smart contract’s pragma statement, as shown below:


Error in pragma statement


In order to fix this, access the drop-down menu in the Solidity Compiler option:

Changing the compiler version drop-down menu (with bi-directional arrows)


Change the compiler version to “0.8.7+commit.e28d00a7”, to obtain the following result:

Change to 0.8.7 version as in pragma statement


Another common error involves making syntax errors, such as the one involving the “cupcakeBalances” mapping:


Syntax error: no such mapping has been declared earlier, right?


If you haven’t noticed already, the red exclamation point enclosed in a red box indicates the statement that has an error. In this case, once you correct the typo by adding a “k” and hit Ctrl + S, that sign disappears.


After correcting the typo, the error is resolved


With a resolution of these two simple errors, you’ll also find a green tick mark appearing with the Solidity Compiler icon. Clearly, you’ve successfully compiled your first smart contract.


So now, it’s time to deploy your smart contract to the Testnet but this will be covered in a future blog post.


That said, we’re building a developer-centric Metaverse for you to learn more about Web3 Development and build your skills.


Sign up to join the Lumos Metaverse Whitelist for exclusive access: