Labs ICT
⭐ Pro Login

Smart Contracts

Self-executing code on the blockchain

Smart Contracts

A smart contract is a self-executing program stored on a blockchain that automatically enforces the terms of an agreement when predefined conditions are met. They are the building blocks of decentralized applications.

Smart Contract vs Traditional Contract


  Traditional Contract:           Smart Contract:
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚  Written agreement   β”‚       β”‚  Code on blockchain  β”‚
  β”‚  Enforced by lawyers β”‚       β”‚  Enforced by code    β”‚
  β”‚  Slow settlement     β”‚       β”‚  Instant execution   β”‚
  β”‚  Trust required      β”‚       β”‚  Trustless           β”‚
  β”‚  Can be disputed     β”‚       β”‚  Tamper-proof        β”‚
  β”‚  Private             β”‚       β”‚  Transparent         β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

How Smart Contracts Work


  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚  1. Developer writes contract in Solidity  β”‚
  β”‚         β”‚                                  β”‚
  β”‚         β–Ό                                  β”‚
  β”‚  2. Contract compiled to EVM bytecode      β”‚
  β”‚         β”‚                                  β”‚
  β”‚         β–Ό                                  β”‚
  β”‚  3. Contract deployed to blockchain       β”‚
  β”‚     (gets a unique address)               β”‚
  β”‚         β”‚                                  β”‚
  β”‚         β–Ό                                  β”‚
  β”‚  4. Users interact by sending transactionsβ”‚
  β”‚         β”‚                                  β”‚
  β”‚         β–Ό                                  β”‚
  β”‚  5. Contract code executes automatically  β”‚
  β”‚         β”‚                                  β”‚
  β”‚         β–Ό                                  β”‚
  β”‚  6. State changes recorded on-chain       β”‚
  β”‚     (permanent and transparent)           β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Example: Simple Escrow Contract


  // SPDX-License-Identifier: MIT
  pragma solidity ^0.8.20;

  contract Escrow {
      address public buyer;
      address public seller;
      uint256 public amount;
      bool public released;

      constructor(address _seller) payable {
          buyer = msg.sender;
          seller = _seller;
          amount = msg.value;
      }

      function releaseFunds() public {
          require(msg.sender == buyer, "Only buyer");
          require(!released, "Already released");
          released = true;
          payable(seller).transfer(amount);
      }

      function refund() public {
          require(msg.sender == seller, "Only seller");
          require(!released, "Already released");
          released = true;
          payable(buyer).transfer(amount);
      }
  }

Properties of Smart Contracts

Deterministic: Same input always produces same output across all nodes.

Immutable: Once deployed, code cannot be changed (unless upgrade pattern is built in).

Transparent: Anyone can inspect the code and verify its behavior.

Autonomous: No human intervention needed once conditions are met.

πŸ§ͺ Quick Quiz

What is a smart contract?