In the second part of our Ethereum programming tutorial we will set up a private Ethereum development network, and implement our first smart contract.

This tutorial is based on a geth version later than 1.6, namely 1.7.2. What makes worth mentioning it is the fact that in version 1.6 the inline Solidity compiler support was removed from geth. For whatever reason. But by the time of writing these lines (end of 2017) there is no ramp-up tutorial to be found out there which reflects this fact. Even the official Ethereum "Hello world" tutorial still refers to the Solidity compiler being included with geth!

But no worry: I'll do that for you! Here comes the first rookie walkthrough for creating, compiling, deploying, and running a Solidity Ethereum contract utilizing the command line Solidity compiler!

1. Setup the private Ethereum Development Node

First create a new data directory for your private blockchain. We do this right beneath our home directory:

$ mkdir ~/privatechain

Then start a geth development network pointing to this data directory:

rpc: Enable remote procedure calls via http on this geth instance, so we can connect to it from another node (i.e. terminal window)

rpcaddr: The IP address where RPC shall be accessible. In this case it's the local host since we are on the same machine.

rpcport: The port to access RPC. As 8545 is the standard port, we also could have left out this option.

dev: This flag creates a development network, so we do not connect to the "real" Ethereum network where we would have to spend real Ether (meaning real money) to execute our contracts. It also conveniently preconfigures the first block (the Genesis) of our network. We will cover creating our own Genesis Block in a later tutorial.

Just take a look into your privatechain directory. You should find the subdirs geth and keystore there!

2. Connect a geth console to the Development Node

You will have recognized that our running geth node is quite busy with its own affairs. It's not being impressed by typing commands into its terminal window. Not even by poems! So we have to get in contact:

Open another macOS terminal window. I'd suggest using a different color for it so you never mix up the terminals later on. We will use the grass style terminal throughout this tutorial - as it comes preconfigured with macOS - to reference the remote command line interface (CLI) geth console.

So just execute this in the new console window replacing the "as" with your user name:

$ geth attach ipc://Users/as/privatechain/geth.ipc

Congratulations! You just opened a remote console to our Ethereum node!

But what about the "ipc" thing when we where talking about "rpc" earlier? Just that simple: IPC or Inter Process Communication can be used when separate processes need to interact on the same machine while RPC must be used when the processes run on different machines.

3. Create the first account

Now we have to create an external account on our Ethereum network. An account enables us to perform transactions on the network (i.e. make use of it). To do so we utilize geth's Management API:

> personal.newAccount('password')

'password' literarily is the password here. So feel free to use your own! This command will output the address of the account created. It's a good idea to write it down somewhere!

Now check the account really exists:

> personal.listAccounts

This should output an array containing the account just created. So in my case it's:["0x839d4ed149062e6e2e7ab772167f366282c600ce"]

This first account automatically is set as the coinbase in our network. This is where all the mining rewards will got to. You can check it using the web3 API:

> eth.coinbase

This should output the same account just created.

4. Create the Contract Source Code

Now it's time to write our first Ethereum contract definition. If you are familiar with object oriented programming languages it helps to think of a contract definition as a class definition.

For better maintainability we will store the contract files in a separate directory. So just go ahead an create a folder ~/ethcontracts!

Right inside this folder create a new text file named MyFirstContract.sol. This is your first Solidity source code file! Feeling great?

I think there isn't a detailed walkthrough required. You will have noticed that in the first line we define the smallest Solidity version supported by this contract. Then we create a contract named MyFirstContract which has one method that simply reflects its input.

But now comes the challenging part:

5. Compile the Contract

As told earlier in this tutoriel, calling the Solidity compiler right from the geth console has been removed from geth 1.6. So we have to compile our source file outside the running Ethereum network, and import it into it. In order to do so we need to have the Solidity compiler installed as explained in the first part of this tutorial.

As we have two instances of macOS terminal already running, we will need a third one. Believe it or not! So please open another terminal instance. We will use the ocean theme to identify it in this tutorial.

First we'll do a test run to check if the Solidity compiler is working as expected. So please issue:

What happens here is that we create an actual JavaScript file MyFirstContract.js where we assign the output JSON object of the Solidity compiler to a variable called MyFirstContractCompiled.

Now we're ready for the next step:

6. Import the contract

We want to get the precompiled contract from the compiled JavaScript file into our Ethereum node and prepare it for deployment to our development network.

So we return to our geth console and load the JavaScript file just compiled (Note: We assume that the geth console was started from your user's home directory. If not, you have to specify the absolute path to the MyFirstContract.js file):

> loadScript("./ethcontracts/MyFirstContract.js")

The resulting output should be:

true

So let's check if everything went right by outputting the value of the MyFirstContractCompiled variable:

Now we have to create an actual contract object from the compiled contract definition, which we can instantiate on our Ethereum network later. To achieve this, we have to select the abi part of our MyFirstContract stored in the MyFirstContractCompiled variable and pass it to the web3.eth.contractfunction:

7. Deploy the contract

Now that we have our MyContract object, we want to deploy it to our Ethereum development network.

As you know: There's no free lunch! Especially not on the Ethereum network. Every transaction has its cost. And contract deployment is a transaction! So we have to check first if we have enough Ether to actually do the deployment.

For convenience we define a neat variable pointing to our coinbase account (where all the mined ether goes to)..

> MyAccount = eth.coinbase

..and check the funds:

> eth.getBalance(MyAccount)

If this returns 0, we have to mine some Ether:

> miner.start()

This will start the mining process on our initial geth development node. Just check your first terminal window (the one that is black in this tutorial, but is likely to be white on your Mac).

Meanwhile, we want to know the expenses of deploying our contract. The deployment itself is a Ethereum transaction with the compiled binary contract data as payload. We can use the web3.eth.estimateGas function to simulate this transaction and estimate the gas required to execute it.

First we create a hex string from the bin section of MyFirstContractCompiled and store it in a variable for later use. This is done by suffixing the bin string with "0x":

Now our contract is deployed to the Ethereum development network. But it's not alive yet: Its address is still undefined!

8. Activate and test the Contract

What we've done in the step before is telling the Ethereum network that we want to execute a transaction which creates our contract as an executable unit of code in the network. Now it's up to the miners to decide when and where this transaction will be executed. In real life they would take this decision based on the amount of gas we provided. But since we are in the test net, and we currently have only one miner, our transaction will be executed anyway! So let's start mining again:

> miner.start()

After a few seconds check the contract instance again:

> MyContractInstance

It should have an address set now! So our contract is alive and we can test it.
But first let's stop mining:

> miner.stop()

Then we call the reflect method of our contract:

> MyContractInstance.reflect.call("I'm alive!")

And voilà: Your contract tells you that it is alive!

If you wonder why we had to add call to the actual method name, you will find the answer here.

Friday, November 17, 2017

This is a step by step tutorial to prepare your Mac for the development of Ethereum Smart Contracts in Solidity.

We assume you have a fresh macOS installation, with no development tools and no Ethereum software (like a wallet app) installed. It is always a good idea to do development in an isolated environment which is not used for any other purpose.

1. Install homebrew

Homebrew is the packet manager for macOS you will need for installation of the most tools we use throughout this tutorial.

2. Install Ethereum

This will as well install one of the most important tools for our development plans: geth, the go command line interface for Ethereum.

Just check it by issuing

$ geth

If everything went right, geth will start up and begin synchronizing the Ethereum blockchain to your Mac. As we don't want this by now, enter [CTRL]+[C] in the terminal window running geth. Just ignore the errors popping up, if any!

3. Install the Solidity Compiler

Solidity is the programming language of choice for implementing smart contracts on the Ethereum network. In order to be able to run our contracts, we have to compile them. So we need the compiler:

$ brew install solidity

Depending on the power of your Mac it might take a while for the installation to complete. Even if it lingers around an installation step (e.g. installing "boost") for several minutes (or even hours): Just be patient and wait! It will finish! I suggest spending your time watching the CPU load in Activity Monitor.

Congratulations! Now you're up and running to start your own Ethereum development network and start implementing your first smart contract!