Welcome to this introduction to smart contracts! In this series, we will learn the fundamentals of smart contracts in Ethereum. We will also build a smart contract from scratch using the Solidity language.
Here’s a quick recap of what we have learned so far.
We learned about geth, the command line interface into Ethereum.
We learned how to create a private blockchain on our computer.
We also learned how to mine ether on our private blockchain.
If you need to refresh these topics, watch our videos in this series, in the Youtube channel called “Turbo Prep”.
So, here are the goals for today’s video.
We will learn about the basics of smart contracts in Ethereum.
We will learn about EVM, or the Ethereum Virtual Machine, and why it’s important for understanding smart contracts.
We will also learn about the Solidity language, for creating smart contracts.
Lets get started!
To begin with, we will look at the basics of smart contracts in Ethereum.
What is a smart contract?
It is a “Mechanism To Distribute Assets Among A Fixed Number Of Parties”.
Let’s go through each aspect of this definition.
Parties refers to accounts on the Ethereum blockchain. As we have seen in prior videos, accounts can send ether to each other using transactions.
Smart contracts treat each of these accounts, as parties to a smart contract.
Smart contracts are nothing but software programs, that reside on the Ethereum blockchain.
This is what makes Ethereum’s blockchain different than some other blockchains, in that, it not only allows currency to reside on it, but software code as well.
A smart contract needs a way to distribute assets, usually ether, amongst its accounts. It does so using Ethereum transactions.
In other words, just like a human operating an account can send ether to another human operating an account, a smart contract can send ether to another account, multiple accounts, or even other smart contracts.
Here are some examples of smart contracts.
An escrow agreement can be modelled as a smart contract, where it will store ether on behalf of one or more parties, and release it upon completion of certain conditions.
On the same lines, an employment agreement can be modelled as a smart contract, where it will release a fixed amount of ether to the employee, upon completion of a finite amount of work.
An auction is a smart contract between bidders, who get ownership of a digital asset based on two conditions: one, the submission of the highest bid, and two, the payment of the final bid.
Lastly, voting can be modelled as a smart contract. We will learn to build a voting system in the next video.
Now, let’s get into more details around smart contracts, by learning about the EVM and Solidity.
What is the EVM?
It is the “Part of the Ethereum blockchain that handles state and logic”.
A key point to remember is this:
In Ethereum, a smart contract is similar to an object in an object oriented language such as C++ or Java.
Objects in Ethereum are nothing but accounts.
This means, each account can have its own state and logic. Again, this is no different than objects in object oriented languages having variables and methods.
Since accounts can communicate using transactions, so too can smart contracts send transactions to each other.
So – just like a tank is a truck with a gun and ammunition, Ethereum accounts are objects that have transactions and ethers.
Now, the EVM executes bytecode. Smart contracts exist as bytecode, which is executed by the Ethereum Virtual Machine.
It is similar to bytecode used in the Java JVM or C# CLR.
Finally, every transaction and execution of bytecode costs ether.
This mechanism ensures that any code running on the EVM does not waste resources, by going into an infinite loop, for instance.
Now we come to Solidity. Why do we need it?
Solidity is the “Language for writing smart contracts targeting Ethereum”.
It is the most popular way of writing Ethereum contracts as of now.
Other languages are also being created, such as Serpent, for Python programmers.
Several compilers are available for compiling smart contracts written in Solidity.
SolC and the browser-based compiler are the two most common compilers for Solidity.
These compilers compile Solidity into EVM bytecodes, which can then be deployed into the Ethereum blockchain.
This completes the entire lifecycle of a smart contract in Ethereum.
It starts as a Solidity file, which is compiled into EVM bytecodes, which are deployed into the Ethereum blockchain, ready to receive transactions.
This ends our introduction to smart contracts.
In the next video, we will learn to build and deploy a voting smart contract using Solidity in our lab.
Thanks for watching!