Saturday 15 September 2018

Solidity - I

I am really excited to be writing on another emerging area that has caught the imagination of many in the past decade: Blockchain. It is only appropriate at the start itself to share the link to the seminal paper on Bitcoin. Following the arrival of Bitcoin, many cryptocurrencies have been invented and there has been a lot of effort to leverage the underlying Blockchain technology for transactions. 

For this post, we look at Solidity Solidity is high-level language for implementing smart contracts in Ethereum. It has borrowed from existing languages as C++, Python, and Javascript. We will be using the online Remix IDE for all the work in this post. The reason for using this online IDE is to directly getting a hands on feel for Solidity rather than getting bogged down with the usual setting up or installation of some tools, etc.

Remix IDE can be accessed by navigating to https://remix.ethereum.org as shown below:













We will write, compile, and run smart contracts in Remix. In this post, we will run a simple smart contract and this will get us familiarized with Remix as well. There are four sections in Remix as shown below:













The blue rectangle on the left is the file browser that can be used to create files with .sol suffix that will have the smart contracts written to. The files can be called in the editor indicated by the green rectangle. To the right of the editor we can see the admin section indicated by orange border that has the following tabs: Compile, Run, Analysis, Testing, Debugger, Settings, and Support. Lastly, below the editor, we see the Remix terminal that can be used for checking transactions details and start debugging, running JavaScript scripts, and executing common command to interact with the Remix interface. Note that like the language Solidity, Remix IDE also is subject to frequent changes. All that is recorded in this post is as it exists on the date this post is written.

Let us see our first smart contract:

pragma solidity ^0.4.25;
contract greeter {
    function greet() public pure returns (string) {
        return "Hello, World!";
 }
}

Let us quickly review this code before we run in Remix IDE. The pragma directive as the first line of the smart contract specifies the version of the Solidity compiler that should be used for compiling the following smart contract. greeter is the name of the smart contract and it contains a function called greet. Functions are by default public in Solidity. pure keyword indicates that the function greet() will neither read nor modify the storage state. returns spells out the return type and it is string in this case. The next line merely returns the "Hello, World!" string. Let us run this in Remix IDE. Click on + icon in the file browser on the left top as shown below:
















In the window that comes up, enter greeter.sol as shown below:










Click on OK. Now we can enter our first smart contract in the editor as shown below:
















In the admin section, click on Start to Compile button under Compile tab. On successful compilation, a light green message with the contract name is seen as shown below:












Since this has compiled successfully, we can deploy it. Click on the Run tab. The Run details are shown:












Before deploying, make sure that the Environment is set to Javascript VM and that greeter appears in the dropdown above the Deploy button. Click on the Deploy button. Once the Deploy button is clicked, we see an entry under Deployed Contracts as shown below:













Click on the entry seen. A button called greet corresponding to the function defined in the contract is seen. Click on that to see Hello, World! as shown below:

























This concludes the first post on smart contracts in Solidity