An Introduction to Solidity and Remix đź‘ľ

gmchad.eth
7 min readJun 11, 2022

--

đź“š Introduction

The quickest way to get started developing in solidity is with the Remix browser-based IDE (integrated development environment).

https://remix.ethereum.org/

Remix has all the development tools needed to start writing solidity code, compiling code (turning code into machine language), and deploying smart contracts onto the Ethereum network.

Let’s dive into some of the features Remix has to offer and try deploying a smart contract.

đź”® Solidity

We’re going to start with the 1_Storage.sol smart contract found under the contacts folder in the default workspace tab.

1_Storage.sol

Don’t worry if this is your first time looking at solidity or code for that matter, we’re going to walk through this smart contract step by step.

Line 3: pragma solidity ≥0.7.0 <0.9.0; — All smart contracts start with this line which serves to specify to the compiler which version of solidity this contract should target. For this contract, we’re targeting versions greater or equal to 0.7.0 and less than 0.9.0.

Line 10: contract Storage {...} —All smart contracts must begin with the contract keyword followed by the name of the contract. This contract is called Storage.

Line 12: uint256 number; — This is a variable that stores an unsigned integer up to 256 bits. An unsigned integer means a positive number with no decimal points. 256 bits represent how big this integer can be, in this case, the max size in solidity. To learn more about bits and bytes here is a great article.

Line 18–20: function store(uint256 num) public { number = num } — These lines represent a function that takes in one parameter, uint256 num , and sets the variable numberfrom Line 12 to that value. A function is a stored piece of code that can be reused and called by other code and sometimes users of the smart contract. The public tag indicates anyone outside of the contract can call this function, meaning you or me.

Line 26–28: function retrieve() public view returns (uint256) { return number } — You can probably guess that this function just returns the variable number . This function is public view , which indicates it’s open for anyone to call and does not modify any contract variables. In solidity, you also need to specify what a function returns if it does so. In this case number is a uint256 so we specify that at the end of the function signature.

Even though this is a very basic contract that simply stores and returns a number, there are quite a few language features demonstrated. Let’s now compile and deploy this contract.

🪄 Compilation

Compiling a Contract

Head over to the third tab on the left-hand navigation bar and you should be presented with the above screen. Compiling a contract is the process of turning human-readable solidity code into Bytecode that the Ethereum Virtual Machine (EVM) can process. You can think of the EVM as the distributed operating system of Ethereum. To learn more you can jump into the rabbit hole here.

Make sure the Compiler version is set within the range specified by the pragma , 0.8.7 is fine. You also want to make sure the Contract chosen is Storage (1_Storage.sol) . Sometimes when your contract depends on other contracts (this is called inheritance), Remix has trouble figuring out which contract you want to deploy.

Once you have your parameters set, you can go ahead and click the blue compile button and wait for the green checkmark to pop up. If there are issues, Remix will let you know.

ABI & Bytecode

You’ll notice on the bottom left that you can also copy the ABI and the Bytecode of the compiled contract. ABI stands for Application Binary Interface and it’s a format that other programs can use to understand how to interact with a specific contract. All Dapps or decentralized applications use smart contract ABIs to connect frontend applications to smart contracts.

Let’s take a look a the specific ABI of this contract.

Contract ABI

The ABI is written in JSON (javascript object notation), a common format for representing data in many programming languages. We won’t go into detail here, but you’ll notice the common keywords between the smart contract and the ABI.

The key takeaway is that the ABI is used by other programs to interact with smart contracts.

We won’t take a look at the Bytecode here because it’s mostly gibberish and out of scope for this article, but feel free to take a peek!

Let’s move on to deploying the contract and interacting with it on Remix!

🚀 Deployment

Deploying a Contract

Head over to the fourth tab which is represented by the Ethereum symbol. Let’s go over what each tab means.

Environment: This specifies where the contract will be deployed to. For this tutorial, we will be using the Javascript VM, which is a virtual blockchain that is running in the browser. It’s great for local testing but is not a real blockchain. The most common other option is Injected Web3 , which will use your Metamask environment and real accounts to deploy to a either test network or the Ethereum main network, commonly called mainnet.

Account: This is the account from where the contract will be deployed. With the Javascript VM, this is a test account that only lives in the browser. If you were to switch to Injected Web3 , you would see the account your Metamask is currently targeting.

Gas Limit: The max amount of gas you’re willing to spend to deploy the contract or interact with a contract.

Value: Some smart contract functions require you to send ETH to them. An example of this is minting functions for NFTs. You can select the amount to send in this tab here. WEI is just a denomination of ETH, you can play around with the conversions here.

Contract: The contract you are targeting to deploy. In our case, this should be Storage

Lastly, the At Address button allows you to point Remix to a contract that’s already deployed in the case where you would want to interact with a live contract from Remix.

Okay, we’re ready to deploy! Hit the orange Deploy button and it should show up under the Deployed Contracts section.

🕹️ Interaction

Interacting with a Contract

Once deployed, you can interact with a smart contract through Remix. Try specifying a number and hitting the store button. You should see something happen under the terminal tab.

Terminal Tab

If you can’t see the terminal, there should be a pull-up tab at the bottom of the screen you can use to bring it up. The terminal is where you can see the output of any transaction you send, including the contract creation transaction. Some things to notice from this transaction.

Transaction Hash: the unique id of the transaction

From: the account that executed the transaction

To: the contract address the transaction was sent to

Gas: the gas used by the contract

Decoded input: the data that was sent to the contract in the transaction. In our case this was 10 , the number to be stored in the contract.

The last thing to do is retrieve the number from the contract. Go ahead and click the retrieve button and you should see the number that you stored pop up!

An important takeaway is that retrieving data from a contract via an external account doesn’t cost gas as long as there are no variables (state) being updated.

Congrats! If you made it this far you’ve successfully walked through all the steps of understanding, compiling, deploying, and interacting with a smart contract via the Remix IDE!

If you want to challenge yourself, try going through the same steps with the 3_Ballot.sol contract except instead of using the Javascript VM network, try deploying to a test network by using theinjected web3 environment option during deployment.

Hint: You’ll need to change your Metamask network to a testnet like rinkeby and request some free rinkeby eth.

Tip: If you want to learn more about solidity when walking through 3_Ballot.sol , https://solidity-by-example.org/ is a great site to learn the language essentials.

If you liked this tutorial, feel free to follow me on Twitter https://twitter.com/ultrasoundchad

--

--