Owner Management: Teaches how to restrict access to certain functions for contract owners.
Banking Functions: Demonstrates how users can deposit and withdraw Ether.
Balance and Loan Tracking: Illustrates keeping track of user balances and loan amounts.
Events: Shows how to log contract actions for transparency using events.
Modifiers: Demonstrates using function modifiers for access control and logical checks.
These contracts can be used to teach the basic functionality associated with each concept in the original SimpleBankcontract.
1. Owner Management
A contract where only the owner can execute certain functions.
solidityCopy code// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;contract OwnerContract {addresspublic owner;constructor() { owner = msg.sender; // Set the owner as the account that deploys the contract }modifieronlyOwner() {require(msg.sender == owner,"Only the owner can call this function"); _; }functionchangeOwner(address newOwner) publiconlyOwner { owner = newOwner; }functionownerOnlyFunction() publiconlyOwner {// Only owner can execute this function }}
2. Banking Functions (Deposit and Withdrawal)
A simple contract where users can deposit Ether, and only the owner can withdraw it.
solidityCopy code// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;contract DepositWithdraw {addresspublic owner;mapping(address=>uint256) public balances;constructor() { owner = msg.sender; }functiondeposit() publicpayable { balances[msg.sender] += msg.value; }functionwithdraw() public {require(balances[msg.sender] >0,"No balance to withdraw");uint256 amount = balances[msg.sender]; balances[msg.sender] =0;payable(msg.sender).transfer(amount); }functionownerWithdraw() public {require(msg.sender == owner,"Only the owner can withdraw all funds");payable(owner).transfer(address(this).balance); }}
3. Balance and Loan Tracking
A contract that tracks balances and loans for users.
solidityCopy code// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;contract LoanTracker {mapping(address=>uint256) public balances;mapping(address=>uint256) public loans;functiondeposit() publicpayable { balances[msg.sender] += msg.value; }functiontakeLoan(uint256 amount) public {require(amount >0,"Loan must be greater than 0"); loans[msg.sender] += amount; }functionrepayLoan() publicpayable {require(loans[msg.sender] >0,"No outstanding loan");require(msg.value == loans[msg.sender],"Must repay exact loan amount"); loans[msg.sender] =0; }functiongetLoanAmount() publicviewreturns (uint256) {return loans[msg.sender]; }}
4. Events
A contract that demonstrates emitting events to log actions such as deposits and withdrawals.
A contract that demonstrates the use of function modifiers for access control.
solidityCopy code// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;contract ModifierExample {addresspublic owner;constructor() { owner = msg.sender; }modifieronlyOwner() {require(msg.sender == owner,"You are not the owner"); _; }functionrestrictedFunction() publiconlyOwner {// Only the owner can call this function }functionopenFunction() public {// Anyone can call this function }}