Introduction

The basics

Installing the client

brew linkapps

A quick overview of AlethZero

Choices, choices

Our frontend





createTransaction();

Our backend

contract.send(account, amount)

def init(): self.storage[msg.sender] = 10000 def code(): to = msg.data[0] from = msg.sender value = msg.data[1] if self.storage[from] >= value: self.storage[from] = self.storage[from] - value self.storage[to] = self.storage[to] + value

init

def init(): self.storage[msg.sender] = 10000

msg.sender

msg.sender

send

def code(): to = msg.data[0] from = msg.sender value = msg.data[1] if self.storage[from] >= value: self.storage[from] = self.storage[from] - value self.storage[to] = self.storage[to] + value

msg.sender

from

msg.data[0]

to

msg.data[0]

msg.data[1]

if self.storage[from] >= value:

self.storage[from] = self.storage[from] - value

self.storage[to] = self.storage[to] + value

This tutorial is the result of many days of studying best practices on how to leverage what's available on Ethereum now, and making the technology available to users, several months away from the network go live date (March 2015). For this reason, keep in mind development practices are likely to change heavily throughout the next few months, and the below is in no way indicative of what you should expect from the finished product. I have also kept a very narrow focus and omitted much information. For more advanced documents please instead refer to 'further reading' at the bottom of this page.And now, onwards to a decentralized web! Please join us on IRC if you have questions!Ethereum enables the decentralized web, referred to as 'web 3'. What makes it different from web 2 is that on Ethereum, there are no webservers, and therefore no middlemen to take commissions, steal your data or offer it to the NSA, and of course nothing to DDoS.A dapp ('decentralized app') consists of two parts: a frontend, written in HTML or QML, and a backend (think of it as the 'database' for your frontend). We'll focus here on an HTML dapp.Good news: if you like bootstrap, or any other framework, you can continue using them, as the frontend of dapps have full network access, and CDNs are accessible. In fact, for all intent and purposes, developing a frontend for a dapp written HTML is the exact same as developing a website, and converting from web 2 to web 3 is in many cases trivial.Even better news: you get reactive programming baked in (which should please angular, meteor and derby fans), by simply using callback functions, and there's no new framework to learn.Even even better news: because Ethereum relies on cryptographic principles to function, every dapp knows the pseudonymous identity of each user. That's right, users won't need to 'create accounts' or 'log on' to access your dapps, and you can think of this as openID by default, everywhere.There is no bad news, except maybe for the bad actors from the web 2 world, such as dishonest exchanges running with the money or gambling sites falsifying random number information to make a quick buck. On Ethereum, the backend operations are validated by all nodes on the network, meaning that a backend will always will do what its code says it does. This is why you might have heard people call Ethereum 'trustless' - there's no need for users to trust into a central authority to 'do the right thing'.The client is in a constant state of flux, and the development version which you will be using is updated almost every day. To insure that the tutorial works for you, make sure that you are running the very latest version (it's a simple matter of upgrading every day as a matter of habit, please see the individual tutorials on how to do so).We'll be using AlethZero, the C++ implementation of Ethereum designed for developers. In particular, we're going to install the 'development' version, which contains all the latest goodies.OS X users: please follow this guide . You can stop following the tutorial after you executed thecommand, then come back here.Ubuntu users: please follow this guide . While a bit less user-friendly to install than its OS X counterpart, AlethZero is developed on ubuntu and therefore works very well on this platform.Windows users: while it is possible to build AlethZero on Windows, at this point in time it is far too complex considering the beginner scope of this tutorial. Instead, if you do not have access to an OS X or linux computer, use a virtual machine and boot Ubuntu.If all went well, you should see something like this after you start AlethZero, depending on your platform and screen resolution:If your mileage varies, and not all the interface is visible, manually close all panes in the display by clicking their 'x' symbols until they are all gone, resize the screen to fit your resolution and re-add them manually by right clicking just below the titlebar (and just to the right of the 'refresh' button).What's going on here? At the center of the screen is a browser window, a Webkit view to be exact. You can browse the existing web from there, just like any other browser, try google for example.The rest of the panels contain debug and technical information that we'll make use of a bit later. While useful for developers, this is not a very-user friendly interface, so the final Ethereum browser, tentatively called 'Mist' and build on top of the Go Ethereum implementation , will have a completely different look and feel. In fact, it's likely that once Ethereum is launched, the etherbrowser will look a bit like this:The above is a concept of a potential look and feel for the etherbrowser, accessing a dapp that itself acts as a dapp store.As far as options are concerned, feel free to reorganize your screen a bit. You can also drag and drop panels on top of each other to 'stack' them.So, now that we have made ourselves at home, let's build our frontend, but... what should we build?Ethereum is a generalist programming platform. You can build games, financial applications, gambling apps, insurance companies, social networks, well, anything really! That said, it's always good to keep in mind your users' needs first and foremost, so what do you think they are missing from the centralized web? Well, trust of course! (as some iCloud users recently learned the hard way).So, for today's tutorial we'll write a simple application that functions a bit like a bank, but with a transparent ledger that the whole world can audit. We'll issue 10,000 tokens, and because it wouldn't be much fun to keep all the tokens for ourselves, we'll also create a function that allows us to send them to our friends.This way, we are issuing our own money (in a very basic way, but still). In the web 2 world it would be similarly trivial to build such an app in PHP and MySql, but your users would have to trust you with being an honest accountant, that your ledger stay consistent, that the government wouldn't 'help' itself with some tokens, that hackers wouldn't break into your server, that your employees would be honest at all times and hadn't planted a backdoor, or that your programmers wouldn't have made a single mistake, ever... well, you get the point.Note: feel free to grab all the code from https://github.com/stephantual/firstdapp or copy-paste the examples below.Let's have a look at our interface:This is of course pure HTML, but the eagle-eyed amongst you will have noticed we're using the bootstrap framework to make the layout look a bit nicer.Here's the full code:The code is straightforward: at the top, a balance placeholder, which we'll update dynamically using javascript a bit later on, located in a 'mytoken.js' file.This is followed by a very simple form, with only two fields and a button. The first,, will allow us to specify the account address we wish to send our tokens to. The second,, specifies how many tokens will be sent.Finally, a button that calls the (yet to be written) javascript function, which will call our backend and execute the transaction.To load it in AlethZero, paste the code in a plain text editor and save it as 'myToken.html'. Then simply type the URL of the HTML file on your local hard drive in the AlethZero browser address bar, in my case: file:///Users/stephan/path/to/code/mytoken.htmlOur backend (referred to as a 'contract' in Ethereum) is going to use a language very similar to Python, amusingly called Serpent. There are several other contract languages you can use when building Ethereum backends, including Mutan (similar to Go), LLL (similar to Lisp) and Solidity (similar to C++). We'll use Serpent because it's easy and has many examples available to learn more from.As we mentioned above, we're building a little bank, so we'll need to do two things:1) Instantiate at least one balance with some tokens, to get things rolling when the contract is first created.2) Build a 'send' function similar toin order to move funds around.Let's dive in, and feel free to open your favorite text editor to edit your first contract!Don't panic, it's much simpler than it looks. Contracts are divided into two parts. The first section, entitled, is code that initializes the contract, its default parameters during instantiation if you will. That initialisation code gets run only once when the contract is created, and never again.The second part of the contract is the contract code itself, the stuff that will live forever on the Ethereum network, immutable, and backed by millions of nodes insuring it returns the expected results every time. In our case, it's a simple function that checks that the sender has a large enough balance and, if so, transfers the tokens from one account to the other.Let's look at this is a bit more detail:This is the initialization of our contract: it will only run once (because it's within the init block), and it does only one thing: initialize one account with a balance of 10,000 tokens. Storage in Ethereum contracts is structured as a key/value pair store: a key identifies where in the contract ledger the account lives, and requesting that key will return its value (in this case, 10,000).Who'syou ask? Well, it's us! When we ask for the contract to be created we are the sender of the message, hence, we are. This is a unique identifier on the network that cannot be forged, thanks to the cryptographic laws that Ethereum implements 'under the hood'.Good, so now we have assigned ourselves an initial balance, let's have a look at the 'send' function, which will be executed each time we call the contract from now on. Note that because our contract in this particular case has only one function, we didn't bother naming the function, it's assumed that every call to the contract will involve sending tokens.The first three lines are variable definitions: you probably noticed that this time the value ofis assigned to the variable, which makes sense as it's the account calling the contract that is sending tokens.The valueis assigned to the variable. If you've ever written 'batch' programs, and you have a hunch this refers to the first argument that is sent to that contract, you'd be right.represents the first argument sent to the contract, a bit like a the first parameter to a function call. And, as you probably guessed correctly, refers to the second argument.To send tokens, we first check that the balance of the sender is higher or equal to the amount they are sending. This makes sense, you wouldn't want to send tokens that don't exist would youThis is of course handled by the if statement:If the balance checks out, we subtract the amount being sent from the sender balance:, then add to the balance of the account receiving the tokens:Voila! Our forum won't allow me to place more text in a single post, so I'm continuing in the comments (see below).