Remix IDE

The following code was inspired by [StephenGrider](https://github.com/StephenGrider/EthereumCasts). The Inbox.sol is a common smart contract example to identify how to read and write to a blockchain through a smart contract. Minor updates have been made to the code to align with a newer version of Solidity.

Remix IDE#

Remix is a browser-based integrated development environment that allows users to write smart contracts in Solidity and Vyper. For this course, we will be writing all of our smart contracts in Solidity.

Once the smart contracts are ready for the Ethereum blockchain, there are a variety of different tools to allow us to deploy and run them. We can deploy on a local blockchain, a test blockchain, or on the main Ethereum blockchain. As we progress through this chapter, we will explain what each means.

Getting Started with Remix#

Remix can be found at http://remix.ethereum.org/, or a secure version at https://remix.ethereum.org. It is important to take note of which you select, because to send our smart contract to the blockchain, there are certain limitations if we access Remix through the secure protocol https. For the following few sections, I will be using the http version of Remix.

We will see the following screen:

remix-1

From this page, we will select "Solidity" as the environment we want to use.

remix-2

Upon selecting the "Solidity" environment, the "Solidity Compiler" will appear on the left. We can specify a few parameters here so that we are all aligned. Solidity is a tricky language because it evolves quite frequently, so pay close attention to the configuration we are applying here.

Compiler#

In the Solidity Compiler panel on the left, we're going to specify our compiler, to process the smart contract that we write.

The compiler can produce various outputs, ranging from simple binaries and assembly over an abstract syntax tree (parse tree) to estimations of gas usage. These outputs can be used in web applications that interact with the smart contract once we are done deploying it to the blockchain.

Configure the compiler as follows:

  • Compiler: v0.6.12

  • Language: Solidity

  • EVM Version: Compiler Default

  • Auto Compile: Flag Selected

  • Enable optimization: Flag Selected and number set to 200

Once these values are selected, your Solidity compiler settings should look like this:

remix-4

We will be using Solidity version 0.6.12 to write the smart contracts for this book.

File Explorer#

Now that we've added the appropriate compiler settings, let's jump into the File Explorer. You will notice that there is a toolbar on the left of your Remix screen.

remix-5

From this toolbar, you can get to the file explorer by clicking the following button:

remix-6

This will show the following next to the toolbar:

remix-7

The files that you see may be different from what is shown in the book because this is your first time using Remix.

From this section, we can do five things:

  1. View the smart contracts that we've created (if this is your first time, you may see two files already available)

  2. Create a new file

  3. Publish the files that you have on GitHub as a gist

  4. Copy all files into another version of Remix

  5. Add local storage to the file explorer

For the purposes of this chapter, we will only be viewing the files we want to work with and creating a handful of files to write smart contracts in.

Let's click the + icon to add our first contract.

This will prompt you to add a file name for the smart contract that you are creating.

remix-9

We will call this file "Inbox.sol". Once we put in the name, let's close the popup -the file should already be open in Remix. The file is blank, and we are now ready to start writing some Solidity.

remix-10

Creating a Smart Contract with Remix#

The following code was inspired by StephenGrider. The Inbox.sol is a common smart contract example to identify how to read and write to a blockchain through a smart contract. Minor updates have been made to the code to align with a newer version of Solidity.

Initial Set-Up#

Now that we have our smart contract file created, let's start adding some code.

The first thing that you have to do is to identify which version of Solidity you are using. We selected version ^0.6.12. The ^ lets the compiler know that the 0.6.12 is the minimum version of Solidity we want to use. To ensure that the Solidity processor knows what version we are using, we have to start the file by declaring that:

The next line of code declares the contract. In Solidity, use the keyword contract followed by the name of the contract. The contents of the contract will be contained within curly braces {}.

The purpose of this smart contract is to store a simple message on the blockchain. The message will be of type string and will be publicly available. This means that if anyone can find this contract on the blockchain, the message that is stored will be readable to that user.

To declare a public variable within the smart contract, add the following code inside the contract brackets:

The first keyword is the type of variable we want to create, the second keyword is to identify to the Solidity processor that this variable should be publicly available, and the final word is the name of the variable, message.

Since this line of code is not a function (or a modifier... more on that later), the line must end with a semicolon (;).

Now that we have our variable out of the way, the next thing is to create a constructor function. In Solidity, like other programming languages:

  • functions are modules of code that accomplish a specific task. They take in data, process it, and return a result.

  • constructors are special functions that are executed when the smart contract is initially created.

To declare a constructor in Solidity, use the keyword constructor. Next you have to identify if this function takes any inputs. In our case, our constructor will take a string variable which will be used to update the message variable that we just created. Finally specify the constructor's visibility. Let's keep it public. Once this is complete, add curly braces (the rest of our code will reside within these).

A few notes here about syntax and styling:

  1. There are two keywords associated with string variables in Solidity: storage and memory. If you anticipate creating functions that make state changes, storage might be the best option. If you need a copy of the data but don't necessarily want to manipulate the contract state use memory. When passing in string variables into functions, they should be passed in with the memory keyword.

  2. If you are passing in variables, a styling suggestion is to prefix your variable name with an underscore (_).

In this constructor, we will be updating the message variable with the string that we are passing into the constructor. Inside the constructor, let's set the message of the smart contract to the _message variable we are passing in.

Adding More Functions#

One other thing we might want to do with this smart contract is have the ability to change messages once the contract is available on the blockchain. When the smart contract is initially deployed, the state of the Ethereum blockchain gets updated with the message that we initially set. Since we are creating a smart contract called "Inbox", one of the things we may want to do is update the message that is stored at this smart contract. The variable, message, will get updated.

To do this, we will add a function called changeMessage. This function will take in a string variable called _changeMessage and it will be publicly accessible.

To declare a function in Solidity, use the function keyword and then name the function. Once the function is declared, add the variables you will pass in and the visibility. It should look something like this:

 

This page is a preview of Creating an ERC20 Token on Ethereum

No discussions yet