Your “Mr. Watson come here, I want to see you” moment is close

Of course I’m referring to the first telephone conversation, illustrating what I felt when I managed to send a custom coin to somebody (actually: from one of my test accounts to another) for the first time.

And of course you are smart enough to notice that this is a false analogy, as I have not invented Ethereum, just started to play with it.

Anyway, it was a big relief, when I succeeded after several hours of failed attempts — which you can actually spare for yourself (more on these attempts later).

When you start thinking about blockchain programming, you should bear in mind, that

there are two different stages, which are very different in complexity: writing smart contracts, and creating distributed applications.

1. The easier adventure: writing and running smart contracts.

Smart contracts describe the business logic of your applications, eg how you want to issue a custom coin, what actions with your coin you want to support for its holders, etc. Or the rules of a marketplace or a voting system. The contracts can be written eg in the Solidity language, which is a variant of JavaScript.

The contracts can be deployed with existing applications, like the Ethereum Wallet to the test Ethereum blockchain for testing, or to the live Ethereum blockchain for live usage.

In order to start playing with smart contracts, you don’t need anything more than that.

If you are proficient in JavaScript, you just need to read through the syntax rules of Solidity, and you are good to go.

If not, but you are familiar with any other programming language (like me with Python), you will be able to get by quickly by analogy, after putting some effort into understanding the basics of JavaScript.

(If you are not familiar with any programming language altogether, I cannot help you, you should start a crash course on JavaScript. It’s easy, and very useful for other purposes anyway. In other situations I would recommend Python as a first programming language to learn, but if you are into Ethereum, JavaScript is the right choice.)

In any way, it is good practice to start with fully functional contract examples, which you can copy + paste modularly.

You can find tutorials on the Ethereum web page, which guide you through the process of building different kinds of contracts from the basic “minimum viable” versions to quite complex setups:

Warning:

When you start reading forums, eg on the Ethereum web page, you will surely see posts very soon suggesting you deploy your contracts with an application called testrpc (RPC standing for Remote Procedure Call) to the test blockchain, and create distributed web apps to interact with them using the Truffle framework.

Don’t start with this.

Believe me, it adds unnecessary complexity to your initial steps towards understanding the basic mechanics of smart contracts.

Been there, done that — more on that later.

You should do the following instead:

  • Download the Ethereum Wallet from the Ethereum web page,
  • choose connecting it to the test chain,
  • start mining Ether (in the menu Develop > Start mining — you will get test Ether, of course, which you cannot sell for money), so that you can pay for your transactions,
  • follow the instructions in the above mentioned tutorials to deploy your contracts (if you have multiple contracts in one script because of inheritance, you will be prompted to choose one of them before deploying; in the case of the cryptocurrency tutorial mentioned above I’ve chosen “MyAdvancedToken”, because it inherits from “owned” and “token”, see contract MyAdvancedToken is owned, token{...}, and it worked — choosing another contract may or may not work, I didn’t try)
  • create multiple accounts with the wallet, so that you can send coins (or Ether for that matter) between them
  • you can use the GUI (= Graphical User Interface) of the wallet to issue commands corresponding to the functions contained in your contract.
In this way, you will also have your “Mr. Watson come here, I want to see you” moment, eg when you send coin from one of your account to another, very soon.

Limitations:

The Ethereum Wallet is just the first step. If you want to get a lot of users who use your Ethereum based system, you will have to build apps (so called distributed apps of DApps) for them as an interface. More on that later.

2. The hard core mission: writing distributed apps

Now comes the part when you should read the posts on testrpc and Truffle.

(There seems to be an intermediary step between the Ethereum Wallet and building DApps, the Metamask Chrome plugin as an interface for your users. Metamask is on my list to explore, as soon as I’m there, I will report on it.)

Actually, I started with this step, before checking out Ethereum Wallet, that’s why I’m warning you from doing this.

It cost me very long hours until I got everything needed up and running.

For me the main source of problems was Python, so I share what I learnt.

Warning: Python version matters

I you are a Python aficionado like me, you should use, whenever you can, the latest version of it. Python 3.x versions have a lot of improvements over the latest of the 2.x series, 2.7, two of the most crucial ones being unicode handling, and printing to file.

I use the Anaconda distribution with Python 3.6 (which is not the latest Python version, there exists alredy 3.7). Anaconda touts itself as “Superpowers for People Who Change the World — Leading Open Data Science Platform Powered by Python”. Using Anaconda with Jupyter Notebook as a user interface and publication platform (which allows you to combine executable code, formatted text and on the fly generated charts in one web page) will blow your mind (see an example).

The challenge is, OSX also uses Python to manage its file system operations, and they stick with the 2.6 version.

So when I tried to install the necessary tools for deploying DApps, I got a looong list of error messages, from which it was not at all obvious what to do.

After several hours of trying the decipher and googling after error messages I could come up with the diagnosis and the solution:

OSX decides which version of Python to use, based on its PATH system variable. After installing Anaconda, the value of PATH (which you can see after issuing the echo $PATH command in Terminal) looks like this:

/Library/Anaconda/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:

instead of the original value:

/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:

The value of the PATH variable is actually not a single path, but an ordered list of paths (actually, folders), divided by colons. When OSX needs to decide how to resolve the python command, it launches that version of Python, which it first founds in any of these folders.

As you can see, the first path leads to the Anaconda folders, and we know that in there is contained Python 3.6. If you issue the command which python in Terminal, the result confirms this:

/Library/Anaconda/anaconda/bin/python

But the original Python 2.6, which belongs to OSX, sits in the “usr/bin” folder.

So the trick here is to duplicate this folder at the beginning of the PATH variable, with the PATH=/usr/bin:$PATH command. After this, the result of the echo PATH command will be this:

/usr/bin:/Library/Anaconda/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

So the first folder, in which OSX will find a Python version, will be “usr/bin”. Correspondingly the result of the which python command will be:

/usr/bin/python

And the installation of every software we need will work as advertised.

The PATH variable change described above is temporary, which means it’s only valid for the current Terminal session, but this is exactly what we need, because the usage of the installed software doesn’t depend on the default Python version any more. (There is also a way to change the value of the PATH variable forever, if need be, you will find it easily via a Google search.)


So back to the list of tools you will need to write and deploy test web DApps:

  • Node.js, which is a server side JavaScript library. Part of Node.js is the NPM package manager, everything else you will install using that.
  • For OSX, you will need XCode Command Line Tools, see how to install here. (I didn’t know that I don’t need a developer account and the whole XCode package for this, so I started with creating a developer account and installing XCode — one time maybe I also will need it.)
  • testrpc, you can install it via the npm install -g ethereumjs-testrpc command in Terminal. This is a test Ethereum client, which gives you at each launch 10 test addresses with enough Ether to pay for transactions. You should let it run in the background in a separate Terminal window, while you are working with the other tools.
  • Truffle, which is an Ethereum web app framework. You can install it via the npm install -g truffle command. It creates the necessary JavaScript and HTML files for your web app. Part of Truffle is web3, which is the JavaScript API to the Ethereum Virtual Machine (EVM), the runtime environment for smart contracts. Make sure you have the latest version installed of Truffle. I spent several hours trying to find and fix a bug, which prevented me from starting the server on localhost which serves my app. It turned out that the bug was known, and a few days earlier fixed, I just had to upgrade Truffle. Truffle comes with a default contract for issuing a coin, called Metacoin. Here you have a tutorial on how to deploy it to the blockchain, and build the web app to interact with it.
  • You will also need a code editor. Just use Atom, which is one of the most popular ones within the Ethereum community.

Once again, the order I recommend you to follow:

  1. Start playing with smart contracts, using the Ethereum Wallet.
  2. Wen you already know what they are good for, and think you need to build your own web app, learn and use Truffle.