0% found this document useful (0 votes)
24 views1 page

Unit 3

Uploaded by

sy13052003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views1 page

Unit 3

Uploaded by

sy13052003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Mastering Truffle: A Comprehensive Guide for Smart Contract Development

Slide 1: Title Slide

Title: Mastering Truffle: A Comprehensive Guide for Smart Contract Development


Subtitle: Including JavaScript Basics

Slide 2: What is Truffle?

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

Slide 3: Why Use Truffle?

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

Slide 4: Introduction to JavaScript

Role of JavaScript:
Powers web development and blockchain integration
Allows interaction with Ethereum contracts via Web3.js

Slide 5: Variables and Data Types

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:

let name = "Alice"; // String


let age = 30; // Number
let isDeveloper = true; // Boolean

Slide 6: Functions in JavaScript

Explanation:
Functions are reusable blocks of code that perform specific tasks.
Code Example:

function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice"));

Slide 7: Objects and Arrays

Objects: Store key-value pairs.


Arrays: Store lists of values.
Code Example:

let person = { name: "Alice", age: 30 };


let numbers = [1, 2, 3, 4];

console.log(person.name); // Output: Alice


console.log(numbers[0]); // Output: 1

Slide 8: Control Flow

Explanation:
Control flow includes decision-making (if-else) and loops.
Code Example:

let age = 30;


if (age > 18) {
console.log("Adult");
} else {
console.log("Minor");
}

for (let i = 0; i < 5; i++) {


console.log(i);
}

Slide 9: JavaScript in the Browser vs. Node.js

Explanation:
Browser: JavaScript can manipulate web pages and interact with APIs.
Node.js: JavaScript can run server-side, handle files, and databases.

Slide 10: Introduction to Node.js

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.

Why is Node.js important?:

It enables JavaScript to be used for full-stack development (both frontend and backend).
Popular for building fast, scalable network applications.

Key Features:

Event-driven and non-blocking I/O.


Cross-platform compatibility (Windows, macOS, Linux).

Common Uses:

Web servers
RESTful APIs
Real-time applications (e.g., chat, gaming)

Slide 11: How Node.js Works

Single-Threaded Event Loop:

Node.js operates on a single thread but can handle many connections simultaneously via
asynchronous (non-blocking) callbacks.

Non-Blocking I/O:

Node.js handles I/O operations (reading/writing files, databases) asynchronously, allowing it to


process multiple requests at once without waiting for previous requests to complete.

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:

const http = require('http');

const server = http.createServer((req, res) => {


res.write('Hello, World!');
res.end();
});

server.listen(3000, () => {
console.log('Server is listening on port 3000');
});

Slide 12: Node.js vs Traditional Server Models

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:

In traditional models, multiple threads/processes are spawned to handle concurrent requests,


consuming more memory.
Node.js uses a single thread for all requests, making it lightweight and efficient.

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:

fs.readFile('file.txt', 'utf8', (err, data) => {


console.log(data); // Asynchronous, doesn't block execution
});

Slide 13: Node.js Modules

What are Modules?:


Node.js uses modules to organize and reuse code. Modules can be built-in (core modules like fs ,
http ) or custom.

Common Core Modules:

http : Used to create servers and handle HTTP requests.


fs : Used for file system operations like reading and writing files.
path : Provides utilities for working with file and directory paths.

Using Modules:

You can load modules using the require() function.

Code Example:

const fs = require('fs');

// Read a file asynchronously


fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});

Custom Modules:

You can create your own modules by exporting code from one file and importing it into another.

Example:

// greet.js (custom module)


module.exports = function greet(name) {
return `Hello, ${name}!`;
};

// app.js
const greet = require('./greet');
console.log(greet('Alice')); // Output: Hello, Alice!

Slide 14: npm (Node Package Manager)

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:

Thousands of reusable packages available in the npm registry.


Package version control and dependency management.

Common npm Commands:

npm install <package-name> : Installs a package locally in your project.


npm install -g <package-name> : Installs a package globally.
npm init : Initializes a new project with package.json .

Code Example:

# Initialize a new project


npm init

# Install a package locally


npm install express

# Example: Using Express in Node.js


const express = require('express');
const app = express();

app.get('/', (req, res) => {


res.send('Hello, Express!');
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

Setting Up Truffle

Slide 15: Installing Node.js and npm

Step-by-Step Guide:
1. Download Node.js from Node.js official site.
2. Follow installation steps.
Code Example:

node -v
npm -v

Slide 16: Installing Truffle

Command:
Install Truffle globally using npm.
Code Example:

npm install -g truffle

Slide 17: Creating a New Truffle Project

Command:
Use truffle init to create a new project.
Code Example:

truffle init

Slide 18: Directory Structure

Explanation:
Contracts: Contains Solidity smart contracts.
Migrations: Contains migration scripts for deployment.
Test: Contains testing files.

Writing and Compiling Smart Contracts

Slide 19: Introduction to Solidity

Explanation:
Solidity is a contract-oriented language used for writing smart contracts on Ethereum.

Slide 20: Writing Your First Smart Contract

Code Example:

pragma solidity ^0.8.0;

contract HelloWorld {
string public greeting = "Hello, World!";
}

Slide 21: Compiling Smart Contracts with Truffle

Command:

truffle compile

Slide 22: Understanding the ABI (Application Binary Interface)

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"
}
]

Deploying Smart Contracts

Slide 23: Introduction to Truffle Migrations

Explanation:
Migrations are scripts to deploy contracts to the blockchain.

Slide 24: Writing Migration Scripts

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).

Example: Writing a Migration Script

Sample Migration Script ( 2_deploy_contracts.js ):

const HelloWorld = artifacts.require("HelloWorld");

module.exports = function(deployer) {
// Deploy the HelloWorld contract
deployer.deploy(HelloWorld);
};

Explanation:

artifacts.require(“HelloWorld”): Loads the compiled contract.


deployer.deploy(HelloWorld): Deploys the HelloWorld contract to the blockchain.

Running truffle migrate

Command:
Run the migration command to deploy contracts.

truffle migrate

Output:

Truffle compiles the contract (if not already compiled).


It runs the migration scripts in sequence.
Contracts are deployed to the specified network (e.g., local or testnet).

Example: Migrating to a Local Blockchain (Ganache)

Setup:

Make sure Ganache is running on port 8545.

truffle-config.js should contain the local development network configuration:

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*", // Match any network id
}
}
};

Run Migration:

truffle migrate --network development

Output:

Compiling your contracts...


===========================
> Compiling ./contracts/HelloWorld.sol

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:

Contracts are deployed to the local blockchain.


The contract’s address and transaction hash are displayed.

Slide 25: Deploying Contracts to a Local Blockchain

Explanation:
Use Ganache as a local blockchain.
Code Example:

truffle migrate --network development

Slide 26: Configuring Networks in Truffle

Code Example:

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
}
}
};

Slide 27: Deploying Contracts to a Testnet

Explanation:
Deploy contracts to Ethereum testnets like Ropsten.
Code Example:

truffle migrate --network ropsten

You might also like