Chip Off The Ol’ Blockchain – Chip 2: Deploy a Smart Contract

Most people have probably heard about blockchains and even fewer people probably understand how they actually work. But even so, what makes blockchain technology itself so appealing is not about what you can do with the blockchain itself, rather what you can do with Smart Contracts. Smart Contracts, or “distributed apps” or “DApps” as they are sometimes called extend blockchains by allowing developers to apply programming logic to blockchains. This opens up countless possibilities how one can use Blockchains.

In this tutorial, we’ll deploy a simple smart contract and interact with it. It doesn’t do much, but we have to start somewhere!

A Crash Course in Smart Contracts

Ethereum was one of the first implementation of Smart Contracts. While Ethereum does not prescribe a particular language for writing smart contracts, the most popular language by far is Solidity.

Smart contracts have the ability to be called by way of RPC or remote procedure calls. A contract will typically have a set of functions that are setup in such a way that they can be invoked from outside the contract, even remotely. This, in effect, creates an API that can be used to interact with the contract.

In addition to exposing some functions, smart contracts also maintain a state. A contracts state is all the data that is managed by the contract that is stored on a blockchain. The state of a contract is modified by the functions that are called and the new state is recorded on the blockchain until the next time a function on the contract is called. Unlike a database though, the contract state does not overwrite the old state, rather it creates new blocks on the blockchain. This means that every change to the contract is visible on the blockchain.

Consider the following code:

pragma solidity ^0.4.21;

contract HelloWorld {
    uint myStorageData;

    function HelloWorld(uint v) public {
    myStorageData = v;
    }


    function setStorageData(uint newValue) public {
        myStorageData = newValue;
    }

    function getStorageData() public constant returns (uint) {
        return myStorageData;
    }
}

The variable myStorageData is a state variable for this smart contract. It can be modified by calling setStorageData by way of a remote procedure call. getStorageData is a “getter” function that retrieves a value from the contract, and HelloWorld is the constructor for the contract.

Tooling

There are several tools available for developing smart contracts on Ethereum. Two of the most accessible tools are Ganache and Truffle. Ganache is a GUI based tool that sets up a personal blockchain that can be used for development. Download and run Ganache to get started.

Truffle is a framework and set of utilities that automate building, testing, and deploying smart contracts to Ethereum networks. Truffle runs at the command line because its intent is more than just development. It’s intended to be scriptable so it can be used for automation processes for DevOps.

To get Truffle:

  1. First install Node if you have not already done so.
  2. After Node is installed, start a Terminal session (Mac or Linux) or Powershell on Windows.
  3. If you’re running on Windows, install the windows-build-tools package for npm.
     Windows:
     npm install -g windows-build-tools
    
  4. Then install truffle.`
    Mac, Linux, and Windows:
    npm install -g truffle

Create and Deploy a Contract

  1. Now that Ganache is up and running and Truffle is installed, you can now build and run your first contract. In a Terminal session or Powershell, create a folder and CD to that folder.
     mkdir hello-world
     cd hello-world
    
  2. Initialize the folder.
     truffle init
    
  3. Use Truffle to create a contract.
     truffle create contract HelloWorld
    
  4. The previous command created a file called HelloWorld.sol in the contracts folder in the hello-world. Open this file in your favorite text editor and paste the code for the HelloWorld contract above over the default code in the contract and save it.
  5. Now, create a deployment.
     truffle create migration helloworld
    
  6. The previous command created what’s called a migration script in the migrations folder, which is used to deploy contracts to an Ethereum network. The file will be look something like 1524597135_helloworld.js. Open this file and paste the following code over the default code, then save it.
     var HelloWorld = artifacts.require("HelloWorld");
    
     module.exports = function(deployer) {
      deployer.deploy(HelloWorld, 5);
     };
    
  7. Open truffle.js in the root of the hello-world folder. This file contains configuration information for Truffle. Paste in the following code over the defaults and save it. What this is doing is configuring Truffle to talk to Ganache.
     module.exports = {
       networks: {
         ganache: {
           host: "127.0.0.1",
           port: 7545,
           network_id: "*" // Match any network id
         }
       }
     };
    
  8. Now you’re ready to deploy your contract. To do this, connect to the network with the Truffle console. The --network flag tells Truffle what network to use from the truffle.js file.
     truffle console --network ganache
    
  9. In the Truffle console, deploy the contract. This will compile and deploy the contract to Ganache. The --reset flag forces Truffle to recompile and redeploy the contract regardless of what state it might be in.
     deploy --reset
    
  10. Watch output from the previous step. It will look something like the following. The encoded hex is the address for the contract. Copy the address.
     HelloWorld: 0xe4e47451aad6c89a6d9e4ad104a7b77ffe1d3b36
    
  11. Now, interact with the contract. You can do this by calling functions and viewing data on the contract. Paste over <YOUR ADDRESS HERE> with your address.
     HelloWorld.at("<YOUR ADDRESS HERE>").getStorageData()
    

    This will retrieve the data stored in the state variable.

     HelloWorld.at("<YOUR ADDRESS HERE>").setStorageData()
    

    This will change the value. Retrieve the value again to see that it was change.

  12. Now, you can exit the console by pressing Ctrl + C twice.

Conclusion

That’s it! You setup a blockchain and deployed a contract to it. In our next post we’ll explorer how to interact with a contract using JavaScript. Stay tuned for part 3!

Stay Informed

Sign up for the latest blogs, events, and insights.

We deliver solutions that accelerate the value of Azure.
Ready to experience the full power of Microsoft Azure?

Atmosera is thrilled to announce that we have been named GitHub AI Partner of the Year.

X