The blockchain bootcamp · Chapter 3 of 18 · Blockchain · new chapter every Saturday evening
By the end of this chapter: Run a local node and get instant, free, disposable transactions to learn on.
The problem
Emma is a front‑end engineer who has just been handed a ticket to add a “view latest block” widget to the company’s dashboard. The design mock shows the block number, its hash, the timestamp, and a badge that says “finalised”. Emma has never written blockchain‑related code, but she has a working React codebase and a CI pipeline that already ships daily. She knows that to build this feature, she'll need to understand how to interact with a blockchain, but she's not sure where to start.
She begins by reading the documentation for various blockchain platforms, but quickly becomes overwhelmed by the complexity of the concepts and the sheer amount of information available. She tries to set up a testnet account, but the process is slow and cumbersome, and she's not sure if she's doing it correctly. After several hours of struggling, Emma realizes that she needs a more hands-on approach to learning about blockchain development. She wants to be able to run a local node and get instant, free, disposable transactions to learn on, but she's not sure how to get started.
The problem is that Emma needs to get a local blockchain environment up and running, but she's not sure what tools to use or how to configure them. She's tried reading the documentation, but it's not giving her the hands-on experience she needs to build her widget. She's wasting time and getting frustrated, and she's starting to worry that she won't be able to deliver the feature on time.
Before you start
To follow along with this chapter, you'll need to have Node.js (version 16 or higher) installed on your machine, as well as a code editor or IDE of your choice. You should also have a basic understanding of blockchain concepts, such as blocks, chains, and hashes, which were covered in Chapter 1 of this series. Additionally, you should be familiar with the concepts of wallets, keys, and addresses, which were covered in Chapter 2. You can verify that Node.js is installed by running the command node -v in your terminal, which should output the version number.
Setting up a local node
To get started with running a local node, we'll be using a tool called Ganache. Ganache is a popular development tool for Ethereum and other blockchain platforms that allows you to run a local node and get instant, free, disposable transactions.
Installing Ganache
To install Ganache, you can use the following command: npm install -g ganache. This will install Ganache globally on your machine, so you can use it from any directory. Once the installation is complete, you can verify that Ganache is installed by running the command ganache --version, which should output the version number.
Starting the local node
To start the local node, you can use the following command: ganache. This will start a new local node and output the node's configuration to the console. You should see output that looks something like this:
Ganache CLI v6.12.2 (ganache-core: 2.13.2)
Available Accounts
==================
(0) 0x742d35Cc6634C0532925a3b844Bc454e4438f44e (100 ETH)
(1) 0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc (100 ETH)
(2) 0x4C0759Ce94624A3616d63B457141d7d51Bf5e7B0 (100 ETH)
...
This output shows the available accounts on the local node, each with a balance of 100 ETH.
Build it
Now that we have our local node up and running, let's build a simple script to interact with it. We'll use the Web3.js library to connect to the node and send transactions.
Step 1: Install Web3.js
To install Web3.js, you can use the following command: npm install web3. This will install the Web3.js library and its dependencies.
Step 2: Connect to the local node
To connect to the local node, you can use the following code:
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
This code creates a new instance of the Web3.js library and connects to the local node at http://localhost:8545.
Step 3: Send a transaction
To send a transaction, you can use the following code:
const account = web3.eth.accounts[0];
const recipient = web3.eth.accounts[1];
const amount = web3.utils.toWei('1', 'ether');
web3.eth.sendTransaction({
from: account.address,
to: recipient.address,
value: amount,
});
This code sends a transaction from the first account to the second account, with a value of 1 ETH.
When this breaks
There are several ways that this setup can break. Here are a few common issues:
- Ganache doesn't start: If Ganache doesn't start, you may need to check that you have the latest version installed, or that there are no other processes using the same port.
- Web3.js can't connect: If Web3.js can't connect to the local node, you may need to check that the node is running and that the port is correct.
- Transactions don't send: If transactions don't send, you may need to check that the accounts have sufficient balance, or that the gas price is set correctly.
What it costs
Running a local node and sending transactions has several costs:
- Time: Setting up a local node and writing scripts to interact with it can take several hours.
- Resources: Running a local node requires significant computational resources, including CPU, memory, and disk space.
- Complexity: Interacting with a blockchain can be complex and error-prone, especially for developers without prior experience.
In the interview
In an interview, you may be asked questions like:
-
How do you set up a local blockchain environment?
- A weak answer might be "I use a cloud provider to set up a testnet account."
- A strong answer would be "I use a tool like Ganache to run a local node, and then use a library like Web3.js to interact with it."
-
What are some common issues that can arise when running a local node?
- A weak answer might be "I'm not sure, I've never had any issues."
- A strong answer would be "Some common issues include Ganache not starting, Web3.js not being able to connect, and transactions not sending. I would troubleshoot these issues by checking the node's configuration, the port, and the accounts' balances."
Your tasks
- Set up a local node using Ganache and send a transaction using Web3.js.
- Experiment with different gas prices and see how they affect the transaction.
- Try to break the setup by changing the port or the accounts' balances, and then troubleshoot the issues.
- Write a script to automate the process of sending transactions and monitoring the node's configuration.
- Research other tools and libraries that can be used to interact with a blockchain, such as Ethers.js or Truffle.
Your tasks this week
Do the exercises above before the next chapter. Reading a tutorial and doing
one are different activities and only one of them changes what you can build.
Stuck on any of them? Say so — describe what you tried and what happened:
tell me where you got stuck. I read every one, and the questions
that come back more than twice get answered in the next chapter.
The blockchain bootcamp
Chapter 3 of 18. New chapter every Saturday evening.
Next: Ethereum: accounts, gas and the life of a transaction.
· The full syllabus and every chapter so far
· Subscribers also get the condensed notes for this chapter, the running
recap of everything the series has covered, and the extended guidance:
subscribe
Written by Amit Chakraborty — founding engineer and senior architect: React Native, AI and RAG systems, production architecture. Portfolio · LinkedIn · GitHub.
Need this built, reviewed or taught to your team? Get in touch or email amit@devamit.co.in. Available for senior and founding engineering roles, consulting and training, remote worldwide.
Top comments (0)