Smart Contracts
Documentation and architecture for on-chain programs. Understanding how smart contracts work on Solana: account models, authority patterns, and instruction lifecycle.
Program Overview
Portfolio Demo Program
A simple demonstration program showcasing Solana program architecture, account management, and instruction handling.
This program serves as a reference implementation for understanding how Solana programs work, including PDA derivation, account validation, and state management.
Account Model
Counter Account
PDAA Program Derived Address that stores a counter value. The PDA is derived from a seed and the program ID.
Fields
count(u64)The current counter value
authority(Pubkey)The account authorized to increment the counter
bump(u8)The bump seed used for PDA derivation
Authority Model
The program uses Program Derived Addresses (PDAs) for account ownership. The counter account is owned by the program itself, with an authority field specifying who can modify it.
- ▸Counter account is a PDA derived from a seed
- ▸Authority is specified during initialization
- ▸Only the authority can increment the counter
- ▸Program owns the account, ensuring data integrity
Instruction Lifecycle
initialize
Creates a new counter account as a PDA and sets the initial authority.
Accounts
counterauthoritysystem_programExecution Flow
Derive the counter PDA from seed and program ID
Create the account with required space
Set initial count to 0
Set authority to the provided pubkey
Store the bump seed
increment
Increments the counter value. Can only be called by the authority.
Accounts
counterauthorityExecution Flow
Verify the authority matches the counter account authority
Verify the authority signed the transaction
Increment the counter value
Update the account data
update_authority
Updates the authority of the counter account. Can only be called by the current authority.
Accounts
countercurrent_authoritynew_authorityExecution Flow
Verify the current authority matches
Verify the current authority signed
Update the authority field
Save the account data
Design Principles
Stateless Programs
Programs are pure functions. All state lives in accounts, not in the program itself.
Example: The counter value is stored in an account, not in program memory.
Account-Based State
All persistent data is stored in on-chain accounts with explicit ownership.
Example: The Counter account is owned by the program, ensuring only the program can modify it.
Deterministic Execution
Programs execute identically across all validators, ensuring consensus.
Example: The increment instruction always adds 1, regardless of which validator processes it.
Explicit Locks
Read/write permissions are enforced at the program level through account validation.
Example: Only the authority can increment; this is checked explicitly in the instruction handler.
Source Code
View the complete Anchor program implementation in the contracts directory.