Learn TRUFFLE with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.