Ethereum Smart Contract Development
上QQ阅读APP看书,第一时间看更新

A smart contract in seven lines of code

In this section you will learn to code your first smart contract in just seven lines of code (Loc), no hidden terms and conditions. And did I say that you do not need any special software, apart from your web browser, or to spend any real ethers from your pocket? Let's see. I am using Windows 7 (64 bit OS) and a Google Chrome browser:

  1. Open your Google Chrome browser and type remix solidity in Google, as show in Figure 3.1:
Figure 3.1: Google search for remix solidity online compiler
  1. Click the highlighted link in Figure 3.2. You can also type the following in your browser URL box to get to the website directly: http://remix.ethereum.org:

The present remix ethereum webpage is going through rapid changes and version increments. Meanwhile, and older yet stable version is provided to Solidity users for practice in the following link:

https://yann300.github.io/remix04/

Figure 3.2: Remix—Solidity IDE
  1. Click the plus sign as marked in Figure 3.3 to create a new untitled Solidity (.sol) file.
Figure 3.3 Untitled Solidity file in remix IDE

Rename it HelloWorld.sol and confirm the renaming, as shown in Figure 3.4:

Figure 3.4: Untitled.sol renamed to HelloWorld.sol
  1. Write the following seven lines of code in the HelloWorld.sol file, as shown in Figure 3.5:
Figure 3.5: HelloWorld.sol

Choose the JavaScript VM Environment option, as shown in Figure 3.6:

Figure 3.6: JavaScript VM acting as a blockchain simulator
  1. Click the Create button, as highlighted in Figure 3.7. Voila! You just created your smart contract. The only caveat is that we would be simulating it on our local machine using in-memory, which mimics an Ethereum blockchain:
Figure 3.7: Our first smart contract of 368 bytes
  1. To execute this contract, we press that myFirstHelloWorld button, as shown in Figure 3.8. We get the desired output: Hello World !
Figure 3.8: Execution of myFirstHelloWorld function using a smart contract
  1. We can also explore the byte codes, which are generated as a result of this smart contract execution, by pressing the Launch debugger | Instructions or just by clicking the Contract details (bytecode, interface etc) link. Figure 3.9 depicts the 368 bytes of virtual machine code generated by the hello world smart contract execution:
Figure 3.9: 368 bytes [000 to 367] of machine code for Hello World smart contract