Loading...
Solidity is a contract-oriented, high-level programming language for implementing smart contracts. Solidity is much similar to JavaScript.
You can use solidity to create contracts for uses as voting, crowdfunding, multi-signature wallets.
What is Ethereum?
Ethereum is a decentralized blockchain platform that runs smart contracts.
The Ethereum Virtual Machine, also known as EVM, is the runtime environment for smart contracts in Ethereum. The Ethereum Virtual Machine focuses on providing security and executing untrusted code bt computers all over the world.
What is Smart Contract?
A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract.
Solidity - Basic Syntax
A solidity source file can contain any number of contract definitions, import directives, and pragma directives.
Let's take a simple example of a Solidity file -
Firstly you can define our file name with extension .sol example: Test.sol
Open code in remix IDE
pragma solidity >=0.4.0 <0.8.0;
contract Test {
unit storedData;
function set(unit _x) public {
storedData = _x;
}
function get() public view returns(unit){
return storedData;
}
In this code, the first line is a pragma directive which tells the source code is written for Solidity version 0.4.0 or anything newer that does not break functionality up to, but not including, version 0.8.0.
If you want to define our contract 0.8.0 version:
Open code in remix IDE
pragma solidity ^0.8.0;
Contract
A solidity contract is a collection of code and data that resides at a specific address on the Ethereum Blockchain.
Importing Files
If you want to import statement that are very similar to javascript means that files those Solidity supports. We use the OpenZepplin Libraries in the future.
If you want to import any file like "Other.sol".
Open code in remix IDE
import "Other.sol";
Reserved Keywords
In solidity, they have reserved keywords -
abstract, apply, auto, let, in, of, override, promise, typeof, try, sizeof, etc.
Solidity - First Application
We're using Remix IDE to compile and run our solidity code.
Firstly, copy this code in Remix IDE code section.
Example
Open code in remix IDE
pragma solidity ^0.8.0;
contract Test{
function get() public view retuns(unit){
unit a = 2;
uint b=2;
uint c = a+b;
return c;
}
click Start to Compile button.
then, click Deploy button.
Select Solidity Test at 0xa334g.... in the drop-down.
Click getResult Button to display the result.
Output
Open code in remix IDE
0: uint256: 4
Solidity - Comments
Solidity supports both C-style and C++-style comments.
They have two types of comments single line or multiple lines.
In Single line comment, " // ".
Double line comment, " /* */ ".
Solidity - Types
While writing a program in any language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values.
They have different type of value type keyword, example:
Open code in remix IDE
pragma solidity ^0.8.0;
// This the single-line comment. Comments does not effect to our code.
/* */ Multi line comment.
contract Test{
// In unit they have value from uint8 to uint256. uint means unsigned integer means only positive integer.
unit a;
bool b;
string c;
}
Solidity - Variables
Solidity supports three types of variables.
State Variables − Variables whose values are permanently stored in contract storage.
Local Variables − Variables whose values are present till the function is executing.
Global Variables − Special variables exist in the global namespace used to get information about the blockchain.
Open code in remix IDE
pragma solidity ^0.8.0;
contract Test {
uint storedData; // State variable
constructor() public {
storedData = 10;
}
function getResult() public view returns(uint){
uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return storedData; //access the state variable
}
}
Solidity - Variable Scope
Scope of local variables is limited to function in which they are defined but State variables can have three types of scopes.
Public − Public state variables can be accessed internally as well as via messages. For a public state variable, an automatic getter function is generated.
Internal − Internal state variables can be accessed only internally from the current contract or contract deriving from it without using this.
Private − Private state variables can be accessed only internally from the current contract they are defined not in the derived contract from it.
Open code in remix IDE
pragma solidity ^0.8.0;
contract C {
uint public data = 30;
uint internal iData= 10;
function x() public returns (uint) {
data = 3; // internal access
return data;
}
}
contract Caller {
C c = new C();
function f() public view returns (uint) {
return c.data(); //external access
}
}
contract D is C {
function y() public returns (uint) {
iData = 3; // internal access
return iData;
}
function getResult() public view returns(uint){
uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return storedData; //access the state variable
}
}
Operator
1. Arithmetic Operators
+, -, *, /, ++, --, %
2. Comparison Operators
==, !=, >, <,>=, <=
3. Logical Operators
&&, ||, !
4. Bitwise Operators
&, |, ^, ~, <<,>>, >>>
5. Assignment Operators
=, +=, -=, *=, /=, %=
Conditional Operator ( ? : )
The conditional operator first evaluates an expression for a true or false value then executes one of the two given statements depending upon the result of the evaluation.
Open code in remix IDE
pragma solidity ^0.8.0;
contract Test{
uint a = 6;
unit b = 2;
unit c = (a>b)?true:false;
function result() public view returns(uint){
return c;
}