Unit 3
Unit 3
Definition:
Truffle is a popular development framework for Ethereum, allowing developers to compile, migrate,
and test smart contracts easily.
Key Features:
Smart contract compilation
Contract migration and deployment
Built-in testing framework
Network management for deploying to various Ethereum networks
Benefits:
Simplifies blockchain development
Automates testing and migration
Supports multiple Ethereum networks
Comparison with Other Frameworks:
Truffle vs Hardhat: Truffle has better out-of-the-box testing and migration tools.
Truffle vs Embark: Truffle is easier for newcomers to Ethereum development.
JavaScript Fundamentals
Role of JavaScript:
Powers web development and blockchain integration
Allows interaction with Ethereum contracts via Web3.js
Explanation:
Variables store data that can be used later in a program. JavaScript supports several data types.
Types:
Strings, Numbers, Booleans, Arrays, Objects
Code Example:
Explanation:
Functions are reusable blocks of code that perform specific tasks.
Code Example:
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice"));
Explanation:
Control flow includes decision-making (if-else) and loops.
Code Example:
Explanation:
Browser: JavaScript can manipulate web pages and interact with APIs.
Node.js: JavaScript can run server-side, handle files, and databases.
Explanation:
Node.js is a runtime for executing JavaScript outside the browser, commonly used for backend and
blockchain development.
Code Example:
node --version
Definition:
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, allowing developers to run
JavaScript on the server side.
It enables JavaScript to be used for full-stack development (both frontend and backend).
Popular for building fast, scalable network applications.
Key Features:
Common Uses:
Web servers
RESTful APIs
Real-time applications (e.g., chat, gaming)
Node.js operates on a single thread but can handle many connections simultaneously via
asynchronous (non-blocking) callbacks.
Non-Blocking I/O:
Event-Driven Architecture:
Node.js is built around an event-driven architecture, meaning it responds to events (e.g., HTTP
requests) with callback functions.
Code Example:
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});
Blocking vs Non-Blocking:
Traditional servers like Apache use blocking I/O, where each request blocks the thread until it
finishes.
Node.js uses non-blocking I/O, meaning it can handle many requests concurrently without
pausing other operations.
Concurrency:
Code Comparison:
Blocking I/O:
const fs = require('fs');
const data = fs.readFileSync('file.txt', 'utf8'); // Blocks execution
console.log(data);
Non-Blocking I/O:
Using Modules:
Code Example:
const fs = require('fs');
Custom Modules:
You can create your own modules by exporting code from one file and importing it into another.
Example:
// app.js
const greet = require('./greet');
console.log(greet('Alice')); // Output: Hello, Alice!
What is npm?:
npm is the default package manager for Node.js. It allows developers to install, update, and
manage external libraries (packages) to enhance the functionality of their applications.
Key Features:
Code Example:
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Setting Up Truffle
Step-by-Step Guide:
1. Download Node.js from Node.js official site.
2. Follow installation steps.
Code Example:
node -v
npm -v
Command:
Install Truffle globally using npm.
Code Example:
Command:
Use truffle init to create a new project.
Code Example:
truffle init
Explanation:
Contracts: Contains Solidity smart contracts.
Migrations: Contains migration scripts for deployment.
Test: Contains testing files.
Explanation:
Solidity is a contract-oriented language used for writing smart contracts on Ethereum.
Code Example:
contract HelloWorld {
string public greeting = "Hello, World!";
}
Command:
truffle compile
Explanation:
The ABI is used to interact with contracts from external apps (e.g., Web3.js).
Example:
[
{
"constant": false,
"inputs": [],
"name": "greet",
"outputs": [{ "name": "", "type": "string" }],
"type": "function"
}
]
Explanation:
Migrations are scripts to deploy contracts to the blockchain.
Here’s a detailed slide explaining the truffle migrate command, along with an example:
Purpose:
The truffle migrate command is used to deploy smart contracts to a specific network (e.g.,
local blockchain, testnet, or mainnet).
It automates the deployment process by running migration scripts.
Default Behavior:
Truffle runs all migration files located in the migrations directory in sequential order.
Contracts are deployed to the network specified in truffle-config.js .
Migration Scripts:
Migration scripts define how contracts should be deployed. Each script is responsible for a
specific deployment task (e.g., deploying a contract, upgrading a contract).
module.exports = function(deployer) {
// Deploy the HelloWorld contract
deployer.deploy(HelloWorld);
};
Explanation:
Command:
Run the migration command to deploy contracts.
truffle migrate
Output:
Setup:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*", // Match any network id
}
}
};
Run Migration:
Output:
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x...
> Blocks: 0 Seconds: 0
> contract address: 0x...
> block number: 1 block timestamp: ...
> account: 0x...
> gas used: 261495
> gas price: 20 gwei
2_deploy_contracts.js
=====================
Deploying 'HelloWorld'
----------------------
> transaction hash: 0x...
> contract address: 0x...
> gas used: 181234
> gas price: 20 gwei
Summary
=======
> Total deployments: 2
Result:
Explanation:
Use Ganache as a local blockchain.
Code Example:
Code Example:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
}
}
};
Explanation:
Deploy contracts to Ethereum testnets like Ropsten.
Code Example: