## An Ethereum Package for R

Bitcoin has become synonymous with “cryptocurrency”. Ethereum is another cryptocurrency which, although not as hyped at Bitcoin, presents some attractive characteristics. The foremost of these is the ability to create sophisticated smart contracts.

This post introduces the new ether package for interacting with the Ethereum network from R.

The package has just been listed on CRAN. You can find it here.

Alternatively, to access more recent code, install from GitHub using devtools.

## Connecting to the Ethereum Network

The package uses a JSON-RPC API to communicate with the Ethereum network. The API is documented here. You can use either a local or public node to connect to the network.

### Local Node

To run a local node you’ll need to install Geth. With Geth on your machine, synchronise with the network.

It’ll take some time (and space!) to download all of those blocks. Once you are synchronised you can stop Geth and restart it with RPC enabled.

By default the ethereum package will connect to port 8545 on localhost.

### Public Node

If you’re impatient or just want to give this a quick try, then you might find the public RPC offered by Infura to be a good option. Register to create a key. You can then use this key to point the ethereum package to the Infura RPC interface.

## Interact with the Ethereum Blockchain

We can validate our connection by checking the protocol version.

Ethereum operations have an associated execution fee which is expressed in units of gas. Gas, in turn, translates into Ether. What is the current gas price?

What’s the number of the most recent block?

Check against Etherscan. Yup! That looks right.

Check if your node is mining.

This will only be TRUE if you’re running a local node and you’ve started mining. If you are mining then you’ll also have set a coinbase address.

### Analysing Accounts

If you’re running a local node and you’ve created an account then you should be able to see it from within R.

If you are using Infura then you won’t see any accounts.

However, due to the open nature of Ethereum, we are able to check the balance in any account on the network. These accounts are pseudo-anonymous, so although we’re able to see the current balance, we’re not sure who an account belongs to.

How many transactions have been initiated from this address?

### Burrowing into Blocks

How many transactions are there in a specific block? There are two ways of addressing a block: using the block address or the (sequential) block number.

We can delve into the details of a block.

Look at the table of transactions.

### Tampering with Transactions

We can also access specific transactions directly using their unique hash. For example, the sixth transaction (index 0x5) from the block above.

## Breaking Down the Blockchain

And the earth was without form, and void; and darkness was upon the face of the deep. Genesis 1:2, King James Version

Let’s go all the way back to the beginning and take a look at the first block in the Ethereum blockchain, the “Genesis” block. To put that into some context, we’ll also grab the following four blocks. You can retrieve multiple blocks using the get_blocks() helper function.

Taking a look at selected fields for those blocks we see that each has a number, where the Genesis block is assigned the number 0x0 and subsequent blocks are numbered sequentially. Each block has a time stamp. The Genesis block has a NA time stamp because it was created synthetically rather than being mined. Importantly, each block has a unique hash.

Each block also has a parentHash. This is what links the blocks together. See, for example, that block number 0x1 has a parentHash which corresponds to the hash for block number 0x0. Similarly, block number 0x2 has a parentHash which corresponds to the hash for block number 0x1.

## Future Plans

As mentioned earlier, one of the differentiating features of the Ethereum blockchain is its ability to accommodate smart contracts. The next featues I plan building into the ethereum package will (hopefully) be for interrogating and interacting with smart contracts.

I’d also like to clean up some of the function results, converting lists to data frames, for example. There are a few functions which each achieve the same output but with different input parameters. For example, the RPC functions eth_getBlockByHash() and eth_getBlockByNumber() have already been consolidated into the eth_getBlock() function.