- Toshendra Kumar Sharma
- December 31, 2019
A smart contract is used to describe the computer program code which is capable of facilitating, executing, and enforcing an agreement using Blockchain technology. Smart contracts help you to exchange money, property, shares, or anything of value in a transparent and conflict-free way avoiding the services of any third parties or middlemen. The term ‘smart contracts’ was first proposed by Nick Szabo, the American computer scientist, who invented the virtual currency called ‘BitGold’ in 1998. He defined smart contracts as computerized transaction protocols executing the terms of the contract. Smart contracts render transactions transparent, traceable, and irreversible.
What is Solidity?
It is an object-oriented high-level programming language used to implement smart contracts. Solidity helps create contracts for crowdfunding, blind auctions, voting, and multi-signature wallets. Solidity is influenced by languages such as Python, JavaScript, and C++. It supports inheritance and is statically typed. It is always important to use the latest version of solidity while deploying smart contracts as new features and bug fixes are regularly introduced.
Let us understand the steps involved in creating and deploying your first smart contract. This article will focus on writing and deploying a smart contract using Remix IDE.
Structure of a Smart Contract
A solidity smart contract includes the following:
- Data- This maintains the current state of the contract.
- Function- This applies the logic for transitioning the state of the contract.
There is a standard structure followed by solidity smart contracts. Any smart contract starts with the following statement.
Pragma Directive
The ‘pragma’ keyword is used to enable some compiler features or checks.
1
pragma solidity >=0.4.0 <=0.6.0
This statement defines that the smart contract (source file) will not compile with a compiler earlier than 0.4.0 and the version after 0.6.0. By introducing this declaration, no unintended behavior will be introduced if a new version of the compiler is introduced.
Contract Declaration
This is declared through the keyword ‘contract.’ This will declare an empty contract identified by the name of “PurchaseOrder.” This is depicted as follows:
1
2
contract PurchaseOrder{
}
Developing Smart Contracts
There are two types of variables one needs to be familiar with in smart contracts.
Value Type – These variables are passed by value. This means that they are always copied when they are used in assignments or as function arguments. Integers and booleans addresses are some prominent examples.
Reference Type – These variables are of complex types and are passed by reference. Copying these is expensive, and hence it needs to be managed carefully, and these variables do not fit into 256 bit.
Adding Data in a Smart Contract
The first step involves adding some data variables to the smart contract. For every order, there has to be a quantity associated with it. For example, in the case of a purchase order, there has to be a certain product quantity associated with it. The variable which is introduced now is an unsigned integer, and it is represented by uint256. Here, 256 signifies the storage of 256 bits. Let us now understand these terms.
- INT refers to an integer.
- U means unsigned, which means that this type can represent only positive integers and not both positive and negative integers.
- 256- This refers to the 256 bits in size.
- The minimum value of uint 256 can be assigned to 0.
- The maximum value that can be assigned for uint 256 is 2^256-1.
Defining the Constructor
A constructor is one that is called at the time of deployment of a contract. The constructor uses some values to initialize the contract. It is also possible to create a parameterized constructor which can be created by passing a variable and initializing the function using the passed in value. The access modifier “public” associated with the constructor is the key point to note here. The keyword “public’ denotes that anyone can access the function. Hence, this is not a restricted function.
Adding Functions
We will now add functions to make our program interactive. Functions refer to controlled capabilities that can be added to a program. Any function will always be preceded by the keyword function. A function declaration looks like this: “function <function name> <access modified> <state mutator> <return value>”.
Get Function
For any program, the most common requirement is to read the stored value. For example, let us consider the product_quantity value. To provide this capability, a read or get function is added. In this function, we will not manipulate the stored value, but will just retrieve the stored value. The get function can be broken down as follows:
- Keyword-Function name; Value- get_quantity()
- Keyword- Access modifier; Value- Public (The function can be accessed by anyone)
- Keyword- state-mutator; Value- View (The function only reads the state of the contract and does not change it)
- Keyword- Returns; Value- Defines a variable of type uint256.
Setter Function
It is necessary to read the data, but it is also necessary to have the capability to write/update data. This capability is added through a setter function. This function will take a value from the user in the form of an input parameter. We will now break down our set function. We will now add a sample function to update the value of product quantity.
Keyword- Function name; Value- update_quantity
Keyword- Access modifier; Value- Public
Keyword- state-mutator; Value- This is not needed as the state is updated by the functions.
Keyword- Returns; Value- Returns a variable of type uint256.
Smart Contract Deployment
It is now time to deploy the smart contract. We will use Remix Online IDE to test the smart contract. Remix is an online playground that is used for Ethereum smart contracts. It is completely based on a browser. It provides an integrated development environment where you can write your smart contracts. Remix provides an online solidity compiler capability. Remix IDE helps compile a smart contract seamlessly using a specific compiler version. It helps in the quick testing of a smart contract. Remix is equipped with a complete toolset to start the development of the smart contract and unit testing of the smart contract, without the need for installation on your local machine. By using Remix IDE, you can get started with smart contract development just with an internet connection and a browser.
Let us understand the steps of deploying a smart contract with Remix IDE.
- Create a new file by clicking on the plus icon.
- This will create an empty file. Click to open this file.
- Copy-paste the contract in this file.
- Click on the second icon that is just below the file icon in the left menu for the solidity compiler option to appear.
- Go to the compiler label and select the compiler version. After selecting the compiler version, click on the file. This will compile the smart contract.
- Following compilation, click on the compiler details button. This will provide you with two key information, such as bytecode and application binary interface. Bytecode is one where the operational code of the Ethereum Virtual Machine (EVM) and smart contract logic are converted into bytecode after compilation. The application binary interface is a JSON file that provides the details of all the methods exposed in the smart contract.
- To test the smart contract, it needs to be deployed. To deploy the smart contract, click on the icon in the left menu below the compile icon.
Interacting with the deployed contract
- The deployed contract has interaction methods using which you can interact with the smart contract.
- These are the public methods mentioned in the contract.
- There is an input box that you can use to input your parameters.
Any operation which causes the write operation to the state of a contract results in a transaction.
Conclusion
Using these steps, you can now create your first smart contract. In this article, we have understood what it takes to test the smart contract right from deploying the smart contract to a transaction initiation. I hope you have understood and imbibed the new concepts that were touched upon in this article. If you are one who wants to build your career in blockchain, be sure to check out the blockchain council and get exclusive updates.