How to Learn Solidity for free

Spread the love

Solidity is the programming language used by Ethereum to write smart contracts. It is similar to JavaScript but has some unique features. In this article, we’ll cover how to learn Solidity for free.

Solidity is an object-oriented programming language that runs on top of the blockchain technology behind the cryptocurrency Ethereum. This means that it allows users to create decentralized applications (dApps) that run without any central authority.

The Basics of Solidity

Solidity is a relatively new programming language developed by Ethereum. It was designed specifically for writing smart contracts. Smart contracts are computer programs that execute automatically when certain events occur. They allow two parties to interact with each other securely and efficiently.

Start with the Basics:

The Ethereum blockchain allows us to run code using the Ethereum Virtual Machine (EVM) and smart contracts on the blockchain.

Smart contracts include all of our application’s business logic – all variables and functions that are part of the smart contract, which will serve as the starting point for your entire project.

Solidity, a programming language similar to Javascript and C, is used to create Smart Contracts.

How to install Solidity

Remix IDE

A remix is an online tool for writing Solidity smart contracts, then deploying and running them.

Just visit https://remix.ethereum.org from your browser, and we can start coding.

install remix

As you can see, you can choose between Solidity and Vyper. Both are languages ​​for writing smart contracts. Vyper is similar to python, and Solidity is similar to javascript.

Both can be compiled to EVM bytecode, like Javascript and Typescript. We are choosing Solidity.

On the left is the file browser. By default, there are two .sol files, just to demonstrate the basic syntax (ballot.sol is the smart contract, and ballot_test.sol is the script used to test the smart contract).

You just need to click that plus button, and we can start writing our first smart contract.

solidity smart contract

All Solidity source code should begin with a “version pragma,” specifying which version of the Solidity compiler to use. This is to avoid issues with future compiler versions, including changes that may cause the code to break.

It looks like this:

version pragma ^0.4.00;

Note: The source files shown above will not compile with compilers earlier than version 0.4.0, nor will they run on compilers starting with version 0.5.0.

You then create the contract by entering the reserved word contract and the name of the .sol file (it is important that the contract name matches the file name, we’ll discuss why later). as far as we are concerned

contract MyFirstContract {

}

solidity first contract

Let’s compile it. All you have to do now is go to the left-hand compilation tab and press the large compile button. You’ll see problems and warnings here if there’s a problem with the code (sorry for Solidity, it’s still a “new language”).

Under our current contract, everything is fine because we don’t actually do anything.

Now, I’m going to show you some mistakes on purpose. You can manually select a compiler from this drop-down menu.

solidity version

Let’s choose version 0.4.26, for example. Now, compile it again. Now, you will see the “The compiler has not been loaded” error.

This is because we specified the pragma to use a compiler version higher than 0.5.0. Just change the compiler version again, and the error will go away.

solidity compiler

Let’s Start Coding

Value types in Solidity

Boolean

keyword: bool

The value is a constant, i.e., true or false.

Integer

Keyword: int/uint (uint8 to uint256, stride 8 (unsigned, up to 256 bits), int8 to int256)

Signed and unsigned integers of various sizes.

Example:

1
2
3
contract MySample{
 uint UnsignedInt =50;
 }

Address

keyword: address

Holds a 20-byte value (the size of an Ethereum address). Address types are also present members and serve as the basis for all contracts.

Address members: balance and transfer

The balance of an address can be queried using the property balance, and either can be sent to the address using the transfer function.

1
2 
3 
4
address x = 0x123; 
address myAddress = this;
if (x.balance < 10 && myAddress.balance > = 10)
x.transfer(10);

String

String: String literals are written in double or single quotes like “foo” or ‘bar.’

Used for UTF data of arbitrary length.

1
string language = "Solidity";

These value types can interact with each other in expressions that contain operators. Next, in our Solidity tutorial, let me tell you about the various operators.

Operator

The operators in Solidity are the same as in JavaScript. Solidity has four types of operators:

Arithmetic operators

Solidity has very simple math. The following is similar to most programming languages:

  • Increase: x + y
  • Subtraction: x – y
  • Multiplication: x * y
  • Division: x/y
  • Round/Remainder: x%y

Solidity also provides the option to use the exponential operator as follows:

1 uint x = 10 ** 3; // equal to 10^3 = 1000

Increment operator

Stability of increment operators: a++, a-, ++a, -a, a+=1, a=a+1

Similar rules apply to other programming languages.

bitwise operators

The following are the operators: (bitwise OR) ‘|’, (bitwise exclusive OR), (bitwise negation) ‘~’, (bitwise right shift) ‘>>’, (bitwise left shift) ‘< <‘

Logical Operators

Logical operators in Solidity: ! (logical negation), && (logical sum), || (logical or), == (equality), != (not equal)

example:

1 
2
3
4 
5 
6 
7 
8 
9 
10
11
12
13
14
15
contract operators { 
// Arithmetic Operators
// +,-,*,/, %, ** 
// Incremental Operators
// a++, a--, a+=1, a=a+1,++a,--a ;
a=10; 
a= a++; //here, output will be 10, because the value is first returned and then then increment is done 
a=++a; 
//Logical Operators
!, &&, ||, ==, !=
isOwner = true && false;
var orValue= 0x02 | 0x01; // output would be 0x03
//Bitwise Operators~,>>, <<; 
function Operators() {
// Initialize state variables here}}

Solidity Data Structure

Solidity provides three types of data structures:

  • Structs
  • Arrays
  • Mapping

Structure Structs

Solidity provides a way to define new types in the form of Structs. Structs are custom types that can group multiple variables.

1
2
3
4
5
6
7
8
9
pragma solidity ^0.4.0;
contract Ballot {
struct Voter { // Struct
uint weight1, weight2, weight3;
bool voted;
address delegate1, delegate2, delegate3, delegate4;
string name;
uint vote1, vote2, vote3, vote4, vote5;
uint height1, height2, height3 } }

Note: Structures can only have 16 members, beyond which the following error may occur: Stack too Deep The stack is too deep.

Structures allow the creation of more complex data types with multiple properties.

Now, what if you need some collection, say addresses? Well, like most languages, Solidity has arrays.

Arrays

Arrays in Solidity can have compile-time fixed size, or they can be dynamic.

1 
2
uint[3] fixed; //array of fixed length 3
uint[] dynamic; //a dynamic array has no fixed size. It can keep growing

You can also create an array of structures. Using the previously created Voter structure:

1 Voter[] voting;

Note: Declaring an array as public will automatically create a getter method for it.

1 Voter[] public voting;

Mappings

Maps can be thought of as hash tables, they are virtually initialized such that every possible key exists and is mapped to a value whose byte representation is all zeros: the default value of the type.

The mapping declaration is:

1 Mapping(_Keytype => _ValueType )

Note: Keytype can be almost any type except dynamically sized arrays, contracts, enums and structs.

Example:

1
2
3 
4 
5 
6 
7 
8 
9
10
contract MappingExample {
mapping(address => uint) public balances;
function update(uint newBalance) { 
balances[msg.sender] = newBalance; }} 
contract MappingUser {
function f() returns (uint) { 
MappingExample m = new MappingExample() ; 
m.update(100);
return m.balances(this);
}}

Compile and Testing the Contracts:

To compile, just repeat the steps below (compile .sol button or cmd/ctrl+S on keyboard and it will recompile automatically)

To actually see how it works (if it compiles without errors), you need to deploy the contract.

To do this, navigate to the Deployment tab from the left, for Environment, select JavaScriptVM and click the Deploy button.

solidity compilation

Key point to learn Solidity:

Solidity is a very simple language, but being a good Solidity developer requires understanding how everything works on Ethereum.

  • Solidity is a high-level programming language with a syntax similar to ECMAScript (Javascript).
  • It compiles to EVM bytecode, which only the EVM understands.
  • The compiler is called Solc.

deploy solidity contract

Conclusion

The first lesson is more theoretical than actual coding, but beginners need to understand how things work on Ethereum. In the upcoming tutorials, we’ll write more interesting code and learn how to deploy our token on the Ethereum blockchain.

 


Spread the love
Leave a Reply

Your email address will not be published.

You May Also Like