Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Starting With Ethereum: Writing a Contract

DZone's Guide to

Starting With Ethereum: Writing a Contract

In Part 2 of this series on blockchain for developers, we take a look at how to get started writing smart contracts using the Solidity language.

· Security Zone ·
Free Resource

Discover how to provide active runtime protection for your web applications from known and unknown vulnerabilities including Remote Code Execution Attacks.

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 us 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

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 restarting, you can select 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

Let's just write a contract that adds two 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 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.

Please enable JavaScript to view the comments powered by Disqus.

Find out how Waratek’s award-winning application security platform can improve the security of your new and legacy applications and platforms with no false positives, code changes or slowing your application.

Topics:
security ,ethereum ,blockchain ,smart contracts

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}