How We Use Ganache and Ganache-CLI

In-memory virtual blockchain and the command line interface

Jayper Sanchez
Shyft Network
5 min readSep 12, 2018

--

How We Use Ganache and Ganache-CLI

Shyft Network is a blockchain platform specializing in KYC and AML technology based on the open-source Ethereum network. Shyft’s blockchain platform standardizes the KYC and AML verification process. Since I started with Shyft Network, I have learned so much in such a short time: I started getting myself familiar with Solidity; I got the chance to learn how the compiler works and how it sends OpCode data to Shyft’s Ethereumjs-vm for execution; I can write up a technical blog all day long on all the numerous components of the Shyft ecosystem.

Focusing on Ganache and Ganache-CLI

For those who are still just getting started learning about blockchain, I’d like to just go over quickly about what a blockchain is, what it’s not and why it is so technologically disruptive.

What is Blockchain?
Blockchain is a constantly growing ledger that keeps permanent records of all the transactions that have taken place, in a secure, chronological and immutable way. It is like a decentralized database — but it is not a database. In a traditional database, someone has full control of the data. In a traditional database, access can be granted to allow Create, Read, Update and Delete (CRUD) privileges. However there are no central owners of a blockchain ledger and any transaction has multiple copies. Everyone in the peer to peer network has a copy of this ledger. However, everyone on the network will have to agree that the new transaction is valid. Otherwise, this transaction will be rejected. Every transaction is written into a file, and when the file gets full, it creates a new file or block, and each block is cryptographically chained together by using the hashed value of all transactions in the block combined with the hash of the previous block. Blockchain provides a single source of truth that is verifiable, tamper-proof, and unchangeable.

Blockchain is not:

  • A business network
  • A replacement for databases
  • A messaging solution
  • Meant for high-performance transactions
  • Meant to replace transaction processing

Blockchain technology does provide the following value:

  • Blockchain enables us to assign value to anything and can be transferred over the internet without any middlemen
  • Digital Assets or Cryptocurrency, eg Bitcoin, Litecoin or Ether and Smart Contracts
  • Can securely assign ownership of an asset and know that this cannot be altered but can be securely transferred to a new owner
  • Blockchain is distributed so there is no single point of failure. Unlike a traditional database, where if the owner fails, the whole system can be brought down

To develop smart contracts, it is recommended, though not mandatory, to use a framework.

To write transactions to the blockchain, you need a couple of things. First, you must have the following installed: NPM and NodeJS. The framework we have chosen to use is Truffle. Truffle uses the Solidity compiler so you can compile and deploy smart contracts. Now, as a developer, you will be deploying your changes very often to test for proper functionality. You can’t deploy your ongoing smart contract changes to the public blockchain, because this costs money and requires you to wait for the deployment and invocation transactions to be mined. Remember, everything you write in the blockchain network is permanent and cannot be removed, so we need some sort of a playground or a sandbox to play around with. This is where Ganache, and Ganache-CLI specifically, becomes very useful. For instructions on how to set up a development environment, including Ganache-CLI, go here.

Ganache is an in-memory virtual blockchain.

You can consider it your personal blockchain. With it, you can compile and migrate to test your smart contract changes. Once you close Ganache, all those changes will disappear. Ganache replaces the old TestRPC — in fact, Ganache WAS TestRPC.

There are two versions of Ganache. A UI version and a command line version. In order for us to get our feet really wet with Ganache, I will focus on Ganache-CLI. It is the command line interface. Assuming you’ve installed Ganache, you will need to open a terminal window. If you are using Windows 10, I suggest you install and use Powershell. If you are a Linux or MacOS user, well, we’ve got our Terminal window.

To start Ganache-CLI, just run the following command at the prompt, “ganache-cli”. Your screen should look essentially like the image below:

There are three items I’d like you to know about:

  • Available Accounts
  • Private Keys, and
  • the Listening port.

Ganache will create a total of 10 virtual accounts each starting with 100 Ether. This is very useful for developers because we will need virtual accounts to pay for gas when running transactions on the blockchain. Along with the 10 virtual accounts comes 10 private keys. These are the keys used to sign a transaction being written to the blockchain. Then comes the port number that Ganache listens to. That port number is 8545 by default. Truffle will need to be directed to write to this port in order to do work with Ganache.

When you compile and deploy a smart contract or run any transaction against Ganache, you will get an output from the Ganache terminal window which is the transaction receipt.

Using Truffle’s getTransaction function call, the details of each transactions is as follows

Note a few fields in the transaction details of the smart contract migration:

  • hash
  • nonce
  • blockhash
  • blocknumber
  • transactionindex
  • from
  • to
  • value
  • gas
  • gasPrice
  • Input

Things to make note of are hash which is the hash value of the transaction number for this deployment. Blocknumber is the block where the transaction or smart contract is written on. Gas that was paid to deploy the smart contract; in this case it is 1 ether. Gasprice is the cost of gas we were willing to pay for. The input field is the hash or signature value of the actual data content of the smart contract.

Ganache is a great virtual server tool that you can use to test your smart contracts. It also helps you to understand and picture what is going on when you write transactions to a blockchain.

***
Shyft is building the world’s first modern, secure, multi-stakeholder Blockchain-based trust network that enables KYC/AML attested data transfers. Join our Telegram (
https://t.me/shyftnetwork), follow us on Twitter (https://twitter.com/shyftnetwork), GitHub (https://github.com/ShyftNetwork) and other channels found on https://www.shyft.network.

--

--