0% found this document useful (0 votes)
7 views21 pages

Unit 5 BC Appli

The document provides an overview of smart contracts, detailing their definition, properties, components, and interaction within blockchain environments. It discusses the programming languages used, the workflow of creating and deploying smart contracts, and compares smart contracts with traditional legal contracts. Additionally, it introduces the Truffle framework for developing and managing smart contracts on Ethereum, outlining its architecture and common issues faced during development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

Unit 5 BC Appli

The document provides an overview of smart contracts, detailing their definition, properties, components, and interaction within blockchain environments. It discusses the programming languages used, the workflow of creating and deploying smart contracts, and compares smart contracts with traditional legal contracts. Additionally, it introduces the Truffle framework for developing and managing smart contracts on Ethereum, outlining its architecture and common issues faced during development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT 5

BLOCKCHAIN
APPLICATIONS
Smart contracts, Truffle Design and issue-Blockchain Applications in
Supply Chain Management, Logistics, Smart Cities, Finance and Banking,
Insurance,etc- Case Study.
Smart Contract
Smart Contract
• A smart contract is a computer program stored on a blockchain that
automatically executes actions (like transferring money, verifying a
condition, or issuing a token) when predefined conditions are met.
• A smart contract is simply a program that runs on the Ethereum
blockchain (or similar blockchains).
• It consists of:
• Code (its functions and logic)
• Data (its state or variables)
• It resides at a specific address on the blockchain, called the contract
address.
Key Properties of Smart Contracts
Property Meaning
Automatically runs when triggered.(when
Self-executing predefined conditions are met.
Once deployed, cannot be changed(unless special
Immutable
upgrade mechanisms are built-in.)
Distributed Stored across all nodes on a blockchain.
Transparent Anyone can verify the contract and its execution.
Trustless No need to trust intermediaries or middlemen.
Secure Cryptographically protected and tamper-proof.

Essentially a series of conditional ("if-then")


If-Then Logic statements coded in the blockchain.
Every node on the blockchain re-executes the
Consensus Validation contract to validate and remain consistent.
Components of a Smart Contract
pragma solidity ^0.8.0;

Component Meaning contract SimplePayment {


Define what actions the contract can perform (e.g., address public owner;
Code (Functions)
transfer funds, issue tokens).
Variables that hold current values (e.g., user balances, constructor() {
State (Data) owner = msg.sender;
voting counts).
Unique location on the blockchain where the smart }
Contract Address
contract is deployed.
function pay() public payable {}

function withdraw() public {


Events: Notification signals for external listening (e.g., UI updates). require(msg.sender == owner, "Only
owner can withdraw");
Modifiers: Conditions placed on functions (e.g., only the owner can
call a function). payable(owner).transfer(address(this).ba
lance);
}
Constructor: A special function that runs once when the contract is
}
deployed.
Interaction with Smart Contracts
• Applications or Users can:
• Call functions
• Change the contract’s state (if permitted)
• Initiate transactions
• Smart contracts are triggered when a user sends a transaction calling
one of its functions.

Smart Contract Programming Languages


• Solidity (most popular)
• Vyper (more secure, stricter version)
• (Others: Rust and Go in different blockchain systems like Solana or Hyperledger
Fabric.)
Once written, the smart contract code is:
• Compiled into bytecode
• Deployed and executed by the Ethereum Virtual Machine (EVM).
How a Smart Contract Works
• Create Contract:
Developer writes code (typically in Solidity for Ethereum).
• Deploy to Blockchain:
Upload the compiled contract code onto the blockchain.
• Trigger Conditions:
When certain inputs/events occur, the contract automatically executes.
• Execute Actions:
Actions such as transferring tokens, sending notifications, updating a
ledger, etc., happen automatically.
• Record Outcomes:
The result of the execution is recorded immutably on the blockchain.
Contract Creation
• A developer writes the smart contract code (using languages like Solidity for
Ethereum).
• Code defines: if (payment_received) {
release_product_to_buyer();
• Rules (conditions and triggers) }
• Actions (what should happen if conditions are true)
• Storage (important data to save)
Contract Deployment
• The smart contract is compiled into bytecode.
• It is deployed to the blockchain using a transaction.
• Once deployed, the smart contract gets a unique address on the blockchain.
• From this point, the contract exists permanently (immutable).
Interaction with Contract
• Users interact with the contract by sending transactions.
• Transactions call specific functions inside the contract.
• Example:
• You click a button on a DApp → the app uses Web3.js to call a smart
contract function → the blockchain records this action

Condition Checking (Triggering)


• When a function is called:
• The contract checks if predefined conditions are satisfied.
• Conditions are logical statements coded into the contract.
require(balance[msg.sender] >= amount, "Insufficient funds");
If the condition fails, the transaction reverts (fails without changes).
If it passes, the action proceeds.
Automatic Execution User (frontend app)
• Once conditions are validated: ↓
Sends Transaction (function call)
• Smart contract executes the action automatically. ↓
Smart Contract (code executes)
• Examples of actions: ↓
• Transfer cryptocurrency Condition Verification (IF statements, require(),
etc.)
• Update ownership ↓
• Mint an NFT Automatic Action Execution (token transfer,
• Record a shipment status ownership update, etc.)

• Execution is deterministic — Blockchain Ledger Update (immutable record)

same input always gives the same output.

Result Recording
• The blockchain stores the result of the execution permanently.
• Every node in the blockchain network updates its copy of the ledger.
Key Characteristics During Functioning
Feature Explanation
Deterministic Every node executes the contract the same way.
Immutable Once deployed, logic cannot be changed easily.
No need to trust a third party; blockchain enforces
Trustless
rules.
After deployment, no one needs to manage it
Autonomous
manually.
Executing smart contract functions costs gas (ETH in
Gas Fee Required
Ethereum).

Smart Contracts vs Traditional Legal Contracts


Aspect Traditional Contract Smart Contract
Medium Paper or digital document Blockchain code
Enforcement Courts, legal systems Blockchain network
Can involve subjective terms (e.g., Only objective, programmable
Complex Conditions
"good faith") conditions
Automation Manual enforcement Fully automated execution
+---------------------------------------------------------+
Smart Contract Architecture | Blockchain Platform (Ethereum, etc.) |
| |
| +-----------------------------------------------------+ |
|| Smart Contract (Code Layer) ||
| | - Functions (Business Logic) ||
| | - State Variables (Storage) ||
| | - Events (Notification triggers) ||
| +-----------------------------------------------------+ |
| |
| +-----------------------------------------------------+ |
| | Smart Contract Storage (Immutable Data, Ledger)
||
| +-----------------------------------------------------+ |
| |
+---------------------------------------------------------+
DApp and Smart Contract Interaction Diagram
+-----------------+ +------------------+ +----------------------+
| Frontend (DApp) | <----HTTP---->| Web3.js / Ethers.js|<--RPC---> | Ethereum Blockchain |
| (React, Angular)| | Wallet Integration | | (Smart Contracts) |
+-----------------+ +------------------+ +----------------------+
|
(MetaMask, WalletConnect for signing)
Types of Smart Contracts
• Smart contracts can be classified based on function, application
domain, or execution characteristics.
Based on Functionality
Type Description Example
Executes automatically once
Escrow payments: Auto-release of
Self-executing Contracts conditions are met without human
funds upon delivery.
intervention.
Contract monitors compliance and
Loan smart contract charging
Self-enforcing Contracts enforces penalties/rewards
penalties if EMI unpaid.
automatically.
Legally enforceable contracts Digital mortgage contracts (signing
Smart Legal Contracts where code represents a legal and transferring property
agreement. ownership).
Based on Control
Type Description Example
Open to anyone to interact Uniswap, Aave smart contracts
Public Smart Contracts
(deployed on public blockchains). (anyone can use).
Access is restricted to authorized Supply chain contracts limited to
Private Smart Contracts
parties only. company participants.
Only selected validators/actors can Hyperledger Fabric chaincode
Permissioned Smart Contracts
execute or validate transactions. smart contracts.

Based on Application Area

Domain Example Smart Contract


Finance (DeFi) Lending, Borrowing, Staking, Insurance payout.
Healthcare Patient health records management.
Real Estate Property sale transfer automation.
Logistics Shipment tracking and payment release.
Gaming (NFTs) Ownership of digital assets.
Smart Contract Workflow +----------------+ +---------------------+ +-----------------+
| User/Application| -----> | Blockchain (Smart |----> | Execute
Contract |
| (Web/App) | | Contract Deployment) | | Actions
|
+----------------+ +---------------------+ +-----------------+
|
v
+-------------------+
| Store Data/Events |
Flow: +-------------------+
User → Trigger Smart Contract → Execute Logic → Store Results
Truffle Design and issue
Truffle Framework: Design &
Architecture
• Truffle is a development environment, testing framework, and asset
pipeline for Ethereum. It simplifies the smart contract lifecycle from
development to deployment.
Components:
• Truffle: Development environment
• Ganache: Personal blockchain for testing
• Drizzle: Front-end library

• Truffle = Developer Toolkit for Ethereum Smart Contracts


Truffle Design (Architecture)
Component Description
Command-line interface to run compile, migrate,
Truffle CLI test commands.
A standard folder layout to manage contracts,
Truffle Project Structure
migrations, tests, and configs.
Scripts to deploy contracts step-by-step to
Truffle Migrations different blockchains.
A REPL (interactive terminal) to interact with
Truffle Console deployed contracts directly.
Built-in testing using JavaScript (Mocha/Chai
Truffle Test frameworks).
Settings for networks, compiler version, gas
Truffle Config File (truffle-config.js)
limits, etc.
Truffle Design (Architecture)
Design Flow: project/

├── contracts/ # Solidity smart contracts
• Write smart contracts (.sol files). │ └── Migrations.sol
• Compile contracts (truffle compile). │
• Deploy contracts (truffle migrate). ├── migrations/ # JavaScript deployment scripts
│ └── 1_initial_migration.js
• Test contracts (truffle test). │
• Interact with contracts from DApps. ├── test/ # Unit tests (Mocha/Chai)
│ └── test_contract.js

├── truffle-config.js # Project configuration
How Truffle Works (Design Workflow)

•Write Contract → Create .sol files inside contracts/


•Compile Contract → truffle compile
•Converts Solidity into EVM bytecode + ABI.
•Write Migration → Create deployment scripts inside migrations/
•Deploy Contract → truffle migrate
•Deploys contract to configured blockchain (Ganache, Testnet, Mainnet).
•Test Contract → truffle test
•Use JS-based test cases to ensure logic works.
•Interact with Contract → truffle console
•Real-time function calling from command-line.
Common Issues in Truffle Development
Issue Reason Solution
Using wrong Solidity compiler version for Set compilers version in truffle-
Version Mismatch your contract. config.js
Network Configuration Correct the networks block in truffle-
Wrong network ID, RPC URL, or port.
Error config.js
Increase gas limit in deployment
Gas Estimation Fails Contract needs more gas than estimated. settings.
Optimize contract code and
Out of Gas Errors Infinite loops or expensive computations. functions.
Wrong dependencies/order in migration Ensure contract addresses are correct
Migration Failures scripts. and dependencies deployed first.
Testing or deployment can't find compiled Make sure truffle compile succeeded,
Artifacts Not Found contract. clean with truffle migrate --reset.
Ganache Connection Start Ganache manually or automate
Refused Local blockchain not running. it.
Testing Errors Always await smart contract function
(async/await) Promise handling mistakes in JavaScript tests. calls.

You might also like