Reader small image

You're reading from  Truffle Quick Start Guide

Product typeBook
Published inJun 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789132540
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Nikhil Bhaskar
Nikhil Bhaskar
author image
Nikhil Bhaskar

Nikhil Bhaskar is the founder and CEO of Ulixir Inca 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.
Read more about Nikhil Bhaskar

Right arrow

Web3 and Solidity in Truffle

This chapter will introduce you to 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 to connect to and help develop blockchain and/or smart contract-based applications. Moreover, this chapter will give you a brief refresher about Solidity, including functions, data types, visibility specifiers, and events.

Specifically, you will:

  • Learn and understand the reasons for the use of web3 in an isolated environment in Truffle
  • Learn and understand the reasons for the use of web3 in Truffle
  • Learn and understand the use of various types of functions and data types in Solidity
  • Learn and understand when and why events are used
  • Learn and understand how to use the appropriate type and visibility specifier when defining a function
...

Technical requirements

What is web3?

We mentioned web3 briefly in Chapter 1, Truffle for Decentralized Applications, and we even used it without fully understanding it. The purpose of Chapter 1, Truffle for Decentralized Applications, was to get a fully functional and working Dapp on a local blockchain so you could understand the power of Truffle.

Now, it's time to get more granular.

Web3 is a collection of APIs that allows JavaScript to access blockchain-based data and perform common Ethereum functions such as getting all accounts in a particular network, getting the balance of an account, or getting the particular block number.

Let's take a look at some examples:

var balance = web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1");
console.log(balance); // instanceof BigNumber
console.log(balance.toString(10)); // '1000000000000'
console.log(balance.toNumber...

Web3 in Truffle

Now that we have a clear understanding of web3 in an isolated environment, it's time to get out of our comfort zone and see how it interacts with Truffle. We've already seen a bit of this in Chapter 1, Truffle for Decentralized Applications.



Let's refer back to our app.js file under chapter1/app/javascripts/ (https://github.com/PacktPublishing/Truffle-Quick-Start-Guide/blob/master/chapter1/app/javascripts/app.js).

Let's see how we import web3:

import { default as Web3} from 'web3';

Now, let's see how we set the blockchain network provider for web3:

window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545"));

Remember how we simply glossed over this in Chapter 1, Truffle for Decentralized Applications? Hopefully, you now have a better understanding of what's going on here. Since we don&apos...

Solidity – a refresher

This is by no means a comprehensive tutorial on Solidity. This section will give you enough knowledge to build a fully functional Truffle Dapp. For more information on Solidity, the documentation is very handy; check it out here: https://solidity.readthedocs.io/en/v0.4.21/.

If you already have a good grasp of Solidity, you can skip the rest of this chapter and move on to Chapter 3, Choosing an Ethereum Client for Your Dapp.

In this section, we will cover Solidity basics. Further, you will find subsections for important components of Solidity. Let's start with our first component—data types.

Data types

Solidity, just like any other language, has some data types. Of them, let's...

Summary

Great job. We've covered a lot in this chapter.

Just as we did with web3, the best way to learn or catch up on Solidity is to play with it inside of an isolated environment. I've created a file for you called solidity-playground.sol. You can find it here: https://github.com/PacktPublishing/Truffle-Quick-Start-Guide/blob/master/chapter2/solidity-playground.sol. This contains the same SimpleTransfer contract. It's for you to play with, add/alter functions, and so on—it's all yours. You can use an online Solidity IDE called Remix (https://remix.ethereum.org) to run your contract.

See you in Chapter 3, Choosing an Ethereum Client for Your Dapp, where you will learn how to interact with various Ethereum clients and choose the correct one for your Dapp.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Truffle Quick Start Guide
Published in: Jun 2018Publisher: PacktISBN-13: 9781789132540
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Nikhil Bhaskar

Nikhil Bhaskar is the founder and CEO of Ulixir Inca 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.
Read more about Nikhil Bhaskar