Building "Smart Contract" on Ethereum Blockchain using Remix IDE

Written By: Dr. Amol Dhumane


Introduction of Smart Contract:

-------------------------------------------------------------------------------------------------------------------------

Smart contract is a small program that runs on Ethereum blockchain

A smart contract, once it is deployed on Ethereum block cannot be modified/changed.

You can change the data of the smart contract, but only the code of smart contract can do that.

You need not have to admin the smart contract once you deploy it. The Ethereum blockchain takes care of it.

Deploying smart contract requires money (you can pay this using “Ether”).

Using smart contract, you can do financial transfer as long as the financial assets that you transfer are native to the blockchain.

Solidity is the most popular language to write smart contracts.

Solidity is similar to javascript, but one difference is, we need to compile solidity code (in javascript, we write a code and run it in the browser)

Solidity code is compiled into bytecode. This bytecode contains series of elementary instructions that are sent to Ethereum blockchain

Solidity is a typed language where variables are having some type associated with them.

As a developer, we need to do two tasks with smart contract:

1.     Deploy smart contract on the blockchain

2.    Interact with smart contract (call functions of the smart contract)

We will develop smart contract on the Remix IDE which is available online. (http://remix.ethereum.org/)

-----------------------------------------------------------------------------------------------------------------------------

Smart contract 1:

pragma solidity ^0.5.0;

This mean compile this smart contract only if the compiler version is between 0.5.0 to 0.5.9 (or 0.5.999 etc) but not 0.6.0 or 0.4.X or anything else.

Now we will define a smart contract with ‘contract’ keyword.

contract SimpleSmartContract {

}

As this is our first smart contract, we will not write anything in this smart contract. A single solidity file may contain several number of smart contracts.

We need not have to save the file. Remix IDE saves the files automatically.

Let’s now compile the smart contract into bytecode and then deploy it on the Ethereum blockchain. 

We can activate auto-compile feature (provided in Compiler configuration) of remix IDE to automatically compile the smart contracts.

Now lets click on deploy and run transactions button from left side panel of remix IDE, select appropriate smart contract and click on the deploy button. If you click multiple times on deploy button, multiple instances of smart contract will be created. (below image shows 2 instances of the smart contract are deployed). These instances have their own address in the blockchain.

Smart contract instances are independent of each other. 

-----------------------------------------------------------------------------------------------------------------------------

Smart contract 2: Hello World Smart Contract

We will learn

1.     How to define a function

2.    What is visibility

3.    What is returns statement with the type of return variable

4.   Data location (in solidity variables are stored in different locations. Data more or less temporary (in javascript data is temporary. When javascript process stops all the data in memory dies), but in solidity, we need to save some variables to the blockchain. So here in this case we are not going to store anything in the blockchain, it will be temporary that will be returned from the function. So we will use the keyword ‘memory’, this means, this is just a temporary variable, we don’t want to save it anywhere.

pragma solidity ^0.5.0;

contract HelloWorld {

    function hello() public returns(string memory) {

        return 'Hello World';

    }

}

Still it will show one warning as given below:


 

In this case we are not going to write anything on the blockchain so we will use ‘pure’ keyword. It says that this is just read only function and don’t do any modification on the blockchain and just returns 'Hello World' string.

So the final code is :

 

Now it’s a time to deploy and interact with the code. Click on deploy button. Observe the ether amount in the account. Ether gets consumed while deploying the smart contract. As shown below.

 

Now expand the deployed contract. Hello button will appear. Remix analyses the smart contract and creates a button for each function for interacting with it. Now click on that hello world to get the ‘Hello World’ string. It is shown below.


-----------------------------------------------------------------------------------------------------------------------------

Smart contract 3: Simple Storage (part-I)

Lets write a first statement

pragma solidity ^0.5.0;

lets build a smart contract

 

contract SimpleStorage {

string public data;

}

 

Here data variable is of type string. It is public in nature, so anyone from outside can interact it. As this is not a temporary variable, so we need not have to define storage ‘memory’ to it as that of the last smart contract example. We can initialize the variables by giving it certain value. Refer the following code.


As the data variable has public visibility, so it will appear in the form of button after deployment of smart contract. When we specify public visibility to the data, solidity automatically creates a getter function of the same name of the variable and you can call this function that you created by yourself. Here the value of the variable is fixed, we cannot able to change it through smart contract.

Now lets set the value to the data variable at runtime, for that we will write a set function which will be public in nature and it will accept data from user and will assign that accepted value to the ‘data’ variable declared in the smart contract.


 Here, remember that ‘_data’ variable is a temporary variable stored in ‘memory’ while ‘data’ is not a temporary variable.

-----------------------------------------------------------------------------------------------------------------------------

Smart contract 3: Simple Storage (part-II)

In this case we will use the keyword ‘view’ which is little bit similar to the keyword ‘pure’ which we have seen earlier. With ‘pure’ we can read a static value that we hard-code in a function but if you want read a storage from smart contract then you need to use the keyword ‘view’. In smart contact-II, ‘Hello World’ string was hard-coded, so we used ‘pure’ keyword. ‘view’ keyword indicates that the function is read-only.

 Now in this case, by continuing with the earlier example, we will make use of ‘view’ keyword along with the getter function as given below:



Following figure shows the transaction receipt of ‘set’ function execution. It uses some amount ‘gas’ for execution of the transaction as we are modifying the value of ‘data’ stored on the blockchain.



In short, ‘set’ function is executed as a transaction and ‘get’ function is used as a call. The ‘view’ keyword in the ‘get’ function indicate that it is a read-only function.

 ----------------------------------------------------------------------------------------------------------------------------

Smart contract 4: Advanced Storage


Explanation of Gas:

When you send a transaction on Ethereum network you have to pay gas. Gas is an abstract unit that measures computational difficulty of executing the transaction. If a transaction does lot of computation the gas cost will be higher and if a transaction does less computations, the gas cost will be less. When you send a transaction, you specify what is maximum amount of the gas that you are willing to spend to execute the transaction. By default, remix compiler puts a very large value here which is 3 million but we don’t need this much high value.


If you go to the remix console and analyse the transaction to add a new element to the array, you can see what is the actual gas cost of the transaction.

 


Now lets change the Gas limit and deploy and run section while deploying the transaction. Make it as same as that of the execution cost of the transaction shown in the above image.



Now run the transaction again. As the transaction execution cost is same as that of the required execution cost, the transaction will be successful.

Now change the gas limit below 48663 which is a minimum gas limit required for executing the transaction. Make it as 48660 as given below.



Now again add a value into array and check the remix console. We will get a error that “transaction run out of gas”

It means that, if we reduce the gas limit below the requirement of the transaction, the transaction doesn’t get executed. So it is necessary to have sufficient gas for the execution of the transaction.

-----------------------------------------------------------------------------------------------------------------------------

Smart contract 5: CRUD (Create, Read, Update and Delete)


 

-----------------------------------------------------------------------------------------------------------------------------

 Smart contract 5-Part-II: CRUD (Create, Read, Update and Delete)

Let’s do some modification in the above code and remove the repeated code (i.e. for loops that we used in show and update function) and replace that code with a function. 


-----------------------------------------------------------------------------------------------------------------------------


Smart contract 5-Part-III: CRUD (Create, Read, Update and Delete)

Let’s do some modification in the above code and give some message on the console if some tries to do operation on the user which does not exists. For the same we used inbuilt revert() function in the find() function that is defined at the end.


-----------------------------------------------------------------------------------------------------------------------------


 




 









Comments