Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 47 ยท Working With Contracts From Outside

Truffle, Ganache and web3.js: The Classic Toolchain

Set up and read the classic Truffle, Ganache and web3.js workflow: init, compile, migrate, networks, HDWalletProvider, call versus send, and what replaced it today.

Search for Solidity tutorials and you will find thousands that use Truffle, Ganache and web3.js. That trio was the standard toolkit from about 2016 to 2022. Truffle was a development framework (compile, test, deploy), Ganache a personal blockchain on your computer, and web3.js the JavaScript library for talking to a node. In 2023 the maintainers announced that Truffle and Ganache would be sunset, and most teams moved to Hardhat or Foundry (see Your Solidity Toolbox) with ethers or viem (see Talking to Contracts with JavaScript).

Why learn it anyway? Because older code bases, books and answers use it, and because the ideas (migrations, a local chain with funded accounts, an artifact containing the ABI, call versus send) are the same in every modern tool. This lesson walks through the classic workflow, marks what changed, and lets you run a tiny model of it.

The pieces

Tool Role Modern replacement
Truffle Project layout, compile, migrate (deploy), test Hardhat, Foundry
Ganache (and the older TestRPC) A local blockchain with 10 funded accounts; desktop app or CLI Hardhat Network, Anvil
web3.js JavaScript library: connect to a node, send transactions, call contracts ethers.js, viem
HDWalletProvider Signs transactions with a mnemonic when deploying to a public network Hardhat accounts, cast/forge scripts
Infura / Alchemy Hosted node access (an RPC URL) Still used

TestRPC was the first in-memory test chain; it became Ganache CLI and later Ganache. Every one of these starts a node that speaks the same JSON-RPC protocol, so a tool such as web3.js cannot tell them apart.

Setting up (the classic way)

You needed Node.js, Truffle and a local chain:

Terminal
npm install -g truffle          # the framework
npm install -g ganache          # the local chain (CLI form)
ganache                         # starts a chain at http://127.0.0.1:8545

Ganache prints ten accounts, each with 1000 test ETH, and their private keys. The 12-word mnemonic that generates them is also printed. Anyone with a mnemonic controls the accounts, which is fine for a throwaway chain and never acceptable for real money. A code editor such as Visual Studio Code with a Solidity extension rounds out the setup. The in-browser Remix IDE skips all installation and is the fastest way to start.

A Truffle project

Create a project and a contract:

Terminal
mkdir hello-world
cd hello-world
truffle init                     # creates contracts/, migrations/, test/, truffle-config.js
truffle create contract HelloWorld
truffle compile                  # writes build/contracts/HelloWorld.json
truffle migrate                  # deploys using the files in migrations/

truffle init creates this layout:

TEXT
contracts/          your .sol files
migrations/         numbered deployment scripts (1_initial_migration.js, 2_deploy.js, ...)
test/               tests in JavaScript or Solidity
truffle-config.js   networks and compiler settings

A migration is a script that deploys contracts and runs in numeric order. Truffle remembers which ones have run on each network (in a small Migrations contract), so re-running truffle migrate only runs new scripts.

JavaScript
// migrations/2_deploy_hello.js
const HelloWorld = artifacts.require("HelloWorld");

module.exports = function (deployer) {
  deployer.deploy(HelloWorld, "Hello, chain!");   // constructor arguments follow the contract
};

Compilation writes an artifact build/contracts/HelloWorld.json containing the ABI, the bytecode and, after migrating, the deployed address for each network. Tests and front ends read the ABI from there. Hardhat and Foundry have the same concept with different folder names.

Configuring networks

truffle-config.js lists the networks you can deploy to:

JavaScript
require("dotenv").config();
const HDWalletProvider = require("@truffle/hdwallet-provider");

module.exports = {
  networks: {
    development: { host: "127.0.0.1", port: 8545, network_id: "*" },   // Ganache
    sepolia: {
      provider: () =>
        new HDWalletProvider(process.env.MNEMONIC, process.env.RPC_URL),
      network_id: 11155111,
      gas: 5500000,
    },
  },
  compilers: { solc: { version: "0.8.24" } },
};

Deploy with truffle migrate --network sepolia (or --network main for the real network, if you named it). Notes:

  • Secrets stay out of the code. The mnemonic and RPC URL come from a .env file read by the dotenv package, and .env must be listed in .gitignore. A mnemonic committed to a public repository is drained by bots within minutes.
  • Test networks change. Tutorials from the early 2020s deploy to Goerli and use a "Goerli faucet" to get free test ETH. Goerli was shut down in 2024. Today you would use Sepolia (or Holesky/Hoodi for staking tests) and a Sepolia faucet. The workflow is identical; only the network name and chain ID change. The details are in Deploying to Testnet and Mainnet.
  • A wallet such as MetaMask holds the test accounts you use in a browser. It is configured to point at a local chain or at a test network.

Compiling and deploying without a framework

Truffle is not magic. You can compile with the solc package and deploy through web3.js yourself:

JavaScript
// compile.js
const solc = require("solc");
const fs = require("fs");

const input = {
  language: "Solidity",
  sources: { "Example.sol": { content: fs.readFileSync("Example.sol", "utf8") } },
  settings: { outputSelection: { "*": { "*": ["abi", "evm.bytecode"] } } },
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
const contract = output.contracts["Example.sol"].Example;
module.exports = { abi: contract.abi, bytecode: contract.evm.bytecode.object };
JavaScript
// deploy.js
const { Web3 } = require("web3");
const { abi, bytecode } = require("./compile");

const web3 = new Web3("http://127.0.0.1:8545");
const [deployer] = await web3.eth.getAccounts();

const instance = await new web3.eth.Contract(abi)
  .deploy({ data: "0x" + bytecode, arguments: [42] })
  .send({ from: deployer, gas: 1_000_000 });

console.log("deployed at", instance.options.address);

This is exactly the pipeline of "compile, then send a transaction with no recipient whose data is the bytecode and the constructor arguments". See ABI Encoding and Low-Level Calls for what the data field contains.

web3.js: talking to a deployed contract

To use a contract you need three things: a provider (the node URL), the ABI (the list of functions) and the address.

JavaScript
const { Web3 } = require("web3");
const web3 = new Web3("http://127.0.0.1:8545");        // Ganache or any node

const counter = new web3.eth.Contract(abi, address);

// Reading: free, no transaction, no gas, no signature
const value = await counter.methods.count().call();

// Writing: a real transaction, costs gas, needs a sender
const receipt = await counter.methods.increment().send({ from: accounts[0] });
console.log(receipt.events);   // decoded events from the logs

The key distinction is call versus send. A call runs the function locally on the node against the current state and returns the result; nothing is recorded. A send is a signed transaction that changes state and appears in a block. This matches the view/pure and state-changing split you met in Functions and Visibility. Other commonly used pieces are web3.eth.getBalance(address), web3.utils.toWei("1", "ether"), and contract.events.EventName() for subscribing to logs (Events and Logs).

web3.js changed its API between major versions (1.x to 4.x, for example the Web3 class became a named export), another reason old tutorials sometimes fail on a fresh install. Always check the version your code was written for.

Try it: a tiny migration runner

The real tools need a network and a compiler, but their logic is easy to model. This stand-in keeps a list of numbered migration scripts, runs only those that have not run yet, and writes an artifact with the deployed address, the way Truffle does.

JS Numbered migrations and artifacts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: call versus send

The second model is a local chain with accounts that hold test ETH and a counter contract. It shows that a call is free and changes nothing, a send costs gas and changes state, and that a send needs a funded sender.

JS web3-style call and send

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the commands, project layout, config shape, migration order, call versus send, and the Truffle/Ganache sunset describe the classic workflow. What is the stand-in: the code blocks for Truffle and web3.js are shown read-only because they need Node, a compiler and a node; the two models above are plain JavaScript with made-up addresses and gas numbers, and do not talk to any blockchain.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does Ganache provide?

    Ganache (and its predecessor TestRPC) runs a local, resettable chain with pre-funded accounts so you can develop without spending real money.
  2. 2. In web3.js, what is the difference between call() and send()?

    A call simulates the function on a node and returns its result. A send creates a real transaction that is mined into a block.
  3. 3. Why must a .env file with a mnemonic be listed in .gitignore?

    A mnemonic generates the private keys. Publishing one means losing any funds on its accounts, usually within minutes.
  4. 4. What is a Truffle migration?

    Migrations run in numeric order, and Truffle records which have already run on each network.
  5. 5. What replaced the Goerli test network for most developers?

    Goerli was shut down, and Sepolia became the standard public test network for application developers.

Summary

  • The classic stack was Truffle (project, compile, migrate, test), Ganache or TestRPC (local chain) and web3.js (client library). Truffle and Ganache are sunset; Hardhat, Foundry, ethers and viem carry the same ideas.
  • truffle init, create contract, compile and migrate produce artifacts with the ABI, bytecode and deployed addresses; networks and the compiler version live in truffle-config.js.
  • Use HDWalletProvider with a mnemonic and RPC URL from .env for public networks, and keep secrets out of version control. Goerli is gone; use Sepolia.
  • You can compile with solc and deploy with web3.js yourself: deployment is just a transaction with no recipient and the bytecode as data.
  • contract.methods.fn().call() reads for free; .send({ from }) writes and pays gas.

Next: take a contract to a public test network in Deploying to Testnet and Mainnet.