A decentralized social media application built on Solana blockchain using the Anchor framework. This MVP features user profiles, posts, likes, and comments, all stored on-chain using Program Derived Addresses (PDAs).
- User Profiles: Create and manage blockchain-based user profiles
- Post Creation: Share content with IPFS-hosted images
- Social Interactions: Like posts and add comments
- Decentralized Storage: All data stored on Solana blockchain
- React Frontend: Simple, intuitive web interface
- Wallet Integration: Connect with Phantom, Solflare, and other Solana wallets
The application uses three main account types, all implemented as PDAs:
-
Profile PDA:
["profile", user_pubkey]- Stores username and post count
- One profile per user wallet
-
Post PDA:
["post", user_pubkey, post_index]- Contains metadata URI (IPFS link), likes, and comment count
- Sequential indexing for multiple posts per user
-
Comment PDA:
["comment", post_pubkey, comment_index]- Links to parent post with commenter info
- Sequential indexing for multiple comments per post
Profile {
owner: Pubkey, // Wallet that owns this profile
username: String, // Display name (max 32 chars)
post_count: u64, // Number of posts created
bump: u8, // PDA bump seed
}
Post {
owner: Pubkey, // Profile that created this post
index: u64, // Post number for this user
metadata_uri: String, // IPFS URI (max 200 chars)
likes: u64, // Like counter
comment_count: u64, // Number of comments
timestamp: i64, // Creation time
bump: u8, // PDA bump seed
}
Comment {
post: Pubkey, // Post this comment belongs to
commenter: Pubkey, // Wallet that created comment
comment_text: String, // Comment content (max 280 chars)
index: u64, // Comment number on this post
timestamp: i64, // Creation time
bump: u8, // PDA bump seed
}- Rust (latest stable)
- Solana CLI (v1.18+)
- Anchor (v0.32.1)
- Node.js (v18+)
- Yarn or npm
git clone <repository-url>
cd social-app# Install Anchor dependencies
yarn install
# Install frontend dependencies
cd app
npm install
cd ..anchor buildThe build process includes optimization flags to reduce stack usage:
opt-level = "z"for size optimizationlto = "fat"for link-time optimizationcodegen-units = 1for better optimization
anchor testThis runs the comprehensive test suite covering:
- Profile creation and validation
- Post creation with metadata
- Like functionality
- Comment system
- Error handling and edge cases
First, ensure your wallet has devnet SOL:
# Check your wallet address
solana address
# Airdrop devnet SOL (may need to run multiple times)
solana airdrop 2 --url devnet
# Check balance
solana balance --url devnetanchor deploy --provider.cluster devnetAfter deployment, note the program ID displayed in the output.
Update app/src/utils/anchor.ts with your deployed program ID:
export const PROGRAM_ID = new PublicKey("YOUR_PROGRAM_ID_HERE");cd app
npm run devThe application will be available at http://localhost:5173
The program is configured for devnet deployment in Anchor.toml:
[programs.devnet]
social_app = "6t5PjEe4SW7JbtHxbttwZ9gyGGmLhahA9r2v2E3iPTsZ"
[provider]
cluster = "devnet"
wallet = "~/.config/solana/id.json"Current Devnet Program ID: 6t5PjEe4SW7JbtHxbttwZ9gyGGmLhahA9r2v2E3iPTsZ
-
Install Vercel CLI (optional):
npm install -g vercel
-
Deploy:
vercel
-
Configure Environment:
- The
vercel.jsonconfiguration is already set up - Build command:
cd app && npm run build - Output directory:
app/dist
- The
-
Access Your App:
- After deployment, Vercel will provide a URL
- Share this URL to allow others to use your app
Frontend URL: [To be added after deployment]
The test suite (tests/social-app.ts) covers:
Happy Path Tests:
- β Profile creation
- β Post creation with metadata
- β Like functionality
- β Comment creation
Unhappy Path Tests:
- β Duplicate profile creation
- β Post creation without profile
- β Empty string validations
- β Invalid PDA access
# Run all tests
anchor test
# Run tests with logs
anchor test -- --nocapture
# Run specific test file
anchor test tests/social-app.tsawait program.methods
.initProfile("your_username")
.accounts({
profile: profilePDA,
user: wallet.publicKey,
systemProgram: SystemProgram.programId,
})
.rpc();await program.methods
.createPost("ipfs://your-metadata-uri")
.accounts({
post: postPDA,
profile: profilePDA,
user: wallet.publicKey,
systemProgram: SystemProgram.programId,
clock: SYSVAR_CLOCK_PUBKEY,
})
.rpc();await program.methods
.likePost()
.accounts({
post: postPDA,
user: wallet.publicKey,
})
.rpc();await program.methods
.createComment("Your comment text here")
.accounts({
comment: commentPDA,
post: postPDA,
user: wallet.publicKey,
systemProgram: SystemProgram.programId,
clock: SYSVAR_CLOCK_PUBKEY,
})
.rpc();social-app/
βββ programs/
β βββ social-app/
β βββ src/
β βββ lib.rs # Main program entry point
β βββ error.rs # Custom error definitions
β βββ state.rs # Account structures
β βββ instructions/
β βββ mod.rs # Module exports
β βββ initialize.rs # Initialization logic
β βββ profile.rs # Profile management
β βββ post.rs # Post creation & likes
β βββ comment.rs # Comment functionality
βββ tests/
β βββ social-app.ts # Test suite
βββ app/
β βββ src/
β β βββ components/ # React components
β β βββ contexts/ # Wallet context
β β βββ utils/ # Anchor utilities
β β βββ idl/ # Generated IDL
β βββ package.json
βββ Anchor.toml # Anchor configuration
βββ Cargo.toml # Rust workspace
βββ README.md # This file
βββ DEPLOYMENT.md # Deployment guide
βββ PDA_GUIDE.md # PDA architecture guide
programs/social-app/src/lib.rs: Program entry point and instruction routingprograms/social-app/src/state.rs: Profile, Post, and Comment account structuresprograms/social-app/src/error.rs: Custom error typesprograms/social-app/src/instructions/: Modular business logic by featureapp/src/utils/anchor.ts: Frontend Anchor integrationapp/src/idl/social_app.ts: Generated TypeScript typesAnchor.toml: Deployment configuration
See programs/social-app/README.md for detailed information about the modular program structure.
- All accounts use PDAs for deterministic addressing
- Input validation on all string fields
- Ownership verification on mutable operations
- Bump seeds stored to prevent PDA grinding attacks
- No admin keys or centralized control
Stack overflow error during build:
- Solution: The
Cargo.tomlincludesopt-level = "z"to fix this - If issues persist, try
cargo cleanand rebuild
Anchor version mismatch:
anchor --version # Should be 0.32.1Insufficient funds:
solana airdrop 2 --url devnetWrong network:
solana config set --url devnetWallet not connecting:
- Ensure you're on devnet in your wallet settings
- Try refreshing the page
- Check browser console for errors
Program ID mismatch:
- Verify
PROGRAM_IDinapp/src/utils/anchor.tsmatches deployed program
ISC
Contributions are welcome! Please feel free to submit a Pull Request.
Built with β€οΈ on Solana