This Solidity contract implements a simple Calculator that provides two mathematical operations - division and modulus (remainder). The contract ensures that division by zero is not allowed and performs necessary checks to ensure correctness of the results.
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 takes two unsigned integer values a
and b
as input parameters and returns the result of dividing a
by b
. It throws an error if b
is zero, as division by zero is not allowed. The function employs the require
statement to enforce this condition and uses the assert
statement to ensure that the result is correct.
This function allows the contract admin to change the price of the book.
This function takes two unsigned integer values a
and b
as input parameters and returns the modulus (remainder) of a
divided by b
. It throws an error if b
is zero, as division by zero is not allowed. The function uses the %
operator to calculate the modulus and asserts the correctness of the result using the assert
and revert
statements.
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.
To use this Calculator contract, you can follow these steps:
-
Open a Solidity development environment like Remix at https://remix.ethereum.org/. 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.
-
Create a new file and save it with a .sol extension (e.g.,
Calculator.sol
).
-
Copy and paste the provided contract code into the file. To use the
Library
contract, follow these steps: -
Ensure that the Solidity compiler version is set to
0.8.18
(or a compatible version). -
Deploy the contract by providing the initial book price and quantity.
-
Compile the contract by clicking on the "Solidity Compiler" tab and then clicking "Compile Calculator.sol".
-
The deploying account becomes the contract's administrator.
-
Deploy the contract by navigating to the "Deploy & Run Transactions" tab, selecting the
Calculator
contract from the dropdown menu, and clicking "Deploy". -
The admin can change the book price using the
changePrice
function. -
Once the contract is deployed, you can interact with it by calling its functions.
-
The admin can add new books to the library using the
addBook
function.
- Users can deposit Ether into their accounts using the
addMoney
function.
Assuming the contract is deployed, you can call the divide
and mod
functions to perform mathematical operations.
6. 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.
Piyush Bhardwaj www.linkedin.com/in/piyush-bhardwaj-384ba426b