Smart Contracts: Definition, Working, Writing & Deploying
Hello friends, Hope you are doing fine and having a good time in your life. The topic that I am going to discuss today is a very interesting and important one in the context of blockchain. I will discuss smart contracts in this article and I am sure you will learn the concept by the end of this tutorial. I have mentioned smart contracts in my previous articles and defined them before.
Before discussing smart contracts, let’s first revise what a contract is, and then we will talk about smart contracts.
Contract
- When two persons or two parties want to carry out a deal or a transaction such as buying property, they need some proof for future reference.
- So, a contract between two or more individuals is a legal document that serves as proof for the transaction.
- All conditions of the transaction are mentioned in the contract and must be fulfilled by the involved parties.
This was the basic idea and revision of the concept of a contract. Now, I will introduce a smart contract in the next section.
Smart Contract
When reading about blockchain and decentralized applications, you must have encountered the term “Smart Contract”. The name may indicate to you that it could be a contract that executes smartly or that has smart conditions. Well, in actuality, it is a contract whose conditions fulfill when it executes. If the conditions of such a contract are not met, then the execution fails.
- In the Ethereum blockchain, a smart contract is actually a program that is deployed on Ethereum.
- The smart contract contains logic and it is written in some high-level language.
- An example of a high-level language is solidity that we will discuss in the upcoming tutorials.
- A smart contract is a set of rules that are coded in the form of a program. These rules govern the transactions between the Ethereum accounts. The transaction could be a transfer of assets, transfer of money, or a change of Ethereum state.
- In Ethereum, the type of account having a smart contract is called “Contract Account”.
- Smart contracts can have balance and can also store data. They contain several functions that can be called by other accounts.
I have told you earlier that only an externally owned account can initiate a transaction on Ethereum and call a smart contract. Once initiated, the smart contract can call other smart contracts deployed on Ethereum or it can deploy a new contract.
Writing Smart Contracts
In this part of the tutorial, we are going to discuss writing smart contracts for Ethereum. Various tools are available for writing smart contracts. First, let’s talk about Remix.
Remix
- The remix is an Integrated Development Environment for writing smart contracts.
- It is a browser-based IDE and helps in developing contracts easily.
- The language used is Solidity.
- It helps in the following functions:
- Writing and developing smart contracts
- Deploying contracts on Ethereum networks
- Troubleshooting contracts
- A remix is an open-source tool available freely for everyone. So, if you are not comfortable using the online version, you can simply download it and use its functionality.
- The downloaded version can be used offline on a local computer system.
- The general steps for writing a smart contract using the online tool are given below:
- Open your browser and type https://remix.ethereum.org/ in the address bar.
- The site will show some default contracts. You can use that, study that or you can simply delete that if not needed.
- On the left bar, there would be an option to create a new file. Use that to create your smart contract.
- Write the code for your smart contract in the newly made file.
- The next steps are to compile the contract using a solidity compiler and deploying it on the network.
- After deployment, the functions written in the contract code can be executed.
This was a brief introduction to remix. Let’s talk about Solidity now.
Solidity
- Solidity is a high-level programming language used for writing smart contracts.
- This language has similarities with C and JavaScript languages.
- Solidity is case-sensitive.
- The extension of a solidity file is .solc and this file contains code.
- You can write and read such a file in any text editor.
- The solidity compiler is used for the compilation of code and named “solc”. The compiler generates the bytecode of the smart contract.
- This bytecode is generated for the Ethereum virtual machine as it cannot understand code in the high-level language.
- You can install solc using npm by typing the following command in the command prompt.
npm install –g solc
Remix and Solidity are the two essential tools for writing smart contracts. In the next part, we will discuss the deployment of contracts.
Deploying Smart Contracts
The contracts are deployed after compilation. The solidity compiler performs this task and generates two main outputs that are listed below:
- ABI
- Bytecode
Now let’s explore these two concepts.
ABI
- ABI stands for Application Binary Interface.
- ABI of a smart contract contains a declaration of all public and external functions. It also contains their parameters and returns types.
- ABI is the definition of a contract. Any account that wants to execute a contract function uses its ABI.
- After deployment, the contract instance is created using its application binary interface.
Bytecode
- The bytecode is understandable by Ethereum and helps in deploying the contract on the Ethereum virtual machine.
- Ethereum virtual machine cannot understand solidity so bytecode is a must.
ABI definition and bytecode both are necessary for deployment. The transaction for deploying a contract on EVM takes place and an address is generated for the smart contract. This address is the identifier of the contract. Any function of the contract can be called and executed using the contract address.
So, guys, this was all about smart contracts. We discussed the main idea and some steps to create a smart contract. The two main tools for creating a contract are also discussed in this tutorial. I hope you have learned these concepts and enjoyed reading them. That’s all for today. Take care and have fun.