Popularized by the Ethereum blockchain, Smart Contracts are a set of codes designed to perform a set of instructions on the blockchain. They self execute themselves when predefined conditions are met.
For instance, if there is a smart contract between a borrower and a lender with a predefined condition that as soon as the XYZ amount hits the borrower’s bank account, it should be transferred to the lender. The transaction will automatically trigger when that happens. If there was no smart contract, the lender would have to trust the borrower to pay them as soon as they get the money. Plus, the transaction would be dependent on a third party which is the bank.
Smart contracts enable individuals to transact with each other in a trustless manner without the need for an intermediary.
Why should you use Smart Contracts?
Autonomous/ No intermediary
Like we discussed, Smart contracts remove the need for intermediaries in any transaction. It enables two parties to engage with each other directly. Referring to the same example as above, the smart contract will execute the transaction without the bank taking any action. Likewise, smart contracts can also eliminate agents and legalese involved in renting or buying a house, supply chain management, etc.
The fact that an application is centralized means that you are at the mercy of a single entity. For instance, if Google wants, they can take down your website tomorrow or maybe delete some parts of it. They can control your activity on their platform and have access to all of your information. The decision-making here is centralized.
Being built on the blockchain allows smart contracts to have the highest level of security. All the records of the smart contracts are maintained on the blockchain, where no one can tamper with any data unless they manipulate all the nodes on the network which is impossible. Also, once deployed, the smart contract code cannot be altered unless already specified in the code.
How does Smart Contract work?
At its core, a smart contract code is a bunch of “if this happens…, then do this..” instructions.
These already exist in today’s world, like banks’ automatic bill payment feature is also an “if this happens…, then do this..” instruction. But the beauty of a smart contract is that it is built on the blockchain, allowing it to be Autonomous, Trustless, and Secure.
All the smart contract instructions need to be written in code and deployed on the blockchain network, which will then be replicated by all the network nodes to run, verify and execute the contract.
In instances where smart contracts need external data to execute transactions, oracles can be used. Oracles are a way to feed the blockchain with external data.
How to write a Smart Contract
Let’s look at an Ethereum powered smart contract example to understand how smart contracts are written and study their functioning in more depth. Microsoft Azure has a bunch of samples on their GitHub page; let’s discuss how an asset transfer smart contract works:
Firstly we need to define the parties involved in the transaction.
In this case, there are four parties in the contract — the buyer, the seller, the inspector, and the appraiser. The buyer and the seller exists to buy and sell the asset. The inspector will inspect the assets before the buyer makes a purchase. While the appraiser makes the asset considerable to buyers and facilitates sale for the seller. The seller would be the initiator of the smart contract, and the rest are the participants.
Then the smart contract code should define the role of all the parties involved like below:
It should also entail multiple states to define the smart contract condition. These states define numerous steps required in order to complete the transaction. The one we are discussing here has ten states as shown below:
And then finally, the workflow of the smart contract should be laid out:
A workflow lays out all the permutations and combinations of the smart contract and describes all the possible flows. Each party has a specific role to follow. Instance roles indicate that only the user with the application role assigned to the specific contract can take action on the contract.
The highlighted path shows how an asset transfer can take place between the buyer and the seller — The seller places the asset up for sale, the potential buyer places an offer, then negotiation and once the offer is agreed upon, the inspector and the appraiser working for the instance buyer will participate. After which, the transaction can be complete.
This is the code for the same:
How do DApps use Smart Contracts?
DApps, or decentralized apps, are nothing but a bunch of smart contracts working together. Real-life transactions/exchanges aren’t so straightforward as we saw above. They are a lot more complex. They involve multiple transactions between multiple parties. So DApps combine numerous smart contracts to conduct such real-life transactions in a decentralised manner.
An example of the same is UniSwap — Uniswap is a decentralised cryptocurrency exchange created by putting a bunch of smart contracts together. It enables users to conduct peer-to-peer cryptocurrency trades without the need for a centralised authority. If buyers and sellers on the platform agree to exchange their cryptocurrencies at a certain price, they can do so.
The Uniswap protocol runs on two primary types of smart contracts; the core and periphery. Core contracts protect the safety of users interacting with the platform. They spell out the logic of pool generation on the exchange, the pools, and the interactions between the assets in the pool.
Periphery contracts interact with one or more core contracts. It defines the methods for users to interact with core contracts.
There are around 3000+ decentralised applications across different sectors like finance, real estate, supply chain management etc., being built so far. All of them use multiple smart contracts to eliminate third parties in the process.