Smart contracts are self-executing contracts with the terms of an agreement between buyers and sellers being directly written Into lines of code. Smart contracts permit trusted transactions and agreements to be carried out among disparate anonymous parties without the need for a central authority, legal system, or external enforcement mechanism. A smart contract, just like any other contract establishes the term of an agreement. But smart contract differs from every other contract, in the sense that a smart contract's term is code running on a blockchain like Ethereum. Smart contracts are called "smart" because it is a term that is established and executed as code running on a blockchain rather than on a paper in a lawyer's office. It makes it possible to securely automate and decentralize virtually any kind of deal or transaction, no matter how complex because it is run on a blockchain as said in the definition.
Benefits of smart contracts
Smart contracts allow developers to take advantage of blockchain security, reliability, and accessibility while offering sophisticated peer-to-peer functionality from loans and insurance to logistics and gaming.
Transparency: Smart contracts are published by a blockchain and can be read and written by anyone who has access to the blockchain.
Simplicity: Because smart contracts are expensive to deploy onto the blockchain and contain sensitive logic dictating the flow of financial transactions they tend to be much smaller and simpler than most codebases.
Security: Automated contracts use the highest level of data encryption currently available, which is the same standard that modern crypto-currencies use. This level of protection makes them amongst the most secure items on the world wide web.
Immutability: Once a smart contract has been deployed, it cant be modified and is guaranteed to function identically no matter when it is called. This allows smart contracts to operate a reliable, trusted third parties because no individual controls the smart contract, it can act as a financial intermediary, a trustworthy automated market maker, or much more by guaranteeing impartiality.
Types of account present in every smart contact
Externally owned accounts(EOAs): This type of account is managed by a human user, This account is controlled by their private keys.
Contract account: This type of account is managed by their underlying smart contract code. The contract account cont instantiate actions on their own, they can only respond to transactions received from the EOAs. It is controlled by their code.
Smart contract example
This is a smart contract written in Solidity programming language
Storage
pragma solidity ^0.4.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
The first line simply tells that 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.5.0). This is to ensure that the contract does not suddenly behave differently with a new compiler version. The keyword pragma is called that way because, in general, pragmas are instructions for the compiler about how to treat the source code (e.g. pragma once). A contract in the sense of Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. The line uint storedData; declares a state variable called storedData of type uint (unsigned integer of 256 bits). You can think of it as a single slot in a database that can be queried and altered by calling functions of the code that manages the database. In the case of Ethereum, this is always the owning contract. And in this case, the functions set and get can be used to modify or retrieve the value of the variable.