Blockchain and Ethereum Certification Tr... (38 Blogs) Become a Certified Professional
AWS Global Infrastructure

What are Smart Contracts? A Beginner’s Guide To Smart Contracts

Last updated on Jun 05,2023 10.5K Views

Shashank
Shashank is a Research Analyst at Edureka. He is an expert in... Shashank is a Research Analyst at Edureka. He is an expert in Blockchain technology with profound knowledge in Ethereum, smart contracts, solidity, distributed networks...
1 / 4 Blog from Smart Contracts

The phrase and concept of “Smart Contracts” were proposed by Nick Szabo with the vision to extend the functionality of electronic transaction methods, such as POS (point of sale), to the digital realm. Smart contracts help you exchange property, shares, or anything of value in a transparent, conflict-free way while avoiding the services of a middleman.
Ethereum code-Smart Contracts-edureka

Following are the topics that we are going to cover in this Smart Contract blog:

    1. What are Smart Contracts?
    2. Smart Contracts By Nick Szabo
    3. Why We Need Smart Contracts?
    4. Smart Contracts: Complexity As Per Use Cases
    5. Smart Contracts Use Case: Healthcare Industry
    6. Smart Contracts Advantages
    7. Blockchain Platforms For Writing Smart Contracts
    8. Tools for Writing & Deploying Smart Contracts
    9. Programming Languages For Writing Ethereum
    10. Property Transfer Smart Contract

    What are Smart Contracts?

    Smart contracts are a self-operating computer program that automatically executes when specific conditions are met.Smart Contracts working-smart contracts-edureka

    Smart Contracts = Trustless Contracts

    With smart contracts, you can share anything of value, among strangers in a conflict-free transparent way.

    You can also think of smart contracts as a Blockchain based vending machine. Just like vending machine are configured to take in dollars to dispense your choice of item, smart contracts use ether as a fuel to execute code based on pre-configured rules.

What are Smart Contracts | Deploy Your First Ethereum Smart Contract | Edureka

  1. Smart Contracts By Nick Szabo 

    In his paper, Szabo proposed the execution of a contract for synthetic assets, such as derivatives and bonds. “These new securities are formed by combining securities (such as bonds) and derivatives (options and futures) in a wide variety of ways. Very complex term structures for payments can now be built into standardized contracts and traded with low transaction costs, due to computerized analysis of these complex term structures,” he wrote.

 

  1. Why We Need Smart Contracts?

    We can automate many solutions by writing a contract on a Blockchain to make things simple and efficient. let’s compare and evaluate how smart contracts are better than traditional contracts. 

    Smart Contracts differences- Smart Contracts-edureka

    Smart Contracts: Complexity As Per Use Cases

    The complexities of contracts depend on the use cases you dream to code on the blockchain. The image below will give you a better clarity on the complexities of the smart contracts.Smart contracts complexities-smart contracts-edureka

    With contextualized smart contracts not only the companies will get automated, but also a decentralized autonomous government could be formed. 


    Let’s understand how smart contracts work with help of use case.

    Smart Contracts Use Case: Healthcare Industry

    Patient Data Management: When it comes to patient data management, there are two main issues in the healthcare industry:

    • First, each patient is unique, therefore access to complete medical records are essential in order to adapt the treatment and provide personalized care
    • Second, sharing information among the medical community is a major challenge

    Now, the above-stated issues can be resolved using smart contracts functionality on blockchains.health record mangement-smart contract-edureka

    Smart Contract Advantages

    Here’s what Smart Contracts give you:Smart contract capability-smart contract-edureka

    Blockchain Platforms for Writing Smart Contracts

    While Ethereum is the most popular platform for writing contracts, it is not the only one. The following are some of the other platforms used for writing smart contracts:

    Bitcoins: Bitcoin uses Script that has limited capabilities when processing documents. The Scripts are specifically designed to process Bitcoin transactions.

    Hyperledger Fabric: In Fabric, Chaincode is programmatic code deployed on the network, where it is executed and validated by chain validators together during the consensus process.

    NXT: It’s a public blockchain platform that contains a limited selection of templates for smart contracts. You have to use what is given, you can’t write your own code.

    Side Chains: Side chains enhances Blockchains performance and privacy protections. They also add capabilities like smart contracts, secure handles, and real-world property registry.

    Tools for Writing & Deploying Smart Contracts

    1. Mist Browser – It is a tool to browse and use dApps. It is a separate browser that can be used to browse dApps and interact with them.
    2. Truffle Framework – Truffle is a popular development framework for Ethereum. It has built-in smart contract compilation, linking, deployment, and binary management.
    3. Metamask – MetaMask is a bridge that allows one to visit the distributed web of tomorrow in their browser today. It allows users to run Ethereum dApps right in their browser without running a full Ethereum node.
    4. Remix – Remix is a web browser based IDE that allows users to write Solidity smart contracts, then deploy and run the smart contract.

    Programming Languages For Writing Ethereum Smart Contracts

    Solidity & Serpent are two primary languages for writing Ethereum Smart Contracts.

    Solidity: It is a contract-oriented high-level language with syntax similar to that of JavaScript and it is designed to target the Ethereum Virtual Machine (EVM).

    Serpent: Serpent is a high-level language designed for writing Ethereum contracts. It is very similar to Python, but as of September 2017, Solidity is the preferred language of development for Ethereum developers.

    Though Solidity is currently the most popular language for smart contracts, there are a few upcoming smart contract languages which can become important in the future.

  2. Upcoming Programming Languages

    1. Viper: Viper has a Python-like indentation scheme. It focuses on security and language and compiler simplicity.
    2. Lisk: Lisk uses javascript as a smart contract language which makes it easier for a developer to code applications.
    3. Chain: Chain provides enterprise-grade blockchain infrastructure with SDKs in popular languages such as Ruby, Java, and NodeJS.

    Now, since Solidity is most widely used programming language for writing Ethereum Smart Contracts, let me walk you through a contract  written in Solidity.

    Property Transfer Smart Contract

    Problem: Currently, we use the central authority to transfer the property ownership.  This makes it time-consuming and attracts a lot of extra expenses too with an additional burden of document management. Also, since the system is centralized, there is always a possibility of fraudulent.Land Registry-Smart Contracts-edureka

    Solution: The plan is to use the technology to make the details of real estate transactions visible to all parties – banks, brokers, government officials, buyers and seller

    Alright, let me write a Smart Contract for you:

    
    pragma solidity ^0.4.11;
    
    // We're making this use case for mimicking the real world property transfer
    // Pre-requisite of this use case is that:
    // A digital identity is in-place
    // Govt agrees to put the land records on the public blockchain
    // Each Development Authority (DA) becomes the defacto owner of the property that exist under their constituency/legislative body
    // When all the above conditions are met, then DA(owner) can easily attach the respective property to their rightful owner after thorough verification.
    // we shall be formulating the function around this set assumption.
    // we're assuming that each DA shall deploy their own smart contract as per their rule and regulation. This whole smart contract is written by considering DA as the owner, who can allot property.
    // A govt can become a layer on top of these DA. and the Govt can decide, which DA(address) becomes the owner of which contituency.
    // We can extend this easily. But after going through this smart contract, you shall be able to figure out, how the things might work.
    contract PropertyTransfer {
    
    address public DA; // DA shall be the owner, we shall be initializing this variable's value by the address of the user who's going to deploy it. e.g. let's say DA itself.
    
         uint256 public totalNoOfProperty; // total no of properties under a DA at any point of time. they should increase as per the allotment to their respective owner after verification.
     // Below is the constructor whose code is run only when the contract is created. 
    function PropertyTransfer() {
       DA = msg.sender; // setting the owner of the contract as DA.
    }// modifier to check the tx is coming from the DA(owner) or not
    modifier onlyOwner(){
       require(msg.sender == DA);
       _;}
    // This structure is kept like this for storing a lot more information than just the name
    struct Property{
       string name;  //keeping the map of the property against each address. we shall provide name to the property
       bool isSold;   // we're keeping the count as well for each address
    } 
    mapping(address => mapping(uint256=>Property)) public propertiesOwner; // we shall have the properties mapped against each address by its name and it's individual count.
    mapping(address => uint256) individualCountOfPropertyPerOwner; // how many property does a particular person hold
    event PropertyAlloted(address indexed _verifiedOwner, uint256 indexed _totalNoOfPropertyCurrently, string _nameOfProperty, string _msg);
    event PropertyTransferred(address indexed _from, address indexed _to, string _propertyName, string _msg);
    // this shall give us the exact property count which any address own at any point of time
    function getPropertyCountOfAnyAddress(address _ownerAddress) constant returns (uint256){
       uint count=0;
    for(uint i =0; i<individualCountOfPropertyPerOwner[_ownerAddress];i++){
       if(propertiesOwner[_ownerAddress][i].isSold != true)
       count++;
        }
       return count;
    }
    // this function shall be called by DA only after verification
    function allotProperty(address _verifiedOwner, string _propertyName)
    onlyOwner
    {
       propertiesOwner[_verifiedOwner][individualCountOfPropertyPerOwner[_verifiedOwner]++].name = _propertyName;
       totalNoOfProperty++;
       PropertyAlloted(_verifiedOwner,individualCountOfPropertyPerOwner[_verifiedOwner], _propertyName, "property allotted successfully");
    }
    // check whether the owner have the said property or not. if yes, return the index
    function isOwner(address _checkOwnerAddress, string _propertyName) constant returns (uint){
       uint i ;
       bool flag ;
    for(i=0 ; i<individualCountOfPropertyPerOwner[_checkOwnerAddress]; i++){
    if(propertiesOwner[_checkOwnerAddress][i].isSold == true){
       break;
    }
    flag = stringsEqual(propertiesOwner[_checkOwnerAddress][i].name,_propertyName);
    if(flag == true){
       break;
       }
    }
    if(flag == true){
       return i;
    }
    else {
       return 999999999; // We're expecting that no individual shall be owning this much properties
    }}
    // functionality to check the equality of two strings in Solidity
    function stringsEqual (string a1, string a2) constant returns (bool){
    return sha3(a1) == sha3(a2)? true:false;
    }
    // transfer the property to the new owner
    // todo : change from to msg.sender
    function transferProperty (address _to, string _propertyName)
    returns (bool , uint )
    {
    uint256 checkOwner = isOwner(msg.sender, _propertyName);
    bool flag;
    
    if(checkOwner != 999999999 && propertiesOwner[msg.sender][checkOwner].isSold == false){
         // step 1 . remove the property from the current owner and decrase the counter.
         // step 2 . assign the property to the new owner and increase the counter
       propertiesOwner[msg.sender][checkOwner].isSold = true;
       propertiesOwner[msg.sender][checkOwner].name = "Sold";// really nice finding. we can't put empty string
       propertiesOwner[_to][individualCountOfPropertyPerOwner[_to]++].name = _propertyName;
       flag = true;
       PropertyTransferred(msg.sender , _to, _propertyName, "Owner has been changed." );
    }
    else {
       flag = false;
       PropertyTransferred(msg.sender , _to, _propertyName, "Owner doesn't own the property." );}
    return (flag, checkOwner);
    }}
    

    So, you’ve just programmed a way to transfer property on a decentralized network. How Cool is that!!

    You can see that the potential for [smart contracts] to alter aspects of society is of significant magnitude.

    With that, I conclude this Smart Contract blog. I hope you enjoyed reading this blog and found it informative. 

    Got a question for us? Please mention it in the comments section and we will get back to you at the earliest.

    If you wish to learn Smart Contracts, build a career in the domain of Blockchain and gain expertise in Ethereum programming, get enrolled in live-online Blockchain Developer Course here, that comes with 24*7 support to guide you throughout your learning period.

Comments
1 Comment

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What are Smart Contracts? A Beginner’s Guide To Smart Contracts

edureka.co