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:
- Starting with Ethereum - Setup
- Starting with Ethereum - Writing a contract (this post)
- Starting with Ethereum - Deploying and running a contract
- Starting with Ethereum - Smart contracts
- 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.
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.
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 .
Then click on the Browse Repositories… button, and search for Solidity.
After restart, 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
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-functionsNote 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).
- Create a new
.sol
file and name itMathematics
- or reuse the default one. As opposed to Java, there’s no check that the contract’s name matches the file’s. - Paste the above code.
- On the left side, notice the menu:Compiler tab. By default, the Auto compile checkbox is enabled.
- Go to the menu:Run tab.
- As the above code has no interaction with Ethereum, choose the "JavaScript" VM for Environment.
- 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.
- 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.