Skip to content

Blazearth/defi_insta

Repository files navigation

Solana Instagram MVP

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).

🌟 Features

  • 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

πŸ—οΈ Architecture

Program Structure

The application uses three main account types, all implemented as PDAs:

  1. Profile PDA: ["profile", user_pubkey]

    • Stores username and post count
    • One profile per user wallet
  2. Post PDA: ["post", user_pubkey, post_index]

    • Contains metadata URI (IPFS link), likes, and comment count
    • Sequential indexing for multiple posts per user
  3. Comment PDA: ["comment", post_pubkey, comment_index]

    • Links to parent post with commenter info
    • Sequential indexing for multiple comments per post

Account Structures

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
}

πŸ“‹ Prerequisites

πŸš€ Getting Started

1. Clone the Repository

git clone <repository-url>
cd social-app

2. Install Dependencies

# Install Anchor dependencies
yarn install

# Install frontend dependencies
cd app
npm install
cd ..

3. Build the Program

anchor build

The build process includes optimization flags to reduce stack usage:

  • opt-level = "z" for size optimization
  • lto = "fat" for link-time optimization
  • codegen-units = 1 for better optimization

4. Run Tests

anchor test

This runs the comprehensive test suite covering:

  • Profile creation and validation
  • Post creation with metadata
  • Like functionality
  • Comment system
  • Error handling and edge cases

5. Deploy to Devnet

Get Devnet SOL

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 devnet

Deploy the Program

anchor deploy --provider.cluster devnet

After deployment, note the program ID displayed in the output.

Update Frontend Configuration

Update app/src/utils/anchor.ts with your deployed program ID:

export const PROGRAM_ID = new PublicKey("YOUR_PROGRAM_ID_HERE");

6. Run the Frontend

cd app
npm run dev

The application will be available at http://localhost:5173

🌐 Deployment

Program Deployment

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

Frontend Deployment (Vercel)

  1. Install Vercel CLI (optional):

    npm install -g vercel
  2. Deploy:

    vercel
  3. Configure Environment:

    • The vercel.json configuration is already set up
    • Build command: cd app && npm run build
    • Output directory: app/dist
  4. 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]

πŸ§ͺ Testing

Test Structure

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

Running Specific Tests

# Run all tests
anchor test

# Run tests with logs
anchor test -- --nocapture

# Run specific test file
anchor test tests/social-app.ts

πŸ“– Usage Guide

Creating a Profile

await program.methods
  .initProfile("your_username")
  .accounts({
    profile: profilePDA,
    user: wallet.publicKey,
    systemProgram: SystemProgram.programId,
  })
  .rpc();

Creating a Post

await program.methods
  .createPost("ipfs://your-metadata-uri")
  .accounts({
    post: postPDA,
    profile: profilePDA,
    user: wallet.publicKey,
    systemProgram: SystemProgram.programId,
    clock: SYSVAR_CLOCK_PUBKEY,
  })
  .rpc();

Liking a Post

await program.methods
  .likePost()
  .accounts({
    post: postPDA,
    user: wallet.publicKey,
  })
  .rpc();

Adding a Comment

await program.methods
  .createComment("Your comment text here")
  .accounts({
    comment: commentPDA,
    post: postPDA,
    user: wallet.publicKey,
    systemProgram: SystemProgram.programId,
    clock: SYSVAR_CLOCK_PUBKEY,
  })
  .rpc();

πŸ”§ Development

Project Structure

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

Key Files

  • programs/social-app/src/lib.rs: Program entry point and instruction routing
  • programs/social-app/src/state.rs: Profile, Post, and Comment account structures
  • programs/social-app/src/error.rs: Custom error types
  • programs/social-app/src/instructions/: Modular business logic by feature
  • app/src/utils/anchor.ts: Frontend Anchor integration
  • app/src/idl/social_app.ts: Generated TypeScript types
  • Anchor.toml: Deployment configuration

See programs/social-app/README.md for detailed information about the modular program structure.

πŸ” Security Considerations

  • 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

πŸ› Troubleshooting

Build Issues

Stack overflow error during build:

  • Solution: The Cargo.toml includes opt-level = "z" to fix this
  • If issues persist, try cargo clean and rebuild

Anchor version mismatch:

anchor --version  # Should be 0.32.1

Deployment Issues

Insufficient funds:

solana airdrop 2 --url devnet

Wrong network:

solana config set --url devnet

Frontend Issues

Wallet 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_ID in app/src/utils/anchor.ts matches deployed program

πŸ“š Resources

πŸ“„ License

ISC

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


Built with ❀️ on Solana

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors