Learn Truffle - 10 Code Examples & CST Typing Practice Test
Truffle is a comprehensive development framework for Ethereum, providing tools for smart contract compilation, deployment, testing, and network management.
View all 10 Truffle code examples →
Learn TRUFFLE with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Truffle Simple Smart Contract Deployment
// contracts/Counter.sol
pragma solidity ^0.8.0;
contract Counter {
uint256 public count = 0;
function increment() public { count += 1; }
}
// migrations/2_deploy_contracts.js
const Counter = artifacts.require('Counter');
module.exports = function(deployer) {
deployer.deploy(Counter);
};
// Run using `truffle migrate`
A minimal example showing how to deploy a simple smart contract using Truffle.
Truffle Interact with Contract
const Counter = artifacts.require('Counter');
module.exports = async function(callback) {
const counter = await Counter.deployed();
console.log('Initial count:', (await counter.count()).toString());
await counter.increment();
console.log('Updated count:', (await counter.count()).toString());
callback();
};
Interact with a deployed Truffle contract using JavaScript.
Truffle Sending Ether
const Payment = artifacts.require('Payment');
module.exports = async function(callback) {
const payment = await Payment.deployed();
await payment.sendTransaction({ from: web3.eth.accounts[0], value: web3.utils.toWei('1', 'ether') });
console.log('Payment sent');
callback();
};
Send Ether to a contract using Truffle's JS interface.
Truffle Event Listening
const Counter = artifacts.require('Counter');
module.exports = async function(callback) {
const counter = await Counter.deployed();
counter.CounterIncremented().on('data', event => {
console.log('Event:', event.returnValues);
});
callback();
};
Listen to events emitted by a Truffle contract.
Truffle Contract with Constructor
// contracts/Greeter.sol
pragma solidity ^0.8.0;
contract Greeter {
string public greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
}
// migrations/2_deploy_contracts.js
const Greeter = artifacts.require('Greeter');
module.exports = function(deployer) {
deployer.deploy(Greeter, 'Hello Truffle');
};
Deploy a contract with constructor parameters using Truffle.
Truffle Call View Function
const Greeter = artifacts.require('Greeter');
module.exports = async function(callback) {
const greeter = await Greeter.deployed();
const message = await greeter.greeting();
console.log('Greeting:', message);
callback();
};
Call a read-only function from a deployed contract using Truffle.
Truffle Sending Tokens (ERC20)
const MyToken = artifacts.require('MyToken');
module.exports = async function(callback) {
const token = await MyToken.deployed();
await token.transfer(web3.eth.accounts[1], 100);
console.log('Tokens transferred');
callback();
};
Interact with an ERC20 token contract deployed via Truffle.
Truffle Gas Estimation
const Counter = artifacts.require('Counter');
module.exports = async function(callback) {
const counter = await Counter.deployed();
const gas = await counter.increment.estimateGas();
console.log('Estimated gas:', gas);
callback();
};
Estimate gas usage for a contract function call.
Truffle Reading Past Events
const Counter = artifacts.require('Counter');
module.exports = async function(callback) {
const counter = await Counter.deployed();
const events = await counter.getPastEvents('CounterIncremented', { fromBlock: 0, toBlock: 'latest' });
console.log(events);
callback();
};
Fetch past events emitted by a deployed contract.
Truffle Sending Transaction with Value
const Payment = artifacts.require('Payment');
module.exports = async function(callback) {
const payment = await Payment.deployed();
await payment.pay({ from: web3.eth.accounts[0], value: web3.utils.toWei('0.5', 'ether') });
console.log('Payment transaction completed');
callback();
};
Send a transaction with Ether to a function that is payable.
Frequently Asked Questions about Truffle
What is Truffle?
Truffle is a comprehensive development framework for Ethereum, providing tools for smart contract compilation, deployment, testing, and network management.
What are the primary use cases for Truffle?
Compile and migrate smart contracts. Test smart contracts using automated frameworks. Manage Ethereum network configurations. Interact with deployed contracts. Integrate with frontend dApps
What are the strengths of Truffle?
Simplifies Ethereum project management. Comprehensive suite of development tools. Supports automated and repeatable deployments. Facilitates contract testing and debugging. Integrates with Ganache for quick development cycles
What are the limitations of Truffle?
Primarily Ethereum-focused. Requires understanding of Node.js environment. Less modular than libraries like Ethers.js. Testing syntax may require additional setup. Heavier framework for small, simple projects
How can I practice Truffle typing speed?
CodeSpeedTest offers 10+ real Truffle code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.