Solidity & Smart Contracts for Dummies

Hey, slow down... so what exactly is solidity, and what makes these contracts so “smart”?

Williams Peter
Coinmonks
Published in
7 min readMar 2, 2023

--

Photo by Nenad Novaković on Unsplash

What is Solidity?

Solidity is a high-level, contract-oriented programming language used to create smart contracts on the Ethereum blockchain. It is the most popular language for writing Ethereum smart contracts and is designed to make it easier to write secure, reliable, and efficient code.

Solidity is a Turing-complete language, meaning that it can be used to write programs of any complexity. It is also designed to be easy to understand and use, making it an ideal language for beginners to learn.

What are Smart Contracts?

Smart contracts are self-executing contracts that are written in code and stored on the blockchain. They enable users to enter into agreements without the need for intermediaries or third parties.

Smart contracts are programmed to automatically execute when certain conditions are met, and can be used for a variety of purposes, such as buying and selling goods, transferring funds, and managing digital assets.

A simple analogy of what this means can be found in the story below;

Once upon a time, two friends, Jill and Jack, wanted to start a business together. They wanted to make sure that each of them was held accountable for their contributions and that they would both be able to benefit financially from their hard work. To ensure that this happened, they decided to use a smart contract. A smart contract is essentially a digital agreement stored on the blockchain that is enforced by code.

With a smart contract, Jill and Jack could set up a system where they could both easily keep track of their contributions to the business and where they would both get paid according to the terms they agreed upon. Whenever either of them put in their time, the smart contract would automatically record it and update the payment schedule accordingly. This way, Jill and Jack never had to worry about one of them not getting paid for their hard work. With the help of the smart contract, Jill and Jack were able to succeed in their business venture and were both able to benefit from their hard work. While the two parties are happy with the outcome, nobody can really argue with lines of code. Sounds practical, right?

This brings us to the point that you guys came here for. I will be walking you through the process of writing your first smart contract using Solidity.

1. Set up the Environment

We will be using a development platform called Remix. It is a development environment for writing and testing smart contracts in the Solidity programming language. Your screen should look like this or something similar:

Remix Development Environment

After which you can create a new file by clicking the “New File” button, which will prompt you to name the new file and add the solidity extension (.sol) to the end of the file name.

Remix Workspace Environment

2. Version Definition

The first line in solidity must always mention the current version. It should seem as follows:

Version at the time of writing: 0.8.0

3. Writing your First Contract

You should skip a line and type the following:

Every new contract must begin with the word “contract” followed by the name of the contract, whose first letter should be capitalized, and followed by the open and closed curly brackets, which will contain the logic.

You can use a double forward slash to comment on your code, which in turn is helpful in describing what the code does or something else.

4. The Constructor and Global Variables

Before we start writing code, we should already have the terms of the contract clearly defined.

In lines 5 and 6, the two addresses (Jill and Jack) are used to store the addresses of the two people. In solidity, variables must be declared with their types. The owner in this case is of type "address,” which is unique to Solidity and represents an Ethereum wallet address. This will be useful later on when calling specific functionality meant only for the owners (Jill and Jack).

In lines 7 and 8, the two uint256 variables (jillHours and jackHours) are used to store the hours worked by each person.

In lines 9 and 10, the two uint256 variables (jillPay and jackPay) are used to store their corresponding payments.

Lines 12–19 is the constructor function. This special function will execute automatically upon the contract’s deployment.

The “public” keyword is used to declare a function or a state variable as publicly visible and callable by other contracts. It is also known as a “visibility modifier,” which lets the contract know who is allowed to call the function.

Here, we set the owner to “msg.sender”, which is a built-in global variable of the address that is calling the function (in this case, Jill). The jack variable is set to the null address (0x000000000000000000000000000000000000). The jillHours, jackHours, jillPay, and jackPay variables are set to 0.

5. Functions and more Logic

You should skip the next two lines and start typing:

The addJack function is used to add Jack to the contract. It requires that the jack variable be set to the null address (0x00000000000000000000000000000000) before Jack can be added. Once the requirement is met, the jack variable is set to the address of Jack.

After the next two lines below the closing bracket of the function, write another function, and it should look like this:

Lines 28–37, which contain the addJillHours and addJackHours functions, are used to add Jill’s and Jack’s hours to the contract. It requires that the sender of the transaction be the same as the address stored in both the jill and jack variables, respectively. If the requirement is met, the jillHours and jackHours variables are incremented by the number of hours passed in the parameter.

6. Let’s Make the Money

Let’s recap. So far, we’ve learned about global variables and their types, the constructor, special keywords such as "public," built-in variables like “msg.sender”, and “require” and functions. We’ve built the framework; now let’s tie it all together so we can complete our contract.

In the last part of this tutorial, we’ll implement a way that when either of them puts in their time, the smart contract will automatically record it and update the payment schedule accordingly.

Line 40 declares a function that is used to calculate Jill’s pay. It multiplies the jillHours variable by 10 and stores the result in the jillPay variable. It then returns the value of the jillPay variable.

Line 46 declares a function that is used to calculate Jack’s pay. It multiplies the jackHours variable by 10 and stores the result in the jackPay variable. It then returns the value of the jackPay variable.

This brings us to the end of our smart contract, and our next question will be, how can we use it? Sit back and be calm as you read how to deploy and interact with the smart contract we just created.

7. Deploying our Smart Contract

Your screen should look like this: You can achieve this view by clicking on the last icon on your left.

Then head over to the compiler tab by clicking on the second-to-last icon on your left. Also, ensure your compiler version matches the one below (0.5.7), and click the Compile ghost.sol button. Then your screen should look like this

Head back to the environment section of the platform by clicking the last icon. You can use the default values of the respected fields, i.e., environment, account, gas limit, value, and contract (this is compiled by the Remix platform). Then proceed to click on the “Deploy” button, which will go ahead and deploy the smart contract. You would see the result in the terminal section of the panel, showing the status of either a failure or a success.

Conclusion

Photo by Lama Roscu on Unsplash

You too are an amazing human being if you’ve read this far, and I guess you should understand the basic concept of how a smart contract works, how to write a basic contract, and how to deploy the contract. With time, you can use this technology to do a lot of things and automate a lot of processes, just like Jack and Jill do in their business.

If you have any questions or contributions, please drop them in the comment section. Thank you for reading.

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Williams Peter
Coinmonks

Ex-CEO at Kosmero | FullStack Engineer (MERN) | Web2 | Web3 Frontend Engineer | Technical Writer | Developer Relations