Last week, we explained smart contracts.
Smart contracts are nothing but computer programs which run on the Ethereum Blockchain.
We stepped back to understand contracts in general & when the terms & conditions of a contract are turned into computer code that automatically executes depending on these conditions, that code is a smart contract.
The word "contract" in the context of Ethereum has no relevance or legal meaning.
We then explored the analogy of a vending machine to explain the smart contract concept.
Contract Life Cycle
Again, let us step back to a real life contract and describe the life cycle of such a contract. What are the stages of a contract life cycle? They are (Source : here):
Stage 1 - Preparation to Negotiate
Stage 2 - Prepare Draft
Stage 3 - Contract negotiation & Creation
Stage 4 - Contract Execution
Stage 5 - Tracking & Performance Management
Stage 6 - Closing, Renewal & Reporting
The above stages are self - explanatory. Now, what does the above stages have to do with a smart contract?
Smart Contract Life Cycle
Just like a normal contract, a smart contract also goes through a life cycle. The stages are :
Stage 1 - Create a Smart contract
Stage 2 - Execute a Smart contract
Stage 3 - Delete a Smart contract
Let us explain each of the above stages :
Stage 1 - Create a Smart Contract
How do we create a smart contract? Write a program - it's as simple as that. After writing the program, the contract needs to be deployed on the Ethereum Blockchain.
Smart contracts are normally written in a high level language (e.g., Solidity). Let us again step back & understand what is a high level language?
Computer programs are a set of instructions given to a computer to perform an action(s) & if applicable, provide an output. Normally, the instructions provided is closer to something humans can understand like the below code in python :
print("Hello, my name is Blockchain!!")
It is very clear what the above code does, it prints
Hello, my name is Blockchain!!
Why is it clear? Because we understand the language used which is English like. It is not exactly English as there are syntax rules to be followed depending on the language used.
Now, take a look at the below code in Assembler - Intel x_86, Linux (Source : here):
SECTION .data msg: db "Hello, my name is Blockchain!!\n" len equ $ - msg SECTION .text global start start: move edx, len move ecx, msg move ebx, 1 move eax, 4 int 0x80 move ebx, 0 move eax, 1 int 0x80
Looks more complicated right?
The first set of instructions as shown above is in python which is a High level language and the second set of instructions is given in Assembler which is a Low level language. The main difference (among others) between high & low level languages are :
Programmers / Humans can easily understand high level language compared to computers &
Computers can easily understand low level language compared to humans
To execute a program, the instructions need to be understood by the computer and not by humans. This is because it is the computer which executes the instructions & computers understand only 0s & 1s.
To make the computer "understand" the program, we have to convert the instructions from high level to low level machine language. This conversion is called compiling the program. A compiler is used to compile a program meaning it is used to convert the instructions in that program from high level to low level language.
A compiler is a computer program that translates computer code written in one programming language (the SOURCE language) into another language (the TARGET language). The name "compiler" is primarily used for programs that translate source code from high-level programming language (Python, C etc) to a lower level language (assembly language, object code or machine code) to create an executable program. (Source : here).
There is also something known as interpreter which does the same function as a compiler (translate high level language to low level) but does it differently. Python uses interpreter instead of compiler.
The below visualization makes it more clear:
So, to create a smart contract, we have to write a set of instructions in a programming language (Like Solidity) & deploy them on the Ethereum Virtual Machine. To deploy the smart contract, these instructions need to be compiled to lower level BYTE CODE that runs in the Ethereum Virtual Machine (EVM) (Refer Week 36 for explanation of EVM).
Once compiled, a contract is then created through a special transaction on the Ethereum platform. An address is then allocated to the contract. This address can be used in transactions as a recipient (of funds) or to call one of the contract's functions (Remember the data field in a transaction?).
Each contract deployed in the Ethereum Blockchain is identified through this address
More smart contracts next week!!