Truffle Development Environment Setup
Truffle Suite is one of the oldest smart contract development frameworks: compiler, migrations, test runner, and Ganache for local network. Despite Hardhat and Foundry taking most new projects, Truffle remains in production at hundreds of projects launched before 2021. Setup from scratch looks different from three-year-old tutorials.
Configuring truffle-config.js for Modern Projects
Key point — network and provider. To work with Infura or Alchemy:
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,
`https://sepolia.infura.io/v3/${process.env.INFURA_KEY}`
),
network_id: 11155111,
gas: 5500000,
confirmations: 2,
timeoutBlocks: 200,
skipDryRun: true
}
},
compilers: {
solc: {
version: "0.8.20",
settings: {
optimizer: { enabled: true, runs: 200 },
viaIR: true // through Yul IR — better for complex contracts
}
}
},
plugins: ["truffle-plugin-verify"]
};
runs: 200 is a tradeoff between deployment cost and call cost. For contracts with high call frequency increase to 1000+.
Migrations: Order Matters
Migration files are numbered: 1_initial_migration.js, 2_deploy_token.js. Truffle tracks executed migrations via the Migrations contract on-chain. If a migration already ran — it's skipped on retry.
Typical migration with dependencies between contracts:
const Token = artifacts.require("MyToken");
const Staking = artifacts.require("StakingContract");
module.exports = async function(deployer, network, accounts) {
await deployer.deploy(Token, "MyToken", "MTK", web3.utils.toWei("1000000"));
const token = await Token.deployed();
await deployer.deploy(Staking, token.address, accounts[0]);
};
Ganache Integration
Ganache 7.x runs as a package (@ganache/core) or CLI (ganache). For deterministic tests — fix the seed:
ganache --seed 42 --accounts 10 --defaultBalanceEther 1000
Or via ganache.fork for forking mainnet — like hardhat node --fork.
Tests in JavaScript and Mocha
Truffle uses Mocha + Chai. Contracts are available via artifacts.require. Async/await is supported:
const Token = artifacts.require("MyToken");
contract("MyToken", accounts => {
it("mints initial supply to deployer", async () => {
const token = await Token.deployed();
const balance = await token.balanceOf(accounts[0]);
assert.equal(balance.toString(), web3.utils.toWei("1000000"));
});
});
Verify contract after deployment via truffle-plugin-verify:
truffle run verify MyToken --network sepolia
Timeline Estimates
Setting up Truffle from scratch: 2-3 hours. Includes network configuration, Ganache, basic migrations, test environment, and verification plugin.







