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
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.