Solidity Lessons

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

These Solidity Lessons are free to read, use, and share. If you found these helpful, my only ask is that you view more pages for the algorithm and keep learning! 1

Introduction to Solidity

Imagine you have a digital notepad that nobody can erase or change, not even you once you’ve written something on it. Solidity is like the pen for that notepad, but instead of writing regular notes, you’re writing rules.

These rules are called smart contracts, and they automatically carry out tasks when certain conditions are met. Solidity is the programming language used to create these smart contracts on the Ethereum blockchain. It’s a key tool for building applications that can execute transactions, create markets, or even run social networks, all without needing a middleman.

When you look at a Solidity program, you’ll see it has a structure that tells the computer what to do and when.

For example, it has pieces called “variables” that store information, like the number of digital coins you have. It also has “functions,” which are like commands that can move those coins from one person to another. Starting to learn Solidity is like learning any new language… it might seem strange at first, but once you get the basics, it starts to make a lot of sense.

In the next parts of this guide, we’ll go step by step through how to set up your own Solidity playground, write a simple contract, and eventually, build your own digital agreements that can do all sorts of neat things.

Whether you’re looking to create your own cryptocurrency or just curious about how blockchain technology works, understanding Solidity is your starting point. We’ll take it slow and make sure everything is as clear as it can be, so you can not only follow along but also start creating on your own.

To wrap up, Solidity is a powerful language for writing smart contracts on the Ethereum blockchain. It’s crucial for developing decentralized apps (dApps) and has a unique set of rules and syntax that you’ll get familiar with as you learn more. The beauty of Solidity lies in its ability to automate complex transactions securely and transparently, which is a big leap forward in how we think about digital agreements.

Setting Up a Development Environment

If you’re new to coding and want to get started with Solidity, you’re in the right place.

The first thing you need is a place to write your code. For a beginner, Remix is the perfect tool. It’s like using Google Docs instead of Microsoft Word; there’s no need to install anything on your computer. You can write your Solidity code right in your web browser!

Remix is a powerful, user-friendly online IDE that runs in your web browser. It’s designed for writing, compiling, and testing Solidity code with just a few clicks. To start using Remix, all you need to do is go to its website. Double check that the URL is https://remix.ethereum.org/, as there are copycat websites out there mimicking the real Remix, but will steal your crypto when you “approve” a transaction. (Also, never run code that you don’t understand, for this same reason.)

Once you’re there, you can immediately start writing your code. There’s no setup or installation required, which is perfect when you’re itching to dive in.

Using Remix, you can write your Solidity code in the browser and see the magic happen. It’s as simple as writing an email. There’s a section where you can type your code, a button to compile it (which is just a fancy word for translating your code into something the Ethereum network can understand), and another part of the screen where you can see the results of your work.

What’s great about Remix is that it comes with a bunch of sample contracts. This means you can look at examples and even tinker with them to see what changes do.

Don’t worry if you hit a snag. Remix is not just a tool; it’s also a teacher. It will point out if something’s wrong with your code and give you hints to fix it. It’s like having a friendly coach by your side.

As a beginner, you might make mistakes, and that’s okay! Remix is a sandbox—it’s a safe space to build, knock things down, and build them again – just make sure your environment has the letters VM in it (short for Virtual Machine).

How To Write Your First Contract

Ready to write your first smart contract? It’s going to be a simple one called “Hello World”. This is a traditional first step for learning any new programming language. Think of it as the digital equivalent of learning how to say “hello” in a new language.

First, go to the Remix website. You’ll see an interface split into several parts. There’s a file explorer on the left, a code editor in the middle, and panels for deployment and transaction on the right. To start fresh, click on the “File” icon in the top left corner and then on “New File”. You can name your file something like “HelloWorld.sol”. The “.sol” part is just like “.docx” for Word documents – it tells the computer that this is a Solidity file.

In the code editor, you’ll type your Solidity code. Here’s a very simple “Hello World” contract to get you started:

// Specifies the version of Solidity, using semantic versioning.
// The code will compile with any compiler version starting from 0.8.0 (inclusive)
// and less than 0.9.0 (exclusive).
pragma solidity ^0.8.0;

// Declare a contract named 'HelloWorld'
contract HelloWorld {
    // Declare a state variable 'greeting' of type 'string'
    string public greeting = "Hello, World!";
    
    // A public function that returns the value of 'greeting'
    function sayHello() public view returns (string memory) {
        return greeting;
    }
}

Copy this code into your Remix editor. The first line you see, which starts with //, is a comment—it’s just for humans to read and doesn’t affect the code. The pragma solidity line is a way of telling Remix which version of Solidity you’re using. The contract HelloWorld { … } part is the actual contract. Inside, there’s a piece of data called greeting that stores the words “Hello, World!”. The function sayHello is a simple command that, when called, will spit out whatever greeting says.

Now, to bring your contract to life, click the “Compile” button, which looks like a little book. After compiling, go to the “Deploy & Run Transactions” panel.

From there, you can deploy your contract to a simulated blockchain (that’s the virtual part we mentioned earlier) right there in Remix. Just click “Deploy,” and your contract will go live on the simulated network.

After deploying, you’ll see your contract appear under “Deployed Contracts”. If you click on it, you’ll see the sayHello function. Pressing this will show you the “Hello, World!” message.

Congratulations, you’ve just interacted with your first smart contract!

This is just a beginning, but it’s a big step. You’ve written a contract in Solidity, compiled it, and deployed it, even if it’s just on a simulated blockchain for now.

Solidity Basics

Now that you’ve dipped your toes into Solidity with your first smart contract, it’s time to understand some of the building blocks of Solidity code: data types and functions. Data types are the kinds of data you can store and manipulate, while functions are the actions you can perform on that data.

In Solidity, there are several basic data types that you’ll use often:

  • bool: Short for boolean, it can only be true or false.
  • uint: Stands for unsigned integer, meaning it’s a number that can’t be negative.
  • int: Represents integers, both negative and positive.
  • address: Holds an Ethereum address, which is like an email address for Ethereum accounts.
  • string: Stores a sequence of characters, like words or sentences.

Imagine data types as different shapes of blocks in a game. Each shape can only fit in its matching hole. For example, you can’t put a number where only a true or false (bool) should go.

Now, let’s talk about functions. There are many different types of functions, but they all act as a process that can be repeated. Functions are like instructions you give to the computer. They can read data (view functions), change data (pure functions don’t change or read any data on the blockchain), or perform actions that change the state of the blockchain (transaction functions).

Here’s a function that changes data:

function setGreeting(string memory _newGreeting) public {
    greeting = _newGreeting;
}

This function, setGreeting, allows you to change the greeting variable we set in the “Hello World” contract. The string memory _newGreeting part is a parameter — it’s a bit like giving someone a box where they can put anything they want, as long as it fits into the box.

So, if you wanted to change “Hello, World!” to “Hello, Solidity!”, you would call the setGreeting function with the new message as the parameter.

It’s essential to understand that when you change data with a function, it’s like updating a record. Every time you change something on the blockchain, it’s permanent. That’s why getting familiar with these concepts is so crucial before you start writing more complex contracts.

In your next Solidity projects, you’ll find that combining different data types and functions allows you to do all sorts of interesting things. You could keep track of digital assets, manage transactions, or even create a voting system!

In our upcoming sections, we’ll delve into more complex data types like arrays and structs, and control structures like if statements and for loops that let your contracts make decisions and run repetitive tasks. For now, experiment with the setGreeting function and try creating new functions in your “Hello World” contract. Each step you take builds your coding skills for the future.

Keep exploring, and remember, each line of code gets you closer to becoming a confident Solidity developer!

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.