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.
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.
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.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.