Hands-On Smart Contract Development with Solidity and Ethereum
by Kevin Solorio, Randall Kanna, David H. Hoover
Chapter 4. Our First Smart Contract
Now that we have everything installed, it is time to build our first contract. In following the tradition of introductory programming books, our first program will greet us with “Hello, World!”
In developing this program, we are going to learn how to use the tools provided by Truffle for creating and testing our application. We will also begin to explore the Solidity language, including a look at functions and state variables.
Our goal for this chapter is to find a rhythm in how we go about building our application and discovering if we are on the right track. To help us there, we will adopt test-driven development (TDD) for that instant feedback loop.
Let start by setting up our project.
Setup
As we get set up, we will need a directory to hold our new application. Let’s first create a directory called greeter and change into our new directory. Open your terminal and use the following commands:
$mkdir greeter$cdgreeter
We are now going to initialize a new Truffle project as follows:
$ truffle init
This command will generate the following output:
✔ Preparing to download✔ Downloading✔ Cleaning up temporary files✔ Setting up boxUnbox successful. Sweet!Commands:Compile: truffle compileMigrate: truffle migrateTest contracts: truffle test
Our greeter directory should now include the following files:
greeter ├── contracts │ └── Migrations.sol ├── migrations │ └── 1_initial_migration.js ├── test └── truffle-config.js
Notice that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access