A decentralized book database built on Ethereum using Solidity smart contracts. This project allows users to store, manage, and organize books on the blockchain with ownership controls and persistent storage.
- Add Books: Register new books with title and publication year
- Edit Books: Update existing book information (title and year)
- Remove Books: Delete books from the database (owner-only)
- View Books: Query stored books by ID
- Access Control: Only contract owner can remove books
- Book Counter: Track total number of books in the database
The BookDatabase contract includes:
- Book Structure: Each book contains a title (string) and publication year (uint16)
- Unique IDs: Auto-incrementing IDs for each book
- Owner Permissions: Only the contract deployer can remove books
- Data Validation: Prevents empty titles and invalid years
- Solidity ^0.8.28 - Smart contract development
- Hardhat - Development framework and testing
- TypeScript - Type-safe development
- Ethers.js - Ethereum interaction
- Chai - Testing assertions
- Node.js (v20 or higher)
- pnpm package manager
- Git
- Clone the repository:
git clone <repository-url>
cd book-database- Install dependencies:
pnpm install- Create a
.envfile in the root directory:
INFURA_URL=your_infura_endpoint
SECRET=your_wallet_mnemonic
ETHERSCAN_API_KEY=your_etherscan_api_keyStart a local Hardhat node:
pnpm startRun the test suite:
pnpm testRun tests with gas reporting:
REPORT_GAS=true pnpm testDeploy to local network:
pnpm run deploy:devDeploy to Sepolia testnet:
pnpm run deploy:prodUse Hardhat console for direct interaction:
pnpm run consoleExample interactions:
// Get contract instance
const BookDatabase = await ethers.getContractFactory("BookDatabase");
const bookDatabase = await BookDatabase.attach("CONTRACT_ADDRESS");
// Add a book
await bookDatabase.registerBook({ title: "The Great Gatsby", year: 1925 });
// Get book count
const count = await bookDatabase.count();
// Get book by ID
const book = await bookDatabase.books(1);
// Edit a book
await bookDatabase.editBook(1, { title: "Updated Title", year: 1925 });registerBook(Book memory newBook)- Add a new book to the databaseeditBook(uint32 id, Book memory newBook)- Update an existing bookremoveBook(uint32 id)- Remove a book (owner only)books(uint32 id)- View book details by IDcount()- Get total number of books
removeBook()function is restricted to the contract owner- All other functions are publicly accessible
The project includes comprehensive tests covering:
- Book registration and counting
- Book editing functionality
- Book removal with proper permissions
- Access control enforcement
- Local: Hardhat development network (Chain ID: 31337)
- Sepolia: Ethereum testnet (Chain ID: 11155111)
This project is licensed under the MIT License.