Truffle Quick Start Guide - Nikhil Bhaskar - E-Book

Truffle Quick Start Guide E-Book

Nikhil Bhaskar

0,0
28,14 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Develop, test, and deploy decentralized applications for Ethereum platform




Key Features



  • Build your first Ethereum Dapp with Truffle: the most popular Ethereum development framework



  • Build, compile, and deploy smart contracts in your development environment



  • Embrace Blockchains and utilize it to create new generation of secured and scalable apps



Book Description



Truffle is a world-class development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier.







If you are a web developer wanting to try your hand at developing Dapps with Truffle, then this is the book for you. This book will teach you to write smart contracts and build Dapps with Truffle. You will begin with covering the basics of Truffle, briefly explaining how it integrates Solidity and Web3, in orderto start building a mini decentralized application. Also, you will dive into migration, testing and integrating Truffle with the use of popular JavaScript frameworks. Lastly, you will ship your decentralized application and package it into a product. Moreover, you will go through the best practices in Truffle,so as to increase your proficiency in building Dapps with Truffle.







By the end of the book, you will be able to write smart contracts and build decentralized applications with Truffle on Ethereum blockchains.




What you will learn



  • Understand the fundamentals of Truffle and Web3



  • Build a decentralized application with Truffle, while choosing the correct Ethereum client



  • Connect your Dapp to Ethereum clients including Geth, Parity, and Ganache



  • Migrate and test your Dapp with the correct networks such as Ropsten and Rinkeby



  • Package a decentralized application into a user-friendly product by integrating Truffle with JavaScript frameworks such as Angular, React and Vue



  • Explore tools including Ethereum Package Manager, the Registrar and browser wallets, and exploit third-party smart contract libraries.



  • Evaluate the common migration pitfalls and how to mitigate them



Who this book is for



This book is for web developers who are interested in the new world blockchain. Some basic understanding of JavaScript and web services is required. No prior knowledge of Decentralized applications or blockchain is required.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 148

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Truffle Quick Start Guide
Learn the fundamentals of Ethereum development
Nikhil Bhaskar
BIRMINGHAM - MUMBAI

Truffle Quick Start Guide

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor:Kunal ChaudhariAcquisition Editor: Reshma RamanContent Development Editor:Mohammed Yusuf ImaratwaleTechnical Editor: Sushmeeta JenaCopy Editor: Safis EditingProject Coordinator:Hardik BhindeProofreader: Safis EditingIndexer:Tejal Daruwale SoniGraphics:Jason MonteiroProduction Coordinator:Deepika Naik

First published: June 2018

Production reference: 1260618

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78913-254-0

www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the author

Nikhil Bhaskar is the founder and CEO ofUlixir Inc—a newly founded tech company that builds decentralized and traditional software.

He completed B9lab's Ethereum Developer Course, and he is now a certified Ethereum developer.

Aside from running Ulixir, he spends his time traveling and eating. He is a bit of a digital nomad; this year, he's lived in five countries and plans to live in six more before the year ends.

About the reviewer

Aasim is a full-stack blockchain engineer at Inncretech LLC, Princeton, New Jersey. He has a masters degree in Information Systems with a graduate certification in Business Intelligence from Steven's Institute Of Technology, Hoboken, New Jersey. He works with the R&D team of blockchain and data science for making POCs around AI, Ethereum and Hyperledger blockchain for clients to incorporate in their proprietary systems, which helps them upgrade to new cutting-edge technologies.

I would like to thank my family and friends for supporting me on my journey of life, which helped me grow into a knowledgeable and intellectual person. I would also like to express my gratitude to my company's co-founder and CTO for believing in me and giving me this opportunity to explore blockchain in depth. I would sincerely like to thank the author and his team for giving me this opportunity to review this book.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Truffle Quick Start Guide

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the author

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Code in Action

Conventions used

Get in touch

Reviews

Truffle for Decentralized Applications

Technical requirements

What is Truffle?

Let's build a mini Dapp

Let's build a to-do list

Initializing a Truffle project

Peeping into the folders

Peeping into the root files

Housekeeping before we write code

Writing our first smart contract

Adding an owner

Creating a reward method

Securing your contract with modifiers

Adding a utility method

Wrapping up

Building a user interface

Simple styling

Performing the build steps

Let's write some JavaScript

References

Summary

Web3 and Solidity in Truffle

Technical requirements

What is web3?

Ganache-CLI

Web3 in Truffle

Solidity – a refresher

Data types

Arrays

Structs

Mapping

Visibility specifiers

Functions

Function modifiers

Function types

Events

Summary

Choosing an Ethereum Client for Your Dapp

Technical requirements

Geth, Parity, and Ganache

What is an Ethereum client?

Geth

How does Geth work?

The power of Geth

Common GETH actions

Parity

The power of Parity

Common Parity actions

Ganache-CLI

The power of Ganache-CLI

Common Ganache-CLI actions

Choosing the correct Ethereum client

Truffle and Ethereum clients

Truffle and Geth

Truffle and Parity

Truffle and Ganache-CLI

Summary

Migrating Your Dapp to Ethereum Blockchains

Technical requirements

Let's migrate

Migrating your contracts to Ropsten with Parity

Creating a new Ropsten Parity account

Syncing Parity to Ropsten

Adding funds to our Parity coinbase account

Migrating our contract to Ropsten

Migrating our contract to kovan

Common migration pitfalls

Summary

Truffle and Popular JavaScript Technologies

Technical requirements

Truffle and Angular

angular-truffle-starter-dapp

Peeping into the src folder

Running the starter Dapp

Part 1

Part 2

Diving into Web3Service

MetaCoinService

Truffle and React

The Truffle and React starter

Peeping into the src folder

Diving into the Truffle and React code

Truffle and Node

Running our Truffle and Node application

The node run steps

Summary

Testing Your Dapp

Technical requirements

Writing unit tests with Solidity

Testing the TaskMaster

Writing unit tests with JavaScript

Testing for Solidity events

Summary

Truffle Gotchas and Best Practices

Gas versus gas limit versus gas price

Gas

Gas price

Gas limit

Sync issues in Geth

Security best practices

Summary

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

This book provides an intuitive, step-by-step, and engaging guide on how to write smart contracts and build decentralized applications with Truffle on Ethereum blockchains.

The first section will cover the basics of Truffle, briefly explain how it integrates Solidity and Web3, and get the reader to build a mini-decentralized application. The following sections will dive into migration, testing, and combining Truffle with popular JavaScript frameworks. The final section of this book will cover the best practices and common mistakes in Truffle to increase the reader's level of proficiency in building Dapps with Truffle.

This book will contain code snippets, exercises, and a project that the reader will continuously build throughout their reading journey.

Who this book is for

This book is for JavaScript developers who are interested in learning about writing smart contracts and building decentralized applications on blockchain. The reader of this book will be required to have some basic understanding of JavaScript and web services. Basic knowledge of Ethereum and/or blockchain is also required.

What this book covers

Chapter 1, Truffle for Decentralized Applications, introduces you to Truffle and explains why it is used and how it works from a high level. It will also demonstrate how JavaScript, Solidity, and Web3 interact inside the basic Truffle environment.

Chapter 2, Web3 and Solidity in Truffle, concerns Web3 and its related APIs and uses in Truffle. You will gain an understanding of the fundamentals of Web3 and how it is used in Truffle.

Chapter 3, Choosing an Ethereum Client for Your Dapp, is about various Ethereum clients, and will highlight the use case for each one and show you how to integrate each Ethereum client with your Truffle application.

Chapter 4, Migrating Your Dapp to Ethereum Blockchains, teaches you how to correctly migrate your Truffle application to Ethereum blockchains. You will also learn about common pitfalls when attempting to migrate your application.

Chapter 5, Truffle and Popular JavaScript Technologies, illustrates the use of Truffle with modern JavaScript technologies such as Angular, React, and Node.js. You will learn how to integrate Truffle with JavaScript frontend libraries and frameworks as well as on the backend with Node.js.

Chapter 6, Testing Your Dapp, demonstrates to you the importance of thoroughly testing your smart contracts, detailing various techniques and approaches for successfully testing a Truffle application.

Chapter 7, Truffle Gotchas and Best Practices, explains the common mistakes people make when developing with Truffle, as well as how to remedy them. Lastly, you will learn and understand how to diagnose and resolve common blockchain issues, such as chain syncing, as well as some best security practices.

To get the most out of this book

As mentioned before, basic knowledge of JavaScript and web development, as well as of Ethereum/blockchain, is required. To get the most out of this book, have the GitHub repository (mentioned in the chapters) by your side; simply follow the instructions outlined in each chapter.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Truffle-Quick-Start-Guide. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/TruffleQuickStartGuide_ColorImages.pdf.

Code in Action

Visit the following link to check out videos of the code being run:http://bit.ly/2K80ovD

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

web3.eth.getAccounts(callback(error, result){ ... })

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

web3.eth.getAccounts() .then(function (accounts) {

console.log(JSON.stringify(accounts, null, 2));

}) .catch(function (error) { console.error(error); });

Any command-line input or output is written as follows:

make geth

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Truffle for Decentralized Applications

This chapter will introduce you to Truffle, and explain why it is used and how it works from a high level. Moreover, this chapter will demonstrate how JavaScript, Solidity, and web3 interact inside the basic Truffle environment, and how Truffle gives the developer control over all three said aspects of a decentralized application. Lastly, you will build a small project in Truffle and understand its power.

Specifically, you will:

Learn and understand the reasons for the use of Truffle

Learn and understand how Truffle harnesses popular technologies, such as JavaScript and web3, from a high level

Learn and understand how to write basic decentralized applications in Truffle

Technical requirements

You will berequiredto have basic knowledge of JavaScript and web development as well as Ethereum/blockchain. Finally, to use the Git repository of this book, the user needs to install Git.

The code files of this chapter can be found on GitHub:https://github.com/PacktPublishing/Truffle-Quick-Start-Guide/tree/master/Chapter01

Check out the following video to see the code in action:http://bit.ly/2MfQSnk

What is Truffle?

In short, Truffle is a framework to write, compile, deploy, and test decentralized applications on Ethereum blockchains. For this chapter, you can also think of Truffle as a framework that attempts to seamlessly integrate smart contract development and traditional web development.

Granularly, within the Truffle environment, you can write JavaScript for the frontend, Solidity for smart contracts, and use web3 as a bridge to connect various blockchain networks to the client.

If you are not familiar with web3, or you need a refresher on Solidity, worry not. The subsequent chapter will cover these two technologies in sufficient detail before you start building more complex decentralized applications.

For now, it is enough to know that Truffle combines JavaScript, Solidity, and web3 to allow you to write complete and testable decentralized applications:

Can you write decentralized applications without Truffle? Sure you can. Truffle simply makes the process of compiling, building, and migrating your application easier by automating certain monotonous aspects.

You will see how this is done toward the end of this chapter when you build a small but complete decentralized application. I will walk you through code snippets, provide a working repository for reference, and explain to you fundamental build concepts as you write code. Most importantly, I want to ensure you start coding as soon as possible. So, let's get started.

Let's build a mini Dapp

The best way to start is with a simple Hello World app, but we can do better than that. Let's build a simple Dapp that you will have the chance to expand upon throughout this entire book. This way, you can build a Dapp like a true software engineer; that is, you will iteratively add features and make improvements to your application as you see fit.

Let's build a to-do list

Productivity is a virtue that resonates a lot with software engineers and developers alike. In the book Algorithms to Live By, Brian Christian states that:

"When the future is foggy, it turns out you don't need a calendar—just a to-do list."

Many of us have come to know that a to-do list helps to organize chaos.

So, let's create a to-do list, except with a little twist. This will be a to-do list on the blockchain, where you get others to do your work for you. Of course, you have to pay them with a token, call it TodoCoin, for their work.

You will create this app as the owner