The Library
contract represents a simple library system on the Ethereum blockchain. It allows users to buy books from the library by depositing Ether into their accounts. The contract is managed by an admin who can change the book's price, add new books to the library.
-
admin
(address): The Ethereum address of the contract administrator who has special privileges, such as changing the book's price and adding books to the library. -
bookPrice
(uint): The price of a single book in Ether. Users need to have sufficient balance to purchase a book. -
bookQuantity
(uint): The current quantity of books available in the library. -
userBalance
(mapping): A mapping that stores the balances of users. The keys of the mapping are Ethereum addresses, and the corresponding values are the respective balances in Ether.
-
isAdmin
: A custom modifier that restricts access to functions only to the contract's administrator. It ensures that only the admin can execute certain functions, like changing the book's price and adding new books. -
hasMoney
: A custom modifier that checks whether a user has sufficient balance to buy a book. If the user's balance is less than the book's price, the transaction is reverted with an error message.
The contract constructor is executed only once during deployment and accepts two parameters: _bookPrice
and _bookQuantity
. These parameters set the initial book price and quantity in the library. The sender of the deployment transaction becomes the admin
of the contract.
This function allows the contract admin to change the price of the book.
This function enables the contract admin to increase the quantity of books in the library by one.
This function allows users to add Ether to their account balance in the library.
Users can use this function to buy a book from the library. The function checks if the user has enough balance to afford the book before processing the transaction.
To use the Library
contract, follow these steps:
-
Deploy the contract by providing the initial book price and quantity.
-
The deploying account becomes the contract's administrator.
-
The admin can change the book price using the
changePrice
function. -
The admin can add new books to the library using the
addBook
function. -
Users can deposit Ether into their accounts using the
addMoney
function. -
Users can buy a book by calling the
buyBook
function, which deducts the book price from the user's balance and decreases the book quantity.
Pranay Raj