Defining smart contract components and structure
By the end of this session, you will be able to:
A smart contract is a self-executing program stored on a blockchain that automatically enforces the terms of an agreement when predetermined conditions are met, without requiring intermediaries.
Traditional: "Party A deposits $1000. When Party B delivers the goods and Party A confirms receipt, release the money to Party B."
Smart Contract: Code that holds $1000, automatically releases it to Party B when Party A calls the confirmReceipt() function.
The executable logic that defines contract behavior
Solidity, Vyper, Rust, Move
Persistent data stored on the blockchain
On-chain, immutable, expensive
The virtual machine that runs the contract
EVM, WASM, Move VM
Flow: calldata → selector dispatch → modifiers → function → state update → events/return → receipts
Key concerns: visibility, access control, reentrancy, gas efficiency, storage layout, ABI compatibility
An Ethereum smart contract is a program deployed on the blockchain that runs on the Ethereum Virtual Machine (EVM). Its anatomy can be understood in layers:
fallback() runs for unmatched callsreceive() runs for plain ETH transfersonlyOwner / onlyRole – restrict accessnonReentrant – block reentrancy attackswhenNotPaused – pause/unpause featurespayable, view, pure)Transfer in ERC-20)constructor: Runs once on deployment, sets initial statereceive(): Handles plain ETH transfersfallback(): Catch-all for unexpected calls or proxy forwardingemit Event(...) logs stored in transaction receipts, indexed via topicsdelegatecall, staticcall, call// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleEscrow {
// State variables
address public buyer;
address public seller;
uint256 public amount;
bool public isComplete;
// Events
event FundsDeposited(uint256 amount);
event FundsReleased(address to, uint256 amount);
// Constructor
constructor(address _seller) {
buyer = msg.sender;
seller = _seller;
isComplete = false;
}
// Functions
function deposit() public payable {
require(msg.sender == buyer, "Only buyer can deposit");
require(msg.value > 0, "Must deposit some amount");
amount = msg.value;
emit FundsDeposited(msg.value);
}
function release() public {
require(msg.sender == buyer, "Only buyer can release");
require(amount > 0, "No funds to release");
require(!isComplete, "Already completed");
isComplete = true;
payable(seller).transfer(amount);
emit FundsReleased(seller, amount);
}
}
| Visibility | Description | Access | Use Case |
|---|---|---|---|
| public | Accessible from anywhere | Internal + External | Main contract interface |
| external | Only callable from outside | External only | API functions |
| internal | Only within contract and derived | Internal only | Helper functions |
| private | Only within current contract | Current contract only | Internal logic |
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function withdraw() public onlyOwner {
// Function body
}
struct User {
string name;
uint256 balance;
bool isActive;
}
mapping(address => User) public users;
uint256[] public balances;
address[] public userAddresses;
Write and test contract code
Deploy to blockchain network
Users interact with contract
Contract becomes inactive
Once deployed, smart contracts are typically immutable. This means:
Restrict function access to authorized users
address public owner;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
Emergency stop mechanism for critical situations
bool public stopped = false;
modifier stopInEmergency() {
require(!stopped);
_;
}
Safe way to handle Ether transfers
mapping(address => uint) pendingWithdrawals;
function withdraw() public {
uint amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
payable(msg.sender).transfer(amount);
}
Manage contract states and transitions
enum State { Created, Locked, Released }
State public state;
modifier inState(State _state) {
require(state == _state);
_;
}
In the next session, we'll explore the Life Cycle of smart contracts, tracing their journey from development through deployment and execution, including upgrade patterns and termination strategies.