Skip to content

cesarMalanco/Solana-Library

Repository files navigation

📚 Solana Library Program

Solana Rust Anchor License

A decentralized on-chain library management program built with Anchor on Solana


🎯 About The Project

Solana Library Program is a smart contract built using the Anchor framework that allows users to create and manage a personal on-chain digital library.

Each user can:

  • Create their own library (PDA-based account)
  • Add books
  • Remove books
  • Toggle book availability
  • View stored books

All data is stored directly on-chain using Program Derived Addresses (PDAs).


✨ Features

📖 Library Management

  • Create a personal library (PDA per user)
  • Store library name
  • Owner-based access control

📚 Book Management

  • Add books with:

    • Name
    • Number of pages
    • Availability status
    • Delete books by name
    • Toggle availability (available / not available)
    • View all stored books via transaction logs

🔐 Security

  • Owner validation using require!

  • PDA derived from:

    • Static seed ("biblioteca")
    • Owner public key
    • Program ID
  • Custom error handling with Anchor #[error_code]


🏗️ Program Architecture

📦 Main Account

Biblioteca {
    owner: Pubkey,
    nombre: String,
    libros: Vec<Libro>
}

📘 Internal Struct

Libro {
    nombre: String,
    paginas: u16,
    disponible: bool
}

🔑 PDA Seeds

["biblioteca", owner_pubkey]

Each user can only create and manage their own library.


🚀 Installation & Setup

Prerequisites

Make sure you have installed:

Requirement Version
Rust Latest stable
Solana CLI 1.17+
Anchor CLI 0.29+
Node.js 18+
Yarn Latest

🔧 Quick Start

# Clone the repository
git clone https://github.com/cesarMalanco/solana-library.git

# Navigate into project
cd solana-library

# Install dependencies
yarn install

# Build the program
anchor build

# Run local validator
solana-test-validator

# Deploy locally
anchor deploy

# Run tests
anchor test

🧪 Available Instructions

Instruction Description
crear_biblioteca Creates a new library PDA
agregar_libro Adds a new book
eliminar_libro Removes a book by name
ver_libros Logs all books
alternar_estado Toggles availability

📁 Project Structure

solana-library/
│
├── programs/
│   └── solana_library/
│       └── src/lib.rs        # Smart contract logic
│
├── tests/                    # Mocha tests
├── migrations/               # Deployment scripts
│
├── Anchor.toml               # Anchor configuration
├── Cargo.toml                # Rust dependencies
├── package.json              # JS dependencies
└── tsconfig.json             # TypeScript config

🛡️ Error Handling

The program defines custom errors:

NoEresElOwner
LibroNoExiste

These ensure:

  • Only the library owner can modify data
  • Invalid operations are safely rejected

🧠 Key Concepts Used

  • Program Derived Addresses (PDAs)
  • Anchor #[account] macro
  • InitSpace for storage calculation
  • require! macro for access control
  • Custom error codes
  • On-chain vector storage

🌐 Deployment

To deploy to Devnet:

solana config set --url devnet
anchor deploy

To deploy to Mainnet:

solana config set --url mainnet-beta
anchor deploy

📜 License

This project is licensed under the MIT License.


👨‍💻 Author

César M.

Built with ❤️ using Rust, Anchor & Solana


About

A decentralized on-chain library program built with Rust and Anchor on Solana, enabling users to create libraries, manage books, and track records directly on the blockchain.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors