From Solidity to Blockchain: A Beginner’s Guide to Smart Contract Deployment with Truffle and Ganache
Description:
Embark on your journey from Solidity code to the blockchain realm with our beginner-friendly guide to deploying smart contracts using Truffle and Ganache. This comprehensive tutorial walks you through each step, demystifying the process and empowering you to bring your decentralized applications to life seamlessly.
Description of the device used
- Ubuntu 22.04.3 LTS
Requirements used on the device:
- Truffle v5.11.5 (core: 5.11.5)
- Ganache v7.9.1
- Solidity — 0.8.21 (solc-js)
- Node v16.20.2
- Web3.js v1.10.0
The steps are as follows:
Create a new folder for the project and open it in visual studio code
This folder will be created for the truffle project that will be created.
mkdir project-name
cd project-name
code .
Download the truffle extension for VS Code
to make the process easier, download and use the “Truffle for VSCode” extension, you can download directly at vscode or at the link here
Download and customize the requirements based on what is listed on the startup extension
After you install it, you will be shown some related software installation requirements for some future processes. So, you can customize the related software needs before proceeding to the next process.
Create a new network for the ganache server
If everything is installed, the next process is to create a ganache server to deploy the smart contract later.
In this process I followed the default advice to create a new network.
Start the network
if the network has been created, start it by right-clicking the network name, then click start ganache
You can see that the ganache server has started with the check in output (left side of the terminal in vscode)
Initiate truffle project
to start the truffle project, you can open a terminal in vscode and run the command below
truffle init
after running the command, look in vscode explorer, you can see some folders and files, including truffle-config.js has been created automatically, this is a set of truffle project
Create a smart contract
next process, you can create a smart contract with the execute command below, change “contractName” with your contract name
truffle create contract contractName
here iam create 2 contract with name TransactionContract and TransactionDetailContract
after the command is executed, look at the “contracts” folder, you can see a new smart contract with the extension name .sol has been created.
Okay, now customize the smart contract that was just created as needed.
Network configuration in the truffle-config.js file
Next, set the network configuration in truffle-config.js so that it can connect to the ganache server that was run earlier
Note: customize the port, hostname like the previously created ganache server
Create a new migration
create a new migration to deploy to the ganache server by executing the command below, replace “migrationName” with the name of the migration to be created.
truffle create migration migrationName
because earlier I created 2 smart contracts, then here I create 2 migrations
after the command is executed, it will create a new file in the “migration” folder, this is the migration file
Open it completely and adjust it to the needs of the smart contract that will be deployed
Run the migration
After the migration file has been adjusted, the next process is to run the migration for the smart contract deployment process by executing the command below.
truffle migrate
Smart contract successfully deployed
done, the smart contract was successfully deployed on the ganache server, to see the contract address you can see it in the terminal at “contract address”
If the terminal has been cleared, it can be seen in ./build/contracts, find the word “address”
and if you want to monitor activity on the ganache server that has been running, can be checked in the output on the left side of the terminal on vscode