Quantcast
Domlur, Bangalore, India
+91 974-005-4621

The Perfect Recipe of Smart Contract Development

Smart Contracts have taken a lot of real-life industries with a storm, and they just don’t seem to stop soon. With so many developers getting interested in smart contract development, the ecosystem of smart contracts and dApps based on them has been running rampant over the last couple of years. There are a myriad of reasons compelling enough for a number of companies to indulge in smart contracts development. Even if you choose to outsource your work and start looking for smart contract developers, the importance of knowing how to create really robust smart contracts won’t be of lesser importance.

 

Background before Smart Contract Development.

 

smart contract development

 

The blockchain has literally whipped up a storm in the science and tech sector. Opening up new avenues which many thought could never be opened, the blockchain has made an immediate impact on every field that it’s touched. Bitcoin and Ethereum have led the attack, and it’s been a very impactful one, to say the least. One of my favorite use cases of smart contracts is the automated monitoring along with transparent recordkeeping. A gazillion of problems in various industries will be solved just by ensuring that these two use cases of smart contracts are implemented properly. Be it water, electricity, food, and whatnot, their movement can be tracked in real-time using blockchain and smart contracts. With proper and robust smart contract development techniques being implemented, massive inefficiencies and even frauds can be prevented that occur in private as well as government bodies.  

One of the most revolutionary features about the blockchain boom has been the Ethereum smart contracts. I am sure readers of this article are aware of what smart contracts are and may be familiar with smart contract development to some extent, so I won’t go into much detail about that here. How they work, and the step-by-step process of smart contract development, is what we will be covering today. For a more basic and simpler explanation of smart contracts, you can read our guide on cryptocurrency exchange application where I’ve already discussed it.

Not all smart contract development projects are perfect. A good smart contract developer has to steer clear of Bugs like transaction ordering dependence, timestamp dependence, and the likes can cause smart contracts to fail, and that is exactly what we look to avoid. After much research and deliberation, the perfect recipe for developing smart contracts has been curated. Read on to know how to deploy smart contract, without any bugs or failures.

 

Basic terminology

 

smart contract development

 

Before we venture into the big bad world of smart contract development, it is important that developers be aware of some basic terms that they are likely to see multiple times. These terms relate to the smart contract development process, the platforms used, and other technical terms.

  • Public key cryptography. It refers to the long hash generated when a crypto wallet is created. Public keys are used to verify the authenticity of transactions, i.e. whether a signature is really from a given person or not.
  • Node. Created to access the Ethereum blockchain, by using the Ethereum Virtual Machine. A full node, in order to operate, must download the full blockchain. This step calls for the smart contract developers to be decisive and calculative at the same time. 
  • Ethereum Virtual Machine. Refers to the entirety of the blockchain, the platform to execute smart contracts, etc.
  • Miner. A node that processes blocks on the blockchain. They compete amongst each other in order to verify the next block on the blockchain, and the first miner to do so gets some ether as an incentive.
  • Proof of work. The process described above. Once a block is verified, every node on the network updates to that block, hence the network achieves consensus.
  • DApp. A decentralized app is typically an app using smart contracts. They can be run centrally or locally, and users are typically represented by wallet addresses. A comprehensive understanding of the dApps and how they work will surely help in the smart contract development process. It will make sure that the gas costs and mining expenses don’t shoot up the roof.

 

Getting into it: Setting up an Smart Contract Ethereum node

 

smart contract development

 

Ethereum nodes are devices that run on the Ethereum blockchain. In order to deploy a smart contract, one must create a full Ethereum node on their device. This also enables us to mine blocks, make and approve transactions on the blockchain, and interact with the blockchain in other ways. The most popular language to write nodes in is Go, with C++ and Python close behind. Let us discuss the steps to get a node set up on your device. Do make sure that you have at least 35 GB of free hard drive space since the entire blockchain will be downloaded on your device.

 

  • Install Geth

Visit the geth Ethereum website, download the geth setup, follow the on-screen steps and get it installed on your machine. This is the very first step of your smart contract development project.

 

  • Set it up

Open up the command prompt, navigate to the directory where you installed geth, and start syncing to the Ethereum network with the geth command. When you do this the first time, geth will proceed to download the entire blockchain.   Establishing our own private network

 

  • Genesis.json Block                                                                                                                                                                                                            

 The third step is totally about making the necessary arrangements of where your smart contract will reside. Here, we will deploy our own blockchain, and initialize the first block, which will be the genesis.json block. Create separate folders on your machine for the data storage, write the following code into notepad and save it as genesis.json.

 

{

 “coinbase”   : “0x0000000000000000000000000000000000000001”,

 “difficulty” : “0x20000”,

 “extraData”  : “”,

 “gasLimit”   : “0x2fefd8”,

 “nonce”      : “0x0000000000000042”,

 “mixhash”    : “0x0000000000000000000000000000000000000000000000000000000000000000”,

 “parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,

 “timestamp”  : “0x00”,

 “alloc”: {},

 “config”: {

       “chainId”: 15,

       “homesteadBlock”: 0,

       “eip155Block”: 0,

       “eip158Block”: 0

   }

}

 

  • Initializing the first block

Go to geth, and initialize the genesis.json block. Connect to your private chain using the datadir command, and you will see that the IPC endpoint will now open. Using this, we will connect geth to programs such as metamask, remix and mist. This section follows next.

 

Metamask, Remix and Mist: What’s the deal?

Metamask is a chrome extension that acts as an Ethereum browser and a wallet. Every person involved in the sart contract development process must be familiar with this. Create a wallet and deposit ether into it using this extension. You can also browse the blockchain and interact with DApps with Metamask.

Mist is an electron application which runs connected to the Ethereum blockchain. Whenever you launch it, a geth node is started in the background, meaning an Ethereum blockchain connection is established right away.

The Remix is a web browser IDE, which has features essential to the development of small-sized smart contracts. It can deploy directly to Metamask and Mist, which is why we use the three in conjunction here.

How to set up all the three applications in tandem and take the next steps toward deploying smart contracts? Read on.

 

The basics of Mist

Mist has an interface just like any other web browser, but with enhanced features to support connections to the blockchain. We can interact with geth from Mist itself, using the Ethereum Wallet application. From here, the smart contract developers can view past transactions, create wallet contracts, add/see accounts, etc. After creating accounts, we can send and receive ether from them, and also deploy our contracts from under the ‘Contracts’ tab. Testing that all of this works in a robust manner will be a great idea for your smart contract development project. After you connect to Web 3.0 using Mist, you can then interact with the various Ethereum DApps available online, for instance, on stateofthedapps.com.

 

Using Remix and Solidity

Finally, we get into the hardcore smart contract development side of things. Remix IDE contains features such as static analyses, warnings, integrated debugger, syntax, and error highlighting, and an integrated testing and deployment environment that makes it ideal for smart contract development. On the Remix IDE, we are going to code in Solidity, which is meant to interact with the Ethereum Virtual Machine. It is a high level, contract-oriented language, turing complete, and the analogies with Javascript are clear to see. Inheritance and support of complex user-defined data types are some of the key features of Solidity. Incredible understanding of how the smart contract programming language works is a must before you even begin the smart contract development process. 

 

Coding the contract

We’re going to design a ‘Hello world’ contract, i.e. one for beginners. This should be enough to give you an idea as to what it takes to develop intermediate and advanced contracts, as your knowledge increases. The simple job that our contract will do is assign a variable some value, increment/decrement it, and destroy the contract. We will be incorporating functions and states into our contract, which simply collections of code and data respectively.

To define an integer, we use the keyword uint256, followed by the variable name and value. Now, we develop functions that will help us increment, decrement, and return the value of this ‘counter’. Since the function keyword, and how it’s setup is important to the whole smart contract development process, let’s discuss that too.

The function keyword is used to define one, followed by the name, and the body. After declaring the variables, defining the functions and putting it all together, you need to make sure that the function that returns the value of your variable is a constant one. Returning functions on the Ethereum blockchain costs gas, but no gas is spent when a constant function is called. After all is said and done, your code should look something like this-

 

pragma solidity ^0.4.16;

contract HelloWorld {

uint256 counter = 5; //state variable we assigned earlier

function add() public {  //increases counter by 1

 counter++;

}

function subtract() public { //decreases counter by 1

 counter–;

}

function getCounter() public constant returns (uint256) {

 return counter;

   }

function kill() public { //self-destruct function,

  if(msg.sender == owner) {

   selfdestruct(owner);

       }

}

 

Now is the time to get testing.

 

Testing and deployment

After you have done all the above, testing is of utmost importance before you go ahead and deploy a smart contract on the Ethereum blockchain. Inside the Remix IDE, you should see a ‘run’ tab which will help you get your contract up and running. Test it with the default settings, and you should be able to increment and decrement the value of your variable using the ‘add’ and ‘subtract’ buttons. Once you are satisfied that your contract is working, consider deploying it in Mist.

Fire up Mist, make sure that it is on the private network, confirm that you have some ether in your wallet (or mine it if you don’t), move to the contracts tab and select ‘deploy new contract’. Once you deploy it, you can see it in the wallets tab, and can now mine on the personal network to validate and fully deploy the contract.

To see what the kill() function does, we can deploy the contract, send ether to it, and then click the kill() button to see the ether deposited back into our wallet.

Have a look at our guide on how to launch an ICO using Stellar Blockchain.

 

Concluding words

Congratulations! You now know how to set up an Ethereum node, connect to the blockchain, and code and deploy a smart contract, all in a day’s work! Although the code we discussed was of a basic level (and is available online), following the same logic, you can create more advanced contracts to do whatever you want. Be a part of the blockchain revolution, and go deploy some smart contracts!

Our smart contract developers have developed and deployed numerous smart contracts in the last couple of years. Feel free to get in touch with us for any consultation regarding your smart contract development project.

 

GET IN TOUCH!

Related Posts

Leave a comment