A decentralized on-chain library management program built with Anchor on Solana
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).
- Create a personal library (PDA per user)
- Store library name
- Owner-based access control
-
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
-
Owner validation using
require! -
PDA derived from:
- Static seed (
"biblioteca") - Owner public key
- Program ID
- Static seed (
-
Custom error handling with Anchor
#[error_code]
Biblioteca {
owner: Pubkey,
nombre: String,
libros: Vec<Libro>
}Libro {
nombre: String,
paginas: u16,
disponible: bool
}["biblioteca", owner_pubkey]
Each user can only create and manage their own library.
Make sure you have installed:
| Requirement | Version |
|---|---|
| Rust | Latest stable |
| Solana CLI | 1.17+ |
| Anchor CLI | 0.29+ |
| Node.js | 18+ |
| Yarn | Latest |
# 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| 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 |
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
The program defines custom errors:
NoEresElOwner
LibroNoExisteThese ensure:
- Only the library owner can modify data
- Invalid operations are safely rejected
- Program Derived Addresses (PDAs)
- Anchor
#[account]macro InitSpacefor storage calculationrequire!macro for access control- Custom error codes
- On-chain vector storage
To deploy to Devnet:
solana config set --url devnet
anchor deployTo deploy to Mainnet:
solana config set --url mainnet-beta
anchor deployThis project is licensed under the MIT License.
César M.
Built with ❤️ using Rust, Anchor & Solana