Best guide for Developing, Deploying and Testing Smart Contracts using truffle and ganache .
Overview:
In this guide, we’ll set up an environment that simulates the real Ethereum network without using the usual testnets provided by Ethereum, such as Sepolia or Goerli. Instead, our testnet will be local, using the Ganache client, which automatically mines blocks instantly. This simulates a real blockchain environment without waiting for actual block times, allowing for rapid testing and development. I prefer this method over traditional testnets because it eliminates the need to collect test Ether from faucets, saving time. However, this method has some drawbacks, which we’ll discuss later.
Truffle
Truffle is a comprehensive development framework for building, testing, and deploying smart contracts on Ethereum, streamlining the entire blockchain development process, we’ll link it to the ganache client for our tests rather than the actual Ethereum network ‘mainnet’.
Introduction:
In this guide, we’re not using any IDE. The primary tools are Windows PowerShell and Node Package Manager (npm)."
It might be unusual for beginners to work without any GUI but this method really helped me getting hand on hand with smart contract development using only CLI , so first of all you should install Node.js on your system here’s a link for downloading it :
you could see all versions with this command :
npm list
Following step
Now after setting all requirements lets start developing , Create your project folder and navigate to it as cited in the following screenshot
Truffle will create a new project folder in the specified folder with standard project folders :
Normally the ganache client should start RPC Listening on 127.0.0.1:8545(local host as defined in the truffle config)and you’ll have 10 accounts with 1000 ETH in each one of them as in the following screenshot.
when compiled successfully lets deploy it by running :
truffle migrate
we managed successfully to deploy the contract, Lets try now interacting with it to see if its working as intended or not ,In this concept truffle offer us a tool for interacting with the deployed contract , Its the truffle console.
Interacting with the deployed contract
To summarize, we deployed our contract successfully lets now interact with it , by running this command:
truffle console
you will enter in a JavaScript environment to interact with the deployed contract .here’s a code example to interact with the contract.
const Skaltuchet = await artifacts.require("Skaltuchet").deployed()
const contractAddress = await Skaltuchet.address
const contractBalance = await Skaltuchet.balanceOf(contractAddress)
console.log("Contract SKT balance:", web3.utils.fromWei(contractBalance.toString(), 'ether'), "SKT")
const accounts = await web3.eth.getAccounts()
const ownerAddress = accounts[0]
const ownerBalance = await Skaltuchet.balanceOf(ownerAddress)
console.log("Owner SKT balance:", web3.utils.fromWei(ownerBalance.toString(), 'ether'), "SKT")
const ownerEthBalance = await web3.eth.getBalance(ownerAddress)
console.log("Owner ETH balance:", web3.utils.fromWei(ownerEthBalance.toString(), 'ether'), "ETH")
You can save this code to file.js, then navigate to its directory and run: node file.js. Alternatively, you can execute it command by command in the Truffle console, as shown in the following screenshot.
SUMMARY:
In the previous part we handled interacting with the deployed smart contract and testing it via Truffle console, You can see Truffle with Ganache is quite a good tool for creating deploying, and testing smart contracts, But it has some challenging steps when you want to use deployed contracts on the real Ethereum network (mainnet 1) like using Uniswap router, for example, you would probably need to fork the deployed contract on the ganache client and use an infura provider.
SOCIAL SHARE CARD GENERATOR