Smart contracts are high-level program codes that are compiled to EVM byte code and deployed to the ethereum blockchain for further execution. It allows us to perform credible transactions without any interference of the third party, these transactions are trackable and irreversible. Languages used to write smart contracts are Solidity (a language library with similarities to C and JavaScript), Serpent (similar to Python, but deprecated), LLL (a low-level Lisp-like language), and Mutan (Go-based, but deprecated).
Example: In the below example, we have discussed a sample solidity program to demonstrate how to write a smart contract in Solidity.
- Solidity
// Solidity program to
// demonstrate how to
// write a smart contract
pragma solidity >= 0.4.16 < 0.7.0;
// Defining a contract
contract Test
{
// Declaring state variables
uint public var1;
uint public var2;
uint public sum;
// Defining public function
// that sets the value of
// the state variable
function set(uint x, uint y) public
{
var1 = x;
var2=y;
sum=var1+var2;
}
// Defining function to
// print the sum of
// state variables
function get(
) public view returns (uint) {
return sum;
}
}
Output:
Explanation:
1. Version Pragma:
pragma solidity >=0.4.16 <0.7.0;
Pragmas are instructions to the compiler on how to treat the code. All solidity source code should start with a “version pragma” which is a declaration of the version of the solidity compiler this code should use. This helps the code from being incompatible with the future versions of the compiler which may bring changes. The above-mentioned code states that it is compatible with compilers of version greater than and equal to 0.4.16 but less than version 0.7.0.
2. The contract keyword:
contract Test{ //Functions and Data }
The contract keyword declares a contract under which is the code encapsulated.
3. State variables:
uint public var1; uint public var2; uint public sum;
State variables are permanently stored in contract storage that they are written in Ethereum Blockchain. The line uint public var1 declares a state variable called var1 of type uint (unsigned integer of 256 bits). Think of it as adding a slot in a database. Similarly, goes with the declaration uint public var2 and uint public sum.
4. A function declaration:
function set(uint x, uint y) public function get() public view returns (uint)
- This is a function named set of access modifier type public which takes a variable x and variable y of datatype uint as a parameter.
- This was an example of a simple smart contract which updates the value of var1 and var2. Anyone can call the function set and overwrite the value of var1 and var2 which is stored in Ethereum blockchain. This is an example of a decentralized application that is censorship proof and unaffected to the shutdown of any centralized server. As long as someone is running a single node of Ethereum blockchain, this smart contract will be accessible.
- The variable sum is calculated by adding the values of the variables var1 and var2.
- Function get will retrieve and print the value of the state variable sum.