Posts Tagged ‘blockchain’
  • Starting with Ethereum - Writing a contract

    Ethereum logo

    In the latest post, the required infrastructure to do something on Ethereum was set up. Now is the time to roll up our sleeves and start writing some code on it.

    In this post, I’ll show how to write a trivial contract.

    What’s a smart contract anyway?

    A smart contract is a computer protocol intended to facilitate, verify, or enforce the negotiation or performance of a contract.
    — Wikipedia

    That doesn’t tell much. My understanding is that on the blockchain in general, and on Ethereum in particular, smart contracts are just code. Which is a good thing, since we are developers, and writing code is our bread-and-butter (or at least it should be).

    As Ethereum has an Ethereum Virtual Machine, it can run contracts, code, in the form of dedicated bytecode. However, just like in Java, directly writing such bytecode is not really feasible for non-trivial software. Thus, a high-level language is required. There are several of those available, that compile to EVM bytecode:


    I chose Solidity for several reasons:

    • I’ve had not so great experiences with both Python and C
    • It seems to be the most used language
    • The documentation is solid (pun intended)

    Solidity is a contract-oriented, high-level language for implementing smart contracts. It was influenced by C++, Python and JavaScript and is designed to target the Ethereum Virtual Machine (EVM).

    Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features.

    — Solidity documentation

    There are a couple of options to compile Solidity code locally, including:

    • A Docker image
    • A Homebrew package
    • An IntelliJ plugin: To install the plugin, go to IntelliJ IDEA  Preferences  Plugins. Then click on the Browse Repositories…​ button, and search for Solidity. After restart, you can select File  New  Smart contract to create new Solidity .sol files.

    Last but not least, one can try the online editor, called Remix. Remix can also run the compiled code on a limited set of networks, and offers a debugger, which is a good option to start with.

    A trivial contract

    The documentation provided is of good quality. Please refer to it to write your own code.

    Let’s just write a contract that adds 2 unsigned integers. This is simple, but it allows us to go further easily.

    pragma solidity ^0.4.0; (1)
    contract Mathematic { (2)
        function add(uint a, uint b) public pure returns (uint sum) { (3)
            return a + b;
    1 A Solidity file starts with meta-data telling about its version.
    2 A Solidity file consists of a contract.
    3 Aside from the expected structure (public, returns), notice the pure keyword
    A word on contracts

    Contracts bear a striking similitude to the concept of classes in other languages (such as Java). They can have both state - attributes, and behavior - methods. They can also be abstract.

    In addition, they can have members specific to Solidity, Events, to interact with them.

    Pure functions

    A pure function is a concept inherited from Function Programming. In FP, its return value depends solely on its input parameter(s) and has no side-effects (such as logging, writing to a context, etc.).

    In Solidity, the definition is as follows:

    Functions can be declared pure in which case they promise not to read from or modify the state.
    — Solidity documentation

    Note that the state refers to the the state of the Blockchain. It means that the Blockchain is not only an execution environment for contracts, but can also be seen as a database of sort - though an expensive one (more later).

    Testing the contract

    The easiest way to test the contract is to use Remix (see above).

    1. Create a new .sol file and name it Mathematics- or reuse the default one. As opposed to Java, there’s no check that the contract’s name matches the file’s.
    2. Paste the above code.
    3. On the left side, notice the menu:Compiler tab. By default, the Auto compile checkbox is enabled.
    4. Go to the menu:Run tab.
    5. As the above code has no interaction with Ethereum, choose the "JavaScript" VM for Environment.
    6. Now, with the Mathematics value selected, click on the pink Create button. A new block should appear below: it’s our new "virtual" smart contract.
    7. Notice the add button, that’s the way to call the method. Just fill in 2 integers, separated by a comma and click it. The result should be displayed on the left side of the method block.
      Compiling and executing a smart contract in Remix
    Categories: Development Tags: blockchainethereumsmart contract
  • Starting with Ethereum - Setup

    Ethereum logo

    Many people know about Bitcoin, because of its recent rise (and soon crash?). However, Bitcoin is just a currency, albeit a crypto one. As such, it’s of very limited interest to me. But it’s based on the concept of blockchain and that seems to be much more interesting. While there are a lot of resources available on the Web regarding blockchain, they mainly focus on the concept of blockchain, or how it works internally, not so much on how you can as a developer use it.

    This is the first post in what I hope will be a serie which aims at correcting that. The blockchain implementation I’ll be using will be Ethereum because it’s an already established technology, with a mature ecosystem around it.

    This post will describe how to setup the whole tooling around Ethereum, and lay the foundations of the posts to come.

    The Wallet

    The wallet is the first step when you want to start working with Ethereum. It’s called a wallet because it does hold the Ethereum currency - called ether. It also holds other custom assets - called tokens.

    Ethereum provides such a wallet, just download it from the home page. This software also allows to deploy and run smart contracts.

    For Mac users, there’s also a Homebrew recipe. To install, type:

    brew cask install ethereum-wallet

    Once installed, launch the application. A network needs to be chosen: choose "Rinkeby" which is a test network. At this point, the blockchain is copied locally. Wait…​

    The Ethereum blockchain being copied

    Then, an account needs to be created. Choose a password.

    Wallet with an account configured

    Get funds

    Whatever your stance regarding it, money is at the foundation of the world. In Ethereum, this is no different. On the production network, you’d need to buy ethers from a broker. On "Rinkeby" which is a playground network, one can just ask for ethers.

    Go on and follow instructions to receive ethers. You basically have to post your wallet public key on a social network - to prevent abuse. The rate by which one can ask for ethers is also limited, to prevent abuse.

    Requesting ethers for the Rinkeby network

    Funds should be available soon after the post has been published on the social network of your choice. Check your account, you should be richer.

    The rate limit is:

    • 3 ethers every 8 hour
    • 7.5 ethers per day
    • or 18.75 ethers every 3 days

    Astute readers might notice that it’s better to ask for less, but then more often (3 ether * 8 hours * 3 times * 3 days = 72 ethers).

    The balance can also be checked on Just replace the last segment of the URL path with your account public key.

    Export/import accounts

    Chances are you will have multiple computers, or you will change computer, or even change software. As such, it’s very important to be able to export/import accounts.


    Within the Ethereum wallet, go to File  Backup  Accounts. On OSX, this translates to ~/Library/Ethereum/rinkeby/keystore. There should be one keystore file, starting with UTC, which name matches the public key (the one posted on social media previously). Its content is in JSON format.


    Another available wallet for Ethereum is Metamask, available as a dedicated browser or a Chrome extension.

    1. Install the Chrome extension - it can be uninstalled just afterwards
    2. Locate the extension in the browser bar
    3. Locate the Account button - it’s the second rightmost one
    4. Choose Import Account
    5. In Select Type, choose JSON File
    6. Point to the above file
    7. Type your account password

    At this point, MetaMask should also display your account and its balance. Notice it’s the same account public key and the same balance.

    Account imported in MetaMask
    Categories: Development Tags: blockchainethereum