/ BLOCKCHAIN, ETHEREUM, SMART CONTRACT

Starting with Ethereum - Writing a contract

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.

This is the 2nd post in the Starting Ethereum focus series.Other posts include:

  1. Starting with Ethereum - Setup
  2. Starting with Ethereum - Writing a contract (this post)
  3. Starting with Ethereum - Deploying and running a contract
  4. Starting with Ethereum - Smart contracts
  5. Starting with Ethereum - Industrialization

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
https://en.wikipedia.org/wiki/Smart_contract

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:

Solidity

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
https://solidity.readthedocs.io/en/develop/

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
https://solidity.readthedocs.io/en/develop/contracts.html#pure-functions

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
Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
Starting with Ethereum - Writing a contract
Share this