Basic Solidity Programming
As mentioned in Chapter 2 , Solidity is the most widely used language for writing smart contracts with Ethereum. Basic Solidity programming is quite easy to learn. It’s similar to JavaScript and yet has some features of object-oriented languages such as
- PDF / 1,074,306 Bytes
- 49 Pages / 439.37 x 666.142 pts Page_size
- 97 Downloads / 170 Views
Basic Solidity Programming As mentioned in Chapter 2, Solidity is the most widely used language for writing smart contracts with Ethereum. Basic Solidity programming is quite easy to learn. It’s similar to JavaScript and yet has some features of object-oriented languages such as Java and C++. Hence, some programming experience is desirable before jumping to development in Solidity, although it’s not mandatory.
Prerequisites For learning Solidity programming, you do not need to install any special software on your machine. You just need the following: •
Chrome browser
•
High-speed Internet
Remix Browser The best IDE for learning Solidity is Remix (see Figure 3-1). It is an online browser-based IDE available at http://remix.ethereum.org that comes with a source editor and a file manager as well as with compiling, deploying, and debugging options. While developing, you have to make sure you are connected to the Internet. © Debajani Mohanty 2018 D. Mohanty, Ethereum for Architects and Developers, https://doi.org/10.1007/978-1-4842-4075-5_3
55
Basic Solidity Programming
Figure 3-1. Remix online browser
Chapter 3
56
Chapter 3
Basic Solidity Programming
This IDE comes with a default contract named ballet.sol, which I will discuss later in the chapter in detail. For the time being, you can click this file on the left side of the screen if it’s not open already and then expand it to browse through it. You can also click the + link on the menu to create a new contract. When you write your contract, you can either compile your contract manually by clicking the “Start to compile” button on right side of the Compile tab or click the “Auto compile” check box to get it done on the fly. If something goes wrong in the code, you will see a red box with an X next to the line in the source (see Figure 3-2).
Figure 3-2. Compilation error in Remix Sometimes you can also see many yellow warnings, but they will not stop you from executing the code (see Figure 3-3).
Figure 3-3. Warnings in Remix
Deploying Contracts in Remix Once your contract is compiled successfully, you can click the Run tab on the right side and choose an option from the drop-down. Choose JavaScript VM, which is the default mode when you don’t use Remix with Mist or MetaMask (covered later), as shown in Figure 3-4. It comes with five accounts, each with gas limit of 3,000,000. 57
Figure 3-4. Deploying a smart contract in Remix using the JavaScript VM
Chapter 3
58 Basic Solidity Programming
Chapter 3
Basic Solidity Programming
Solidity File Details Let’s find out more about some Solidity features before starting programming.
Extension A Solidity file is always saved with an .sol extension.
File Storage The file will be available on your Chrome browser unless you either delete it or clear the cache. To avoid accidental deletion of the file from browser storage, you can use Remixd, which enables you to store and sync files in the browser with your local computer. For further details, refer to http:// remix.readthedocs.io/en/latest/tu
Data Loading...