top of page
Search
  • Writer's pictureShruti Jain

Smart Contracts



Welcome back guys!! Today we are going to talk about one of the most crucial and interesting feature of the Blockchain technology- Smart Contract. We are gonna find out why Blockchain is emerging at such a high rate in most of the industries and businesses, and why every new startup is willing to adopt it.


So, What is a contract? Its an agreement between two parties that is signed by them and both have to abide by the rules listed in the contract. This ensures the trustworthiness between the participating parties. However, there have endless number of frauds regarding contracts. They can be manipulated, or a person can be tricked to sign a contract that is not in his favor. It is kind of a really big deal in case of businesses. All kinds of transactions, imports, exports and other services are done with the help of contracts signed between the involved parties. So there has to be a secure trustworthy method to make parties agree upon common conditions and cannot manipulate or back off later.

So.....what to do?

Make it smart ;)




So, a smart contract is a piece of code that can be deployed on a Blockchain and will then exist forever on the network without being able to change. You can write all the logic and conditions of your contract in the code and then deploy it on the Blockchain. Whoever interacts with contract has to follow the conditions of the contract.


Briefing about its technicalities, a smart contract acts as any other program written in a particular language that can be compiled and tested. There are many programming languages that you can write smart contracts in, for example, solidity (the most popular on Ethereum network), vyper, serpent, simplicity, etc. Many Blockchains like Bitcoin, Tezos, Cardano, EOS, Ethereum, Dero, etc. support smart contracts. Talking specifically about Ethereum, it allows nodes to run and interact with the contracts via an Ethereum Virtual Machine (EVM). It is an environment that regulates the state of the Blockchain and where all accounts' data and smart contracts exist. It is responsible for all the state transitions that need to occur on the ledger from Block to Block. To learn more about EVM follow this link.


Now this may seem a little bit off-grid. I mean, turning a simple text of rules and regulations into an executable computer program!!?? How is it even done?

The answer is functions.

A smart contract code consists of functions that are simply few lines of reusable code performing a specific task with imposed conditions for it get executed correctly and generate error in the opposite case. These functions, that are set to public visibility mode are available for interaction with nodes on the Blockchain Network. A user needs to fulfill all the conditions listed inside the function while calling it. Now this bit is typically for people having programming background.


A simple smart contract that allows nodes to participate in a lottery will look like this in solidity language:



pragma solidity ^0.4.17;

contract Lottery{

    address public manager;
    address[] public players;
    address public lastWinner;

    function Lottery() public{        
        manager =msg.sender;    
    }
    
    function enter() public payable{
        require(msg.value> .01ether );        
        players.push(msg.sender);    
    }
    
    function random() private view returns(uint){
        return uint(keccak256(block.difficulty, now, players));                
    }
        
    function pickWinner() public restricted{
        uint index = random() % players.length; 
        players[index].transfer(this.balance);        
        lastWinner = players[index];        
        players =new address[](0);    
    }
    
    function winner() public view returns(address){
        return lastWinner;    
    }
    
    modifier restricted(){
        require(msg.sender == manager);        
        _;    
    }
    
    function getPlayers() public view returns(address[]){
        return players;    
    }
}  

Here the contract Lottery consists of multiple functions that define themselves clearly by their name. All the statements starting with require impose condition on the execution of that particular function. The function gets called if and only if the required condition is fulfilled. The function by the Lottery is the constructor of the contract that initializes the contract state. The msg object is a predefined object in solidity language that corresponds to the current function call. For example, msg.sender gives the address of the account that is calling that function and msg.value gives the amount of crypto(ether or wei here) sent with the function call. All the state manipulator functions are marked as payable. For example, here the enter function manipulates the players array and hence is marked payable, which means some amount of crypto (or gas) is required to execute that particular function.


The public, private, view and returns keywords are used to define the type of function. public, private, internal and external are the access specifiers, i.e., they restrict the access of the function. view means that this function doesn't manipulate the state of the contract. And returns is used to specify the type of the value that the function is returning.


Lastly the modifier is a type of function that just lists the conditions that need to be fulfilled in multiple functions. It contains "_;" at its end which specifies the execution of further lines of code written in the function to which the modifier has been applied to. For example, here the restricted modifier restricts all nodes other than manager to call a function. It is applied to pickWinner function that declares the lottery winner and has to be executed by the lottery owner for obvious reasons.


So, this was a simple understanding of a simple contract on Ethereum. We will learn more about contracts specifically in solidity in upcoming articles.


Till then, STAY TUNED and keep learning Blockchain!! See ya!!




16 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page