A simple Solidity smart contract that allows the owner to add books and toggle their availability.
Users can retrieve book details but cannot modify them.
β Owner can add books to the library
β Users can fetch book details by ID
β Owner can toggle book availability
β Access control ensures only the owner can modify the library
β Fully tested with Hardhat β
This smart contract is written in Solidity (^0.8.28) and tested using Hardhat.
It stores books using a mapping(uint => Book), where each book has:
- A
title - An
author - A
boolean isAvailableto track availability
Example:
struct Book {
string title;
string author;
bool isAvailable;
}
---
π Contract Functions
This contract includes the following functions:
addBook(string title, string author): Owner adds a new book to the library.
getBook(uint _id): Retrieves a book's details (title, author, availability).
toggleAvailability(uint _id): Owner can change a book's availability.
---
π Tech Stack
Solidity ^0.8.28
Hardhat
ethers.js
Chai (for testing)
---
π Installation & Running Locally
Clone the Repository
git clone https://github.com/simob75/solidity-library-contract.git
cd solidity-library-contract
Install Dependencies
npm install
Compile the Smart Contract
npx hardhat compile
Run Tests
npx hardhat test
You should see β
all tests passing.
---
π Future Plans
β
Upload smart contract to GitHub (Done!)
β³ Build a React frontend using ethers.js
β³ Deploy contract to a testnet
β³ Host frontend on Vercel/Netlify
---
π Acknowledgments
Special thanks to Hardhat, ethers.js, and Chai for making Solidity development easier! π
---
π© Connect
π¬ Questions? Feel free to reach out or open an issue!
π GitHub: simob75