Ethereum Hello World App

I’ve been exploring the world of dApps lately, and in that spirit, I thought I’d share my own personal “hello world” Ethereum smart contract. I’ll be assuming that the reader has a basic understanding of how the Ethereum blockchain works, so as a result will not be explaining these concepts in this post. I may address them in later articles.

Installation and setup

Before we do any development we’ll need to get set up with the right tools. This is, of course, only one of many ways to write and deploy a smart contract. A goal of this article is to minimize the time to your first smart contract. Installation should be minimal as well.

You should have node and npm installed. If you don’t, go here. If you aren’t familiar, node is essentially javascript that runs outside of the browser. npm is its corresponding package manager.

In your project directory, run through the npm init sequence, then install the following modules:
1. npm i –save ethjs is a utility for working with the Ethereum ecosystem.
2. npm i –save ethereumjs-testrpc is an RPC for developing and testing Ethereum applications. Note that with the TestRPC, we won’t actually be deploying anything to the Ethereum blockchain or even a testnet.

That’s it – You should be ready to write your first smart contract!

Hello world

Our smart contract will be a simple “hello world” application that was inspired by ethereum.org. Our application will have one feature, that is, it will have a function that will allow us to force it to say the magical phrase that will welcome us to the world of smart contract development.

We will write our contract in Solidity, but ultimately there are several options.

Source code

The application is short. Here it is:

Screen Shot 2017-07-13 at 10.36.55 AM
Ethereum “Hello World” contract

From here, we’ll need to derive two things in order to deploy the contract to the blockchain: the corresponding bytecode and ABI. The ABI specs won’t be covered in much detail here, but it’s essentially a javascript object that defines how to interact with your contract.

There are a number of ways you could go about getting these two things, but one quick way is to visit https://ethereum.github.io/browser-solidity. Here you can paste in your contract, compile, visit the “Contract” tab, and find the following two items:

Bytecode: 6060604052341561000c57fe5b5b61015980…

Interface: [{“constant”:true,”inputs”:[],”name”:”sayIt”,”outputs”:[{“name”:””,”type”:”string”}],”payable”:false,”type”:”function”}]

Deployment

You can now fire up a node console to deploy the contract. The two items above will be referred to as `GreeterBytecode` and `GreeterABI`.

Configure your ETH client to communicate with the TestRPC by typing

const eth = new Eth(TestRPC.provider())

and view your test accounts with eth.accounts(function(accountsError, accounts) { …

The variable accounts will be a list of accounts. For information on how account addresses are generated, see this.

To create your Ethereum contract object, you can call

const Greeter = eth.contract(GreeterABI, GreeterBytecode, { from: accounts[0], gas: “300000”, });

This call “creates a contract object for a solidity contract, which can be used to initiate contracts on an address”. You can read the docs for creating contracts here.

With this in-memory representation of our contract, we’re now ready to deploy to address accounts[0] by calling:

Greeter.new(function(error, txHash) { console.log(txHash); })

A contract deployment returns a transaction hash, txHash, which we can use to retrieve information about that transaction via:

eth.getTransactionReceipt(txHash, function(err, receipt) { console.log(receipt); });

The data returned includes things like blockHash and gasUsed. Right now, we’re interested making a call to our contract. This occurs in the form of a transaction as well, and in order to make that call we’ll need the address that this contract lives at. You can find that in the contractAddress field.

Next, we’ll have to “instantiate” our contract object “by address”. Given a description for a contract and the address that it lives at, we can create new transactions that interact with the functions that live at that address.

greeter = Greeter.at(receipt.contractAddress); gives us a greeter that we can interact with locally much in the same way that we would with an object in an object-oriented programming language. The difference here is that “instantiating” a contract object twice with the same specs and address does not actually create two new items on the blockchain. Both calls would give us an object locally that we can use to communicate with an entity that lives on the blockchain.

So to run the sayIt function, we would just call:

greeter.sayIt().then(function(response) { console.log(response); });

This response should say “Hello World!”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s