Cover photo

Circolors Solidity Sunday #1

Welcome to the first entry to, hopefully, a long running series taking you through the basics of Solidity in bite sized chunks.

As more and more of our net worth gets wrapped up in smart contracts I think it’s increasingly important that switched on users have at least a basic understanding of the contracts they’re interacting with.

As far as a curriculum goes, the first few weeks will be focused on getting the fundamentals of solidity down, before moving onto exploring some real life contracts and how they work, to eventually being able to write more complex contracts ourselves.

I want to preface all of this by saying I’m not a solidity expert and will also be using these blogs as a way of strengthening my own knowledge of Solidity too. But let’s get started shall we?

Getting Started Resources

Remix Solidity Web Editor

Remix Solidity Web Editor

This is an in browser editor + deployer for smart contracts. It’s perfect for beginners as you don’t have to worry about downloading dependencies and you can deploy contracts from your MetaMask.

Solidity By Example

Solidity By Example

This is basically your Solidity bible in times of need. If you forget how a Solidity feature works you can typically find an explanation here as well as some example code to show you how it can be used.

Ropsten Testnet Ether Faucet

Ropsten Testnet Faucet

For these examples we’ll be deploying to an Ethereum test network and getting testnet ether can be a bit of a pain. We’ll start off on Ropsten testnet as I at least know this faucet works. Just enter the address you want to use on testnet on here and within a few mins you should get a little testnet Ether (a little Ether goes much further on Ropsten than it would on mainnet).

Day 1

Today we’re gonna look at about as basic a contract as possible to go over some Solidity features and then give you some challenges on how to take what you’ve learned and add to it.

Our Contract

I’ve made a very simple contract called MyDetails. All it currently just does two things:

  • Save a string (your username)

  • Update the string (change your username)

MyDetails.sol
MyDetails.sol

Let’s review it line by line to understand everything that’s going on:

//SPDX-License-Identifier: MIT

This is just a license to tell people who might come across your code what rights they have to use your code (can read further into the different types if you wish but not necessary right now).

pragma solidity >=0.7.0 <0.9.0;

This is necessary at the top of all your solidity files to tell the complier what version of Solidity you want your file to be compiled using. The current Solidity version is 0.8.12 so our code tells the compiler it can use anything above 0.7 but less than 0.9. Things change with each Solidity update and using an old compiler might mean it doesn’t understand some new feature/syntax that’s been added more recently. While on the other hand using a newer compiler might limit your ability to use some features that have been depreciated (this often becomes an issue when studying with Solidity resources more than a year old).

contract MyDetails {
}

This is your contract (duh), convention is to give it the same name as whatever you call your solidity file (MyDetails.sol) and to keep each contract in it’s own file. Everything you want this contract to know and do, will go between these curly brackets (unless you’re importing other solidity files but we’ll come to that later on).

string public username;

The first line of code in our contract is this. Solidity is a statically typed language. This just means you have to tell the compiler what type a variable is, it doesn’t work it out itself, which is pretty lazy but whatever. So we know that ‘username’ is a string type (text), but also that it is ‘public’. the public setting is for anything that you want people to be able to see easily. An example of this is when you’re minting NFTs from Etherscan, the functions you see in the ‘Read Contract’ and ‘Write Contract’ blocks are public ones (or external but again, worry about that later).

One note to add is unlike javascript where your semi-colons at the end of lines are optional, if you miss a semi-colon in Solidity the compiler will find you and chop off your fingers, so be warned.

So in short, our contract can hold some text (string) that anyone can see (public) called username.

function setUsername(string memory _username) public {
}

Here’s our first function called setUsername (I wonder what it’s going to do?).

This mostly looks similar to javascript if you’re already familiar with that. First you tell solidity you’re making a function, give it a name, then in the brackets let it know what arguments there are to pass into this function and finally as with ‘username’ above you tell the compiler whether you want this function to be public or private.

So our function is taking one argument (string memory _username). String we’ve already covered, so we know that this function wants us to give it a string. ‘memory’ tells the compiler that we only need to remember this argument until the function has completed (it doesn’t get stored on the blockchain).

Then what does this function do?

username = _username;

Relatively self explanatory but what our function does is set the username (the variable we created at the start of our contract), to _username (the argument we just passed into the function).

Using remix you can compile and deploy this contract, either to the ‘Javascript VM’ (locally on your PC) or to an ethereum testnet (change ‘Environment’ to ‘Injected Web3‘). If you use Injected Web3 you’ll get a metamask pop up (make sure you’re not connected to Ethereum mainnet or you’ll end up spending actual ETH deploying). Once your transaction has gone through you should get the ‘Deployed Contract’ tab at the bottom where you can interact with your contract.

If everything has gone to plan you should be able to press ‘username’ to view the current username or enter something into ‘setUsername’ to change the current username.

Remix Deploy
Remix Deploy

Relatively simple right?

Your Challenges

From here I have a few challenges to keep you busy:

  • MyDetails needs to also set and store another variable ‘favoriteNftBluechip’. (easy)

  • MyDetails also needs to set and store a number (not a string!) ‘daysSinceDegened’ (easy > more complex if you wanna get funky)

  • Your users are lazy and don’t wanna have to send 3 separate transactions to change ‘username’, ‘favoriteNftBluechip’ and ‘daysSinceDegened’, can you put them all into one function? (medium)

Primitive Data Types

Reading and Writing Variables

If you’re need of help or wanna flex the cool contract you’ve made you can reach me on twitter or join the Circolors Discord where you’ll find a lovely community of people learning about web3 & generative art.

Concluding Thoughts

If you’ve made it this far (and especially if you’ve made it through all the challenges) congratulations!

If you can’t wait until next week for more Solidity wisdom, my homework task for you all is to start working through CryptoZombies. Some of the solidity is a little outdated but it is still one of the best resources for getting to grips with Solidity, and it integrates some ERC721 stuff too.

If you have any questions/feedback/topics you want to see covered, hit me up on twitter or discord.

I’ll see you again next week. 🤙