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.
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.
A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract.
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
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:
pragma solidity ^0.8.0;
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".
import "Other.sol";
In solidity, they have reserved keywords -
abstract, apply, auto, let, in, of, override, promise, typeof, try, sizeof, etc.
We're using Remix IDE to compile and run our solidity code.
Example
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;
}
Output
0: uint256: 4
Solidity supports both C-style and C++-style comments.
They have two types of comments single line or multiple lines.
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:
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 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.
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
}
}
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.
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
}
}
1. Arithmetic Operators
+, -, *, /, ++, --, %
2. Comparison Operators
==, !=, >, <, >=, <=
3. Logical Operators
&&, ||, !
4. Bitwise Operators
&, |, ^, ~, <<, >>, >>>
5. Assignment Operators
=, +=, -=, *=, /=, %=
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.
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;
}