How to use the command 'ganache-cli' (with examples)

How to use the command 'ganache-cli' (with examples)

Ganache is a personal blockchain for Ethereum development. It allows developers to run a local blockchain network for testing smart contracts and interacting with decentralized applications (dApps). Ganache-CLI is the command-line version of Ganache, offering a lightweight and efficient way to run and manage the blockchain network.

Use case 1: Run Ganache

Code:

ganache-cli

Motivation: This use case allows you to quickly start a local blockchain network for Ethereum development. It is useful when you want to begin testing smart contracts or dApps without the need for a full-fledged Ethereum network or real Ether.

Explanation: The command ganache-cli without any arguments starts Ganache with default settings. It creates a local blockchain network with 10 accounts pre-loaded with 100 Ether each. The network runs on the default port 8545.

Example output:

Ganache CLI v6.12.2 (ganache-core: 2.13.2)

Available Accounts
==================
(0) 0x77768A69BC8F62817E9E0fa95Fa4C366380b33C6
(1) 0x3d7d3Ddbbc8BEB97aFaa3F4A573a0b7F9C158273
(2) 0xA351188097FD6CcB79758451E146c708fbd70279
...

Use case 2: Run Ganache with a specific number of accounts

Code:

ganache-cli --accounts=5

Motivation: Sometimes you may want to have a specific number of accounts in your local blockchain network for testing or simulation purposes.

Explanation: The --accounts flag followed by a number specifies the desired number of accounts in the network. In this example, Ganache will create a local blockchain network with 5 accounts, each having 100 Ether by default.

Example output:

Ganache CLI v6.12.2 (ganache-core: 2.13.2)

Available Accounts
==================
(0) 0x65fcEa25075A2C6A094BfaCC7a7155434F924ab2
(1) 0x718fbFcCb6Fea86Fa5e006A0d0dd4a3a9d478E3f
(2) 0x1181aa476C5d9a4A733fa5A13d5343b20705CFf4
...

Use case 3: Run Ganache and lock available accounts by default

Code:

ganache-cli --secure

Motivation: When testing applications or interacting with smart contracts, you may want the accounts in the local blockchain network to be locked by default, preventing accidental transactions.

Explanation: The --secure flag ensures that all available accounts in the Ganache network are locked by default. This means that any transaction attempt will result in an error, ensuring that you don’t unintentionally send transactions from these accounts.

Example output:

Ganache CLI v6.12.2 (ganache-core: 2.13.2)
...
Locked Account: (0) 0x77768A69BC8F62817E9E0fa95Fa4C366380b33C6
Locked Account: (1) 0x3d7d3Ddbbc8BEB97aFaa3F4A573a0b7F9C158273
Locked Account: (2) 0xA351188097FD6CcB79758451E146c708fbd70279
...

Use case 4: Run Ganache server and unlock specific accounts

Code:

ganache-cli --secure --unlock "account_private_key1" --unlock "account_private_key2"

Motivation: In some cases, you may need to unlock specific accounts in a secured Ganache network. For example, if you want to test a specific transaction flow or interact with a smart contract from a specific account.

Explanation: The --unlock flag followed by an account private key unlocks the specified account(s) in the Ganache network. This allows you to send transactions from these unlocked accounts.

Example output:

Ganache CLI v6.12.2 (ganache-core: 2.13.2)
...
Unlocked Account: (0) 0x65fcEa25075A2C6A094BfaCC7a7155434F924ab2 (0xE1EEB02CB26B972F451e7c503eE914CaB3F8100e)
Unlocked Account: (1) 0x718fbFcCb6Fea86Fa5e006A0d0dd4a3a9d478E3f (0x2146CF2a6B1c701a1759fd7174d96d3e4b9B9a53)
...

Use case 5: Run Ganache with a specific account and balance

Code:

ganache-cli --account="account_private_key,account_balance"

Motivation: This use case is beneficial when you want to have control over a specific account and its initial balance in the Ganache network.

Explanation: The --account flag followed by an account’s private key and balance allows you to add a custom account to the Ganache network with a specific initial balance.

Example output:

Ganache CLI v6.12.2 (ganache-core: 2.13.2)
...
Available Accounts
==================
(0) 0x65fcEa25075A2C6A094BfaCC7a7155434F924ab2 (balance: 1000 Ether)
(1) 0x718fbFcCb6Fea86Fa5e006A0d0dd4a3a9d478E3f (balance: 500 Ether)
(2) 0x1181aa476C5d9a4A733fa5A13d5343b20705CFf4 (balance: 100 Ether)
...

Use case 6: Run Ganache with accounts with a default balance

Code:

ganache-cli --defaultBalanceEther=500

Motivation: In scenarios where you want to simulate a network where accounts have a specific default balance, this use case is useful.

Explanation: The --defaultBalanceEther flag followed by a balance value allows you to set the default balance for all the accounts created in the Ganache network.

Example output:

Ganache CLI v6.12.2 (ganache-core: 2.13.2)
...
Available Accounts
==================
(0) 0x65fcEa25075A2C6A094BfaCC7a7155434F924ab2 (balance: 500 Ether)
(1) 0x718fbFcCb6Fea86Fa5e006A0d0dd4a3a9d478E3f (balance: 500 Ether)
(2) 0x1181aa476C5d9a4A733fa5A13d5343b20705CFf4 (balance: 500 Ether)
...

Use case 7: Run Ganache and log all requests to stdout

Code:

ganache-cli --verbose

Motivation: When debugging or analyzing the behavior of an application or smart contracts, it can be helpful to log all the request details made to Ganache.

Explanation: The --verbose flag enables verbose logging, printing out details of all requests made to Ganache. This includes information like the method, path, request, response, and timing information.

Example output:

Ganache CLI v6.12.2 (ganache-core: 2.13.2)
...
Ganache: Request received: method=eth_accounts
Ganache: Sending response: result=["0x65fcEa25075A2C6A094BfaCC7a7155434F924ab2", "0x718fbFcCb6Fea86Fa5e006A0d0dd4a3a9d478E3f", ...]
...

Conclusion:

The Ganache-CLI command provides various options to customize and control the behavior of the Ganache Ethereum development blockchain. By using different flags, developers can modify the number of accounts, account balances, locking/unlocking accounts, and logging requests. These use cases enable developers to effectively test smart contracts, dApps, and interact with the local blockchain network for Ethereum development.

Related Posts

How to use the command 'cpupower' (with examples)

How to use the command 'cpupower' (with examples)

The ‘cpupower’ command is a tool that provides options for managing CPU power and tuning.

Read More
Using the openttd command (with examples)

Using the openttd command (with examples)

Start a new game openttd -g Motivation: This command is used to start a new game in OpenTTD, the open source clone of the Microprose game “Transport Tycoon Deluxe”.

Read More
How to use the command git gc (with examples)

How to use the command git gc (with examples)

Git gc is a command used to optimize the local repository by cleaning unnecessary files.

Read More