How To Start With Solidity

Welcome to the first article to learn how to start with Solidity used to develop smart contracts and dApps.

What is Solidity

Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs which govern the behaviour of accounts within the Ethereum state.

Solidity is a curly-bracket language designed to target the Ethereum Virtual Machine (EVM). It is influenced by C++, Python and JavaScript. You can find more details about which languages Solidity has been inspired by in the language influences section.

With Solidity you can create contracts for uses such as voting, crowdfunding, blind auctions, and multi-signature wallets.

See more at official Solidity documentation site.

How Can I use Solidity To Develop Smart Contract?

To use Solidity to develop dApps or Smart Contract, you need an integrated development environment.

One of the most used and best Remix IDE. Remix allows development and administration of smart contracts for Ethereum like blockchains.

It has its own compiler and javascript virtual machine which makes development of smart contract super simple.

Remix allows you to develop smart contracts online but it also offers a desktop app you can download and use. If you want to use the desktop version, you can download Remix here.

If you want to use the online version, just click here.

However, if you use the browser version, don’t forget to download your files. They are stored in browser local storage so if your browser local storage gets cleaned up, you’d also lose your files and all progress that you’ve made.

Starting with Remix and Solidity

We will use the browser version to start with our first smart contract development. Open the browser version and you should see the below.

  • The first menu is the main menu. You’ll find there the Compiler, Transactions Deployer, Analytics etc.
  • The second is File Explorer. This is where all your smart contracts are stored — either in browser local storage or on your localhost.
  • The third is a general overview with featured plugins. It’s also the main area where you’ll be creating your code.

Let’s click on the little File icon in File Exporer to create a new smart contract. You can call it mycontract.sol

You will notice that all contracts have .sol extension which stands for Solidity. Once your file is created, it will open a new blank window in the main area. This is where we stard development.

You can also create folders to organise your development but we won’t need it in this chapter as we start with a very simple contract.

Writing Your First Smart Contract in Solidity

Every source file should start with a comment indicated its licence. Detailed overview of licences can be found here in documentation: https://docs.soliditylang.org/en/v0.6.8/layout-of-source-files.html

For the purpose of this excercise, we will use MIT one:

// SPDX-License-Identifier: MIT

After that, you need to define pragma. Pragma enables certain features and checks and calls certain version of compilers. You can define a specific version, from version up, between versions. Below are some examples of codes.

// Uses any version between 0.6.0 to below 0.9.0. We will use this one.
pragma solidity >=0.6.0 <0.9.0;// Uses specific version 0.6.0 so if your Solidity compiler version is above that, the compiler will fail
pragma solidity 0.6.0;// Uses any version above 0.6.0 and below 0.7.0
pragma solidity ^0.6.0;

Next we define the name of the contract and start adding basic functions. We will define public value and also function that will allow us to change the value directly in Remix after our contract is deployed. Public values are used to manipulate smart contracts after they are released to the blockchain.

Note: Once contract is released to blockchain its source code cannot be changed. You can manipulate public variables only.

// CryptoNyan is the name of the contract here. Feel free to define your own name
contract CryptoNyan {
int public value; //Function to change the value of public integer value
function setValue(int _value) public {
// This will set the value variable to _value
value = _value;
}
}

Now let’s go to compile. But before we do, let’s check that your whole code looks like the code below.

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;// CryptoNyan is the name of the contract here. Feel free to define your own name
contract CryptoNyan {
int public value; //Function to change the value of public integer value
function setValue(int _value) public {
// This will set the value variable to _value
value = _value;
}
}

Once you’ve checked it, hit Command+S or CTRL+S. Alternatively, you can go to Solidity Compiler in the very left menu and hit Compile.

You should not see any errors or warnings and your smart contract should be compiled.

If you see any errors, check the code and also check the Compiler version in the Solidity Compiler.

Now go to Deploy & run transactions in the menu. It has the Ethereum icon. Make sure you have Javascript VM selected and hit Deploy.

Congrats, you’ve just deployed your first Smart Contract on test environment.

You should see it at the bottom like this.

Now you can add integer value which would manipulate the contract. You won’t see any effect as we haven’t added code that would show it but that will come in next article.