In Chapter 4, you learned how to create your own private test Ethereum network using the Ethereum client – Geth. While Geth is not that difficult to use to set up an Ethereum blockchain, it still takes considerable effort to set one up. In addition, you also need to create accounts and perform mining in order to obtain Ethers so that you can use them to deploy Smart Contracts and for performing transactions.
A much better option is to use a tool that can emulate an Ethereum blockchain, together with all the required accounts so that you can just focus on testing your Smart Contracts. Well, such tool does exist, and it is called – Ganache.
Ganache is a personal Ethereum blockchain that allows you to test and deploy your Smart Contracts without needing to connect to a real blockchain or set up your own blockchain using an Ethereum client like Geth.
Tip
Ganache was previous known as TestRPC.
In this chapter, you will learn how to download and install Ganache and how to test and deploy your Smart Contracts onto Ganache.
Downloading and Installing Ganache
Ganache comes in two flavors – as a desktop GUI app and as a command line tool. They are both available on the Windows, Mac, and Linux platforms. In the following sections, you will learn how to install the desktop as well as the command-line version of Ganache.
Command-Line Interface
The command-line version of Ganache is a Node.js-based Ethereum client. It uses EthereumJS library to simulate full client behavior and make developing Ethereum applications much faster. It also includes all popular RPC functions and features (like events) and can be run deterministically to make development a breeze.
To install command-line version of Ganache, you would need to install Node.js on your computer. You can then use npm to install the tool.
In Terminal (or Command Prompt), type the following command to install the Ganache command-line interface (CLI):
$ npm install -g ganache-cli
Once the Ganache CLI is installed, you can launch Ganache using the following command:
Mnemonic: gospel oval arm true goat luggage play into artwork sound marble soft
Base HD Path: m/44'/60'/0'/0/{account_index}
Gas Price
==================
20000000000
Gas Limit
==================
6721975
Listening on 127.0.0.1:8545
Observe from the preceding output:
Ganache has pre-created 10 user accounts, each with 100 Ethers.
The section titled “Available Accounts” shows the account addresses of the ten accounts. The section titled “Private Keys” shows the corresponding private keys of each of the ten accounts.
The Ganache CLI is also listening at a local port with port number 8545.
Tip
You will learn how to interact with Ganache through its port in a later section in this chapter.
Graphical User Interface
If you do not want to mess with the command-line interface version of Ganache, you can use the desktop version of it with a graphical user interface. To download the desktop version of Ganache, go to https://truffleframework.com/ganache and click the Download button. For the Mac version, you will download a DMG file. Once it is unzipped, simply drag the Ganache icon and drop it onto the Applications folder (see Figure 7-1).
To run it, double-click the Ganache application in the Applications folder. When you start Ganache for the first time, click the Quickstart button. Figure 7-2 shows Ganache up and running. It shows a list of 10 user accounts, each with 100 Ethers.
Tip
Observe that for the desktop version of Ganache, it is listening at port 7545 (which you can change), instead of the 8545 in the CLI version of Ganache.
If you want to see the private key of each account, click the key icon displayed next to each account (see Figure 7-3).
Tip
Knowing the private key of each account is important if you want to import that that account into a wallet, such as MetaMask.
When you click the BLOCKS tab, you will see that at the moment there is only one single block (Block 0; see Figure 7-4), which is the genesis block of the blockchain.
Creating a Smart Contract
Now that you have learned how to install the two different flavors of Ganache, let’s now write a Smart Contract and see how you can deploy it onto Ganache. For illustration, I shall use the desktop version of Ganache.
Using the Remix IDE on Chrome, create the following contract and name it as ProofOfExistence.sol:
Caution
For the Remix IDE, ensure that you are loading it using http and not https.This is because Ganache only accept connections to it using http. If you load the Remix IDE using https, you would have problems connecting to Ganache later on.
pragma solidity ^0.5.1;
contract ProofOfExistence {
mapping (bytes32 => bool) private proofs;
// store a proof of existence in the contract state
function storeProof(bytes32 proof) private {
proofs[proof] = true;
}
// calculate and store the proof for a document
function notarize(string memory document) public {
storeProof(proofFor(document));
}
// helper function to get a document's sha256
function proofFor(string memory document) private
pure returns (bytes32) {
return sha256(bytes(document));
}
// check if a document has been notarized
function checkDocument(string memory document) public
view returns (bool) {
return proofs[proofFor(document)];
}
}
The preceding Smart Contract does the following:
It allows users to pass in a string through the notarize() function so that the hash of the string can be stored on the blockchain using the proofs state variable (which is of type mapping, which is like as associative array).
It allows users to verify if a string was previously recorded in the blockchain by calling the checkDocument() function and checking if the hash of the string exists in the proofs state variable.
This Smart Contract acts as a simple notarizer in which a user can store a string (e.g., containing an idea) on the blockchain and proving it later on that this idea was already invented earlier.
Deploying the Contract to Ganache
With the Smart Contract created, you shall now deploy it to Ganache. Under the Runtab in the Remix IDE, select Web3 Provider as the environment (see Figure 7-5).
Remix IDE will now prompt you if you want to connect to an Ethereum node. Click OK. In the next screen, enter the Web3 Provider’s endpoint URL (see Figure 7-6).
The preceding endpoint URL is where the Ethereum node (which is Ganache in this case) is listening at. Recall that desktop version of Ganache is listening at port number 7545. Hence, be sure to specify this port number. If you are using the CLI version of Ganache, the port number is then 8545.
Tip
If you have problems in connecting to the rpc (error message: “Not possible to connect to the Web3 provider. Make sure the provider is running and a connection is open (via IPC or RPC).”), this is likely due to the fact that you are using the https version of Remix. To resolve this, simply load Remix using http.
Once the Web3 Provider is connected, you can deploy the contract to Ganache. To do so, click the Deploy button under the name of the contract (see Figure 7-7).
Tip
Be sure to turn on auto compile in the Remix IDE so that your contract is automatically compiled as you type in the code for the contract.
Once the contract has been deployed, you should be able to see the two functions (see Figure 7-8).
Tip
Click the down arrow if you don’t see the two functions – notarize and checkDocument.
Examining Ganache
If you now look at Ganache, you will see that the first account has a balance of 99.99 Ethers (see Figure 7-9).
This is because when you deploy the contract through Remix IDE, Remix IDE uses the first account (by default) in Ganache to pay for the contract deployment. You can change the account to use in the Remix IDE by selecting the Account to use under the Run tab (see Figure 7-10).
Also, under the BLOCKS tab in Ganache, you now see that there is an additional block – Block 1 (see Figure 7-11). This newly created block contains the contract that has been just deployed.
Under the TRANSACTIONS tab, you would be able to see a new transaction (see Figure 7-12), which is the transaction that deployed the contract.
In the LOGS tab, you would be able to see the activities that go on behind the scene in Ganache (see Figure 7-13).
Testing the Contract
Let’s now test out the contract. Enter the string “Hello, Smart Contract!” into the text box next to the notarize button (see Figure 7-14). Then, click the notarizebutton. Next, enter the same string into the text box displayed next to the checkDocument button and click the checkDocument button. You should be able to see the result “0: bool:true”. This indicates that the original was stored successfully on the blockchain and then you can verify its existence on the blockchain.
If you enter a different string into the text box displayed next to the checkDocument button, say “Hello, Solidity!” (see Figure 7-15) and then click the checkDocument button, you should be able to see the result “0: bool:false”.
If you examine Ganache now, you will be able to see the new block created on the blockchain (see Figure 7-16), as well as the new transaction (which is a contract call) that is performed when you click the notarize button.
Connecting MetaMask to Ganache
Since Ganache is a blockchain, you should be able to connect your MetaMask to it. So, let’s see how you can do it.
In MetaMask, click the down arrow and select Custom RPC (see Figure 7-17).
Tip
If you are using the CLI version of Ganache, you can directly select the Localhost 8545 menu item.
You will now see the Settings page (see Figure 7-18). Scroll down the page, and under the New Network header, enter http://127.0.0.1:7545. This is the endpoint URL that Ganache (desktop version) is listening at. Once this is entered, click the “X” button to close the window.
When you now click the down arrow again, you will see a new item named “http://127.0.0.1:7545” (see Figure 7-19). Select it.
You will now see that your existing account has 0 ETH (see Figure 7-20). This is because your existing account is not valid in this new blockchain (which Ganache is hosting).
You need to import the account(s) from Ganache into MetaMask if you want to see their balance. To do so, click the account icon in MetaMask and select Import Account (see Figure 7-21).
In the New Account page, enter the private key of the first account in Ganache (see Figure 7-22). Then, click IMPORT.
The account from Ganache should now be imported into MetaMask (see Figure 7-23).
You can try to import the other accounts from Ganache into MetaMask. You then send Ethers from one account into another.
The ability to connect MetaMask to Ganache is very useful, especially in later chapters where we talk about interacting with your Smart Contracts via the web3.js APIs.
Summary
In this chapter, you learned how to test your Smart Contract in a stand-alone blockchain hosted by Ganache. Using Ganache allows you to simulate running a blockchain without accessing a real one. And it makes testing your Smart Contracts efficient and effortless.