How to set up a local blockchain environment on your Mac
If you have been following my beginner series on Solidity, you'd notice we have been writing, deploying, and interacting with our smart contract from Remix. While Remix is a good solution at first, it is really important to understand how to set up and interact with blockchain on your local machine. This gives you more flexibility, as well as tight control over your code and workflow.
In this post, I'll be sharing with you how to set up a blockchain environment on your local machine using Truffle and Ganache. These are tools that can help you replicate Remix in your local machine. In this tutorial, you'll learn
- How to install and use Truffle
- How to install and use Ganache
- Deploy and interact with your smart contract running on a local blockchain.
Truffle - Solidity development Framework
Truffle is a smart contract development framework. In simple terms, it is a suite of tools that can be used to write, test and deploy smart contracts to the blockchain. To install Truffle, you need to have Node and npm installed on your machine. If you don't have Node and Npm installed, this is a fantastic walkthrough. We'll be deploying the Todo program in our previous tutorial.
To install Truffle globally on your Mac, open your terminal and run:
npm install -g truffle
Once installed, create a project folder on your terminal, and cd into the project.
mkdir todo && cd todo
To create a truffle project
truffle init
Truffle will create these folders:
- /contracts - Contains our smart contracts
- /migrations - Contains scripts for contract deployment
- /test - Contains test files for testing our smart contract
- truffle-config.js - The truffle configuration file.
Open the project in your favorite IDE and create a Todo.sol file inside the contracts folder, then copy and paste our Todo.sol program in the file. We can alternatively continue editing from the terminal using Nano. Nano is a command line text editor.
cd contracts && nano Todo.sol
//contracts/Todo.sol
//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract Play {
uint private id;
struct Todo {
uint id;
string task;
bool isCompleted;
address owner;
}
mapping(address => mapping(uint => Todo)) public todos;
mapping(address => uint) public userTodoCount;
function addTodo(string memory _task) public {
id = id + 1;
Todo memory newTodo;
newTodo.id = id;
newTodo.task = _task;
newTodo.isCompleted = false;
newTodo.owner = msg.sender;
userTodoCount[msg.sender]++;
todos[msg.sender][id] = newTodo;
}
function getUserTodo() public view returns(uint){
return userTodoCount[msg.sender];
}
function getTodos() public view returns (Todo[] memory){
Todo[] memory myTodos;
for(uint i = 1; i <= userTodoCount[msg.sender]; i++){
myTodos[i - 1] = todos[msg.sender][i];
}
return myTodos;
}
function markAsCompleted(uint _id) public {
Todo memory myTodo = todos[msg.sender][_id];
myTodo.isCompleted = true;
}
}
With Truffle, we can easily compile our program. Compilation converts our Solidity code into its Assembly language equivalent. To compile our code using Truffle
truffle compile
If the code is without any errors, Truffle will save the compiled code to /build folder.
Ganache - Local Blockchain
To deploy our smart contract, we need a local blockchain running. Ganache is an application that we can use to create a local blockchain with test wallets and tokens. Truffle comes pre-installed with Ganache, but we can only use it from the terminal. To download the graphical interface, click here
After installing Ganache, open up the application and click on quick start. This will spin up a local blockchain environment with 10 test wallets loaded with 100 fake Ether each. The first wallet is used as the deployment wallet.
Deploy and interact with local Blockchain
To deploy our smart contract, we go back to the terminal and run
truffle migrate --reset
This will recompile our smart contract and deploy it to the local blockchain. To interact with the blockchain, we need to log in to the blockchain environment using Truffle.
truffle console
We can interact with the blockchain via truffle with Javascript. That's the amazing benefit of using a framework like Truffle. To connect to our Todo contract, we run
const t = await Todo.deployed();
This will instantiate an instance of our deployed contract. We can then use the object t to call the available functions in our smart contract. Note that the blockchain is a slow network. So we have to wait for the result of our calls with the keyword await.
To add a todo to our smart contract, we simply call the function addTodo() like so:
await t.addTodo("Solidity is fun");
This will create a new to-do in our smart contract mapping todos. To get your todos, you can call the getTodos()
await t.getTodos();
Truffle makes it easy to connect and interact with your smart contract. If you've been following, I want you to attempt a challenge. This will give you some muscle memory on interacting with Truffle.
Challenge: Call the markAsCompleted() function and pass a todo id
If you did that correctly, congratulations ๐
Now we have seen how to set up and interact with a local blockchain running on your Mac. But that's not really the end goal. External users cannot interact with our smart contract. To make this possible, we need to deploy our smart contract to a real blockchain.
If you wish also to understand how to deploy a smart contract to a real blockchain, you can follow my blog to get notified when the article is published. Thanks for reading!
Subscribe to my newsletter
Read articles from Nnamdi Umeh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nnamdi Umeh
Nnamdi Umeh
Software engineer - Blockchain | JavaScript In my free time, I love to write about tech - My way of giving back to the community.