1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Local Development with Truffle and Web3

Local Development with Truffle and Web3

Contents

keyboard_tab
Ganache, Truffle & Web3
1
Ganache
PREVIEW12m 47s
2
Web3.js
PREVIEW12m 37s
3
Truffle
4m 57s

The course is part of this learning path

Ganache
Overview
Difficulty
Intermediate
Duration
53m
Description

This course explores carries on the previous course and looks at local development with Ganache, Truffle, and Web3.

Transcript

In the previous lectures and sections, we have worked with remix inside this JavaScript virtual machine. And this JavaScript virtual machine really gives you and what it says a virtual machine environment, a blockchain which is running in JavaScript inside the browser, and you cannot access it from outside. But it's very convenient. It's just you you can use it like any other blockchain, just that you're really limited to whatever is happening inside your browser. And as soon as you refresh the browser, it's all gone. Now, what I prefer during my development is not using the JavaScript VM actually. I very much prefer using another very similar blockchain which is running outside of JavaScript but still in a way that I can use it as a developer. I don't have to wait for mining and so on. And one of the most famous implementations for that is Ganache, which is little package from the Truffle guys. Truffle framework is a framework for, think of it like Angular JS for JavaScript is Truffle for Ethereum development or React for JavaScript. So, it's a suite of tools, actually. the Truffle suite. And one of the tools is Ganache, and Ganache comes in two flavors. One is the UI version which you can see here in the picture. And another one is the command line version. Command line interface, CLI version. Going forward, I will use in this course, the command line version. But I still wanted to show you the UI version in case you like the flavor of UI. And if you go to truffleframework.com/ganache, then you see usually for your OS that you're opening with your browser, a download button. I am here on Windows. So, I see a download for Windows button and when I click it, then it will start downloading. Luckily, I already downloaded Ganache, and I can just open it and on Windows. It will launch this installer. Maybe it will launch the store where you have to download the Windows installer first. I already installed the Windows installer in order to show me this pop up, and then I can hit 'Install' and say launch when ready. So, it will launch this. It will take a moment, and then it will start Ganache. I do not want to have my analytics enabled, but when you start Ganache, then you get 10 accounts with 100 ether each. Why can you get 10 accounts with 100 ether each? Because in the inside, Ganache is running your own blockchain which you are in control with, is like your own database. It's just a developer blockchain,  and because you have the whole control over your data, and you basically start with the first block which is the genesis block. You can already define how many ethers are inside your accounts. And those accounts are stored in Ganache, because Ganache is also going to be your wallet. And as your wallet, it is responsible for signing transactions. So, there's a lot of functionality inside Ganache which is actually behind the curtains, usually behind the scenes. You don't see it. All you are really, all you really want to do is you want to have your address and your balance, and the rest is usually from a developer perspective extracted away from you. Then what else do we have here? We have a mnemonic. What is a mnemonic? A mnemonic word spell for seed phrase is of 12 words that can be used to recreate those 10 accounts or potentially more accounts using this HD path. The HD path is also called hierarchical deterministic path. So, it is a way to recreate private keys based on an input string, and that is your input string. So, if you give out this string, the path is usually well known in advance, then you can recreate your private keys based from the private keys as you have seen maybe from the previous lecture, you can go back to your address. So, you don't want to lose at anytime. The mnemonic here in Ganache is not as important, because it's only your own blockchain. But later when we are talking about wallets and so on, then please keep in mind the mnemonics are even more worth than your private keys. All right, what else do we have? We see a current block. We see blocks, which is currently nothing, we see nothing in the transactions. And we see an empty logs window. And when I go to remix, and I see here in Ganache that I have an RPC server running on local host 7545. That means that there is an API where I can connect to. So, from outside of Ganache, I can connect to my Ganache and then run RPC commands, remote procedure commands. Now, when I go inside the Run tab of the remix, then I see a Web3 Provider. And this one lets me connect to an ethereum node. So, I have to put in the right address with the right support local host 7545, and I should be able to connect, and then see my 10 accounts with 100 ether each. And inside here when I select the first account, and give it a little bit more gas limit, I can deploy my GameManager. Now here in my log window, I see exactly the same. But I have my actual data, my blockchain, my database outside of my browser window now. I could take another browser or an external program to connect to this Ganache window as well. Now can go to the blocks, and I see that I have one transaction over here. And inside the block, I can drill down into the transaction details. I can go to the transaction log window, and I can see my actual transaction and I can see my transaction data, and this is my smart contract that I deployed. So, you see everything is very, very open. A blockchain and everything can be seen from from outside. So, everything can be made accessible from outside. Now, if you are preferring the UI version, then stick with the UI version. I very much prefer the CLI version, the command line interface version. because usually I just keep it open. And the reason why I use Ganache from outside is because I can connect from different points in my system from a JavaScript console, from my browser, maybe from another program, maybe from MetaMask. I can connect to this blockchain very much like a database, and I'm not limited to whatever is running inside my browser. So, I'm going to close this now, and now I'm going to show you how to install the CLI version. If you are on Windows, and that comes only with Windows, then you maybe want to install the Windows Build Tools first. If you are on any operating system, and before you install the Windows Build Tools, you need NPM. So, if you have NPM installed, you can easily check that, open a PowerShell on Windows or a terminal on your macOS or Linux, and type in npm- v. And npm- v should show you a version of NPM. And if that shows you something like command not found or command not recognized. I don't know whether it's on Windows. So, if it gives you an error like this one on Windows, then you have to install NodeJS first. NodeJS is a package which can be found on the nodejs.org. And then you just download either with 10.15.1, the long term support version or the 11 version. It doesn't matter as long as you have the node package manager, NPM installed. So, once you install node, but then you should close and reopen your PowerShell window in order to get NPM, and it should pop up this version number. Good, if you have this, then the next thing on Windows, only on Windows, then you have to install -g, for global, the windows-build-tools. This will download a set of tools which are necessary on Windows to actually build something. If you do not want to install them, then you have to install the Visual Studio including the Visual C ++ compiler in order to compile things. Now if you don't want to do that hassle, then just install those build tools. Next thing is npm install -g ganache-cli. With this command, you are downloading and installing the Ganache command line interface version. After this is done, and for me this was pretty fast because I've already installed this before. You can simply start it from anywhere in your system. ganache-cli like this. And it should start very much the same things just in a command line version. 10 accounts, 100 ether each. You even see the private keys, you see the mnemonic, the 12 word seed phrase and the HD path which you need in order to create these private keys based on this mnemonic pack. We are going to use this later in order to work with MetaMask to have something to connect to. There's one more thing which is extremely important. It is listening to local host 8545. That means that this Ganache version, this one which is here running, is not running on local host 7545, it's running on local host port 8545. So, if you want to connect from remix to this, but Ganache that is running inside the inside your PowerShell, inside your terminal, then first of all keep it open, so you cannot close it and then connect to it. So, you have to keep your PowerShell window, your terminal window open, and then you go back to your remix, select the Web3 Provider, and now you can select 8545. And it's connecting. And then you can do exactly the same again, deploy and so on. One more thing, last thing for this lecture is if you close Ganache, all the data will be gone. It is an in memory blockchain simulation. So, it is very much like the browser version back in remix but you can connect from outside. It is meant to be used for developers only. So, do not use Ganache for any production environment or anything. It is really just used for developing. And it comes with a couple of very handy features, like for example, that all the transactions are immediately mined are going go in here and select 0.1 ether 0.1 ether, and start a new game, the transaction is immediately available. If I do not want this, and if I want to have it more in a way that resembles an original blockchain where I have 15 seconds mining time, then I can add different parameters to Ganache. I can say ganache- cli- b 15. And the block mining time will be 15 seconds. So, when I close this and I deploy a new game, then you see here creation of GameManager pending and it will be pending until my transaction is mined, that might take up to 15 seconds. So, you see with Ganache, you can do a lot of simulating, of developing, and it's a very, very handy tool. And from this point on, I want to use Ganache to work with my smart contracts in combination with Truffle. Now, there is no challenge for this section. The thing is, you should go ahead and install Ganache right now. And I'll see you in the next lecture.

About the Author
Students
8
Courses
7

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.

Covered Topics