Ethereum Tutorials

"Hello, World!" Contract

Creating a contract

As should be expected, our first contract is called "Hello, World!" It is a small program located in a block chain, it can be called and when called, it returns the result: a "Hello, World!" string.

To edit a contract, you will need a text editor (I use Notepad++). Create a hello_world.sol file on your local drive. The extension "sol" stands for Solidity, we are going to use this programming language. However, you can use a simple .txt extension if you prefer, as the code we created will be copied from your editor and pasted in Mist's built-in editor.

Here is the code of our first contract:

pragma solidity ^0.4.11;

contract HelloWorld_01
	function HelloWorld_01()
	// ---
	function getGreeting() constant returns (string strGreeting) { return "Hello, World!"; }

If you are familiar with C++ or Java, you will recognize the syntax. The "contract" keyword in Solidity means the same thing as "class" in C++. The contract has a function with the same name the contract has: "function HelloWorld_01()", it is called a constructor. This function is called only once - when the contract is deployed in the network.

Our contract has one more function called "getGreeting", it returns the "Hello, World!" string.

Same way as C++ and Java, Solidity has access modifiers:

public - accessible by all
private - only accessible by functions of this contract
internal - only this contract and contracts deriving from it
external - cannot be accessed internally, only externally

The "getGreeting" function has no modifier explicitely set, so it defaults to public.

Let's deploy the contract. In Mist, select Contracts - Deploy New Contract, and paste the contract's code into the Mist's editor. If everything is correct, you will be offered options: from which account should the contract be deployed. For our test, any account will do. Do not forget to turn on the miner for few seconds.

There are two kinds of functions in contracts: functions that do some work, and functions that do not. Our "getGreeting" function does not do anything that requires Ethereum distibuted network to do any additional job: the value it should return is already calculated (it equals to "Hello, World!") and can be taken directly from the block chain. This sort of functions is marked by "constant" modifier, and it does not cost money to call it.

Calling contract's function

After the contract is deployed, Mist shows it (Mist - Contracts). If you select the contract from the list of available contracts, you will not be offered an option to call the function; instead, you will see the function name and a (constant) result it returns:

By the way, notice a very irritating behaviour of Mist: it parsed the "getGreeting" function name, broke it to words and capitalized what it believes is better.

Visibility is very important for this site. If you like it please link from your page to this URL or share info using Social Buttons below.
(C), all rights reserved

Please read the disclaimer