Blockchain and Smart Contracts

Sabin Nepal

@neymarsabin

About Me!

Blockchain | System and Operations | AI

  • Software Developer with 7+ Years of Experience
  • Blockchain, AI & Programming
  • Studying AI at Kathmandu University
  • Ruby, Python, JavaScript, Go, Solidity
@neymarsabin

Things we will cover today

  • Blockchain Basics
  • Bitcoin and Ethereum Overview
  • Smart Contracts and EVM
  • Wallets and Transactions

What is blockchain?

A magic notebook everyone shares, everyone can write to but no one can erase.

@neymarsabin

Before Bitcoin! The Trust Problem

  • Online transactions rely on middlemen (banks, companies)
  • Central points can be hacked, corrupted, censored
  • "What happens if your bank decides to freeze your account?"
  • "Can you send money to someone in another country without a bank?"

@neymarsabin

Then Came Bitcoin, 2008...

We don't need middlemen if we can trust the math.
@neymarsabin

10,000 BTC for 2 Pizzas, 2010

@neymarsabin

Vitalik Buterin and Ethereum, 2013

Bitcoin is a cool currency. But what if blockchain could run anything like apps, contracts, games, organizations - without a central authority?
  • Launched Ethereum in 2015
  • Turning the blockchain into a global World Computer
  • Major flood of Smart Contracts, DeFi, NFTs, MetaVerse etc.

@neymarsabin

Why Blockchain?

  • Decentralization: No single point of control or failure
  • Transparency: All transactions are visible to everyone
  • Security: Cryptographic techniques ensure data integrity
  • Immutability: Once data is added, it cannot be changed or deleted
  • Trustless: No need to trust a third party; trust the code and math
  • Permissionless: Anyone can join and participate in the network

@neymarsabin

Blocks-Chain

@neymarsabin

Consensus Mechanisms

  • Proof of Work (PoW) -> miners solve complex puzzles to validate transactions (Bitcoin, Ethereum (pre-Merge))
  • Proof of Stake (PoS) -> validators stake tokens to propose and validate blocks (Ethereum (post-Merge), Cardano)
  • Proof of Authority (PoA) -> trusted validators validate transactions (VeChain, POA Network)
  • Others -> Proof of History (Solana)

Proof of Work(PoW)

  • A consensus mechanism where a random node (miner) creates the next block solving a computational puzzle
H( No \\ p_hash || T1 || T2 || ... || Tn ) < Target
  • No = nonce (a random value that miners change to find a valid hash) with brute force method
  • p_hash = previous block's hash
  • Tx = transactions in the block
  • Target = a value that the hash must be less than to be considered valid

Anatomy of a Block

Then, What is Blockchain?

A magic notebook everyone shares and no one can erase.
A digital database or ledger that is distributed among the nodes of a peer-to-peer network.
An append only state machine replicated across a network of computers.
Fault-tolerant, incentive-driven consensus protocol for decentralized systems.

@neymarsabin

Where is Blockchain used?

  • Supply Chain Transparency -> track products from origin to consumers
  • Digital Identity -> secure, verifiable IDs for individuals without centralized databases
  • HealthCare Records -> Share patient data across hospitals
  • Cross-Border Payments -> Fast, low-fee cross border payments
  • Intellectual Property and Royalities, Energy Trading

@neymarsabin

Blockchain Networks

  • Each Chain = a different universe with its own rules
  • Think like linux distros, but for blockchains
  • Think like different countries with their own laws and currencies

Popular chains: Ethereum, Polygon, BSC, Solana, Avalanche

bg:1000

Testnet is a staging and mainnet is a production environments.

bg:1000

  • Bitcoin, Ethereum -> Blockchain OGs
  • BSC, Fantom -> EVM-compatible chains with low fees
  • Polygon -> Layer 2 scaling solution for Ethereum
  • Solana -> High-speed blockchain for decentralized applications
  • Avalanche -> Highly scalable and customizable blockchain platform
  • Cosmos, Polkadot, Near -> Interoperable blockchains ecosystem
  • Arbitrum, Optimism -> Layer 2 rollups for Ethereum scaling

Accounts and Wallets

has a unique public address, derived from a public key, which can be shared with others to receive funds, similar to a bank account number.

  • Account -> Send and receive transactions on the blockchain
  • Wallet -> Tool that manages your private and public keys
  • Hold Keys -> Control over your crypto assets by holding private keys
  • Address -> Public identifier for sending/receiving funds, derived from public key

Public/Private Keys & Hash Functions

  • Unique fingerprints of data, called hashes
  • Minor changes in input produce vastly different outputs (Avalanche effect)
  • Ideally changing one input bit should flip ~50% of output bits
  • Bitcoin uses SHA-256, Ethereum uses Keccak-256
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private_key.pem -out public_key.pem
openssl rsa -text -in private_key.pem

How Wallet Address is Generated?

  • A bitcoin address is created by hashing public key using SHA-256 and then RIPEMD-160
  • Ethereum address is created by taking the last 20 bytes of the Keccak-256 hash of the public key
address = RIPEMD160(SHA256(public_key))  # Bitcoin
address = last_20_bytes(Keccak256(public_key))  # Ethereum
  • Bitcoin address: 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
  • Ethereum address: 0x742d35Cc6634C0532925a3b844Bc454e4438f44e

What's a MetaMask?

  • browser extension and mobile app that serves as a wallet and gateway to blockchain apps
  • Install Metamask from -> metamask.io
  • Supports Ethereum and other EVM-compatible blockchains
  • Allows users to manage their accounts, sign transactions and interact with dApps

MetaMask Workflow

Transactions

  • A transaction is a signed instruction sent from one account to another.
  • It tells the Ethereum network to do something — transfer ETH, deploy a contract, or call a function.
  • changes the blockchain's state.
Transactions are the only way to make something happen on the Ethereum.

Transaction Flow

{
  "jsonrpc": "2.0",
  "method": "eth_sendTransaction",
  "params": [
    {
      "from": "0x5ac5b8D9Efb99b49176eBfcB1A78f8F85FcB0967",
      "to": "0xFb30D953a3fE1298df870C390Cf75A506091C607",
      "gas": "0x5208" (21000 in decimal),
      "gasPrice": "0x9502f900" (25000000000 wei in decimal),
      "value": "0xde0b6b3a7640000" (1 ETH in wei in decimal),
      "nonce": "0x1" (1 in decimal)
    }
  ],
  "id": 1
}

Etherscan Tx Details

{
"lock_time":0,
"size":226,
"inputs":[
  {
    "prev_out":{
      "index":139,
      "hash":"40120e43f00ff96e098a9173f14f1371655b3478bc0a558d6dc17a4ab176387d"
    },
    "script":"483045022100de6fd8120d9f142a82d5da9389e271caa3a757b01757c8e4fa7afbf92e74257c02202a78d4fbd52ae9f3a0083760d76f84643cf8ab80f5
  }
],
"version":1,
"vin_sz":1,
"hash":"d28ca5a59b2239864eac1c96d3fd1c23b747f0ded8f5af0161bae8a616b56a1d",
"vout_sz":2,
"out":[
    {
      "script_string":"OP_DUP OP_HASH160 c568ffeb46c6a9362e44a5a49deaa6eab05a619a OP_EQUALVERIFY OP_CHECKSIG",
      "address":"1JzouJCVmMQBmTcd8K4Y5BP36gEFNn1ZJ3",
      "value":33324,
      "script":"76a914c568ffeb46c6a9362e44a5a49deaa6eab05a619a88ac"
      255
    },
    {
      "script_string":"OP_DUP OP_HASH160 9386c8c880488e80a6ce8f186f788f3585f74aee OP_EQUALVERIFY OP_CHECKSIG",
      "address":"1ET3oBGf8JpunjytE7owyVtmBjmvcDycQe",
      "value":93376,
      "script":"76a9149386c8c880488e80a6ce8f186f788f3585f74aee88ac"
    }
  ]
}

Explore Here: blockchain explorer

Transaction Fees = Instructions Runtime Measurement

  • Gas = unit measuring computational work in Ethereum
  • Every operation costs gas — prevents infinite loops and spam.
  • Gas price × Gas used = Transaction fee
  • Unused gas is refunded; out-of-gas halts execution.
  • All nodes simulate the same contract execution → ensures consensus and security.

EVM Opcodes with Gas Costs

learn more about opcodes here: https://ethereum.org/developers/docs/evm/opcodes/

Smart Contracts: The Code of Web3

If Ethereum is the laptop, Smart Contracts are the apps.

What is a Smart Contract?

  • A program stored on the blockchain
  • Executes automatically when conditions are met
  • Cannot be changed once deployed
  • Lives forever… even if you regret writing it!!

Think of it as:

  • A vending machine
    → You put coins, it gives you snacks.
    → No cashier, no trust issues.

Understanding EVM! -> Heart of Ethereum

  • EVM = Ethereum Virtual Machine, runtime environment for smart contracts.
  • Global decentralized computer, every node executes the same code to ensure consensus.
  • Executes bytecode compiled from high-level languages (e.g., Solidity, Vyper).
  • Deterministic → same input, same output on every node.
  • Each opcode consumes gas (computational cost).

Anatomy of a Smart Contract

  • State Variables, Functions → rules of interaction
  • Events → logs that tell the outside world what happened
  • Modifiers → extra checks on functions (like “onlyOwner”)
uint public count;
function increment() public onlyOwner {
    count++;
}

modifier onlyOwner() {
    require(msg.sender == owner, "Not owner");
    _;
}

What are ERC20 Tokens?

  • ERC-20: The Standard for Fungible Tokens
  • ERC-20 is a smart contract standard for creating fungible tokens on Ethereum.
  • Defines how tokens behave and interact with wallets, exchanges, and other contracts.
  • “Fungible” = every token unit is identical (like ETH or USD).
  • Used for stablecoins, governance tokens, utility tokens, etc.

ERC20 standard: https://eips.ethereum.org/EIPS/eip-20

How do ERC20 Tokens Work?

  • smart contract storing a mapping of balances.
  • When you transfer tokens, it updates the balances in the contract - no phyiscal tokens move.
  • Tokens live as numbers in contract storage, not as separate assets.
  • Compatible with any wallet of dApp that follows the ERC-20 standard.

USDC token: Etherscan USDC

Remix and Co...

https://remix.ethereum.org/

Web3 Application

Applications: Payments

  • Stablecoin (USDC/USDT): fast, global, low-cost settlement
  • Cross-border transfers: no banks, no friction
  • Micropayments: pay-per-use APIs, streaming payments
  • Merchant adoption: crypto checkout (Stripe, Coinbase Commerce)
  • Programmable payments: automated subscriptions & escrow logic

Applications: Rewards, Utility

  • Rewards & Incentives: staking yields, loyalty points, on-chain badges
  • Utility Tokens: access to features, governance, voting power
  • Brand adoption: ticketing, memberships, digital merch
  • Interoperability: assets usable across many apps/games
Applications: NFTs and Digital Collectibles
  • NFTs
  • Collectibles
  • Digital Art
  • In-Game Assets
  • Royalities
  • Metaverse Land
  • Domain Names
Applications: DAOs and Governance
  • Taxation
  • Voting Systems
  • Decentralized Organizations
  • Digitized Ids
  • On-chain Governance
  • Welfare Distribution

Future of Blockchain

  • Scalability -> multiple Layer 2 solutions (rollups, sidechains)
  • Interoperability -> cross-chain bridges and protocols, e.g., Polkadot, Cosmos
  • Privacy -> zero-knowledge proofs, confidential transactions, e.g. Zcash, Tornado Cash
  • Sustainability -> energy-efficient consensus mechanisms, e.g., Proof of Stake

Recap

  • Blockchain, Bitcoin, Ethereum
  • Smart Contracts = self-executing code on blockchain
  • Wallets = manage keys and interact with blockchain
  • Web3 = decentralized apps powered by blockchain
  • Future = scalability, interoperability, privacy, sustainability

Thank You!

Appreciate your time! Feel free to connect, follow and reach out!

Tools to get started!!!

Resources