Raymond Daugherty

Solidity

Day 2 …

Today we’ll be following the tutorial from CryptoZombies, which is basically a fun was to learn Solidity, the code used to create smart contracts that use the Etherium Blockchain.  It looks to be similar to Javascript (which I’m already familiar with, but will absolutely make JS a part of these 100 days), so I’m actually excited to learn this new language. Time to start…

 

Version Pragma

All solidity source code should start with a “version pragma”. This defines what version of Solidity your application runs on, and helps you avoid any future compiler updates that may break your app. For this, I’ll compile the smart contracts with any compiler version in the range of 0.5.0 (inclusive [identified by the “>=”]) to 0.6.0 (exclusive[identified by the “<“]). It’s best to think of it like “I’ll take anything that’s greater than or equal to version 0.5.0, but not version 0.6.0.

It looks like this:

pragma solidity >=0.5.0 <0.6.0;

This should always be the first line in your code. You can think of this like the HTML declaration for standard HTML pages.

 

Opening a Contract

Opening a contract is just Solidity’s way of creating a function. Just as Javascript creates functions with:

function nameOfFunction {
function does something;
}

a Solidity contract is set-up the same way. For example:

contract exampleOfContract{
do the contract thing;
}

 

Variables

Variables, like in any other programming language are simply holders of small bits of data. You can set variables to store any number of data types, like booleans and integers (numbers). With Solidity, you set variables like so:

uint theUnsignedInteger = 123;

Piecing the above code together, we would get a contract like this:

pragma solidity >=0.5.0 <0.6.0;  //1. Solidity version here
contract Example {
  // This will be stored permanently in the blockchain
  uint theUnsignedInteger = 123;
}

That’s it for Day 2. Only 98 more to go.