Getting started with Ganache for local blockchain development and testing

Ganache gives you a way to set up a local blockchain pre-loaded with accounts and ether.

To learn more about Ganache you can go to their homepage.
To install the ganachi-gui go here and select the download button for your operating system.

The ganache-cli has the same functionality as the ganache-gui in a command line interface. To install the ganache-cli follow the directions provided here.

npm install -g ganache-cli

Once installed, run the following command in your terminal to start:

ganache.ganache-cli

If you downloaded the GUI, launch the application and you should see something like this including test accounts preloaded.

If you went the CLI route and run the ganache-cli command then you’ll see a similar output in the terminal.

Now that we have Ganache loaded with test accounts we can connect to web3 and execute some local blockchain transactions between accounts.

mkdir ganacheProject
cd ganacheProject

We’ll install a transaction helper library known as ethereumjs-tx, configure connections, create transactions, then sign and send those transactions and see the results in Ganache.

npm init -y
npm install web3 --save
npm install ethereumjs-tx --save

Next we’ll create an index.js file to contain our code for the connections, transaction, signing the transaction, and finally sending to the receiving account. Create an index.js file within your project directory and populate it with the following code.

// Configuration
var Web3 = require('web3');
const EthereumTransaction = require('ethereumjs-tx').Transaction

// Use the RPC Server address provided by Ganache 
var web3 = new Web3('http://127.0.0.1:7545'); 

// Set Addresses from Ganache
var sendingAddress = 'YOUR FIRST GANACHE TEST ACCOUNT ADDRESS';
var receivingAddress = 'YOUR SECOND GANACHE TEST ACCOUNT ADDRESS';

web3.eth.getBalance(sendingAddress).then(console.log);
web3.eth.getBalance(receivingAddress).then(console.log);

// Create transaction
var rawTransaction = { 
	nonce: '0x00', 
	to: receivingAddress, 
	gasPrice: '0x09184e72a000', 
	gasLimit: '0x6691B7', 
	value: '0x001'
}

// Sign Transaction
var privateKeySender  = new Buffer.from('FIRST GANACHE TEST ACCOUNT PRIVATE KEY', 'hex')
const tx = new EthereumTransaction (rawTransaction, { chain: 'mainnet', hardfork: 'petersburg' })
tx.sign(privateKeySender)
const serializedTransaction = tx.serialize()

// Send Transaction
web3.eth.sendSignedTransaction(serializedTransaction.toString('hex'), function(err, hash) {
  if (!err)
    console.log(hash); 
});

You can also find a copy of the code here on GitHub.

Once you have created the index.js file with the above code make sure to update it with the account addresses and the sending account private key in order for it to run correctly. Also note that the nonce value must be unique and increment so this will need to be updated for subsequent transactions.

node index.js

The output should include the account balances of both the Sending and Receiving accounts and then if the transaction is completed it will also return transaction’s hash value.

You can now see the transaction reflected in Ganache using either the GUI or CLI. Note if you don’t see the transaction hash in your terminal or the transaction in Ganache you may need to double check that your gas limit in the transaction is sufficient based on the Ganache gas price and limit.

You can also see the ETH balance changes in the accounts.

For more information on how best to leverage Ganache see the developer docs here as well as Truffle’s additional guides.

posted by Tyler Wolfe GitHub @tylererc20

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: