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.