Explore the possibilities of Ethereum's smart contract feature

Solidity, the Language!

You can program smart contracts in many languages, but the most common is Solidity [5]. Solidity looks and behaves much like the C language in that you have constructors and functions, and you need to set a variable's type.

Every instance is a contract, and each contract can have one and only one constructor. Functions and values get a visibility setting, telling the compiler which other functions can use them. The public setting, for example, makes a function available to any function on the entire blockchain. As you can see in Figure 4, Solidity also lets you import frameworks. There are other languages under development, but Solidity is a good place to start if you want to learn about programming smart contracts.

Figure 4: On the right, you can see the Solidity source code for importing Zeppelin, a framework for standards.

Development Environments

To truly start developing, you need an IDE; Cakeshop is a good choice. The Cakeshop IDE acts as a daemon, running a local chain that you can do pretty much anything with. (See the box entitled "Setting Up Cakeshop.") You also have the option of putting many nodes up on your local network. In the interface, you will find the basic stuff you need for interacting with a blockchain. You can create accounts and send funds back and forth on the test chain. Follow up by looking at the chain explorer and see how the transaction worked. There is also a sandbox, where you can deploy your own contracts and test all functions.

Setting Up Cakeshop

The developers chose to use Java for Cakeshop. The original place to pick it up is GitHub [6], where you can find a .war file and the source code. Using this binary, you can start it easily enough, but you need to define the nodes. If you just want to see how Cakeshop works, use the snap; it is still in version 0.10.0, so go with a newer version when you have figured out the configuration of the nodes. The snap version has defined nodes built-in, so it starts with a single command.

$ sudo snap install cakeshop

Wait for the install to finish, and run the following command on the command line:

$ cakeshop

With this setup, you can see what address you need to look at to use the web interface. Usually it would be http://172.17.0.1:8080/cakeshop/, but you must look in terminal output to be certain (Figure 5).

You can find more install options on the Cakeshop home page [7]. The page describes several ways to start a Docker instance.

When you have it all installed, you can see all components of a blockchain and even interact with the chain. This blockchain is your own, not the public one! Use the block explorer to investigate what your actions do. You will also have unlimited currency. You can see in Figure 6 what the sandbox looks like.

A smoother way to get started might be to use the Remix IDE online service [8]. Remix lets you load files from your local disk or even directly from GitHub. The features are similar to Cakeshop, but Remix does support plugins. The basic plugin is the Solidity plugin, which gives you full support for the language.

Remix offers many other plugins to choose from, including tutorials, verification tools, and example code. The code covers the simplest storage all the way up to distributed applications. For the really advanced, you also have services to reach outside information (called oracles).

Figure 7 shows the Remix IDE. On the left, you can choose the file explorer, compiler, and whatever plugins you wish to include. To the right, you have the code and a console.

Figure 7: Remix IDE: The sample code above deploys Web3 components.

A Simple Contract

Some basic contracts show up in the default view of Remix. See Listing 1 for an example that shows how the basic syntax works.

Listing 1

A Smart Contract

01 // SPDX-License-Identifier: GPL-3.0
02
03 pragma solidity >=0.7.0 <0.9.0;
04
05 /**
06  * @title Storage
07  * @dev Store & retrieve value in a variable
08  */
09
10
11
12 contract Storage {
13
14     uint256 number;
15
16     /**
17      * @dev Store value in variable
18      * @param num value to store
19      */
20     function store(uint256 num) public {
21         number = num;
22     }
23
24     /**
25      * @dev Return value
26      * @return value of 'number'
27      */
28     function retrieve() public view returns (uint256){
29         return number;
30     }
31 }

This code only contains simple function calls and no constructor, but it is an excellent start for understanding what you need to get right. The top line is not necessary, but many compilers will complain without it. The second line limits which compiler you can use.

Listing 1 has one contract function called Storage; all your code goes inside this function. The first function stores the value in number, and the second function retrieves it.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Welcome

    It is easy to write columns on the scary and annoying stuff, which seems to happen every day, but it is perhaps more important to point out the positive things when they happen, and the Ethereum Merge looks like a very positive thing.

  • The Impact of Crypto Mining

    With climate change wreaking havoc across the planet, we were just beginning to think about how we could use our technology to conserve fuel and reduce our carbon footprint. Then along came crypto mining.

  • Ring Secure Communication

    In the last few years, secure text, voice, and video transmission have become major areas of free software development. One of the leaders in this field is Ring.

  • Interview – Meet Apache Creator Brian Behlendorf

    Apache developer, Brian Behlendorf, talks about Hyperledger and blockchains.

  • Welcome

    Is blockchain finally getting interesting? I guess it always has attracted some attention – if not as an investment at least as a generator of headlines.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News